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] [opt] Fix CFG ignoring local atomics after lower_access #1371

Merged
merged 2 commits into from
Jul 1, 2020
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
22 changes: 17 additions & 5 deletions taichi/analysis/build_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class CFGBuilder : public IRVisitor {
int current_stmt_id;
int begin_location;
std::vector<CFGNode *> prev_nodes;
bool in_offloaded_for;
OffloadedStmt *current_offload;
bool in_parallel_for;

public:
CFGBuilder()
: current_block(nullptr),
last_node_in_current_block(nullptr),
current_stmt_id(-1),
begin_location(-1),
in_offloaded_for(false) {
current_offload(nullptr),
in_parallel_for(false) {
allow_undefined_visitor = true;
invoke_default_visitor = true;
graph = std::make_unique<ControlFlowGraph>();
Expand All @@ -40,7 +42,7 @@ class CFGBuilder : public IRVisitor {

CFGNode *new_node(int next_begin_location) {
auto node = graph->push_back(current_block, begin_location, current_stmt_id,
in_offloaded_for, last_node_in_current_block);
in_parallel_for, last_node_in_current_block);
for (auto &prev_node : prev_nodes) {
CFGNode::add_edge(prev_node, node);
}
Expand Down Expand Up @@ -125,14 +127,23 @@ class CFGBuilder : public IRVisitor {
}

void visit(RangeForStmt *stmt) override {
auto old_in_parallel_for = in_parallel_for;
if (!current_offload)
in_parallel_for = true;
visit_loop(stmt->body.get(), new_node(-1), false);
in_parallel_for = old_in_parallel_for;
}

void visit(StructForStmt *stmt) override {
auto old_in_parallel_for = in_parallel_for;
if (!current_offload)
in_parallel_for = true;
visit_loop(stmt->body.get(), new_node(-1), false);
in_parallel_for = old_in_parallel_for;
}

void visit(OffloadedStmt *stmt) override {
current_offload = stmt;
if (stmt->prologue) {
auto before_offload = new_node(-1);
int offload_stmt_id = current_stmt_id;
Expand All @@ -149,10 +160,10 @@ class CFGBuilder : public IRVisitor {
auto block_begin_index = graph->size();
if (stmt->task_type == OffloadedStmt::TaskType::range_for ||
stmt->task_type == OffloadedStmt::TaskType::struct_for) {
in_offloaded_for = true;
in_parallel_for = true;
}
stmt->body->accept(this);
in_offloaded_for = false;
in_parallel_for = false;
prev_nodes.push_back(graph->back());
// Container statements don't belong to any CFGNodes.
begin_location = offload_stmt_id + 1;
Expand All @@ -168,6 +179,7 @@ class CFGBuilder : public IRVisitor {
begin_location = offload_stmt_id + 1;
CFGNode::add_edge(before_offload, graph->nodes[block_begin_index].get());
}
current_offload = nullptr;
}

void visit(Block *block) override {
Expand Down
3 changes: 1 addition & 2 deletions taichi/ir/control_flow_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ void CFGNode::reaching_definition_analysis(bool after_lower_access) {
auto data_source_ptr = irpass::analysis::get_store_destination(stmt);
if (data_source_ptr) {
// stmt provides a data source
if (after_lower_access &&
!(stmt->is<AllocaStmt>() || stmt->is<LocalStoreStmt>())) {
if (after_lower_access && !(data_source_ptr->is<AllocaStmt>())) {
// After lower_access, we only analyze local variables.
continue;
}
Expand Down