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

Redirect bitwise ops to logical ops in case the arguments are bool. #8597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion src/IROperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ Expr halide_log(const Expr &x_full) {

Expr use_nan = x_full < 0.0f; // log of a negative returns nan
Expr use_neg_inf = x_full == 0.0f; // log of zero is -inf
Expr exceptional = use_nan | use_neg_inf;
Expr exceptional = use_nan || use_neg_inf;

// Avoid producing nans or infs by generating ln(1.0f) instead and
// then fixing it later.
Expand Down Expand Up @@ -2419,54 +2419,81 @@ Expr reinterpret(Type t, Expr e) {
Expr operator&(Expr x, Expr y) {
match_types_bitwise(x, y, "bitwise and");
Type t = x.type();
if (t.is_bool()) {
return std::move(x) && std::move(y);
}
return Call::make(t, Call::bitwise_and, {std::move(x), std::move(y)}, Call::PureIntrinsic);
}

Expr operator&(Expr x, int y) {
Type t = x.type();
check_representable(t, y);
if (t.is_bool()) {
return std::move(x) && make_const(t, y);
}
return Call::make(t, Call::bitwise_and, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
}

Expr operator&(int x, Expr y) {
Type t = y.type();
check_representable(t, x);
if (t.is_bool()) {
return make_const(t, x) && std::move(y);
}
return Call::make(t, Call::bitwise_and, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
}

Expr operator|(Expr x, Expr y) {
match_types_bitwise(x, y, "bitwise or");
Type t = x.type();
if (t.is_bool()) {
return std::move(x) || std::move(y);
}
return Call::make(t, Call::bitwise_or, {std::move(x), std::move(y)}, Call::PureIntrinsic);
}

Expr operator|(Expr x, int y) {
Type t = x.type();
check_representable(t, y);
if (t.is_bool()) {
return std::move(x) || make_const(t, y);
}
return Call::make(t, Call::bitwise_or, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
}

Expr operator|(int x, Expr y) {
Type t = y.type();
check_representable(t, x);
if (t.is_bool()) {
return make_const(t, x) || std::move(y);
}
return Call::make(t, Call::bitwise_or, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
}

Expr operator^(Expr x, Expr y) {
match_types_bitwise(x, y, "bitwise xor");
Type t = x.type();
if (t.is_bool()) {
return std::move(x) != std::move(y);
}
return Call::make(t, Call::bitwise_xor, {std::move(x), std::move(y)}, Call::PureIntrinsic);
}

Expr operator^(Expr x, int y) {
Type t = x.type();
check_representable(t, y);
if (t.is_bool()) {
return std::move(x) != make_const(t, y);
}
return Call::make(t, Call::bitwise_xor, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
}

Expr operator^(int x, Expr y) {
Type t = y.type();
check_representable(t, x);
if (t.is_bool()) {
return make_const(t, x) != std::move(y);
}
return Call::make(t, Call::bitwise_xor, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
}

Expand Down
Loading