-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Refactor] Move VarUseDefAnalysis to header file
#14185
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2763246
refactor
yzh119 d87c1be
rename
yzh119 55c8a4c
remove redundancy
yzh119 a3a60b4
refactor
yzh119 57dcf16
split to three classes
yzh119 e00f5de
fix asf format
yzh119 e60594b
another asf header
yzh119 f1da43b
lint issue
yzh119 38d9e74
include what you use
yzh119 59f47ce
typo
yzh119 096b174
reorganize include
yzh119 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file var_use_def_analysis.cc | ||
| * \brief Classes and functions to analyze var defition and usage. | ||
| */ | ||
| #include "var_use_def_analysis.h" | ||
| namespace tvm { | ||
| namespace tir { | ||
|
|
||
| VarUseDefAnalyzer::VarUseDefAnalyzer(const Array<Var>& defined_vars, bool visit_thread_extent) | ||
| : visit_thread_extent_(visit_thread_extent) { | ||
| for (const Var v : defined_vars) { | ||
| use_count_[v.get()] = 0; | ||
| } | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const AttrStmtNode* op) { | ||
| if (op->attr_key == attr::thread_extent) { | ||
| IterVar iv = Downcast<IterVar>(op->node); | ||
| ICHECK_NE(iv->thread_tag.length(), 0U); | ||
| // thread_extent can appear multiple times | ||
| // use the first appearance as def. | ||
| if (!use_count_.count(iv->var.get())) { | ||
| this->HandleDef(iv->var.get()); | ||
| } | ||
|
|
||
| if (visit_thread_extent_) { | ||
| this->VisitExpr(op->value); | ||
| } | ||
|
|
||
| this->VisitStmt(op->body); | ||
| } else { | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const LetStmtNode* op) { | ||
| this->HandleDef(op->var.get()); | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const ForNode* op) { | ||
| this->HandleDef(op->loop_var.get()); | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const AllocateNode* op) { | ||
| this->HandleDef(op->buffer_var.get()); | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const AllocateConstNode* op) { | ||
| this->HandleDef(op->buffer_var.get()); | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const StoreNode* op) { | ||
| LOG(FATAL) << "Unexpected use of deprecated StoreNode. Please use BufferStoreNode instead."; | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitStmt_(const BufferStoreNode* op) { | ||
| VisitBuffer(op->buffer); | ||
| StmtExprVisitor::VisitStmt_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitExpr_(const LetNode* op) { | ||
| // Weaker SSA condition | ||
| // A single var can be binded in multiple lets | ||
| // but they have to bind to the same value. | ||
| // This is used to allow cases when we reuse a single let | ||
| // expression to construct a nested expr. | ||
| // (let x = 1 in x + 1) * (let x = 1 in x + 1) | ||
| auto it = let_binding_.find(op->var.get()); | ||
| this->VisitExpr(op->value); | ||
| if (it != let_binding_.end()) { | ||
| ICHECK(deep_equal_(it->second->value, op->value)) | ||
| << "Let cannot bind the same var to two different values"; | ||
| } else { | ||
| this->HandleDef(op->var.get()); | ||
| let_binding_[op->var.get()] = op; | ||
| } | ||
| this->VisitExpr(op->body); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitExpr_(const VarNode* op) { | ||
| this->HandleUse(op); | ||
| StmtExprVisitor::VisitExpr_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitExpr_(const ReduceNode* op) { | ||
| for (const auto& iv : op->axis) { | ||
| this->HandleDef(iv->var.get()); | ||
| } | ||
| StmtExprVisitor::VisitExpr_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitExpr_(const LoadNode* op) { | ||
| LOG(FATAL) << "Unexpected use of deprecated LoadNode. Please use BufferLoadNode instead."; | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitExpr_(const BufferLoadNode* op) { | ||
| VisitBuffer(op->buffer); | ||
| StmtExprVisitor::VisitExpr_(op); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::VisitBuffer(Buffer buffer) { | ||
| this->HandleUse(buffer->data.get()); | ||
| auto visit_arr = [&](Array<PrimExpr> arr) { | ||
| for (const auto& element : arr) { | ||
| this->VisitExpr(element); | ||
| } | ||
| }; | ||
|
|
||
| visit_arr(buffer->shape); | ||
| visit_arr(buffer->strides); | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::HandleDef(const VarNode* v) { | ||
| ICHECK(!def_count_.count(v)) << "variable " << v->name_hint | ||
| << " has already been defined, the Stmt is not SSA"; | ||
| ICHECK(!use_count_.count(v)) << "variable " << v->name_hint | ||
| << " has been used before definition!"; | ||
| use_count_[v] = 0; | ||
| def_count_[v] = 1; | ||
| } | ||
|
|
||
| void VarUseDefAnalyzer::HandleUse(const VarNode* v) { | ||
| auto it = use_count_.find(v); | ||
| if (it != use_count_.end()) { | ||
| if (it->second >= 0) { | ||
| ++it->second; | ||
| } | ||
| } else { | ||
| undefined_.push_back(GetRef<Var>(v)); | ||
| use_count_[v] = -1; | ||
| } | ||
| } | ||
|
|
||
| Array<Var> UndefinedVars(const Stmt& stmt, const Array<Var>& args) { | ||
| VarUseDefAnalyzer m(args); | ||
| m(stmt); | ||
| return m.undefined_; | ||
| } | ||
|
|
||
| Array<Var> UndefinedVars(const PrimExpr& expr) { | ||
| VarUseDefAnalyzer m({}); | ||
| m(expr); | ||
| return m.undefined_; | ||
| } | ||
|
|
||
| Array<Var> UndefinedVars(const PrimExpr& expr, const Array<Var>& args) { | ||
| VarUseDefAnalyzer m(args); | ||
| m(expr); | ||
| return m.undefined_; | ||
| } | ||
|
|
||
| } // namespace tir | ||
| } // namespace tvm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file tvm/src/tir/analysis/var_use_def_analyzer.h | ||
| * \brief Variable definition and usage analysis class. | ||
| */ | ||
| #ifndef TVM_TIR_ANALYSIS_VAR_USE_DEF_ANALYSIS_H_ | ||
| #define TVM_TIR_ANALYSIS_VAR_USE_DEF_ANALYSIS_H_ | ||
|
|
||
| #include <tvm/tir/analysis.h> | ||
| #include <tvm/tir/stmt_functor.h> | ||
|
|
||
| #include <unordered_map> | ||
|
|
||
| namespace tvm { | ||
| namespace tir { | ||
|
|
||
| /*! | ||
| * \brief Visitor class to perform use/def analysis, also delete unreferenced lets. | ||
| * \param defined_vars Variables that have been defined. | ||
| * \param visit_thread_extent Whether enters thread extent expressions or not. | ||
| * \sa UndefinedVars | ||
| */ | ||
| class VarUseDefAnalyzer : public StmtExprVisitor { | ||
| public: | ||
| explicit VarUseDefAnalyzer(const Array<Var>& defined_vars, bool visit_thread_extent = true); | ||
| // The fields are publically readible to | ||
| // be accessible to the users. | ||
| bool visit_thread_extent_{true}; | ||
| Array<Var> undefined_; | ||
|
|
||
| std::unordered_map<const VarNode*, int> use_count_; | ||
| std::unordered_map<const VarNode*, int> def_count_; | ||
|
|
||
| private: | ||
| ExprDeepEqual deep_equal_; | ||
| std::unordered_map<const VarNode*, const LetNode*> let_binding_; | ||
| void VisitStmt_(const AttrStmtNode* op) final; | ||
|
|
||
| void VisitStmt_(const LetStmtNode* op) final; | ||
|
|
||
| void VisitStmt_(const ForNode* op) final; | ||
|
|
||
| void VisitStmt_(const AllocateNode* op) final; | ||
|
|
||
| void VisitStmt_(const AllocateConstNode* op) final; | ||
|
|
||
| void VisitStmt_(const StoreNode* op) final; | ||
|
|
||
| void VisitStmt_(const BufferStoreNode* op) final; | ||
|
|
||
| void VisitExpr_(const LetNode* op) final; | ||
|
|
||
| void VisitExpr_(const VarNode* op) final; | ||
|
|
||
| void VisitExpr_(const ReduceNode* op) final; | ||
|
|
||
| void VisitExpr_(const LoadNode* op) final; | ||
|
|
||
| void VisitExpr_(const BufferLoadNode* op) final; | ||
|
|
||
| void HandleDef(const VarNode* v); | ||
|
|
||
| void HandleUse(const VarNode* v); | ||
|
|
||
| void VisitBuffer(Buffer buffer); | ||
| }; | ||
|
|
||
| } // namespace tir | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_TIR_ANALYSIS_VAR_USE_DEF_ANALYSIS_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.