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

Introduce TerminatorValue to force handling of divergence #5496

Merged
merged 21 commits into from
Jan 29, 2024

Conversation

jjcnn
Copy link
Contributor

@jjcnn jjcnn commented Jan 19, 2024

Description

This PR refactors how Value is used during IR conversion. The PR introduces TerminatorValue which forces the converter to handle diverging expressions properly. There were checks for divergence scattered around the code before, but it's now been made more systematic and thus more robust.

Additionally, this change allows for early returns from some functions. For instance, if the condition of a while diverges, then we don't need to generate code for the body of the loop. In fact, if an expression does diverge then the algorithm assumes that a terminator value (e.g., a branch or a return) is returned, but this was not always the case, and led to a number of spurious checks that probably didn't quite work.

The heavy lifting is done by the macro return_on_termination_or_extract, which acts as destructor of TerminatorValues.

Most changes are straightforward, but do review the functions compile_lazy_op, compile_fn_call, compile_if, compile_while_loop, compile_var_decl and compile_array_expr extra carefully, as they contain changes to the conversion algorithm. Mostly the changes are small, but they need an extra pair of eyes. In a few cases the algorithm was and still is a bit convoluted, but that's because the typechecker unhelpfully assigns Unknown instead of Never to diverging expressions, so the first sub-expression needs to be compiled and divergence checked before we can generate IR for anything that relies on the type of the expression.

Finally, I have taken the liberty of removing Value::is_diverging() and using its alias Value::is_terminator() everywhere. The concept of divergence lives in the source language, whereas the concept of terminators lives in the IR, so is_terminator seems to be more appropriate at this stage of the compilation.

Fixes #2051.

Partial fix of #5438 (the call to deterministically_aborts() is removed).

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.

@jjcnn
Copy link
Contributor Author

jjcnn commented Jan 19, 2024

@IGI-111 : This PR isn't actually ready for review yet. I just opened it as a draft to trigger the CI checks.

Sorry, I should have written it in the PR description.

@IGI-111
Copy link
Contributor

IGI-111 commented Jan 19, 2024

Yeah no problem, I understand it's not finished. I try to comment on drafts too so we waste no time in the proper review, but don't feel compelled by it if it's still WIP 😄

@jjcnn jjcnn marked this pull request as ready for review January 23, 2024 16:30
@jjcnn jjcnn requested a review from a team January 23, 2024 16:30
sway-core/src/ir_generation/function.rs Outdated Show resolved Hide resolved
sway-core/src/ir_generation/function.rs Outdated Show resolved Hide resolved
@IGI-111 IGI-111 requested a review from a team January 24, 2024 07:09
@jjcnn
Copy link
Contributor Author

jjcnn commented Jan 24, 2024

I've made a small change to compile_if to improve readability.

IGI-111
IGI-111 previously approved these changes Jan 25, 2024
Copy link
Contributor

@IGI-111 IGI-111 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 requested a review from a team January 25, 2024 13:51
@ironcev ironcev changed the title Introduce DivergingValue to force handling of divergence Introduce TerminatorValue to force handling of divergence Jan 26, 2024
@tritao
Copy link
Contributor

tritao commented Jan 26, 2024

In a few cases the algorithm was and still is a bit convoluted, but that's because the typechecker unhelpfully assigns Unknown instead of Never to diverging expressions, so the first sub-expression needs to be compiled and divergence checked before we can generate IR for anything that relies on the type of the expression.

Is this something we should be looking to fix in the future? Should we create an issue to track this improvement as well?

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 job!! The code is much more cleaner and robust now. Not to mention that the bugs are fixed 😄

Just a few comments worth addressing to avoid terminology clashes and confusions.

sway-core/src/ir_generation/function.rs Show resolved Hide resolved
sway-core/src/ir_generation/function.rs Show resolved Hide resolved
sway-core/src/ir_generation/function.rs Show resolved Hide resolved
sway-core/src/ir_generation/function.rs Show resolved Hide resolved
@jjcnn
Copy link
Contributor Author

jjcnn commented Jan 29, 2024

In a few cases the algorithm was and still is a bit convoluted, but that's because the typechecker unhelpfully assigns Unknown instead of Never to diverging expressions, so the first sub-expression needs to be compiled and divergence checked before we can generate IR for anything that relies on the type of the expression.

Is this something we should be looking to fix in the future? Should we create an issue to track this improvement as well?

I think so. I actually thought there was an issue on it already, but I can't find it. I'll open one up.

@IGI-111 IGI-111 merged commit 61279a7 into master Jan 29, 2024
37 checks passed
@IGI-111 IGI-111 deleted the jjcnn/use-compiled-values-in-ir-conversion branch January 29, 2024 14:29
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.

Improve handling aborting instructions in IR.
5 participants