-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
defer implementation: instead of duplicating the code at every exit path, make a basic block that exit paths point to #283
Comments
That's what |
I'm not sure how this would work inside loops, but I'm guessing that outside of loops this would require a jump to a point near the end of the function. Is |
|
I no longer think this is important to do in stage1. Here's a proposal for how to make this work in stage2: zig codefn entry(cond: bool) i32 {
defer foo();
{
defer bar();
if (cond) {
return 10;
}
baz();
}
quux();
return ret();
} ZIR
So the idea here is to create a block for every defer expression. The defer expression goes after the block ends, which makes it run unconditionally upon exiting the block. Here I've renamed At the very end, the "return" from the function is implied. You can probably spot some easy optimizations to do here. Sometimes blocks could be elided, and defer expressions that occur in a row could be put into the same block. I should come up with an example with |
I spent some time on this. Here are some points:
Therefore I will stick with the duplicated expressions strategy, and mark this issue as an optimization, that needs to be investigated before it is accepted as the plan for the future. |
The duplicated-expression approach can be seen as an optimized version of the multiple-block one where you're inlining every single block. |
What about static variables in the duplicated expression strategy? Does Zig currently correctly handle that? |
From #110
The text was updated successfully, but these errors were encountered: