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

[lang] "ti.chain_compare" now can return python-scope constants #1356

Merged
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
11 changes: 4 additions & 7 deletions python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,10 @@ def subscript(value, *indices):
def chain_compare(comparators, ops):
assert len(comparators) == len(ops) + 1, \
f'Chain comparison invoked with {len(comparators)} comparators but {len(ops)} operators'
evaluated_comparators = []
for i in range(len(comparators)):
evaluated_comparators += [expr_init(comparators[i])]
ret = expr_init(True)
ret = True
for i in range(len(ops)):
lhs = evaluated_comparators[i]
rhs = evaluated_comparators[i + 1]
lhs = comparators[i]
rhs = comparators[i + 1]
if ops[i] == 'Lt':
now = lhs < rhs
elif ops[i] == 'LtE':
Expand All @@ -133,7 +130,7 @@ def chain_compare(comparators, ops):
now = lhs != rhs
else:
assert False, f'Unknown operator {ops[i]}'
ret = ret.logical_and(now)
ret = logical_and(ret, now)
return ret


Expand Down
6 changes: 3 additions & 3 deletions taichi/ir/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ Expr load(const Expr &ptr) {
Expr ptr_if_global(const Expr &var) {
if (var.is<GlobalVariableExpression>()) {
// singleton global variable
TI_ASSERT_INFO(
var.snode()->num_active_indices == 0,
"Please always use 'x[None]' (instead of simply 'x') to access any 0-D tensor."
TI_ASSERT_INFO(var.snode()->num_active_indices == 0,
"Please always use 'x[None]' (instead of simply 'x') to "
"access any 0-D tensor.");
return var[ExprGroup()];
} else {
// may be any local or global expr
Expand Down
21 changes: 21 additions & 0 deletions tests/python/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ def func():
assert a[2] # ti.append returns 0


@ti.all_archs
def test_no_duplicate_eval_func():
a = ti.var(ti.i32, ())
b = ti.var(ti.i32, ())

@ti.func
def why_this_foo_fail(n):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix in #1380.

return ti.atomic_add(b[None], n)

def foo(n):
return ti.atomic_add(ti.subscript(b, None), n)

@ti.kernel
def func():
a[None] = 0 <= foo(2) < 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here @yuanming-hu.


func()
assert a[None] == 1
assert b[None] == 2


@ti.require(ti.extension.sparse)
@ti.all_archs
def test_chain_compare():
Expand Down