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 Erroneous handling of ndarray in real function in CFG #8245

Merged
merged 1 commit into from
Jun 30, 2023
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
24 changes: 24 additions & 0 deletions taichi/analysis/alias_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ AliasResult alias_analysis(Stmt *var1, Stmt *var2) {
if (!var1 || !var2)
return AliasResult::different;

if (var1->is<ExternalTensorBasePtrStmt>() ||
var2->is<ExternalTensorBasePtrStmt>()) {
auto *base = var1->cast<ExternalTensorBasePtrStmt>();
Stmt *other = var2;
if (!base) {
base = var2->cast<ExternalTensorBasePtrStmt>();
other = var1;
}
auto *external_ptr = other->cast<ExternalPtrStmt>();
if (!external_ptr) {
if (auto *matrix_ptr = other->cast<MatrixPtrStmt>()) {
external_ptr = matrix_ptr->origin->cast<ExternalPtrStmt>();
}
if (!external_ptr)
return AliasResult::different;
}
if (base->is_grad != external_ptr->is_grad)
return AliasResult::different;
if (base->arg_id == external_ptr->base_ptr->as<ArgLoadStmt>()->arg_id) {
return AliasResult::uncertain;
}
return AliasResult::different;
}

// TODO: further optimize with offset inside MatrixPtrStmt
// If at least one of var1 and var2 is local, they will be treated here.
auto retrieve_local = [&](Stmt *var) {
Expand Down
12 changes: 11 additions & 1 deletion taichi/analysis/gather_func_store_dests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ class GatherFuncStoreDests : public BasicStmtVisitor {
return;
}
auto result = irpass::analysis::get_store_destination(stmt);
results_.insert(result.begin(), result.end());
for (const auto &dest : result) {
if (dest->is<ExternalPtrStmt>()) {
continue;
}
if (auto matrix_ptr = dest->cast<MatrixPtrStmt>()) {
if (matrix_ptr->origin->is<ExternalPtrStmt>()) {
continue;
}
}
results_.insert(dest);
}
}

void visit(FuncCallStmt *stmt) override {
Expand Down
19 changes: 19 additions & 0 deletions tests/python/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,22 @@ def test(x: ti.types.ndarray(ndim=1)) -> vec3:
x = ti.Vector.ndarray(3, ti.f32, shape=(1))
x[0] = vec3(1, 2, 3)
assert (test(x) == vec3(1, 2, 3)).all()


@test_utils.test(arch=[ti.cpu, ti.cuda])
def test_real_func_write_ndarray_cfg():
@ti.experimental.real_func
def bar(a: ti.types.ndarray(ndim=1)):
a[0] = vec3(1)

@ti.kernel
def foo(
a: ti.types.ndarray(ndim=1),
):
a[0] = vec3(3)
bar(a)
a[0] = vec3(3)

a = ti.Vector.ndarray(3, float, shape=(2,))
foo(a)
assert (a[0] == vec3(3)).all()