4
$\begingroup$

I've been looking into using SSA with basic block arguments for an IR. However, I have not found a good explanation of how exceptions should be modeled. This is complicated further because for runtimes like .NET or Java, at least in theory, almost any instruction could throw an exception. Additionally, is it possible to lower finally blocks to SSA without duplicating the finally block for success and failure cases?

Is this the right use case for this representation? Or would it be better to only use SSA when targeting native code.

$\endgroup$

2 Answers 2

1
$\begingroup$

The LLVM model for exception handling represents it in IR by saying that each exception handler is a basic block in its own right, and requiring your IR to include the exception handlers as well as the "main" flow of control.

If an exception is thrown, then instead of continuing as normal, you jump to the exception handler basic block, and let it deal with the exception; it can use a phi node to determine where to return control to, based on where you were when the block was called (so a "finally" block can use a phi node to distinguish "coming from a catch handler" from "end of the try block branched here").

$\endgroup$
0
$\begingroup$

The current approach in the Tyr Intermediate Representation (TIR) is to hold a handler pointer in each basic block. Entering and leaving such blocks is done using dedicated branching instructions to simplify IR handling and construction. Throw is an instruction and there are instructions to get the caught exception from the cxa world.

The boundary conditions are

  • TIR is eventually taken and compiled to LLVM IR
  • TIR needs an understanding of the kind of handler because some operations can change handlers
  • Being able to optimize local exception handling is a requirement for Tyr
  • Tyr has a full lattice type theory with must throw types as one bottom type.
  • TIR is only meant to serve Tyr; this one is particularly important if you compare approaches or design an SSA for yourself
  • Exceptions are values like all other values
  • Exception propagation is done using cxa functions and a single C++ RTTI object wrapping tyr.lang.exceptions.Throwable.

My observations so far are that the difference between the current approach and LLVM's approach of having throwing calls branch to two targets is about a factor of two in pointers. Usually, the catch target stays the same and splitting the block there has no benefit for the transformations currently performed on TIR.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .