This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TIR] Well-Formed Verifier (apache#12166)
* tir_well_formed_verifier * fix typo * lint * fix testcase
- Loading branch information
Showing
7 changed files
with
235 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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,137 @@ | ||
/* | ||
* 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 tir/analysis/verify_well_formed.cc | ||
* \brief Check if schedulable tir is well-formed. | ||
*/ | ||
|
||
#include <tvm/runtime/registry.h> | ||
#include <tvm/tir/stmt.h> | ||
#include <tvm/tir/stmt_functor.h> | ||
|
||
#include "../ir/functor_common.h" | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
/*! \brief Verify all Expr inside the block does not contain: | ||
* 1. loop vars outside the current block. | ||
* 2. block vars of parent blocks. | ||
*/ | ||
class BlockVarAccessVerifier : public StmtExprVisitor { | ||
public: | ||
static bool Verify(const PrimFunc& func, bool assert_mode) { | ||
BlockVarAccessVerifier verifier(assert_mode); | ||
verifier(func->body); | ||
return !verifier.has_error_; | ||
} | ||
|
||
private: | ||
explicit BlockVarAccessVerifier(bool assert_mode) : assert_mode_(assert_mode) {} | ||
|
||
void VisitStmt(const Stmt& stmt) final { | ||
if (!has_error_) { | ||
StmtExprVisitor::VisitStmt(stmt); | ||
} | ||
} | ||
|
||
void VisitExpr(const PrimExpr& expr) final { | ||
if (!has_error_) { | ||
StmtExprVisitor::VisitExpr(expr); | ||
} | ||
} | ||
|
||
void VisitExpr_(const VarNode* op) final { | ||
auto it = loop_vars_.find(op); | ||
if (it != loop_vars_.end() && it->second < cur_block_level_) { | ||
has_error_ = true; | ||
if (assert_mode_) { | ||
report_error(op); | ||
} | ||
} | ||
} | ||
|
||
void VisitStmt_(const ForNode* op) final { | ||
ICHECK(loop_vars_.find(op->loop_var.get()) == loop_vars_.end()); | ||
loop_vars_[op->loop_var.get()] = cur_block_level_; | ||
StmtExprVisitor::VisitStmt_(op); | ||
loop_vars_.erase(op->loop_var.get()); | ||
} | ||
|
||
void VisitStmt_(const BlockNode* op) final { | ||
// Do not check boundary if it's a opaque block. | ||
cur_block_level_ += !op->iter_vars.empty(); | ||
|
||
// Step 0. Skip block iter var's domain | ||
|
||
// Step 1. Visit read/write regions | ||
auto fvisit_buffer_region = [this](const BufferRegion& s) { | ||
for (const auto& range : s->region) { | ||
this->VisitExpr(range->min); | ||
this->VisitExpr(range->extent); | ||
} | ||
}; | ||
VisitArray(op->reads, fvisit_buffer_region); | ||
VisitArray(op->writes, fvisit_buffer_region); | ||
|
||
// Step 2. Visit match buffers | ||
VisitArray(op->match_buffers, | ||
[fvisit_buffer_region](const MatchBufferRegion& match_buffer_region) { | ||
fvisit_buffer_region(match_buffer_region->source); | ||
}); | ||
|
||
// Step 3. Visit init and body | ||
if (op->init.defined()) { | ||
this->VisitStmt(op->init.value()); | ||
} | ||
this->VisitStmt(op->body); | ||
|
||
cur_block_level_ -= !op->iter_vars.empty(); | ||
} | ||
|
||
private: | ||
void report_error(const VarNode* var) { | ||
// TODO(siyuan): use the error message from the parser. | ||
LOG(FATAL) << "Well-formedness check failed: outside defined var " << var->name_hint | ||
<< " is used inside the current block."; | ||
} | ||
|
||
/*! \brief The map from outside loop vars to its corresponding block level. */ | ||
std::unordered_map<const VarNode*, size_t> loop_vars_; | ||
/*! \brief Whether it's in assert mode. */ | ||
bool assert_mode_; | ||
/*! \brief Current nested block stack level. */ | ||
size_t cur_block_level_{0}; | ||
/*! \brief Whether there is error. */ | ||
bool has_error_{false}; | ||
}; | ||
|
||
bool VerifyWellFormed(const PrimFunc& func, bool assert_mode) { | ||
if (!BlockVarAccessVerifier::Verify(func, assert_mode)) { | ||
return false; | ||
} | ||
// TODO(Siyuan): add more checks here. | ||
return true; | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tir.analysis.VerifyWellFormed").set_body_typed(VerifyWellFormed); | ||
|
||
} // namespace tir | ||
} // namespace tvm |
This file contains 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
57 changes: 57 additions & 0 deletions
57
tests/python/unittest/test_tir_analysis_verify_well_formed.py
This file contains 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,57 @@ | ||
# 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. | ||
import tvm | ||
import tvm.testing | ||
from tvm.script import tir as T | ||
|
||
|
||
def test_pass_simple(): | ||
@T.prim_func | ||
def element_wise( | ||
A: T.Buffer[(128, 128), "float32"], | ||
C: T.Buffer[(128, 128), "float32"], | ||
): | ||
B = T.alloc_buffer((128, 128), "float32") | ||
for i, j in T.grid(128, 128): | ||
with T.block("B"): | ||
vi, vj = T.axis.remap("SS", [i, j]) | ||
B[vi, vj] = A[vi, vj] * 2.0 | ||
for i, j in T.grid(128, 128): | ||
with T.block("C"): | ||
# It's a opaque block , so it can use outside variables | ||
C[i, j] = B[i, j] * 2.0 | ||
|
||
assert tvm.tir.analysis.verify_well_formed(element_wise) | ||
|
||
|
||
def test_fail_use_out_loop_var(): | ||
@T.prim_func | ||
def element_wise( | ||
A: T.Buffer[(128, 128), "float32"], | ||
B: T.Buffer[(128, 128), "float32"], | ||
): | ||
for i, j in T.grid(128, 128): | ||
with T.block("B"): | ||
vi, vj = T.axis.remap("SS", [i, j]) | ||
# we cannot use `i` since it's defined outside the block | ||
B[vi, vj] = A[i, vj] * 2.0 | ||
|
||
assert not tvm.tir.analysis.verify_well_formed(element_wise, assert_mode=False) | ||
|
||
|
||
if __name__ == "__main__": | ||
tvm.testing.main() |
This file contains 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 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