Skip to content

Commit 52b24f3

Browse files
[bug] Fix abs on unsigned types (taichi-dev#8476)
Issue: fixes taichi-dev#8467 Remove abs on unsigned types --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4139722 commit 52b24f3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: taichi/transforms/simplify.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ class BasicBlockSimplify : public IRVisitor {
133133
}
134134
}
135135

136+
void visit(UnaryOpStmt *stmt) override {
137+
if (stmt->op_type == UnaryOpType::abs) {
138+
auto operand_type = stmt->operand->ret_type;
139+
if (is_integral(operand_type) && is_unsigned(operand_type)) {
140+
// abs(u) -> u
141+
stmt->replace_usages_with(stmt->operand);
142+
modifier.erase(stmt);
143+
}
144+
}
145+
}
136146
template <typename T>
137147
static bool identical_vectors(const std::vector<T> &a,
138148
const std::vector<T> &b) {

Diff for: tests/python/test_abs.py

+10
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ def foo(x: ti.i64) -> ti.i64:
7878

7979
for x in [-(2**40), 0, 2**40]:
8080
assert foo(x) == abs(x)
81+
82+
83+
@test_utils.test()
84+
def test_abs_u32():
85+
@ti.kernel
86+
def foo(x: ti.u32) -> ti.u32:
87+
return abs(x)
88+
89+
for x in [0, 2**20]:
90+
assert foo(x) == abs(x)

0 commit comments

Comments
 (0)