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

Select condition vector lanes must match the true and false value #8465

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

abadams
Copy link
Member

@abadams abadams commented Nov 6, 2024

In our Select node, I believe the ability for the condition to be a scalar while the value is a vector was a mistake that just adds complexity. It's easy enough to just check if the condition is a broadcast if you really care about that case.

One place it added a bunch of complexity is that it meant in the RHS of the simplifier rules, sometimes you needed an implicit broadcast. These checks for an implicit broadcast on every binary node construction were responsible for about a third of the code size in Simplify_Sub.o! It's also unclear if it respects the reduction order we use to prove the simplifier terminates, because those rules were turning an implicit broadcast in the IR into a new actual Broadcast node, which may increase the total number of IR nodes, which the simplifier is not supposed to do. Now the Broadcast node is there explicitly in the input.

The select helper in IROperator.h can still take scalar conditions - but it now broadcasts them before calling Select::make. This matches the type-matching behavior of the other helpers in IROperator.h.

This shaves ~600k of code size from the compiler, and speeds up compilation of Simplify_Sub.o (and presumably other large simplifier modules) by 20%.

The ability for the condition to be a scalar while the value is a vector
was a mistake that added complexity in a few places. It's easy enough to
just check if the condition is a broadcast if you really need to know.

One place it added a bunch of complexity is that it meant in the RHS of
the simplifier rules, sometimes you needed an implicit broadcast. This
was responsible for about a third of the code size in Simplify_Sub.o!
It's also unclear if it respects the reduction order we use to prove the
simplifier terminates, because those rules were turning an implicit
broadcast in the IR into a new actual Broadcast node.
@@ -1497,7 +1497,7 @@ Expr saturating_cast(Type t, Expr e) {
Expr select(Expr condition, Expr true_value, Expr false_value) {
if (as_const_int(condition)) {
// Why are you doing this? We'll preserve the select node until constant folding for you.
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the problem with immediately throwing it away depending on the value of the constant?

Copy link
Member Author

Choose a reason for hiding this comment

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

At the time I wrote the original code, I wasn't sure in what circumstances that would ever happen so I was hesistant to not return a Select node. Maybe you're right though. It would probably only be used that way in generic code, where something is sometimes an Expr and sometimes a constant, for confused people who think you can't use a ternary operator in that case to switch between expressions based on a bool, or for people who prefer the select syntax over C++'s or python's ternary operator for some reason. In all of those cases it's fine to eagerly simplify.

@abadams
Copy link
Member Author

abadams commented Nov 6, 2024

Failures seem to be vulkan related, for which I believe fixes are in progress.

@abadams abadams merged commit b3d42e5 into main Nov 6, 2024
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants