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

constant expressions evaluator should handle local let-expressions #4708

Open
anton-trunov opened this issue Jun 27, 2023 · 3 comments · Fixed by #4780
Open

constant expressions evaluator should handle local let-expressions #4708

anton-trunov opened this issue Jun 27, 2023 · 3 comments · Fixed by #4780
Assignees
Labels
compiler: ir IRgen and sway-ir including optimization passes enhancement New feature or request

Comments

@anton-trunov
Copy link
Contributor

For instance,

script;

fn main() -> bool {
    let result: u64 = 0 + 1 + 2 + 3 + 4 + 5;

    log(result);

    true
}

currently produces the following IR:

script {
    entry fn main() -> bool, !1 {
        entry():
        v0 = const u64 0, !2
        v1 = const u64 1, !3
        v2 = add v0, v1, !4
        v3 = const u64 2, !5
        v4 = add v2, v3, !6
        v5 = const u64 3, !7
        v6 = add v4, v5, !8
        v7 = const u64 4, !9
        v8 = add v6, v7, !10
        v9 = const u64 5, !11
        v10 = add v8, v9, !12
        v11 = const u64 0
        log u64 v10, v11, !16
        v12 = const bool true, !17
        ret bool v12
    }
}

but one would expect no add instructions for the script shown above.

@anton-trunov anton-trunov added enhancement New feature or request 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 and removed compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Jun 27, 2023
@xunilrj xunilrj self-assigned this Jul 4, 2023
@xunilrj
Copy link
Contributor

xunilrj commented Jul 6, 2023

I believe I found some limitations in the "const-eval" module. I will create a PR to address them first.

@xunilrj xunilrj mentioned this issue Jul 6, 2023
7 tasks
IGI-111 pushed a commit that referenced this issue Jul 10, 2023
## Description

This PR is necessary for #4708

It fixes some problems with `const_eval`, most importantly:

1 - It differentiates when an eval cannot be used inside const (there is
a new error for that), and when expressions/statements just do not
produce any value;
2 - It always cleans up `eval_codeblock` (it never shortcircuit with
`?`);

Another advantage of the explicit error is the exact span of the
"offending" expression, which we can use in the future for better error
messages.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Anton Trunov <anton.a.trunov@gmail.com>
@xunilrj
Copy link
Contributor

xunilrj commented Jul 10, 2023

@anton-trunov, I was unsure if changing the IR generation would be beneficial in this case. So I improved the constcombine optimization pass. See #4780

Even if you think the IR generation is a must, I would merge #4780.

Any thoughts?

xunilrj added a commit that referenced this issue Jul 13, 2023
## Description

Closes #4708.

This PR does NOT change the IR generation for expressions. Instead, it
implements constant folding for unary and binary operators. The idea is
that IR generation will be kept simple and stable, and all optimizations
are pushed onto the opt passes.

To better test this, this PR also changes some implementation details on
IR generation tests:

1 - `filecheck` directives are now collected and tested in groups. This
allows a new "::check-ir-optimized::", that can be configured with the
passes you want to test, or just fallback to "o1".

2 - `filechecker` explanations are now pretty-printed, and can be
printed when `verbose=true`. This allows the test to be checked more
easily.


![image](https://github.com/FuelLabs/sway/assets/83425/75d0f628-b271-458d-8c33-7d4d47af789a)

![image](https://github.com/FuelLabs/sway/assets/83425/9591a3d5-b0b4-4623-8a7c-1a34f5f96a99)

`breaking` label because of this change:
https://github.com/FuelLabs/sway/pull/4780/files#diff-b40fc3999876b0ff447de112b508250c7d0e7993f9023db29ddce8590d9b2286L5

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Anton Trunov <anton.a.trunov@gmail.com>
@xunilrj
Copy link
Contributor

xunilrj commented Jul 13, 2023

The optimizer pass solution is done. I will experiment with const-eval-uating expression before IR generation.

@xunilrj xunilrj reopened this Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: ir IRgen and sway-ir including optimization passes enhancement New feature or request
Projects
None yet
2 participants