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

[bug] Fix abs on unsigned types #8476

Merged
merged 4 commits into from
Feb 20, 2024
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
10 changes: 10 additions & 0 deletions taichi/transforms/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ class BasicBlockSimplify : public IRVisitor {
}
}

void visit(UnaryOpStmt *stmt) override {
if (stmt->op_type == UnaryOpType::abs) {
auto operand_type = stmt->operand->ret_type;
if (is_integral(operand_type) && is_unsigned(operand_type)) {
// abs(u) -> u
stmt->replace_usages_with(stmt->operand);
modifier.erase(stmt);
}
}
}
template <typename T>
static bool identical_vectors(const std::vector<T> &a,
const std::vector<T> &b) {
Expand Down
10 changes: 10 additions & 0 deletions tests/python/test_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ def foo(x: ti.i64) -> ti.i64:

for x in [-(2**40), 0, 2**40]:
assert foo(x) == abs(x)


@test_utils.test()
def test_abs_u32():
@ti.kernel
def foo(x: ti.u32) -> ti.u32:
return abs(x)

for x in [0, 2**20]:
assert foo(x) == abs(x)
Loading