-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rethrow's immediate arguments #74
Comments
In #73 (comment), @rossberg said
So suppose we have this code. The rethrow instruction here wants to rethrow to (1).
Do you mean, we can convert the above to this?
This is already more code, and in this case those |
Well, all this becomes unnecessary with first-class exception references. Which exception you throw is a stack operand. And targeting try-catch blocks is no longer needed since you can use regular branches with exception ref argument to jump to a handler. |
Oh I think I see what you mean. So you mean we can convert
to this, by factoring out the handler code to outside?
Well, it looks possible, but it will complicates code generation, because every catch handler code that's targeted by more than one scope has to be all factored out. And this is more code too, because we need more branches, and the factored out handler code should be skipped in some case not in other cases... |
I think it's even simpler:
|
I think my code examples were a bit inaccurate. What I tried to say is, without rethrow's depth argument, code size is much bigger (especially in case of nested try-catches) and code generation becomes unnecessarily complicated. Nested example w/ rethrow depth:
Without rethrow's depth argument, we have to factor out 'handler code' part to outside. Then there has to be a lot more branches to skip the 'handler code' part.
So now we have one more And do you still think we should restore the second argument that specifies which exception to rethrow? |
AFAICS, your two versions are very different: the original does not do the equivalent of all the
In general, you need one block and one extra branch for a handler you want to target with a "rethrow". Assuming that try-catch blocks are dominated by the size of the inner code that is a negligible code size change, I'd argue. |
Oh, right... I didn't need those many And what do you think about the second argument, by which we specify which exception to rethrow? |
All the previous discussions were in the context of the other proposal variant. For some (including me) it actually was one of the key advantages of this one that it made these rethrow oddities completely unnecessary. ;) The code size win seems way too small to still justify introducing yet another special concept. That leaves code generation convenience. But we can essentially implement try with extended rethrow as just a desugaring on top of try as is, so I would be surprised if it was very difficult to handle in a codegen, compared to everything else. Do you see a problem with it? |
I think this was also resolved by #137. |
In the current spec,
rethrow
does not take any immediate argument, and only popsexcept_ref
from the top of stack and rethrows it.I'm planning to add two immediate arguments to
rethrow
. These two are not new; they have been discussed previously.Depths to break out of
Branches can break out of arbitrary number of
block
s andloop
s thanks to their immediate depth argument, butrethrow
currently does not have it. So I am planning to add a depth immediate argument torethrow
, which was first proposed in #29 (comment).One thing we should consider is, depth calculation should be slightly different from that of branches. I'd like to make the depth calculation for
rethrow
the same as that of branches, but for branches, the depth is increased at block start instructions (block
,loop
, andtry
) and is decreased at block end instruction (end
), whereas forrethrow
s, the depth should be decreased not at block end instruction (end
) but atcatch
instruction. For example,Here two
br
instructions are within the sametry
~end
scope, so they branch to the same place. But tworethrow
instructions rethrow to different places, because the level is decreased not atend
but atcatch
.To resolve this, the current LLVM toolchain implemenation maintains separate EH stack for rethrow depth calculation, in the way that EH stack depth is only incremented with try and decremented with catch and does not count other blocks or loops. As in the example below, rethrow does not count all
block
s when computing depth.@mstarzinger and I discussed how to compute rethrow's depth argument over email chain a little, and I'm also not sure if we settled on a conclusion.
Which exception to rethrow
The first version of EH proposal had an immediate argument to specify which exceptions on the stack to rethrow. So
In this example, N is used to disambiguate which caught exception is being rethrown. It could rethrow any of the three caught exceptions. Hence, rethrow 0 corresponds to the exception caught by catch 3, rethrow 1 corresponds to the exception caught by catch 2, and rethrow 3 corresponds to the exception caught by catch 1.
I don't see any use cases of this in C++, but I recall @rossberg suggested to keep this for other languages. Is that right?
The text was updated successfully, but these errors were encountered: