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
Add pseudo-instruction Ladjust_trap_depth
#2322
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gretay-js for taking care of this.
I've looked at this and found it correct (with a few typos in comments).
It's only a small subset of the original #1482 PR, and should be uncontroversial.
I think it deserves a Changes entry, but otherwise should be good to go (I'm a bit biased on this, so an additional review wouldn't hurt though).
Added Changes entry and fixed typos. |
I think @xavierleroy should sign off on this, although I suspect he is in favour, as he probably wrote the original comment about this. @lthls To confirm: have you checked really carefully the part about removing dead code? I've always found that rather subtle. |
I did check carefully, and I don't think it's that subtle. Dead code can be discarded without question, except that code which can modify the stack pointer has compile-time side-effects that need to be preserved, and need to emit the correct cfi instructions. By replacing the actual trap instructions by an adjust_trap_depth instruction, we can update correctly the stack offsets and generate the correct cfi directives without having to also emit unused instructions. The only part where I feel a bit less confident is that the old code used to keep all instructions after a dead |
Prior to PR #2237, I think there was no other dead code possible between encountering the start of dead poptraps / pushtraps and the next label. The change to try-with introduced a jmp from the end of the body of the try-with to its continuation. Previously, there was a fallthrough. That new |
Thanks for the explanation. This confirms my understanding that this patch is correct. |
@xavierleroy Do you have any objections to this change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. One minor suggestion below.
in | ||
if delta_traps = 0 then next | ||
else cons_instr (Ladjust_trap_depth { delta_traps }) next | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code could be simplified by the magic of recursion.
let rec adjust_trap_depth delta_traps next =
match next.desc with
| Ladjust_trap_depth { delta_traps = k } ->
adjust_trap_depth (delta_traps + k) next.next
| _ ->
if delta_traps = 0 then next
else cons_instr (Ladjust_trap_depth { delta_traps }) next
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Rebased and magicked.
6bb003b
to
3dd79b8
Compare
Ladjust_trap_depth replaces dummy Lpushtrap generated in linearize of Iexit to notify assembler generation about updates to the stack. Ladjust_trap_depth is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code. This patch is extract from PR1482 @lthls
Replace Lpushtrap and Lpoptrap with the curresponding Ladjust_trap_depth.
3dd79b8
to
26a4a21
Compare
Merging based on Xavier's approval. |
Ladjust_trap_depth replaces dummy Lpushtrap generated in linearize of Iexit to notify assembler generation about updates to the stack. Ladjust_trap_depth is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code. This patch is extract from PR1482
Ladjust_trap_depth replaces dummy Lpushtrap generated in linearize of Iexit to notify assembler generation about updates to the stack. Ladjust_trap_depth is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code. This patch is extract from PR1482 @lthls
Ladjust_trap_depth replaces dummy Lpushtrap generated in linearize of Iexit to notify assembler generation about updates to the stack. Ladjust_trap_depth is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code. This patch is extract from PR1482 @lthls
Ladjust_trap_depth replaces dummy Lpushtrap generated in linearize of Iexit to notify assembler generation about updates to the stack. Ladjust_trap_depth is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code. This patch is extract from PR1482 @lthls
Ladjust_trap_depth
replaces dummyLpushtrap
generated inlinearize
ofIexit
to notify assembler generation about updates to the stack.Ladjust_trap_depth
is used to keep the virtual stack pointer in sync and emit dwarf information, without emitting any assembly instructions. It therefore avoids generating dead code.There was already a comment in
linearize.ml
saying that this should be done.This PR is based on #1482 @lthls. This PR also includes a more aggressive version of discard_dead_code in linearize, to eliminate dead code that results from Iexit at the end a try-with block, a common situation in large programs. This change along with #2321 eliminate 99% of dead code at the level of compliation unit found in our benchmarks.
This PR passes all tests in Inria's precheck (build # 218).
Here is an example program:
Generated assembly (amd64, compiled with flambda) with dead code after the first "jmp .LL104"