From 011618454da145adbd8377adfa64113e46aaf089 Mon Sep 17 00:00:00 2001
From: Bill Wendling
For each function which does exception processing, be it try/catch blocks - or cleanups, that function registers itself on a global frame list. When - exceptions are being unwound, the runtime uses this list to identify which - functions need processing.
+
For each function which does exception processing — be + it try/catch blocks or cleanups — that function + registers itself on a global frame list. When exceptions are unwinding, the + runtime uses this list to identify which functions need processing.
Landing pad selection is encoded in the call site entry of the function context. The runtime returns to the function via @@ -134,6 +134,7 @@ exceptions are thrown. As exceptions are, by their nature, intended for uncommon code paths, DWARF exception handling is generally preferred to SJLJ.
+ @@ -176,8 +177,8 @@ should take place. Actions typically pass control to a landing pad. -A landing pad corresponds to the code found in the catch portion of - a try/catch sequence. When execution resumes at a landing +
A landing pad corresponds to the code found in the catch portion of + a try/catch sequence. When execution resumes at a landing pad, it receives the exception structure and a selector corresponding to the type of exception thrown. The selector is then used to determine which catch should actually process the exception.
@@ -193,11 +194,8 @@At the time of this writing, only C++ exception handling support is available - in LLVM. So the remainder of this document will be somewhat C++-centric.
-From the C++ developers perspective, exceptions are defined in terms of the - throw and try/catch statements. In this section + throw and try/catch statements. In this section we will describe the implementation of LLVM exception handling in terms of C++ examples.
@@ -210,16 +208,19 @@Languages that support exception handling typically provide a throw operation to initiate the exception process. Internally, a throw operation - breaks down into two steps. First, a request is made to allocate exception - space for an exception structure. This structure needs to survive beyond the - current activation. This structure will contain the type and value of the - object being thrown. Second, a call is made to the runtime to raise the - exception, passing the exception structure as an argument.
+ breaks down into two steps. +In C++, the allocation of the exception structure is done by - the __cxa_allocate_exception runtime function. The exception - raising is handled by __cxa_throw. The type of the exception is - represented using a C++ RTTI structure.
+In C++, the allocation of the exception structure is done by then + __cxa_allocate_exception runtime function. The exception raising is + handled by __cxa_throw. The type of the exception is represented + using a C++ RTTI structure.
Two LLVM intrinsic functions are used to convey information about the landing - pad to the back end.
- -The LLVM landingpad + instruction is used to convey information about the landing pad to the + back end. For C++, the landingpad instruction returns a pointer and + integer pair corresponding to the pointer to the exception structure and the + "selector value" respectively.
+ +The landingpad instruction takes a reference to the personality + function to be used for this try/catch sequence. The + remainder of the instruction is a list of catch and filter + clauses. The exception is tested against the clauses sequentially from first + to last. The selector value is a positive number if the exception matched a + type info, a negative number if it matched a filter, and zero if it matched a + cleanup. If nothing is matched, the behaviour of the program + is undefined. If a type info matched, then the + selector value is the index of the type info in the exception table, which + can be obtained using the + llvm.eh.typeid.for intrinsic.
Once the landing pad has the type info selector, the code branches to the - code for the first catch. The catch then checks the value of the type info + code for the first catch. The catch then checks the value of the type info selector against the index of type info for that catch. Since the type info index is not known until all the type info have been gathered in the backend, the catch code will call the - llvm.eh.typeid.for intrinsic - to determine the index for a given type info. If the catch fails to match - the selector then control is passed on to the next catch. Note: Since the - landing pad will not be used if there is no match in the list of type info on - the call to llvm.eh.selector, then - neither the last catch nor catch all need to perform the check - against the selector.
+ llvm.eh.typeid.for intrinsic to + determine the index for a given type info. If the catch fails to match the + selector then control is passed on to the next catch. Note: Since the landing + pad will not be used if there is no match in the list of type info on the + call to the landingpad + instruction, then neither the last catch nor catch all need to + perform the check against the selector.Finally, the entry and exit of catch code is bracketed with calls to __cxa_begin_catch and __cxa_end_catch.
@@ -318,16 +305,14 @@A cleanup is extra code which needs to be run as part of unwinding - a scope. C++ destructors are a prominent example, but other - languages and language extensions provide a variety of different - kinds of cleanup. In general, a landing pad may need to run - arbitrary amounts of cleanup code before actually entering a catch - block. To indicate the presence of cleanups, a landing pad's call - to llvm.eh.selector should - end with the argument i32 0; otherwise, the unwinder will - not stop at the landing pad if there are no catches or filters that - require it to.
+A cleanup is extra code which needs to be run as part of unwinding a scope. + C++ destructors are a prominent example, but other languages and language + extensions provide a variety of different kinds of cleanup. In general, a + landing pad may need to run arbitrary amounts of cleanup code before actually + entering a catch block. To indicate the presence of cleanups, a + landingpad instruction + should have a cleanup clause. Otherwise, the unwinder will not stop at + the landing pad if there are no catches or filters that require it to.
Do not allow a new exception to propagate out of the execution of a cleanup. This can corrupt the internal state of the unwinder. @@ -337,9 +322,9 @@
When all cleanups have completed, if the exception is not handled by the current function, resume unwinding by calling the - llvm.eh.resume intrinsic, - passing in the results of llvm.eh.exception and - llvm.eh.selector for the original landing pad.
+ resume instruction, passing in + the results of the landingpad instruction for the original landing + pad.C++ allows the specification of which exception types can be thrown from a function. To represent this a top level landing pad may exist to filter out - invalid types. To express this in LLVM code the landing pad will - call llvm.eh.selector. The - arguments are a reference to the exception structure, a reference to the - personality function, the length of the filter expression (the number of type - infos plus one), followed by the type infos themselves. - llvm.eh.selector will return a - negative value if the exception does not match any of the type infos. If no - match is found then a call to __cxa_call_unexpected should be made, - otherwise _Unwind_Resume. Each of these functions requires a - reference to the exception structure. Note that the most general form of an - llvm.eh.selector call can contain - any number of type infos, filter expressions and cleanups (though having more - than one cleanup is pointless). The LLVM C++ front-end can generate such - llvm.eh.selector calls due to - inlining creating nested exception handling scopes.
+ invalid types. To express this in LLVM code the + landingpad instruction will + have a filter clause. The clause consists of an array of type infos. + landingpad will return a negative value if the exception does not + match any of the type infos. If no match is found then a call + to __cxa_call_unexpected should be made, otherwise + _Unwind_Resume. Each of these functions requires a reference to the + exception structure. Note that the most general form of a + landingpad instruction can + have any number of catch, cleanup, and filter clauses (though having more + than one cleanup is pointless). The LLVM C++ front-end can generate such + landingpad instructions due + to inlining creating nested exception handling scopes. @@ -377,29 +360,27 @@The unwinder delegates the decision of whether to stop in a call - frame to that call frame's language-specific personality function. - Not all personalities functions guarantee that they will stop to - perform cleanups: for example, the GNU C++ personality doesn't do - so unless the exception is actually caught somewhere further up the - stack. When using this personality to implement EH for a language - that guarantees that cleanups will always be run, be sure to - indicate a catch-all in the - llvm.eh.selector call +
The unwinder delegates the decision of whether to stop in a call frame to + that call frame's language-specific personality function. Not all + personalities functions guarantee that they will stop to perform + cleanups. For example, the GNU C++ personality doesn't do so unless the + exception is actually caught somewhere further up the stack. When using this + personality to implement EH for a language that guarantees that cleanups will + always be run, be sure to indicate a catch-all in the + landingpad instruction rather than just cleanups.
-In order for inlining to behave correctly, landing pads must be - prepared to handle selector results that they did not originally - advertise. Suppose that a function catches exceptions of - type A, and it's inlined into a function that catches - exceptions of type B. The inliner will update the - selector for the inlined landing pad to include the fact - that B is caught. If that landing pad assumes that it - will only be entered to catch an A, it's in for a rude - surprise. Consequently, landing pads must test for the selector - results they understand and then resume exception propagation - with the llvm.eh.resume - intrinsic if none of the conditions match.
+In order for inlining to behave correctly, landing pads must be prepared to + handle selector results that they did not originally advertise. Suppose that + a function catches exceptions of type A, and it's inlined into a + function that catches exceptions of type B. The inliner will update + the landingpad instruction for the inlined landing pad to include + the fact that B is caught. If that landing pad assumes that it will + only be entered to catch an A, it's in for a rude surprise. + Consequently, landing pads must test for the selector results they understand + and then resume exception propagation with the + resume instruction if none of + the conditions match.
LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to +
In addition to the + landingpad and + resume instructions, LLVM uses + several intrinsic functions (name prefixed with "llvm.eh") to provide exception handling information at various points in generated code.
- -- i8* %llvm.eh.exception() -- -
This intrinsic returns a pointer to the exception structure.
- -- i32 %llvm.eh.selector(i8*, i8*, ...) -- -
This intrinsic is used to compare the exception with the given type infos, - filters and cleanups.
- -llvm.eh.selector takes a - minimum of three arguments. The first argument is the reference to - the exception structure. The second argument is a reference to the - personality function to be used for this try catch sequence. Each - of the remaining arguments is either a reference to the type info - for a catch statement, a filter - expression, or the number zero representing - a cleanup. The exception is tested against - the arguments sequentially from first to last. The result of - the llvm.eh.selector is a - positive number if the exception matched a type info, a negative - number if it matched a filter, and zero if it matched a cleanup. - If nothing is matched, or if only a cleanup is matched, different - personality functions may or may not cause control to stop at the - landing pad; see the restrictions for - more information. If a type info matched then the selector value - is the index of the type info in the exception table, which can be - obtained using the - llvm.eh.typeid.for intrinsic.
- -If a landing pad containing a call to llvm.eh.selector is - inlined into an invoke instruction, the selector arguments - for the outer landing pad are appended to those of the inlined - landing pad. Consequently, landing pads must be written to ignore - selector values that they did not originally advertise.
- -- void %llvm.eh.resume(i8*, i32) noreturn -- -
This intrinsic is used to resume propagation of an exception after - landing at a landing pad. The first argument should be the result - of llvm.eh.exception for that - landing pad, and the second argument should be the result of - llvm.eh.selector. When a call to - this intrinsic is inlined into an invoke, the call is transformed - into a branch to the invoke's unwind destination, using its - arguments in place of the calls - to llvm.eh.exception and - llvm.eh.selector there.
- -This intrinsic is not implicitly nounwind; calls to it - will always throw. It may not be invoked.
- -