[LLVMdev] Catching C++ exceptions, cleaning up, rethrowing

Paul J. Lucas paul at lucasmail.org
Wed May 2 18:52:16 PDT 2012


On Apr 11, 2012, at 10:50 PM, Bill Wendling wrote:

> On Apr 11, 2012, at 8:41 AM, Paul J. Lucas wrote:
> 
>> On Apr 8, 2012, at 10:40 PM, Bill Wendling wrote:
>> 
>>> My suggestion is to use clang to compile a program with exceptions on your Windows box and then use the personality function that it generates.
>> 
>> Assuming I do that and it works, wouldn't IR code generated on Windows then be incompatible with IR-code-compiled-to-machine-code on *nix (because it would use the wrong personality function)?
> 
> I suggested this only so that you can then copy what personality function declaration it uses. :-)

OK, I've finally had time to do this.  (Other, more pressing things kept coming up.)  I built llvm+clang using MinGW32 and compiled a simple C++ program that uses throw/try/catch. The landingpad instruction it generates is (for example):

%14 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)

So this code uses the same personality function as a Unix system... which seems wrong.  Shouldn't it be different for Windows?

>> Doing all the grunt work of emitting landingpads, resumes, having to get the right personality function, etc., seems a lot more work that my original proposal of catching C++ exceptions in my C thunk functions and calling a linked-list of destructors to run myself.  This also has the benefit of not having to deal with platform-specific personality functions.
>> 
>> I realize I'm a newb and there may be something subtle I'm not getting, but I have to ask: why is doing the work in IR "better"?
> 
> [ snip ]
> 
> Keep in mind that catching a C++ exception is a non-trivial matter. You would have to ensure that it has the same semantics as the existing DWARF implementation. E.g., running cleanups on all of the functions up the stack from where it was thrown to where it was caught (a two-phase process). And that's just the tip of the iceberg.

But I'd have my C thunk code catching the exception and it would do the right thing (or rather the C++ compiler would emit the right code for me).  *I* wouldn't have to do anything special.  The C thunk code would call the destructors explicitly and simply re-throw.  I assume that a C++ exception can pass through IR-generated code unmolested -- yes?

- Paul





More information about the llvm-dev mailing list