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

[opt] Properly handle FuncCallStmt in CFG and simplify passes #8139

Merged
merged 4 commits into from
Jun 6, 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
6 changes: 5 additions & 1 deletion taichi/ir/control_flow_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ Stmt *CFGNode::get_store_forwarding_data(Stmt *var, int position) const {

// Check if store_stmt will ever influence the value of var
auto may_contain_address = [](Stmt *store_stmt, Stmt *var) {
if (store_stmt->is<FuncCallStmt>()) {
return true;
}
for (auto store_ptr : irpass::analysis::get_store_destination(store_stmt)) {
if (var->is<MatrixPtrStmt>() && !store_ptr->is<MatrixPtrStmt>()) {
// check for aliased address with var
Expand Down Expand Up @@ -976,7 +979,8 @@ void ControlFlowGraph::reaching_definition_analysis(bool after_lower_access) {
for (int i = 0; i < num_nodes; i++) {
for (int j = nodes[i]->begin_location; j < nodes[i]->end_location; j++) {
auto stmt = nodes[i]->block->statements[j].get();
if ((stmt->is<MatrixPtrStmt>() &&
if (stmt->is<FuncCallStmt>() ||
(stmt->is<MatrixPtrStmt>() &&
stmt->as<MatrixPtrStmt>()->origin->is<AllocaStmt>()) ||
(!after_lower_access &&
(stmt->is<GlobalPtrStmt>() || stmt->is<ExternalPtrStmt>() ||
Expand Down
5 changes: 2 additions & 3 deletions taichi/transforms/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ class BasicBlockSimplify : public IRVisitor {
}
continue;
}
if (block->statements[j]->is<FuncCallStmt>()) {
has_store = true;
}
if (!irpass::analysis::gather_statements(
block->statements[j].get(),
[&](Stmt *s) {
Expand All @@ -107,6 +104,8 @@ class BasicBlockSimplify : public IRVisitor {
else if (auto atomic = s->cast<AtomicOpStmt>())
return irpass::analysis::maybe_same_address(
atomic->dest, stmt->src);
else if (auto func_call = s->cast<FuncCallStmt>())
return true;
else
return false;
})
Expand Down
30 changes: 13 additions & 17 deletions tests/python/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ def inc(x: ti.template()):
x[None] += 1

@ti.kernel
def run_func():
def run_func(a: ti.u1):
x[None] = 10
y[None] = 20
inc(x)
if a:
inc(x)
answer[0] = x[None]
answer[1] = y[None]
inc(y)
if a:
inc(y)
answer[2] = x[None]
answer[3] = y[None]

Expand All @@ -172,7 +174,7 @@ def verify():
assert answer[3] == 21

run_kernel()
run_func()
run_func(True)
verify()


Expand Down Expand Up @@ -324,28 +326,22 @@ def bar(a: ti.i32) -> ti.i32:

@test_utils.test(arch=[ti.cpu, ti.cuda], debug=True)
def test_ref():
# FIXME:
# Failed test if we put assert inside kernel. But test passes with assertion inside kernel if we insert print
# statement after assert.
@ti.experimental.real_func
def foo(a: ti.ref(ti.f32)):
a = 7

@ti.kernel
def bar() -> ti.f32:
def bar():
a = 5.0
foo(a)
return a
assert a == 7

assert bar() == 7
bar()


@test_utils.test(arch=[ti.cpu, ti.cuda], debug=True)
def test_ref_atomic():
# FIXME:
# 1. Failed test on Pascal (and potentially older) architecture
# 2. Failed test if we put assert inside kernel. But test passes with assertion inside kernel if we insert print
# statement after assert.
# FIXME: failed test on Pascal (and potentially older) architecture.
# Please remove this guardiance when you fix this issue
cur_arch = ti.lang.impl.get_runtime().prog.config().arch
if cur_arch == ti.cuda and ti.lang.impl.get_cuda_compute_capability() < 70:
Expand All @@ -358,12 +354,12 @@ def foo(a: ti.ref(ti.f32)):
a += a

@ti.kernel
def bar() -> ti.f32:
def bar():
a = 5.0
foo(a)
return a
assert a == 10.0

assert bar() == 10.0
bar()


@test_utils.test(arch=[ti.cpu, ti.cuda], debug=True)
Expand Down