-
Notifications
You must be signed in to change notification settings - Fork 661
[Refactor] Move ConstrVisitor to src/transform/common/constr_visitor.h for reuse
#1622
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a025139
hoist constr_visitor
silentCoder-dev 5849166
hoist constr_visitor
silentCoder-dev f89e7de
remove "using namespace tir;"
silentCoder-dev ff0bd7c
add TVM_TL_TRANSFORM_COMMON_CONSTR_VISITOR_H_
silentCoder-dev a4f2919
add default case
silentCoder-dev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| #include "tvm/arith/analyzer.h" | ||
| #include "tvm/ffi/base_details.h" | ||
| #include "tvm/ffi/object.h" | ||
| #include "tvm/ir/expr.h" | ||
| #include "tvm/tir/op.h" | ||
| #include "tvm/tir/stmt.h" | ||
| #include "tvm/tir/var.h" | ||
| #include <tvm/ffi/reflection/registry.h> | ||
| #include <tvm/tir/analysis.h> | ||
| #include <tvm/tir/builtin.h> | ||
| #include <tvm/tir/stmt_functor.h> | ||
| #include <tvm/tir/transform.h> | ||
|
|
||
|
kurisu6912 marked this conversation as resolved.
|
||
| namespace tvm::tl { | ||
|
|
||
| struct Constr { | ||
|
|
||
| enum Kind { | ||
| kConstr, | ||
| kBindValue, | ||
| kBindRange, | ||
| } kind; | ||
| bool is_assume = false; | ||
| tir::Var var; | ||
| PrimExpr value; | ||
| Range range; | ||
|
|
||
| Constr(PrimExpr constr, bool is_assume = false) | ||
| : kind(kConstr), value(constr), is_assume(is_assume) {}; | ||
| Constr(tir::Var var, PrimExpr val) | ||
| : kind(kBindValue), var(var), value(val) {}; | ||
| Constr(tir::Var var, Range range) | ||
| : kind(kBindRange), var(var), range(range) {}; | ||
|
|
||
| Constr() = default; | ||
| Constr(const Constr &other) = default; | ||
| Constr(Constr &&other) = default; | ||
| Constr &operator=(const Constr &other) = default; | ||
|
|
||
| PrimExpr ToGenericConstr() const { | ||
| switch (kind) { | ||
| case kConstr: | ||
| return value; | ||
| case kBindValue: | ||
| return var == value; | ||
| case kBindRange: | ||
| return tir::And(var >= range->min, var < (range->min + range->extent)); | ||
| } | ||
| LOG(FATAL) << "Unreachable"; | ||
| return PrimExpr(); | ||
| } | ||
| Constr Substitute(ffi::Map<tir::Var, PrimExpr> subs) const { | ||
| return Constr(tir::Substitute(ToGenericConstr(), subs)); | ||
| } | ||
| void Populate(arith::Analyzer &analyzer) const { | ||
| switch (kind) { | ||
| case kConstr: | ||
| analyzer.EnterConstraint(value); | ||
| break; | ||
| case kBindValue: | ||
| analyzer.Bind(var, value); | ||
| break; | ||
| case kBindRange: | ||
| analyzer.Bind(var, range); | ||
| break; | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| struct ConstrSet { | ||
| ConstrSet Substitute(ffi::Map<tir::Var, PrimExpr> subs) const { | ||
| ConstrSet new_set; | ||
| for (const auto &c : constrs_) { | ||
| new_set.constrs_.push_back(c.Substitute(subs)); | ||
| } | ||
| return new_set; | ||
| } | ||
| void Populate(arith::Analyzer &analyzer) const { | ||
| for (const auto &c : constrs_) { | ||
| c.Populate(analyzer); | ||
| } | ||
| } | ||
| bool CanProve(const PrimExpr &expr) const { | ||
| arith::Analyzer analyzer; | ||
| Populate(analyzer); | ||
| return analyzer.CanProve(expr); | ||
| } | ||
| template <typename... Args> void AddConstr(Args... args) { | ||
| constrs_.push_back(Constr(args...)); | ||
| } | ||
| void Extend(const ConstrSet &other) { | ||
| for (const auto &c : other.constrs_) { | ||
| constrs_.push_back(c); | ||
| } | ||
| } | ||
| std::vector<Constr> constrs_; | ||
| }; | ||
|
|
||
| struct ConstrVisitor : public tir::StmtExprVisitor { | ||
| private: | ||
| using Base = tir::StmtExprVisitor; | ||
| struct Guard { | ||
| std::vector<Constr> &constrs; | ||
| ~Guard() { constrs.pop_back(); } | ||
| }; | ||
| template <typename... Args> Guard MakeGuard(const Args... args) { | ||
| constr_stack_.push_back(Constr(args...)); | ||
| return Guard{constr_stack_}; | ||
| } | ||
|
|
||
| public: | ||
| void VisitIfThenElseExpr(const PrimExpr cond, const PrimExpr true_value, | ||
| const PrimExpr false_value) { | ||
| { | ||
| auto guard = MakeGuard(cond); | ||
| Base::VisitExpr(true_value); | ||
| } | ||
| { | ||
| auto guard = MakeGuard(tir::Not(cond)); | ||
| Base::VisitExpr(false_value); | ||
| } | ||
| } | ||
| void VisitStmt_(const tir::LetStmtNode *op) override { | ||
| auto guard = MakeGuard(op->var, op->value); | ||
| Base::VisitStmt_(op); | ||
| } | ||
| void VisitStmt_(const tir::AttrStmtNode *op) override { | ||
| if (op->attr_key == tir::attr::tilelang_assume) { | ||
| auto expr = Downcast<PrimExpr>(op->node); | ||
| auto guard = MakeGuard(expr, true); | ||
| Base::VisitStmt_(op); | ||
| } else if (op->attr_key == tir::attr::thread_extent || | ||
| op->attr_key == tir::attr::virtual_thread) { | ||
| tir::IterVar iv = Downcast<tir::IterVar>(op->node); | ||
| Range dom = | ||
| Range::FromMinExtent(tir::make_zero(op->value.dtype()), op->value); | ||
| auto guard = MakeGuard(iv->var, dom); | ||
| Base::VisitStmt_(op); | ||
| } else { | ||
| Base::VisitStmt_(op); | ||
| } | ||
| } | ||
| void VisitStmt_(const tir::AssertStmtNode *op) override { | ||
| auto guard = MakeGuard(op->condition); | ||
| Base::VisitStmt_(op); | ||
| } | ||
| void VisitStmt_(const tir::IfThenElseNode *op) override { | ||
| { | ||
| auto guard = MakeGuard(op->condition); | ||
| Base::VisitStmt(op->then_case); | ||
| } | ||
| if (op->else_case) { | ||
| auto guard = MakeGuard(tir::Not(op->condition)); | ||
| Base::VisitStmt(op->else_case.value()); | ||
| } | ||
| } | ||
| void VisitExpr_(const tir::SelectNode *op) override { | ||
| VisitIfThenElseExpr(op->condition, op->true_value, op->false_value); | ||
| } | ||
| void VisitExpr_(const tir::CallNode *op) override { | ||
| static auto op_if_then_else = Op::Get("tir.if_then_else"); | ||
| if (op->op.same_as(op_if_then_else)) { | ||
| VisitIfThenElseExpr(op->args[0], op->args[1], op->args[2]); | ||
| } else { | ||
| Base::VisitExpr_(op); | ||
| } | ||
| } | ||
| void VisitStmt_(const tir::ForNode *op) override { | ||
| if (op->kind == tir::ForKind::kParallel || | ||
| op->kind == tir::ForKind::kVectorized) { | ||
| auto guard_1 = | ||
| MakeGuard(op->loop_var, Range::FromMinExtent(op->min, op->extent)); | ||
| auto guard_2 = MakeGuard(op->extent > 0); | ||
| Base::VisitStmt_(op); | ||
| } else { | ||
| Base::VisitStmt_(op); | ||
| } | ||
| } | ||
| std::vector<Constr> constr_stack_; | ||
| }; | ||
| } // namespace tvm::tl | ||
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
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.