Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Simplify_Exprs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Expr Simplify::visit(const VectorReduce *op, ExprInfo *info) {
return value;
}

if (info && op->type.is_int()) {
if (info && op->type.is_int_or_uint()) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't this also be broken for float? Why is there a type check at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hah. Potentially yes. Good point.

switch (op->op) {
case VectorReduce::Add:
// Alignment of result is the alignment of the arg. Bounds
Expand Down Expand Up @@ -123,7 +123,8 @@ Expr Simplify::visit(const VectorReduce *op, ExprInfo *info) {
case VectorReduce::Add: {
auto rewrite = IRMatcher::rewriter(IRMatcher::h_add(value, lanes), op->type);
if (rewrite(h_add(x * broadcast(y, arg_lanes), lanes), h_add(x, lanes) * broadcast(y, lanes)) ||
rewrite(h_add(broadcast(x, arg_lanes) * y, lanes), h_add(y, lanes) * broadcast(x, lanes))) {
rewrite(h_add(broadcast(x, arg_lanes) * y, lanes), h_add(y, lanes) * broadcast(x, lanes)) ||
rewrite(h_add(broadcast(x, arg_lanes), lanes), broadcast(x * factor, lanes))) {
return mutate(rewrite.result, info);
}
break;
Expand Down
18 changes: 18 additions & 0 deletions test/correctness/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,24 @@ void check_vectors() {
int_vector);
check(VectorReduce::make(VectorReduce::Max, Broadcast::make(int_vector, 4), 8),
VectorReduce::make(VectorReduce::Max, Broadcast::make(int_vector, 4), 8));

{
// h_add(broadcast(x, 8), 4) should simplify to broadcast(x * 2, 4)
check(VectorReduce::make(VectorReduce::Add, broadcast(x, 8), 4),
broadcast(x * 2, 4));
}

{
Expr const_u8 = cast(UInt(8), 3);
check(VectorReduce::make(VectorReduce::Add, broadcast(const_u8, 9), 3), broadcast(cast(UInt(8), 9), 3));
}

{
// Test VectorReduce::Add on a variable of unsigned type to ensure the multiplied factor
// keeps the correct type and avoids type-mismatch assertion failures.
Expr u8_x = Variable::make(UInt(8), "u8_x");
check(VectorReduce::make(VectorReduce::Add, broadcast(u8_x, 9), 3), broadcast(u8_x * cast(UInt(8), 3), 3));
}
}

void check_bounds() {
Expand Down
Loading