Fix vector_reduce simplification bug.#9031
Merged
alexreinking merged 1 commit intomainfrom Mar 15, 2026
Merged
Conversation
alexreinking
approved these changes
Mar 15, 2026
Member
alexreinking
left a comment
There was a problem hiding this comment.
The rewrite rules appear sound and the tests look correct.
Member
|
#8928 strikes again! |
abadams
reviewed
Mar 15, 2026
| } | ||
|
|
||
| if (info && op->type.is_int()) { | ||
| if (info && op->type.is_int_or_uint()) { |
Member
There was a problem hiding this comment.
Can't this also be broken for float? Why is there a type check at all?
Contributor
Author
There was a problem hiding this comment.
Hah. Potentially yes. Good point.
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.
Gemini said:
An analysis of the debug prints and Halide's source code reveals that
vector_reduce_add(x9((uint8)3))incorrectly constant-folds intox3((uint8)3)instead ofx3((uint8)9). This evaluation happens because of two closely related bugs in the simplifier forVectorReduce:Incorrect Integer Check for Bounds Tracking:
In
Simplify::visit(const VectorReduce *op, ExprInfo *info), bounds tracking attempts to properly scale bounds of an addition by multiplying the current bounds by the reduction factor. However, the condition checking if bounds should be tracked checks ifop->type.is_int(). In Halide,.is_int()checks if the type is a signed integer; it evaluates tofalsefor unsigned integer types likeuint8. As a result, the reduction scaling is completely skipped, leaving the initial bounds unchanged ([3, 3]). The mutator spots these single-point bounds and prematurely folds the expression down to exactly3. We need to use.is_int_or_uint().Missing
IRMatcherStructural Rewrite Rule:Currently, the
IRMatcherstructurally matchesvector_reduce_addof an explicit multiplication with a broadcast (h_add(x * broadcast(y, ...), ...)). However, unlikeVectorReduce::MinandVectorReduce::Maxwhich properly flatten a bare broadcast,VectorReduce::Addlacks the rule to reduceh_add(broadcast(x, arg_lanes), lanes). Addingbroadcast(x * factor, lanes)structurally captures reductions of broadcasts into equivalent scaled broadcasts even when dealing with variable expressions.Here are the fixes for both issues:
Fixes #9030.