<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>On Dec 1, 2010, at 1:37 PM, Duncan Sands wrote:</div><div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Executive summary<br>-----------------<br><br>Remove the personality and list of catches out of eh.selector and stick them<br>directly on invoke instructions.<br><br>The invoke instruction<br>----------------------<br><br>The invoke instruction is modified by adding extra catch info to it:<br><br> <result> = invoke <function>(<function args>)<br> to label <normal label> unwind label <exception label> <catch info><br><br>Here <catch info> comprises all the stuff we currently bundle into eh.selector,<br>namely the personality function, a list of catch type infos and filters, and<br>a flag indicating a cleanup (in eh.selector the flag is the number 0). A<br>possible syntax:<br><br><catch info> = [personality <ptr>] [cleanup] [catches <list of catches and filters>]<br><br>Here's an example where there is no cleanup and there are two handlers:<br><br> invoke void @_Z3foov()<br> to label %invcont unwind label %catch.handlers personality <br>@__gxx_personality_v0 catches %struct.__fundamental_type_info_pseudo* @_ZTIi, <br>%struct.__pointer_type_info_pseudo* @_ZTIPKc<br><br>Note that unlike in Bill's proposal there isn't a label for each catch<br>object, just one global label (the existing unwind label).<br><br>The semantics of the invoke instruction are slightly modified: if an exception<br>unwinds and it doesn't match anything in the list of catches and filters,<br>and there is no cleanup, then control doesn't branch to the unwind label,<br>unwinding simply continues out of the function.<br><br>I marked the personality function as being optional since in fact if there<br>is only a cleanup and no catches or filters then the personality is not needed<br>(recent gcc implements this optimization).<br><br>Note that there is no longer any need to append a catch-all (as llvm-gcc<br>sometimes has to) or do any other mucking around to get proper cleanup<br>semantics, the list of catches just corresponds directly to those in the<br>original function.<br></div></blockquote><div><br></div><div>This is similar to my first proposal. But it also suffers from a major problem, which stopped that proposal dead in its tracks. Namely, you have information in one place which needs to be shared in two different, but possibly disjoint, places: the type, filters, and personality information. In order to generate the EH tables, you need to know this information at the throw site and at the place which makes the decision of which catch handler to invoke. There is no guarantee in your proposal that the invoke can be associated with the proper eh.selector call. And because of (C++) cleanups and inlining, it's the rule not the exception.</div><div><br></div><div>Example, if you have this:</div><div><br></div><div> invoke void @foo()</div><div> to label %invcont unwind label %lpad</div><div><div> personality @__gxx_personality_v0</div><div> catches %struct.__fundamental_type_info_pseudo* @_ZTIi,</div><div> %struct.__pointer_type_info_pseudo* @_ZTIPKc</div></div><div><br></div><div>lpad:</div><div> call void @bar(%A* %a) ; a cleanup</div><div> br label %ppad</div><div><br></div><div>ppad:</div><div> %eh_ptr = call i8* llvm.eh.exception()</div><div> %eh_sel = call i32 llvm.eh.selector()</div><div> ; code to clean up.</div><div><br></div><div>The call to @bar can insert an arbitrarily complex amount of code, including invokes, llvm.eh.selector calls, etc. Because there is no relationship between the invoke of @foo and %eh_sel in ppad, we lose that information at ppad, which is where we need it.</div><div><br></div><div>The code in DwarfEHPrepare::MoveExceptionValueCalls that moves the call to llvm.eh.exception into the landing pad, and which you want to do for llvm.eh.selector as well, will only complicate matters. It would introduce PHI nodes for llvm.eh.selector values like it currently does for llvm.eh.exception values.</div><br><blockquote type="cite"><div> invoke void @_Z3foov()<br> to label %"3" unwind label %lpad personality @__gxx_personality_v0 <br>catches %struct.__fundamental_type_info_pseudo* @_ZTIi, <br>%struct.__pointer_type_info_pseudo* @_ZTIPKc, i8* null<br></div></blockquote><div><br></div><div>The use of "i8* null" here is just as bad as it is for the current llvm.eh.selector call. There's no way to determine from this list whether the last value is truly the catchall value or for a catch handler.</div><br><blockquote type="cite"><div>"10": ; preds = %"5"<br> %exc_ptr31 = call i8* @llvm.eh.exception()<br> %filter32 = call i32 @llvm.eh.selector()<br> invoke void @_ZN1CD1Ev(%struct.A* %memtmp)<br> to label %"11" unwind label %fail personality @__gxx_personality_v0 <br>catches i32 1 ; <- this is an empty filter, i.e. one that catches everything<br><br></div></blockquote><div>Filter? What do you mean by this?</div><br><blockquote type="cite"><div>How is it codegened<br>-------------------<br><br>Code generation is like now, only simpler. The DwarfEHPrepare pass, which<br>currently has to do crazy things about catch-all's will still exist but much<br>simpler: it just has to ensure that the value of eh.selector makes sense no<br>matter where it is declared, like it does already for eh.exception, in fact<br>the same code could be used for both.<br><br>Currently when the code generators see an invoke, they rummage around in<br>the landing pad looking for an eh.selector call so they can extract the<br>catch info (and if it doesn't find one, it tries to look in sucessor blocks<br>because loop passes like to move eh.selector there...). Now they don't have<br>to rummage because the needed information is directly attached to the invoke.<br><br></div></blockquote><div>See my point above about the eh.selector call.</div><br><blockquote type="cite"><div>Inlining<br>--------<br><br>Many a plausible seeming exception handling scheme has fallen by the way-side<br>because it interacts poorly with inlining.<br><br>Here is how inlining would work with this scheme. It's pretty close to how<br>it works right now. Suppose you have<br><br> invoke void @foo()<br> to label %invcont unwind label %lpad <foo catch info><br><br>and you want to inline foo. Suppose foo contains an invoke:<br><br> invoke void @bar()<br> to label %invcont2 unwind label %lpad2 <bar catch info><br><br>Then after inlining you have an invoke of bar in which foo's catch info<br>has been appended to bar's:<br><br> invoke void @bar()<br> to label %invcont2 unwind label %lpad2 <joined catch info><br><br>What does appending <foo catch info> to <bar catch info> mean? If the<br>personality functions are different then you are in trouble and need to<br>disallow the inlining! The cleanup flag is the "or" of the foo and bar<br>cleanup flags. The catches are the bar catches followed by the foo<br>catches.<br><br>Now suppose foo contains a call:<br><br> call void @baz()<br><br>Then after inlining you have an invoke of baz with a copy of foo's<br>catch info:<br><br> invoke void @baz()<br> to label %continue unwind label %lpad <foo catch info><br><br>In short inlining is exactly as before, except that you have to append foo's<br>catch info to everything you inline.<br><br>Now suppose foo has an instance of the rewind instruction:<br><br> rewind i8* %exception, i32 %selector<br><br>Then after inlining this becomes:<br><br> eh.set.exception(%exception)<br> eh.set.selector(%selector)<br> br label %lpad<br><br>The calls to eh.set.exception and eh.set.selector ensure that in %lpad the<br>calls to eh.exception and eh.selector return the right values.<br><br>Will everything work?<br>---------------------<br><br>I am confident that it will work fine, for a very simple reason: this is exactly<br>what gcc does! Of course it is in disguise, a wolf in sheep's clothing some<br>might say :) In fact moving closer to gcc like this is probably the best way<br>to be sure that exception handling works properly, since gcc is what everyone<br>tests against whether we like it or not (for example libstdc++ exploits some<br>details of how gcc implements exception handling that are not specified by the<br>standard, i.e. are implementation defined, and this has caused trouble for LLVM<br>in the past).<br></div></blockquote><div><br></div><div>I would suspect that GCC has proper EH table generation mostly because it keeps tables on the side; whereas we do not and cannot. Our current EH tables are pretty poor. I would love to be able to generate tables similar to theirs.</div><div><br></div></div><div><blockquote type="cite"><div>What does it solve?<br>-------------------<br><br>It solves the problem of eh.selector calls being moved far away from landing<br>pads by optimizers (or being placed far away from landing pads by front end<br>authors who don't know that they need to be careful). It solves the problem<br>that LLVM inlining doesn't interact well with cleanups which is the reason why<br>llvm-gcc sticks catch-alls in funny places and has to stand on its head to get<br>things working. This was essentially due to (1) invoke semantics (invoke<br>always unwinds to the landing pad), and (2) inlining an _Unwind_Resume through<br>an invoke resulting in catch info being placed on the _Unwind_Resume and far<br>away from the call that actually throws the exception. People who've worked<br>in the guts of LLVM exception handling know what I'm talking about :) All of<br>this badness just goes away with this scheme.<br></div></blockquote><br><blockquote type="cite"><div>Bad things</div></blockquote><blockquote type="cite"><div>----------<br><br>I hate the way dwarf typeinfos, catches and filters are being baked into the<br>IR. Maybe metadata (see above) helps with this.<br></div></blockquote><br>Metadata cannot be counted on to remain.<br><div><br></div><div>How will your implementation allow us to remove the Horrible Hack from DwarfEHPrepare.cpp? Right now we catch and throw at almost every level that the exception can propagate up. How will your proposal solve this?</div><div><br></div><div>-bw<blockquote type="cite"></blockquote></div><div><br></div></div></body></html>