-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Track whether or not let expressions failed to solve in solver #7982
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
23afcc9
Track whether or not let expressions failed to solve in solver
abadams f7551a8
Merge remote-tracking branch 'origin/main' into abadams/track_failedn…
abadams bc89e96
Merge branch 'main' into abadams/track_failedness_through_solver_lets
steven-johnson 2246721
Remove surplus comma
abadams 9249e4a
Merge remote-tracking branch 'origin/main' into abadams/track_failedn…
abadams c5578b2
Fix use of uninitialized value that could cause bad transformation
abadams 2281f01
Merge branch 'abadams/track_failedness_through_solver_lets' of https:…
abadams 8c20cdf
Merge remote-tracking branch 'origin/main' into abadams/track_failedn…
abadams 77edbbb
Merge remote-tracking branch 'origin/main' into abadams/track_failedn…
abadams 6f5b34d
Merge branch 'main' into abadams/track_failedness_through_solver_lets
steven-johnson 85d7edd
trigger buildbots
steven-johnson ccea846
trigger buildbots
steven-johnson 9bd13a5
trigger buildbots
steven-johnson dae1bfb
trigger buildbots
steven-johnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,18 +44,22 @@ class SolveExpression : public IRMutator { | |
map<Expr, CacheEntry, ExprCompare>::iterator iter = cache.find(e); | ||
if (iter == cache.end()) { | ||
// Not in the cache, call the base class version. | ||
debug(4) << "Mutating " << e << " (" << uses_var << ")\n"; | ||
debug(4) << "Mutating " << e << " (" << uses_var << ", " << failed << ")\n"; | ||
bool old_uses_var = uses_var; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sure which C++ had a terse way to express this pattern (that wasn't an RIAA class)... |
||
uses_var = false; | ||
bool old_failed = failed; | ||
failed = false; | ||
Expr new_e = IRMutator::mutate(e); | ||
CacheEntry entry = {new_e, uses_var}; | ||
CacheEntry entry = {new_e, uses_var, failed}; | ||
uses_var = old_uses_var || uses_var; | ||
failed = old_failed || failed; | ||
cache[e] = entry; | ||
debug(4) << "(Miss) Rewrote " << e << " -> " << new_e << " (" << uses_var << ")\n"; | ||
debug(4) << "(Miss) Rewrote " << e << " -> " << new_e << " (" << uses_var << ", " << failed << ")\n"; | ||
return new_e; | ||
} else { | ||
// Cache hit. | ||
uses_var = uses_var || iter->second.uses_var; | ||
failed = failed || iter->second.failed; | ||
debug(4) << "(Hit) Rewrote " << e << " -> " << iter->second.expr << " (" << uses_var << ")\n"; | ||
return iter->second.expr; | ||
} | ||
|
@@ -75,7 +79,7 @@ class SolveExpression : public IRMutator { | |
// stateless, so we can cache everything. | ||
struct CacheEntry { | ||
Expr expr; | ||
bool uses_var; | ||
bool uses_var, failed; | ||
}; | ||
map<Expr, CacheEntry, ExprCompare> cache; | ||
|
||
|
@@ -388,16 +392,25 @@ class SolveExpression : public IRMutator { | |
const Mul *mul_a = a.as<Mul>(); | ||
Expr expr; | ||
if (a_uses_var && !b_uses_var) { | ||
const int64_t *ib = as_const_int(b); | ||
auto is_multiple_of_b = [&](const Expr &e) { | ||
if (ib) { | ||
int64_t r = 0; | ||
return reduce_expr_modulo(e, *ib, &r) && r == 0; | ||
} else { | ||
return can_prove(e / b * b == e); | ||
} | ||
}; | ||
if (add_a && !a_failed && | ||
can_prove(add_a->a / b * b == add_a->a)) { | ||
is_multiple_of_b(add_a->a)) { | ||
// (f(x) + a) / b -> f(x) / b + a / b | ||
expr = mutate(simplify(add_a->a / b) + add_a->b / b); | ||
} else if (sub_a && !a_failed && | ||
can_prove(sub_a->a / b * b == sub_a->a)) { | ||
is_multiple_of_b(sub_a->a)) { | ||
// (f(x) - a) / b -> f(x) / b - a / b | ||
expr = mutate(simplify(sub_a->a / b) - sub_a->b / b); | ||
} else if (mul_a && !a_failed && no_overflow_int(op->type) && | ||
can_prove(mul_a->b / b * b == mul_a->b)) { | ||
is_multiple_of_b(mul_a->b)) { | ||
// (f(x) * a) / b -> f(x) * (a / b) | ||
expr = mutate(mul_a->a * (mul_a->b / b)); | ||
} | ||
|
@@ -776,6 +789,7 @@ class SolveExpression : public IRMutator { | |
} else if (scope.contains(op->name)) { | ||
CacheEntry e = scope.get(op->name); | ||
uses_var = uses_var || e.uses_var; | ||
failed = failed || e.failed; | ||
return e.expr; | ||
} else if (external_scope.contains(op->name)) { | ||
Expr e = external_scope.get(op->name); | ||
|
@@ -790,11 +804,14 @@ class SolveExpression : public IRMutator { | |
|
||
Expr visit(const Let *op) override { | ||
bool old_uses_var = uses_var; | ||
bool old_failed = failed; | ||
uses_var = false; | ||
failed = false; | ||
Expr value = mutate(op->value); | ||
CacheEntry e = {value, uses_var}; | ||
|
||
CacheEntry e = {value, uses_var, failed}; | ||
uses_var = old_uses_var; | ||
failed = old_failed; | ||
|
||
ScopedBinding<CacheEntry> bind(scope, op->name, e); | ||
return mutate(op->body); | ||
} | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"must use result" should really be the default in the language, with the current behavior opt-in :-/