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

[miscompile] Don't de-negate and change direction of shifts-by-unsigned #6782

Merged
merged 1 commit into from
May 26, 2022
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
16 changes: 9 additions & 7 deletions src/Simplify_Call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Expr Simplify::visit(const Call *op, ExprInfo *bounds) {
// If we know the sign of this shift, change it to an unsigned shift.
if (b_info.min_defined && b_info.min >= 0) {
b = mutate(cast(b.type().with_code(halide_type_uint), b), nullptr);
} else if (b_info.max_defined && b_info.max <= 0) {
} else if (b.type().is_int() && b_info.max_defined && b_info.max <= 0) {
result_op = Call::get_intrinsic_name(op->is_intrinsic(Call::shift_right) ? Call::shift_left : Call::shift_right);
b = mutate(cast(b.type().with_code(halide_type_uint), -b), nullptr);
}
Expand Down Expand Up @@ -165,12 +165,14 @@ Expr Simplify::visit(const Call *op, ExprInfo *bounds) {
}
}

// Rewrite shifts with negated RHSes as shifts of the other direction.
if (const Sub *sub = b.as<Sub>()) {
if (is_const_zero(sub->a)) {
result_op = Call::get_intrinsic_name(op->is_intrinsic(Call::shift_right) ? Call::shift_left : Call::shift_right);
b = sub->b;
return mutate(Call::make(op->type, result_op, {a, b}, Call::PureIntrinsic), bounds);
// Rewrite shifts with signed negated RHSes as shifts of the other direction.
if (b.type().is_int()) {
if (const Sub *sub = b.as<Sub>()) {
if (is_const_zero(sub->a)) {
result_op = Call::get_intrinsic_name(op->is_intrinsic(Call::shift_right) ? Call::shift_left : Call::shift_right);
b = sub->b;
return mutate(Call::make(op->type, result_op, {a, b}, Call::PureIntrinsic), bounds);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/correctness/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ tests(GROUPS correctness
set_custom_trace.cpp
shadowed_bound.cpp
shared_self_references.cpp
shift_by_unsigned_negated.cpp
shifted_image.cpp
side_effects.cpp
simd_op_check.cpp
Expand Down
46 changes: 46 additions & 0 deletions test/correctness/shift_by_unsigned_negated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Halide.h"

using namespace Halide;

template<typename T>
bool test(Func f, T f_expected, int width) {
Buffer<uint32_t> actual = f.realize({width});
for (int i = 0; i < actual.width(); i++) {
if (actual(i) != f_expected(i)) {
printf("r(%d) = %d, f_expected(%d) = %d\n",
i, actual(i), i, f_expected(i));
return false;
}
}
return true;
}

int main(int argc, char **argv) {
Buffer<uint32_t> step(31);
for (int i = 0; i < step.width(); i++) {
step(i) = -i;
}

bool success = true;
Var x;

{
Func f;
f(x) = Expr(-1U) << -step(x);
auto f_expected = [&](int x) {
return -1U << x;
};
success &= test(f, f_expected, step.width());
}
{
Func f;
f(x) = Expr(-1U) >> -step(x);
auto f_expected = [&](int x) {
return -1U >> x;
};
success &= test(f, f_expected, step.width());
}

if (success) printf("Success!\n");
return success ? 0 : -1;
}