-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix exponential blow-up in skip_stages on chains of let-bound selects #9147
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
Open
abadams
wants to merge
3
commits into
main
Choose a base branch
from
abadams/fix_exponential_skip_stages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "Halide.h" | ||
| #include <cstdio> | ||
| #include <vector> | ||
|
|
||
| // Stress test for skip_stages on a Func whose value is read via a long chain | ||
| // of CSE'd let bindings, each carrying a Select gated on an independent | ||
| // scalar Param. The setup: | ||
| // - f has a self-referencing update definition (so it gets | ||
| // compute_at(innermost) and shows up in conditionally_used_funcs). | ||
| // - A chain of derived Exprs over f is built, each referenced multiple | ||
| // times downstream so CSE materialises them as a let chain. | ||
| // - Each chain value contains a Select gated on a Param<bool>. | ||
| // | ||
| // This pattern used to scale exponentially in skip_stages: every nested | ||
| // Select roughly doubled the size of the .used / .loaded predicate that | ||
| // the mutator built up for f, because the boolean form | ||
| // `(t && cond) || (f && !cond)` couldn't recognise that the t and f | ||
| // sub-predicates were the same Expr coming from a let-stashed FuncInfo | ||
| // above. At this chain length the predicate would contain ~2^500 IR | ||
| // nodes -- i.e. skip_stages would crash trying to allocate it long | ||
| // before any wall-clock timeout fired. Post-fix it lowers in a fraction | ||
| // of a second. | ||
|
|
||
| using namespace Halide; | ||
|
|
||
| int main(int argc, char **argv) { | ||
| Var x("x"), y("y"), c("c"); | ||
|
|
||
| ImageParam src(Float(32), 3, "src"); | ||
|
|
||
| constexpr int num_params = 8; | ||
| std::vector<Param<bool>> conds; | ||
| conds.reserve(num_params); | ||
| for (int i = 0; i < num_params; i++) { | ||
| conds.emplace_back("cond" + std::to_string(i)); | ||
| } | ||
|
|
||
| // f: self-referencing update -> can't be inlined, becomes a separate | ||
| // compute_at(innermost) Func, so it shows up in skip_stages's analysis. | ||
| Func f("f"); | ||
| f(x, y, c) = src(x, y, c) * 1.5f + 0.5f; | ||
| f(x, y, c) = clamp(f(x, y, c), 0.0f, 1.0f); | ||
|
|
||
| // Build a long chain of derived expressions. Each entry references | ||
| // the immediately preceding one (chain.back()) plus two pseudo-random | ||
| // earlier ones. The dependency on chain.back() guarantees that every | ||
| // entry is reachable from the final one, so nothing gets dropped as | ||
| // dead. CSE will materialise each entry as a let in the lowered IR. | ||
| // Each chain entry's value contains a Select gated on one of the | ||
| // Param<bool>s. | ||
| constexpr int chain_len = 500; | ||
| std::vector<Expr> chain; | ||
| chain.reserve(chain_len + 3); | ||
| chain.push_back(f(x, y, 0)); | ||
| chain.push_back(f(x, y, 1)); | ||
| chain.push_back(f(x, y, 2)); | ||
| for (int i = 0; i < chain_len; i++) { | ||
| Expr a = chain.back(); | ||
| Expr b = chain[(i * 5 + 1) % chain.size()]; | ||
| Expr d = chain[(i * 7 + 2) % chain.size()]; | ||
| Expr cond = conds[i % num_params]; | ||
| Expr e = select(cond, a * b + d, (a - d) * b) + | ||
| cast<float>(i) * 0.0001f; | ||
| chain.push_back(e); | ||
| } | ||
|
|
||
| Func out("out"); | ||
| out(x, y) = chain.back(); | ||
| out.compile_jit(get_jit_target_from_environment()); | ||
| printf("Success!\n"); | ||
| return 0; | ||
| } |
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.
This (and the helpers above) seems like the kind of thing that is likely to be useful elsewhere in the compiler, maybe even duplicated already. I suppose we didn't want to call
simplify(Select::make(...))here for some reason.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.
True enough, but in this context eager simplification is absolutely required for this to not blow up. If I just made this the default behavior of operator&&/operator||/select I wouldn't have that clear guarantee at this point in the code.