Skip to content

Commit

Permalink
Remove rounding_halving_sub and non-existent arm rhsub instructions (
Browse files Browse the repository at this point in the history
…halide#6723)

* remove arm (s | u)rhsub instructions

* remove rounding_halving_sub intrinsic entirely
  • Loading branch information
rootjalex authored and ardier committed Mar 3, 2024
1 parent 332208e commit 78a874e
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 56 deletions.
8 changes: 0 additions & 8 deletions src/CodeGen_ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,6 @@ const ArmIntrinsic intrinsic_defs[] = {
{"vrhadds", "srhadd", Int(32, 2), "rounding_halving_add", {Int(32, 2), Int(32, 2)}, ArmIntrinsic::HalfWidth},
{"vrhaddu", "urhadd", UInt(32, 2), "rounding_halving_add", {UInt(32, 2), UInt(32, 2)}, ArmIntrinsic::HalfWidth},

// SRHSUB, URHSUB - Halving sub with rounding
{"vrhsubs", "srhsub", Int(8, 8), "rounding_halving_sub", {Int(8, 8), Int(8, 8)}, ArmIntrinsic::HalfWidth},
{"vrhsubu", "urhsub", UInt(8, 8), "rounding_halving_sub", {UInt(8, 8), UInt(8, 8)}, ArmIntrinsic::HalfWidth},
{"vrhsubs", "srhsub", Int(16, 4), "rounding_halving_sub", {Int(16, 4), Int(16, 4)}, ArmIntrinsic::HalfWidth},
{"vrhsubu", "urhsub", UInt(16, 4), "rounding_halving_sub", {UInt(16, 4), UInt(16, 4)}, ArmIntrinsic::HalfWidth},
{"vrhsubs", "srhsub", Int(32, 2), "rounding_halving_sub", {Int(32, 2), Int(32, 2)}, ArmIntrinsic::HalfWidth},
{"vrhsubu", "urhsub", UInt(32, 2), "rounding_halving_sub", {UInt(32, 2), UInt(32, 2)}, ArmIntrinsic::HalfWidth},

// SMIN, UMIN, FMIN - Min
{"vmins", "smin", Int(8, 8), "min", {Int(8, 8), Int(8, 8)}, ArmIntrinsic::HalfWidth},
{"vminu", "umin", UInt(8, 8), "min", {UInt(8, 8), UInt(8, 8)}, ArmIntrinsic::HalfWidth},
Expand Down
23 changes: 0 additions & 23 deletions src/FindIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,10 @@ class FindIntrinsics : public IRMutator {
rounding_halving_add(x, y),
is_x_same_int_or_uint) ||

rewrite(halving_add(widening_sub(x, y), 1),
rounding_halving_sub(x, y),
is_x_same_int_or_uint) ||

rewrite(rounding_shift_right(widening_add(x, y), 1),
rounding_halving_add(x, y),
is_x_same_int_or_uint) ||

rewrite(rounding_shift_right(widening_sub(x, y), 1),
rounding_halving_sub(x, y),
is_x_same_int_or_uint) ||

// Multiply-keep-high-bits patterns.
rewrite(max(min(shift_right(widening_mul(x, y), z), upper), lower),
mul_shift_right(x, y, cast(unsigned_type, z)),
Expand Down Expand Up @@ -521,10 +513,6 @@ class FindIntrinsics : public IRMutator {
halving_sub(x, y),
is_x_same_int_or_uint) ||

rewrite(rounding_shift_right(cast(op_type_wide, widening_sub(x, y)), 1),
rounding_halving_sub(x, y),
is_x_same_int_or_uint) ||

false) {
internal_assert(rewrite.result.type() == op->type)
<< "Rewrite changed type: " << Expr(op) << " -> " << rewrite.result << "\n";
Expand Down Expand Up @@ -592,13 +580,10 @@ class FindIntrinsics : public IRMutator {
if (rewrite(halving_add(x + y, 1), rounding_halving_add(x, y)) ||
rewrite(halving_add(x, y + 1), rounding_halving_add(x, y)) ||
rewrite(halving_add(x + 1, y), rounding_halving_add(x, y)) ||
rewrite(halving_add(x - y, 1), rounding_halving_sub(x, y)) ||
rewrite(halving_sub(x + 1, y), rounding_halving_sub(x, y)) ||
rewrite(halving_add(x, 1), rounding_shift_right(x, 1)) ||
rewrite(shift_right(x + y, 1), halving_add(x, y)) ||
rewrite(shift_right(x - y, 1), halving_sub(x, y)) ||
rewrite(rounding_shift_right(x + y, 1), rounding_halving_add(x, y)) ||
rewrite(rounding_shift_right(x - y, 1), rounding_halving_sub(x, y)) ||
false) {
return mutate(rewrite.result);
}
Expand Down Expand Up @@ -919,11 +904,6 @@ Expr lower_rounding_halving_add(const Expr &a, const Expr &b) {
return (a >> 1) + (b >> 1) + (((a & 1) + (b & 1) + 1) >> 1);
}

Expr lower_rounding_halving_sub(const Expr &a, const Expr &b) {
internal_assert(a.type() == b.type());
return (a >> 1) - (b >> 1) + (((a & 1) - (b & 1) + 1) >> 1);
}

Expr lower_sorted_avg(const Expr &a, const Expr &b) {
// b > a, so the following works without widening.
return a + ((b - a) >> 1);
Expand Down Expand Up @@ -1040,9 +1020,6 @@ Expr lower_intrinsic(const Call *op) {
} else if (op->is_intrinsic(Call::rounding_halving_add)) {
internal_assert(op->args.size() == 2);
return lower_rounding_halving_add(op->args[0], op->args[1]);
} else if (op->is_intrinsic(Call::rounding_halving_sub)) {
internal_assert(op->args.size() == 2);
return lower_rounding_halving_sub(op->args[0], op->args[1]);
} else if (op->is_intrinsic(Call::rounding_mul_shift_right)) {
internal_assert(op->args.size() == 3);
return lower_rounding_mul_shift_right(op->args[0], op->args[1], op->args[2]);
Expand Down
1 change: 0 additions & 1 deletion src/FindIntrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Expr lower_saturating_sub(const Expr &a, const Expr &b);
Expr lower_halving_add(const Expr &a, const Expr &b);
Expr lower_halving_sub(const Expr &a, const Expr &b);
Expr lower_rounding_halving_add(const Expr &a, const Expr &b);
Expr lower_rounding_halving_sub(const Expr &a, const Expr &b);

Expr lower_mul_shift_right(const Expr &a, const Expr &b, const Expr &q);
Expr lower_rounding_mul_shift_right(const Expr &a, const Expr &b, const Expr &q);
Expand Down
1 change: 0 additions & 1 deletion src/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ const char *const intrinsic_op_names[] = {
"return_second",
"rewrite_buffer",
"rounding_halving_add",
"rounding_halving_sub",
"rounding_mul_shift_right",
"rounding_shift_left",
"rounding_shift_right",
Expand Down
1 change: 0 additions & 1 deletion src/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ struct Call : public ExprNode<Call> {
return_second,
rewrite_buffer,
rounding_halving_add,
rounding_halving_sub,
rounding_mul_shift_right,
rounding_shift_left,
rounding_shift_right,
Expand Down
6 changes: 0 additions & 6 deletions src/IRMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1437,8 +1437,6 @@ struct Intrin {
return halving_sub(arg0, arg1);
} else if (intrin == Call::rounding_halving_add) {
return rounding_halving_add(arg0, arg1);
} else if (intrin == Call::rounding_halving_sub) {
return rounding_halving_sub(arg0, arg1);
} else if (intrin == Call::shift_left) {
return arg0 << arg1;
} else if (intrin == Call::shift_right) {
Expand Down Expand Up @@ -1556,10 +1554,6 @@ auto rounding_halving_add(A &&a, B &&b) noexcept -> Intrin<decltype(pattern_arg(
return {Call::rounding_halving_add, pattern_arg(a), pattern_arg(b)};
}
template<typename A, typename B>
auto rounding_halving_sub(A &&a, B &&b) noexcept -> Intrin<decltype(pattern_arg(a)), decltype(pattern_arg(b))> {
return {Call::rounding_halving_sub, pattern_arg(a), pattern_arg(b)};
}
template<typename A, typename B>
auto shift_left(A &&a, B &&b) noexcept -> Intrin<decltype(pattern_arg(a)), decltype(pattern_arg(b))> {
return {Call::shift_left, pattern_arg(a), pattern_arg(b)};
}
Expand Down
7 changes: 0 additions & 7 deletions src/IROperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,13 +1249,6 @@ Expr halving_sub(Expr a, Expr b) {
return Call::make(result_type, Call::halving_sub, {std::move(a), std::move(b)}, Call::PureIntrinsic);
}

Expr rounding_halving_sub(Expr a, Expr b) {
user_assert(a.defined() && b.defined()) << "rounding_halving_sub of undefined Expr\n";
match_types(a, b);
Type result_type = a.type();
return Call::make(result_type, Call::rounding_halving_sub, {std::move(a), std::move(b)}, Call::PureIntrinsic);
}

Expr mul_shift_right(Expr a, Expr b, Expr q) {
user_assert(a.defined() && b.defined() && q.defined()) << "mul_shift_right of undefined Expr\n";
user_assert(q.type().is_uint()) << "mul_shift_right shift must be unsigned\n";
Expand Down
2 changes: 0 additions & 2 deletions src/IROperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@ Expr halving_add(Expr a, Expr b);
Expr rounding_halving_add(Expr a, Expr b);
/** Compute narrow((widen(a) - widen(b)) / 2) */
Expr halving_sub(Expr a, Expr b);
/** Compute narrow((widen(a) - widen(b) + 1) / 2) */
Expr rounding_halving_sub(Expr a, Expr b);

/** Compute saturating_narrow(shift_right(widening_mul(a, b), q)) */
Expr mul_shift_right(Expr a, Expr b, Expr q);
Expand Down
7 changes: 0 additions & 7 deletions test/correctness/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ void check_intrinsics_over_range() {
{halving_add(a_expr, b_expr), (a + b) >> 1},
{rounding_halving_add(a_expr, b_expr), (a + b + 1) >> 1},
{halving_sub(a_expr, b_expr), (a - b) >> 1},
{rounding_halving_sub(a_expr, b_expr), (a - b + 1) >> 1},
};
for (const auto &p : intrinsics_with_reference_answer) {
Expr test = lower_intrinsics(p.first);
Expand Down Expand Up @@ -222,12 +221,6 @@ int main(int argc, char **argv) {
check(u8((widening_add(u8x, u8y) + 1) / 2), rounding_halving_add(u8x, u8y));
check((i32x + i32y + 1) / 2, rounding_halving_add(i32x, i32y));

check(i8((i16(i8x) - i8y + 1) / 2), rounding_halving_sub(i8x, i8y));
check(u8((u16(u8x) - u8y + 1) / 2), rounding_halving_sub(u8x, u8y));
check(i8((widening_sub(i8x, i8y) + 1) / 2), rounding_halving_sub(i8x, i8y));
check(u8((widening_sub(u8x, u8y) + 1) / 2), rounding_halving_sub(u8x, u8y));
check((i32x - i32y + 1) / 2, rounding_halving_sub(i32x, i32y));

// Check absd
check(abs(i16(i8x) - i16(i8y)), u16(absd(i8x, i8y)));
check(abs(i16(u8x) - i16(u8y)), u16(absd(u8x, u8y)));
Expand Down

0 comments on commit 78a874e

Please sign in to comment.