Eagerly constant fold all arithmetic - #771
Open
dsharlet wants to merge 9 commits into
Open
Conversation
vksnk
approved these changes
Feb 9, 2026
|
|
||
| SLINKY_NO_INLINE void test_expr(expr pattern, expr replacement, const std::string& rule_str) { | ||
| SLINKY_NO_INLINE void test_expr(expr pattern, expr replacement, const std::string& rule_str, bool& applied) { | ||
| expr simplified = simplify(pattern); |
Collaborator
There was a problem hiding this comment.
I think there should be applied = false; in the beginning, so it doesn't have to rely on what was passed (and I think it's incorrect to keep it unchanged).
| ASSERT_THAT(constant_upper_bound(min(x, 4)), matches(4)); | ||
| ASSERT_THAT(constant_upper_bound(max(x, 4)), matches(max(x, 4))); | ||
| ASSERT_THAT(constant_upper_bound(!min(x, 0)), matches(1)); | ||
| ASSERT_THAT(constant_upper_bound(!min(x, -1)), matches(1)); // TODO: We might be able to prove this is 0 |
Collaborator
There was a problem hiding this comment.
re TODO: Shouldn't this always be 1?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This changes the construction of expressions to eagerly constant fold arithmetic as we encounter it.
When client code is constructing expressions, they might check if an expression is a constant, but that expression might be constant arithmetic that produces that constant instead. This is an annoying thing to deal with and leads to bugs.
The main downside of this change is that it doesn't maintain expressions as the user expected. This is both surprising in a different way, and this also did affect our tests a lot.
I had hoped this would make the simplifier faster, but it's actually slower. I guess this makes sense, the constant folding logic in the simplifier was cheap, but now we do that logic when constructing all expressions.
The change also uncovered quite a few bugs:
min(a, b) < cand similar cases.!xwas wrongSome of these bugs are pretty surprising, they are not subtle edge cases.