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

Dereferencing as reassignment target #5923

Merged
merged 23 commits into from
Apr 30, 2024

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Apr 26, 2024

Description

This PR implements dereferencing in reassignment targets, as defined in references. The overall effort related to references is tracked in #5063.

Up to now, it was only possible to read the values references refer to. This PR allows values to be written over &mut references. In other words, any *<expression that results in &mut>is now an l-values and can be used in left-hand sides (LHS) of assignments. In most of the cases, the <expression> will very likely be just a reference variable, but any expression that results in &mut is allowed and supported. E.g.;

*mut_ref_to_u64 = 42;

*if condition { &mut x } else { &mut y } = 42;

*max_mut(&mut x, &mut y) = 42;

Additionally, the PR:

The PR also brings additional analysis and checks to IR optimizations. Among other things, it explicitly points out the cases in which a potential usage of a symbol cannot be deterministically confirmed or denied. In this PR properly reacting for such cases is done in some optimizations. Rolling it out fully will be done in a follow up PR #5924. More advanced escape analysis will be done later on, as a part of allocating values on the heap in case of referencing.

This PR implements only dereferencing in LHS. Support for referenced elements in the element based access (without dereferencing) will be done in a separate step as an ongoing work on implementing #5063.

Closes #5736, #5737, #5920, #5922.

Demo

Before:
Assignment to constant - Before

Assignment to decl - Before

After:
Assignment to constant - After

Assignment to decl - After

Expression cannot be assigned to:

Expression cannot be assigned to

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

Copy link

Benchmark for fd7f2c7

Click to view benchmark
Test Base PR %
code_action 5.5±0.11ms 5.6±0.12ms +1.82%
code_lens 329.6±3.90ns 288.1±11.22ns -12.59%
compile 6.2±0.09s 6.1±0.05s -1.61%
completion 5.0±0.08ms 5.0±0.14ms 0.00%
did_change_with_caching 6.5±0.08s 6.1±0.07s -6.15%
document_symbol 1080.0±38.74µs 967.1±28.00µs -10.45%
format 74.1±0.91ms 89.3±1.07ms +20.51%
goto_definition 362.7±4.74µs 353.0±3.80µs -2.67%
highlight 9.0±0.04ms 9.0±0.19ms 0.00%
hover 598.5±6.34µs 598.0±19.65µs -0.08%
idents_at_position 122.7±0.57µs 123.5±2.60µs +0.65%
inlay_hints 667.5±23.68µs 662.4±27.65µs -0.76%
on_enter 495.6±6.85ns 483.1±11.80ns -2.52%
parent_decl_at_position 3.7±0.03ms 3.7±0.18ms 0.00%
prepare_rename 359.5±5.72µs 358.4±24.16µs -0.31%
rename 9.7±0.19ms 9.6±0.14ms -1.03%
semantic_tokens 1035.6±15.45µs 1029.0±15.80µs -0.64%
token_at_position 354.3±3.06µs 355.6±7.33µs +0.37%
tokens_at_position 3.7±0.02ms 3.7±0.06ms 0.00%
tokens_for_file 416.9±1.87µs 421.1±1.78µs +1.01%
traverse 52.5±1.54ms 51.0±2.46ms -2.86%

@ironcev ironcev self-assigned this Apr 26, 2024
@ironcev ironcev added bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged compiler: ir IRgen and sway-ir including optimization passes compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen DCA Everything to do with Dead Code Analysis labels Apr 26, 2024
Copy link

Benchmark for 66646f7

Click to view benchmark
Test Base PR %
code_action 5.6±0.07ms 5.5±0.02ms -1.79%
code_lens 334.9±8.37ns 287.0±21.85ns -14.30%
compile 6.1±0.08s 6.1±0.03s 0.00%
completion 5.0±0.09ms 4.9±0.07ms -2.00%
did_change_with_caching 6.0±0.05s 6.1±0.04s +1.67%
document_symbol 989.3±39.15µs 1050.8±21.43µs +6.22%
format 71.6±1.10ms 86.4±0.89ms +20.67%
goto_definition 361.7±6.25µs 384.3±3.50µs +6.25%
highlight 9.0±0.09ms 9.2±0.10ms +2.22%
hover 608.2±9.06µs 601.4±7.24µs -1.12%
idents_at_position 122.9±0.60µs 123.3±1.22µs +0.33%
inlay_hints 662.0±12.58µs 687.5±9.54µs +3.85%
on_enter 496.8±15.31ns 479.8±12.94ns -3.42%
parent_decl_at_position 3.7±0.02ms 3.7±0.05ms 0.00%
prepare_rename 365.3±5.97µs 356.6±17.05µs -2.38%
rename 9.8±0.13ms 9.6±0.01ms -2.04%
semantic_tokens 1007.5±10.53µs 1033.0±25.52µs +2.53%
token_at_position 360.4±9.09µs 363.8±3.27µs +0.94%
tokens_at_position 3.7±0.03ms 3.7±0.03ms 0.00%
tokens_for_file 417.9±2.20µs 433.5±2.80µs +3.73%
traverse 49.3±1.49ms 49.2±1.14ms -0.20%

Copy link

Benchmark for d32779f

Click to view benchmark
Test Base PR %
code_action 5.5±0.02ms 5.5±0.04ms 0.00%
code_lens 327.3±10.42ns 289.9±4.96ns -11.43%
compile 6.1±0.07s 6.1±0.08s 0.00%
completion 4.9±0.06ms 5.0±0.15ms +2.04%
did_change_with_caching 6.1±0.08s 6.0±0.06s -1.64%
document_symbol 1054.4±27.95µs 929.8±14.51µs -11.82%
format 70.1±0.89ms 87.1±0.92ms +24.25%
goto_definition 363.1±4.77µs 366.8±7.32µs +1.02%
highlight 9.1±0.14ms 8.9±0.03ms -2.20%
hover 597.5±14.17µs 604.2±20.93µs +1.12%
idents_at_position 122.4±0.24µs 123.2±1.62µs +0.65%
inlay_hints 666.4±22.23µs 662.3±27.24µs -0.62%
on_enter 489.4±11.74ns 482.0±13.08ns -1.51%
parent_decl_at_position 3.7±0.02ms 3.7±0.06ms 0.00%
prepare_rename 365.9±8.17µs 361.8±7.04µs -1.12%
rename 9.8±0.14ms 9.5±0.16ms -3.06%
semantic_tokens 1096.3±11.56µs 1039.3±14.25µs -5.20%
token_at_position 356.8±1.80µs 356.0±13.21µs -0.22%
tokens_at_position 3.7±0.04ms 3.8±0.30ms +2.70%
tokens_for_file 419.1±3.76µs 429.0±2.54µs +2.36%
traverse 51.4±1.78ms 51.3±2.09ms -0.19%

@ironcev ironcev marked this pull request as ready for review April 26, 2024 21:10
@ironcev ironcev requested review from vaivaswatha and a team April 26, 2024 21:10
Copy link

Benchmark for 3d17592

Click to view benchmark
Test Base PR %
code_action 5.6±0.07ms 5.6±0.12ms 0.00%
code_lens 324.3±4.60ns 288.7±9.70ns -10.98%
compile 6.2±0.06s 6.2±0.07s 0.00%
completion 5.0±0.08ms 5.1±0.33ms +2.00%
did_change_with_caching 6.2±0.03s 6.2±0.06s 0.00%
document_symbol 994.0±34.71µs 934.7±21.83µs -5.97%
format 72.9±1.10ms 90.2±1.36ms +23.73%
goto_definition 364.1±2.67µs 368.0±8.50µs +1.07%
highlight 9.1±0.07ms 9.0±0.24ms -1.10%
hover 642.4±11.14µs 603.5±23.91µs -6.06%
idents_at_position 122.5±0.59µs 123.5±2.06µs +0.82%
inlay_hints 664.6±19.77µs 668.3±7.61µs +0.56%
on_enter 493.0±13.36ns 482.2±15.95ns -2.19%
parent_decl_at_position 3.7±0.04ms 3.7±0.10ms 0.00%
prepare_rename 356.1±4.05µs 363.3±6.39µs +2.02%
rename 9.9±0.25ms 9.8±0.22ms -1.01%
semantic_tokens 1023.6±18.79µs 1030.7±11.58µs +0.69%
token_at_position 360.4±2.70µs 359.7±3.33µs -0.19%
tokens_at_position 3.7±0.03ms 3.7±0.09ms 0.00%
tokens_for_file 462.5±4.40µs 429.4±1.42µs -7.16%
traverse 51.0±1.46ms 51.5±1.22ms +0.98%

Copy link

Benchmark for 2722d9b

Click to view benchmark
Test Base PR %
code_action 5.6±0.09ms 5.5±0.12ms -1.79%
code_lens 342.1±5.61ns 284.2±6.72ns -16.92%
compile 6.1±0.05s 6.1±0.10s 0.00%
completion 5.0±0.05ms 4.9±0.11ms -2.00%
did_change_with_caching 6.2±0.07s 6.2±0.07s 0.00%
document_symbol 937.8±19.60µs 984.1±34.75µs +4.94%
format 73.0±7.59ms 87.6±1.05ms +20.00%
goto_definition 364.1±4.79µs 361.6±8.86µs -0.69%
highlight 9.2±0.11ms 9.0±0.16ms -2.17%
hover 616.9±13.16µs 595.9±8.78µs -3.40%
idents_at_position 122.9±2.46µs 121.9±0.66µs -0.81%
inlay_hints 672.4±6.60µs 661.1±12.04µs -1.68%
on_enter 465.5±23.65ns 478.9±15.16ns +2.88%
parent_decl_at_position 3.7±0.11ms 3.7±0.03ms 0.00%
prepare_rename 359.6±2.92µs 362.8±15.48µs +0.89%
rename 9.8±0.15ms 9.6±0.41ms -2.04%
semantic_tokens 1050.0±16.64µs 1045.0±18.71µs -0.48%
token_at_position 360.0±10.27µs 354.3±4.25µs -1.58%
tokens_at_position 3.7±0.08ms 3.7±0.05ms 0.00%
tokens_for_file 422.9±7.55µs 423.1±3.49µs +0.05%
traverse 51.7±2.21ms 49.8±2.15ms -3.68%

Copy link

Benchmark for b76f699

Click to view benchmark
Test Base PR %
code_action 5.6±0.13ms 5.5±0.11ms -1.79%
code_lens 324.1±10.40ns 285.8±9.24ns -11.82%
compile 6.1±0.06s 6.2±0.06s +1.64%
completion 4.9±0.02ms 4.9±0.01ms 0.00%
did_change_with_caching 6.0±0.06s 6.2±0.08s +3.33%
document_symbol 1013.0±32.81µs 942.0±17.28µs -7.01%
format 70.7±0.64ms 86.0±1.34ms +21.64%
goto_definition 366.3±5.48µs 365.1±5.96µs -0.33%
highlight 9.0±0.02ms 9.0±0.09ms 0.00%
hover 618.1±25.50µs 601.1±18.78µs -2.75%
idents_at_position 123.3±0.42µs 122.3±1.42µs -0.81%
inlay_hints 672.5±10.46µs 678.3±17.47µs +0.86%
on_enter 507.7±9.11ns 484.1±20.58ns -4.65%
parent_decl_at_position 3.7±0.03ms 3.7±0.03ms 0.00%
prepare_rename 359.8±4.36µs 354.8±7.33µs -1.39%
rename 9.6±0.31ms 9.5±0.14ms -1.04%
semantic_tokens 1050.7±13.51µs 1072.7±15.26µs +2.09%
token_at_position 356.5±2.42µs 352.9±3.52µs -1.01%
tokens_at_position 3.7±0.02ms 3.7±0.03ms 0.00%
tokens_for_file 424.3±6.56µs 422.1±1.40µs -0.52%
traverse 50.2±1.68ms 49.4±1.56ms -1.59%

Copy link
Contributor

@jjcnn jjcnn left a comment

Choose a reason for hiding this comment

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

I have not yet had a chance to look through your tests, so I'm holding off on final approval. I've looked through all the code, though, and except for a couple of minor issues here and there everything looks sound.

Copy link
Contributor

@jjcnn jjcnn left a comment

Choose a reason for hiding this comment

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

One small but (I think) important mistake in one of the tests, but other than that the tests look fine.

@ironcev ironcev requested review from jjcnn, xunilrj and a team April 30, 2024 15:34
Copy link

Benchmark for 4215a05

Click to view benchmark
Test Base PR %
code_action 5.6±0.10ms 5.4±0.03ms -3.57%
code_lens 359.8±29.44ns 289.3±28.37ns -19.59%
compile 6.3±0.04s 6.3±0.04s 0.00%
completion 5.0±0.09ms 4.9±0.20ms -2.00%
did_change_with_caching 6.3±0.07s 6.3±0.06s 0.00%
document_symbol 1010.0±51.15µs 955.5±32.31µs -5.40%
format 72.7±1.71ms 86.9±1.74ms +19.53%
goto_definition 361.0±6.74µs 370.9±7.37µs +2.74%
highlight 9.0±0.15ms 9.0±0.23ms 0.00%
hover 605.7±9.27µs 603.8±17.83µs -0.31%
idents_at_position 123.4±0.35µs 123.0±1.74µs -0.32%
inlay_hints 666.9±16.86µs 665.8±21.00µs -0.16%
on_enter 490.6±17.45ns 479.8±20.92ns -2.20%
parent_decl_at_position 3.7±0.02ms 3.7±0.05ms 0.00%
prepare_rename 355.6±8.34µs 365.7±12.86µs +2.84%
rename 9.6±0.08ms 9.7±0.86ms +1.04%
semantic_tokens 1026.1±7.24µs 1046.3±13.09µs +1.97%
token_at_position 379.0±5.96µs 352.4±2.04µs -7.02%
tokens_at_position 3.7±0.04ms 3.7±0.06ms 0.00%
tokens_for_file 463.8±2.46µs 424.3±2.55µs -8.52%
traverse 51.9±2.04ms 51.5±1.33ms -0.77%

Copy link
Contributor

@jjcnn jjcnn left a comment

Choose a reason for hiding this comment

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

LGTM

@IGI-111 IGI-111 merged commit daee1c2 into master Apr 30, 2024
41 checks passed
@IGI-111 IGI-111 deleted the ironcev/references-in-reassignments branch April 30, 2024 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: ir IRgen and sway-ir including optimization passes compiler General compiler. Should eventually become more specific as the issue is triaged DCA Everything to do with Dead Code Analysis
Projects
None yet
4 participants