About unwind...

Dear LLVM people,

I have started playing with LLVM a little bit and I am thinking of using it to write some linear algebra software for my class.

First, an off-topic “bug report” (?), I tried using llvm under cygwin with Vista 64 bits, and it hangs during the build process. It works fine under Vista 32 bits, with cygwin.

I was reading the documentation and I am very excited by the possibilities of “invoke” and “unwind”. I have two questions.

  1. How does the performance of “invoke” compare to the performance of “call” on a typical x86, 32 or 64 bit platform? How many isns and such?
  2. When I unwind, the stack gets popped but it would make sense to pass some data to the exception handler. I guess I could use a global variable for this, but is there a way to do it on the stack? (Wouldn’t that make more sense also?)

Thanks,

Hi Sebastien.

I have started playing with LLVM a little bit and I am thinking of using it to write some linear algebra software for my class.

I was reading the documentation and I am very excited by the possibilities of "invoke" and "unwind". I have two questions.

1) How does the performance of "invoke" compare to the performance of "call" on a typical x86, 32 or 64 bit platform? How many isns and such?

With the exception of the "setjmp/longjmp" codegen that LLVM supports, invoke has no dynamic cost. It is used to implement C++ "zero-cost" exception-handling, which combine the dynamic execution stack itself and static metadata emitted by the compiler in order to implement unwinding.

2) When I unwind, the stack gets popped but it would make sense to pass some data to the exception handler. I guess I could use a global variable for this, but is there a way to do it on the stack? (Wouldn't that make more sense also?)

Indeed, you should use a global (or thread-local global).

- Gordon

   on United flight 339
     parked for the third hour on the tarmac
       at Logan International Airport
         still waiting for the nor'easter to blow on through
           (but I'm not bitter)

Incidentally, the first item is discussed in the introductory text on Exception Handling in LLVM — LLVM 16.0.0git documentation

- Gordon

2) When I unwind, the stack gets popped but it would make sense to pass some
data to the exception handler. I guess I could use a global variable for
this, but is there a way to do it on the stack? (Wouldn't that make more
sense also?)

Passing extra info on the stack doesn't make much sense because the stack
gets popped...

Also, note that the unwind instruction is currently turned into an abort
call at codegen time!

I suggest you compile some C++ code with exception handling using
llvm-g++ -S -emit-llvm and see how it does it. In short: throwing
an exception is done by a library call which takes an exception object.
Catching is done using the LLVM invoke instruction. The exception
object is retrieved by an llvm.eh.exception intrinsic call. See
http://llvm.org/docs/ExceptionHandling.html

Ciao,

Duncan.

The current llvm.eh intrinsics use c++ type_info objects and the c++
personality function for the Itanium ABI unwinder. What is the best way
for a new language to portably maximize reuse of the existing C++
runtime? Can you use a pointer to some other object and expect the
unwinder to work, or does the personality function inspect the contents
of the type_info as well as its address? Or do you have to write a new
personality function? Is there an example of a non-C++ llvm front-end
that also uses the llvm eh support?

Thanks,
-Jonathan

OK, I have learnt something about llvm from this thread, thanks.

As a side note:

  1. When I unwind, the stack gets popped but it would make sense to pass some

Passing extra info on the stack doesn’t make much sense because the stack
gets popped…

I don’t know how llvm implements unwind, but in principle it is possible to walk (and pop) the stack to the right point, then push a parameter, then push the proper return address, then return.

But nevermind me, I don’t know how llvm works anyway.

Hi,

> 2) When I unwind, the stack gets popped but it would make sense to pass
> some
> Passing extra info on the stack doesn't make much sense because the stack
> gets popped...
>

I don't know how llvm implements unwind,

the unwinder in the gcc runtime does the unwinding.

but in principle it is possible to
walk (and pop) the stack to the right point, then push a parameter, then
push the proper return address, then return.

This is what the gcc unwinder does (more or less), only it uses a register
rather than the stack: when calling the unwinder routine that does the
unwinding you give it a pointer to an exception object, and when execution
is to recommence in a landing pad it puts the value of that pointer in a special
register so you can get hold of it before jumping to the landing pad.

Ciao,

Duncan.