-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
constcombine pass optimizing unary and binary operators #4780
Conversation
So this optimizes patterns of the kind in #4708.
But what if we have While I'm not entirely against this approach, aren't we duplicating const-evaluation logic? |
It would be
Partially. The intersection today is quite small actually. The question can also be: "can we completely replace So that is why I do see them as different. My rationale in this case was: 1 - 2 - If we try to solve "constant folding" before generating the IR, we just need to We can alleviate this by limiting This means that the "expected behaviour", constant folding mixing well with other optimizations, will only be achieved with a fully complete "constant folding" inside the optimizer. 3 - Thus, makes more sense to do no optimization on IR generation. Having the benefits of faster compilation and a more stable IR. |
Likely not. Most const-folding will fail early, I wouldn't expect this to significantly impact compile time at all. The advantage being that we'll have all const-folding done in one place. @IGI-111 @anton-trunov any thoughts? |
I've been wondering if we're going the wrong way and doing too much too early actually. Maybe we should just check const-eval-ness using semantic checks (and syntax markers) and let the optimizer do it's thing. Ultimately we care more about binary size than we do about compile time but there might not be tension. What do the benchmarks on this say? |
Checking for that pretty much is as good as doing the optimization itself. You actually need to evaluate and see that we have a valid const (for example, you can't know something might overflow until you evaluate). So this means that we need to do all of this at the early stage. Why not use the same code right there for non-const expressions too? If it can't be easily reused, then I agree that doing it later like this is the right way. btw, I'm not strongly opposed to this, so I wouldn't mind if we merge this PR (after a proper review). More importantly, the algorithm (both new and existing ones there) are O(n^2), but easily need not be. We should file an Issue for that. |
I think we can follow with the proper review of this PR, then.
Yes. I pretty much just copied what was there. I can create a PR with proper timing to measure improvements later.
I can create another PR after this one, using the |
Alright, I think we can review and merge this. But I'll want to see benchmarks for further changes. |
Is there an advantage of the newly added The improvements to Everything else looks good to me. Once we converge on my point above, I'll approve. |
You mean the tests inside I think they are useful there because tests get much more dense and localized. |
Yes, I mean those tests. Those tests (or specifically the testing code) do not cover anything that isn't covered by the ir_gemeration tests. So this testing may as well be done there. I agree about the searchability but that's coming at the cost of duplication of testing code. Maybe somehow improve searching for the existing code itself? |
They are testing more cases tough. |
Hmmm ok. btw, you can refactor the two functions to avoid duplication of some of the code.
|
Good idea. Done. |
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 whenverbose=true
. This allows the test to be checked more easily.breaking
label because of this change: https://github.com/FuelLabs/sway/pull/4780/files#diff-b40fc3999876b0ff447de112b508250c7d0e7993f9023db29ddce8590d9b2286L5Checklist
Breaking*
orNew Feature
labels where relevant.