[LLVMdev] OCaml and Exceptions
Gordon Henriksen
gordonhenriksen at mac.com
Sun Nov 25 13:40:24 PST 2007
On Nov 25, 2007, at 11:49, Jon Harrop wrote:
> On Sunday 25 November 2007 12:23, Gordon Henriksen wrote:
>
>> On 2007-11-24, at 21:58, Jon Harrop wrote:
>>
>>> - Exceptions
>>
>> http://llvm.org/docs/ExceptionHandling.html
>>
>> LLVM's exception support is tuned toward DWARF "zero-cost
>> exceptions," i.e. C++ exception handling. Anton Korobeynikov and
>> Duncan Sands (who is working on Ada) are probably the experts in
>> this area.
>
> Excellent.
>
> So does zero-cost exception handling in C++ refer to a special case
> where you can statically prove that there are no destructors to
> call, or something?
"Zero cost" refers to the overhead introduced in the case when an
exception is not thrown at all.
C++ has a guiding principal that "you don't pay for it if you don't
use it." In the case of exceptions, this had pretty far-reaching
implications, since each stack-based object calls for a destructor
cleanup block. The resolution was for implementations to introduce no
runtime overhead whatsoever for a try/catch block if no exception is
raised, thus "zero cost". Throw actually became more expensive as a
result of these implementations.
> There is one thing that confuses me about this though. I benchmarked
> exception handling in OCaml and C++ a while ago and found OCaml to
> be ~6x faster and the best explanation I got was that C++ does not
> have zero-cost exceptions
Microbenchmarking raise vs. throw is not very realistic. ZCEH makes a
complicated performance tradeoff wherein the exceptional case is
penalized in order to optimize the common case. Since C++ code has
many implicit try/finally blocks (each stack-based object with a
destructor), this is a clear win. Ocaml code is quite different.
> because it requires destructors to be called as the stack is
> unwound, whereas OCaml can just jump back and leave collection to
> the GC.
GC in general will reduce the amount of cleanup code, which should
reduce DWARF EH's actual overhead (fewer cleanup blocks means smaller
tables and accordingly cheaper unwinds), especially when unwinding
deep stacks. Objective-C 2 on Leopard may be one of the few systems
which has this combination of properties. (But exceptions in Cocoa
code are discouraged even more strongly than in C++.)
Note that there is nothing in LLVM that prevents alternative exception
handling regimes, they just won't benefit from the existing
infrastructure, and will not interoperate with the DWARF runtime.
— Gordon
More information about the llvm-dev
mailing list