Skip to content
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 a simple simplify-cfg optimization for assembly #6197

Merged
merged 3 commits into from
Jun 28, 2024
Merged

Conversation

vaivaswatha
Copy link
Contributor

@vaivaswatha vaivaswatha commented Jun 28, 2024

Description

When compiling code such as

fn main() {
     while true {}
}

we would end up with unreachable code in the assembly:

.program:
.2                                      ; --- start of function: main_0 ---
pusha .2                                ; save all regs
move $$locbase $sp                      ; save locals base register for main_0
cfei i0                                 ; allocate 0 bytes for locals and 0 slots for call arguments.
move $r28 $$reta                        ; save reta
move $r29 $$retv                        ; save retv
.6
ji  .7
.7
ji  .7
.3
cfsi i0                                 ; free 0 bytes for locals and 0 slots for extra call arguments.
move $$reta $r28                        ; restore reta
popa .2                                 ; restore all regs
jmp $$reta                              ; return from call

;; --- Data ---
.data:

Here the label .3 and what follows is unreachable because the program gets stuck in an infinite loops with label .7. This means that the instruction move $r28 $$reta is dead and hence removed by ASM DCE.

Our verifier on the other hand will see a use of $r28 in the unreachable code. But with the definition having been eliminated, it will now flag an error.

The verifier is unaware of reachability. Making the verifier more accurate is probably an overkill. So we workaround this by adding an unreachable code eliminator optimization in the assembly stage. While it has its own benefits (of removing unreachable code), it'll also make the verifier happy as-is. The algorithm is linear in the size of the code.

Closes #6174.

Copy link

Benchmark for aa659f2

Click to view benchmark
Test Base PR %
code_action 5.0±0.10ms 5.3±0.04ms +6.00%
code_lens 281.4±13.21ns 281.3±4.93ns -0.04%
compile 2.5±0.02s 2.6±0.03s +4.00%
completion 4.6±0.01ms 4.7±0.04ms +2.17%
did_change_with_caching 2.4±0.04s 2.5±0.03s +4.17%
document_symbol 922.0±21.46µs 873.3±20.52µs -5.28%
format 75.5±1.12ms 75.1±0.97ms -0.53%
goto_definition 337.8±5.63µs 335.4±6.46µs -0.71%
highlight 8.7±0.21ms 9.0±0.02ms +3.45%
hover 496.6±13.07µs 486.4±5.30µs -2.05%
idents_at_position 119.4±0.34µs 118.7±0.52µs -0.59%
inlay_hints 632.9±12.69µs 638.2±5.87µs +0.84%
on_enter 478.3±18.20ns 467.4±9.10ns -2.28%
parent_decl_at_position 3.6±0.04ms 3.7±0.05ms +2.78%
prepare_rename 337.1±9.79µs 335.2±2.99µs -0.56%
rename 8.9±0.16ms 9.3±0.17ms +4.49%
semantic_tokens 1219.4±31.11µs 1245.8±12.04µs +2.16%
token_at_position 341.5±2.35µs 350.2±1.95µs +2.55%
tokens_at_position 3.6±0.03ms 3.7±0.03ms +2.78%
tokens_for_file 394.9±1.91µs 402.2±3.66µs +1.85%
traverse 37.4±1.02ms 38.4±0.92ms +2.67%

Copy link

Benchmark for d0f0cec

Click to view benchmark
Test Base PR %
code_action 5.1±0.14ms 5.2±0.07ms +1.96%
code_lens 282.9±14.70ns 280.3±8.61ns -0.92%
compile 2.5±0.02s 2.5±0.05s 0.00%
completion 4.6±0.12ms 4.7±0.05ms +2.17%
did_change_with_caching 2.5±0.02s 2.5±0.04s 0.00%
document_symbol 941.8±32.67µs 916.0±43.04µs -2.74%
format 73.0±0.83ms 73.4±0.60ms +0.55%
goto_definition 347.9±10.94µs 344.4±25.21µs -1.01%
highlight 8.7±0.22ms 9.0±0.11ms +3.45%
hover 491.8±7.42µs 492.9±5.29µs +0.22%
idents_at_position 118.8±0.28µs 118.9±0.28µs +0.08%
inlay_hints 682.5±26.38µs 644.6±19.06µs -5.55%
on_enter 467.3±12.05ns 461.3±7.87ns -1.28%
parent_decl_at_position 3.6±0.02ms 3.7±0.03ms +2.78%
prepare_rename 340.1±5.07µs 341.1±6.13µs +0.29%
rename 8.9±0.17ms 9.3±0.12ms +4.49%
semantic_tokens 1240.4±17.63µs 1260.3±13.73µs +1.60%
token_at_position 337.9±2.31µs 335.6±2.83µs -0.68%
tokens_at_position 3.5±0.05ms 3.7±0.04ms +5.71%
tokens_for_file 397.6±4.06µs 397.7±3.85µs +0.03%
traverse 38.1±0.85ms 37.6±0.98ms -1.31%

Copy link
Member

@ironcev ironcev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Super simple solution 😄

Can you please also just enable the two disabled tests:
should_pass/language/break_and_continue_block_ret
should_pass/language/abort_control_flow_good

@ironcev ironcev marked this pull request as ready for review June 28, 2024 10:32
@ironcev ironcev requested a review from a team as a code owner June 28, 2024 10:32
Copy link

Benchmark for 5d5df40

Click to view benchmark
Test Base PR %
code_action 5.0±0.06ms 5.2±0.02ms +4.00%
code_lens 283.9±9.25ns 279.8±4.29ns -1.44%
compile 2.6±0.03s 2.5±0.02s -3.85%
completion 4.7±0.07ms 4.8±0.03ms +2.13%
did_change_with_caching 2.5±0.03s 2.5±0.02s 0.00%
document_symbol 869.1±27.10µs 884.5±22.62µs +1.77%
format 73.9±1.02ms 74.0±0.81ms +0.14%
goto_definition 376.3±7.21µs 402.4±5.87µs +6.94%
highlight 8.7±0.16ms 9.1±0.06ms +4.60%
hover 524.9±9.76µs 552.9±2.65µs +5.33%
idents_at_position 119.3±0.91µs 118.9±0.88µs -0.34%
inlay_hints 612.9±25.08µs 642.3±18.68µs +4.80%
on_enter 479.5±23.47ns 468.0±9.82ns -2.40%
parent_decl_at_position 3.6±0.10ms 3.7±0.04ms +2.78%
prepare_rename 374.2±5.47µs 406.3±10.57µs +8.58%
rename 9.0±0.43ms 9.4±0.24ms +4.44%
semantic_tokens 1261.7±14.19µs 1245.0±7.53µs -1.32%
token_at_position 334.2±2.22µs 336.6±7.20µs +0.72%
tokens_at_position 3.5±0.03ms 3.7±0.02ms +5.71%
tokens_for_file 398.1±2.57µs 406.9±3.78µs +2.21%
traverse 38.7±0.81ms 38.2±0.89ms -1.29%

@ironcev ironcev merged commit fc2a90b into master Jun 28, 2024
38 checks passed
@ironcev ironcev deleted the asm_simplify_dce branch June 28, 2024 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

while true {} loop and if true branching result in Internal Compiler Error
3 participants