From 539fdd97bb2971473673d45710d6dc86370436cf Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 20 Oct 2021 17:36:13 +0800 Subject: [PATCH 1/5] Add alias analysis for PtrOffsetStmt --- taichi/analysis/alias_analysis.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/taichi/analysis/alias_analysis.cpp b/taichi/analysis/alias_analysis.cpp index 24e3acfb8fa8d..9936a6c9b0888 100644 --- a/taichi/analysis/alias_analysis.cpp +++ b/taichi/analysis/alias_analysis.cpp @@ -29,8 +29,15 @@ AliasResult alias_analysis(Stmt *var1, Stmt *var2) { Stmt *origin1 = retrieve_local(var1); Stmt *origin2 = retrieve_local(var2); if (origin1 != nullptr && origin2 != nullptr) { - if (origin1 == origin2) + if (origin1 == origin2) { + if (var1->is() && var2->is()) { + auto diff = value_diff_ptr_index(var1->cast()->offset, var2->cast()->offset); + if (diff.is_diff_certain) { + return diff.diff_range == 0 ? AliasResult::same : AliasResult::different; + } + } return AliasResult::uncertain; + } if (origin1->is() || origin2->is()) return AliasResult::different; TI_ASSERT(origin1->is() && From f6346f9b2d205ce40ba4ab4df47655d8554de1e5 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 20 Oct 2021 17:38:30 +0800 Subject: [PATCH 2/5] Enable store-to-load forwarding of local tensors --- taichi/ir/control_flow_graph.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/taichi/ir/control_flow_graph.cpp b/taichi/ir/control_flow_graph.cpp index b7324a7c73233..2acc1a526e031 100644 --- a/taichi/ir/control_flow_graph.cpp +++ b/taichi/ir/control_flow_graph.cpp @@ -261,15 +261,10 @@ bool CFGNode::store_to_load_forwarding(bool after_lower_access) { if (auto local_load = stmt->cast()) { bool regular = true; auto alloca = local_load->src[0].var; - // TODO: store-to-load forwarding with TensorType Alloca - if (alloca->is()) { - regular = false; - } else { - for (int l = 0; l < stmt->width(); l++) { - if (local_load->src[l].offset != l || - local_load->src[l].var != alloca) { - regular = false; - } + for (int l = 0; l < stmt->width(); l++) { + if (local_load->src[l].offset != l || + local_load->src[l].var != alloca) { + regular = false; } } if (regular) { From 5e03ee9dca4d6982a5561d2afbcd51cbafc80bba Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 20 Oct 2021 17:40:19 +0800 Subject: [PATCH 3/5] Fix live variable analysis with local tensors --- taichi/ir/control_flow_graph.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/taichi/ir/control_flow_graph.cpp b/taichi/ir/control_flow_graph.cpp index 2acc1a526e031..650006e7485df 100644 --- a/taichi/ir/control_flow_graph.cpp +++ b/taichi/ir/control_flow_graph.cpp @@ -706,6 +706,9 @@ void ControlFlowGraph::live_variable_analysis( if (stmt->is() || stmt->is()) { return false; } + if (stmt->is() && stmt->cast()->origin->is()) { + return false; + } if (auto *gptr = stmt->cast(); gptr && config_opt.has_value()) { TI_ASSERT(gptr->snodes.size() == 1); From 0240299a2ab0c6d2879c39f190de2bf3a3dc11d1 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 20 Oct 2021 20:26:49 +0800 Subject: [PATCH 4/5] Fix: TensorType alloca is not a store --- taichi/analysis/data_source_analysis.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taichi/analysis/data_source_analysis.cpp b/taichi/analysis/data_source_analysis.cpp index d1218f947d093..9db350c8b5fb0 100644 --- a/taichi/analysis/data_source_analysis.cpp +++ b/taichi/analysis/data_source_analysis.cpp @@ -42,7 +42,7 @@ std::vector get_load_pointers(Stmt *load_stmt) { Stmt *get_store_data(Stmt *store_stmt) { // If store_stmt provides one data source, return the data. - if (store_stmt->is()) { + if (store_stmt->is() && !store_stmt->ret_type->is()) { // For convenience, return store_stmt instead of the const [0] it actually // stores. return store_stmt; From 0bd8dd7a605f1f2c714f90dd69fb9393742bb45f Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Wed, 20 Oct 2021 16:18:24 +0000 Subject: [PATCH 5/5] Auto Format --- taichi/analysis/alias_analysis.cpp | 6 ++++-- taichi/ir/control_flow_graph.cpp | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/taichi/analysis/alias_analysis.cpp b/taichi/analysis/alias_analysis.cpp index 9936a6c9b0888..c9a74b1167884 100644 --- a/taichi/analysis/alias_analysis.cpp +++ b/taichi/analysis/alias_analysis.cpp @@ -31,9 +31,11 @@ AliasResult alias_analysis(Stmt *var1, Stmt *var2) { if (origin1 != nullptr && origin2 != nullptr) { if (origin1 == origin2) { if (var1->is() && var2->is()) { - auto diff = value_diff_ptr_index(var1->cast()->offset, var2->cast()->offset); + auto diff = value_diff_ptr_index(var1->cast()->offset, + var2->cast()->offset); if (diff.is_diff_certain) { - return diff.diff_range == 0 ? AliasResult::same : AliasResult::different; + return diff.diff_range == 0 ? AliasResult::same + : AliasResult::different; } } return AliasResult::uncertain; diff --git a/taichi/ir/control_flow_graph.cpp b/taichi/ir/control_flow_graph.cpp index 650006e7485df..80ff619a70daf 100644 --- a/taichi/ir/control_flow_graph.cpp +++ b/taichi/ir/control_flow_graph.cpp @@ -706,7 +706,8 @@ void ControlFlowGraph::live_variable_analysis( if (stmt->is() || stmt->is()) { return false; } - if (stmt->is() && stmt->cast()->origin->is()) { + if (stmt->is() && + stmt->cast()->origin->is()) { return false; } if (auto *gptr = stmt->cast();