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

Teach unrolling to exploit conditions in enclosing ifs #7969

Merged
merged 4 commits into from
Dec 8, 2023

Conversation

abadams
Copy link
Member

@abadams abadams commented Nov 30, 2023

Fixes #7968

This also changes the interface to simplify() to accept a vector of boolean Exprs that can be assumed to be true, which is a change I've been meaning to make for a while. The simplifier supports it internally, but it wasn't exposed in the API.

Copy link
Member

@vksnk vksnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a similar optimization can be done in VectorizeLoops, but I guess it's far less common (useful) for vectorize to be used like this in schedules.

@abadams
Copy link
Member Author

abadams commented Dec 4, 2023

Maybe a better design is to have a pass that forces loops that need to have a constant extent to have a constant extent and run it prior to unrolling and vectorization. Those two passes run back-to-back anyway.

@abadams
Copy link
Member Author

abadams commented Dec 4, 2023

Changed to that strategy. As a bonus this now permits vectorization of loops with a constant upper bound on their extent, by injecting a predicate, just like with unrolling. This means you can write slightly simpler schedules when you have something compute_at a GuardWithIf loop. Whereas previously you had to say:

g(x) = f(x) * 2;

g.vectorize(x, 8, TailStrategy::GuardWithIf);
f.compute_at(g, x).vectorize(x, 8, TailStrategy::GuardWithIf);

Now you can just say:

g(x) = f(x) * 2;

g.vectorize(x, 8, TailStrategy::GuardWithIf);
f.compute_at(g, x).vectorize(x);

This makes GuardWithIf schedules a little more similar to ShiftInwards or RoundUp schedules.

@abadams abadams added the release_notes For changes that may warrant a note in README for official releases. label Dec 4, 2023
@abadams
Copy link
Member Author

abadams commented Dec 4, 2023

Adding release notes tag so we remember to mention that you can now vectorize loops that merely have a constant upper bound on their extent.

@vksnk
Copy link
Member

vksnk commented Dec 5, 2023

Changed to that strategy. As a bonus this now permits vectorization of loops with a constant upper bound on their extent, by injecting a predicate, just like with unrolling. This means you can write slightly simpler schedules when you have something compute_at a GuardWithIf loop. Whereas previously you had to say:

g(x) = f(x) * 2;

g.vectorize(x, 8, TailStrategy::GuardWithIf);
f.compute_at(g, x).vectorize(x, 8, TailStrategy::GuardWithIf);

Now you can just say:

g(x) = f(x) * 2;

g.vectorize(x, 8, TailStrategy::GuardWithIf);
f.compute_at(g, x).vectorize(x);

This makes GuardWithIf schedules a little more similar to ShiftInwards or RoundUp schedules.

Ohhh, that's really nice!

@steven-johnson
Copy link
Contributor

Ready to land?

@abadams abadams merged commit 9c099c2 into main Dec 8, 2023
3 checks passed
ardier pushed a commit to ardier/Halide-mutation that referenced this pull request Mar 3, 2024
* Teach unrolling to exploit conditions in enclosing ifs

Fixes halide#7968

* Handle vectorization as well

* Remove unused usings

* Add missing print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_notes For changes that may warrant a note in README for official releases.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specialize in order to unroll intermediate doesn't work with multiple outputs
3 participants