-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[TIR][Analysis][Arith] Implement basic data-flow analysis #13130
Changes from all commits
8af309b
9d2c423
a5921f2
970d748
c849e24
287d0d3
794c0d9
1bbf96f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,7 +292,7 @@ std::function<void()> RewriteSimplifier::Impl::EnterConstraint(const PrimExpr& c | |
// we will compare the already simplified result with the constraint, | ||
// so simplify the constraint as well | ||
PrimExpr new_constraint = operator()(constraint); | ||
for (const PrimExpr& subconstraint : ExtractConstraints(new_constraint)) { | ||
for (const PrimExpr& subconstraint : ExtractConstraints(new_constraint, false)) { | ||
if (SideEffect(subconstraint) <= CallEffectKind::kPure) { | ||
literal_constraints_.push_back(subconstraint); | ||
PrimExpr negation; | ||
|
@@ -1734,7 +1734,7 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const AndNode* op) { | |
// Pattern var to match any expression | ||
PVar<PrimExpr> x, y; | ||
// Pattern var match IntImm | ||
PVar<IntImm> c1, c2; | ||
PVar<IntImm> c1, c2, c3; | ||
PVar<int> lanes; | ||
|
||
if (op->dtype.lanes() != 1) { | ||
|
@@ -1761,6 +1761,55 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const AndNode* op) { | |
|
||
TVM_TRY_REWRITE(x == c1 && x != c2, x == c1 && c1 != c2); | ||
TVM_TRY_REWRITE(x != c2 && x == c1, x == c1 && c1 != c2); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add tests for these. Tests using negative numbers too would be great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, and updated. There were a couple of the rewrite rules that had incorrect behavior for negative numerators, so those are now fixed. |
||
TVM_TRY_RECURSIVE_REWRITE(floordiv(x, c2) == c1 && floormod(x, c2) == c3, x == c1 * c2 + c3); | ||
TVM_TRY_RECURSIVE_REWRITE(floormod(x, c2) == c3 && floordiv(x, c2) == c1, x == c1 * c2 + c3); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE_IF(0 <= x - y * c1 && | ||
x - y * c1<c1, y == floordiv(x, c1), c1.Eval()->value> 0); | ||
TVM_TRY_RECURSIVE_REWRITE_IF(x - y * c1 < c1 && 0 <= x - y * c1, y == floordiv(x, c1), | ||
c1.Eval()->value > 0); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE(c1 < x - y * c1 && x - y * c1 <= 0, y == floordiv(x, c1)); | ||
TVM_TRY_RECURSIVE_REWRITE(x - y * c1 < c1 && 0 <= x - y * c1, y == floordiv(x, c1)); | ||
TVM_TRY_RECURSIVE_REWRITE_IF(0 <= x + y * c2 && x + y * c2 < c1, y == floordiv(x, c1), | ||
c2.Eval()->value == -c1.Eval()->value); | ||
TVM_TRY_RECURSIVE_REWRITE_IF(x + y * c2 < c1 && 0 <= x + y * c2, y == floordiv(x, c1), | ||
c2.Eval()->value == -c1.Eval()->value); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE_IF(x < c1 && floormod(x, c2) < c3, | ||
x < c1 - c2 + c3 && floormod(x, c2) < c3, | ||
c1.Eval()->value % c2.Eval()->value == 0); | ||
TVM_TRY_RECURSIVE_REWRITE_IF( | ||
x < c1 && floormod(x, c2) < c3, x < c1 - floormod(c1, c2) + c3 && floormod(x, c2) < c3, | ||
(c1.Eval()->value % c2.Eval()->value + c2.Eval()->value) % c2.Eval()->value > | ||
c3.Eval()->value); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE_IF(x <= c1 && floormod(x, c2) < c3, | ||
x < c1 + 1 - c2 + c3 && floormod(x, c2) < c3, | ||
(c1.Eval()->value + 1) % c2.Eval()->value == 0); | ||
TVM_TRY_RECURSIVE_REWRITE_IF( | ||
x <= c1 && floormod(x, c2) < c3, x < c1 + 1 - floormod(c1, c2) + c3 && floormod(x, c2) < c3, | ||
(((c1.Eval()->value + 1) % c2.Eval()->value) + c2.Eval()->value) % c2.Eval()->value > | ||
c3.Eval()->value); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE(floordiv(x, c2) == c1 && floormod(x, c2) < c3, | ||
c1 * c2 <= x && x < c1 * c2 + c3); | ||
TVM_TRY_RECURSIVE_REWRITE(floormod(x, c2) < c3 && floordiv(x, c2) == c1, | ||
c1 * c2 <= x && x < c1 * c2 + c3); | ||
TVM_TRY_RECURSIVE_REWRITE(floordiv(x, c2) == c1 && floormod(x, c2) <= c3, | ||
c1 * c2 <= x && x <= c1 * c2 + c3); | ||
TVM_TRY_RECURSIVE_REWRITE(floormod(x, c2) <= c3 && floordiv(x, c2) == c1, | ||
c1 * c2 <= x && x <= c1 * c2 + c3); | ||
|
||
TVM_TRY_RECURSIVE_REWRITE(floordiv(x, c2) == c1 && c3 <= floormod(x, c2), | ||
c1 * c2 + c3 <= x && x < (c1 + 1) * c2); | ||
TVM_TRY_RECURSIVE_REWRITE(c3 <= floormod(x, c2) && floordiv(x, c2) == c1, | ||
c1 * c2 + c3 <= x && x < (c1 + 1) * c2); | ||
TVM_TRY_RECURSIVE_REWRITE(floordiv(x, c2) == c1 && c3 < floormod(x, c2), | ||
c1 * c2 + c3 < x && x < (c1 + 1) * c2); | ||
TVM_TRY_RECURSIVE_REWRITE(c3 < floormod(x, c2) && floordiv(x, c2) == c1, | ||
c1 * c2 + c3 < x && x < (c1 + 1) * c2); | ||
return ret; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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 unwrap_vector_expr.cc | ||
* \brief Utility for tracking currently active constraints | ||
*/ | ||
|
||
#include "unwrap_vector_expr.h" | ||
|
||
#include <tvm/arith/analyzer.h> | ||
#include <tvm/tir/analysis.h> | ||
#include <tvm/tir/builtin.h> | ||
#include <tvm/tir/expr.h> | ||
#include <tvm/tir/expr_functor.h> | ||
#include <tvm/tir/op.h> | ||
|
||
#include <unordered_map> | ||
|
||
namespace tvm { | ||
namespace arith { | ||
|
||
using namespace tir; | ||
|
||
class Scalarizer : public ExprMutator { | ||
public: | ||
explicit Scalarizer(PrimExpr lane) : lane_(lane) {} | ||
|
||
PrimExpr VisitExpr_(const RampNode* op) final { return op->base + lane_ * op->stride; } | ||
|
||
PrimExpr VisitExpr_(const BroadcastNode* op) final { return op->value; } | ||
|
||
PrimExpr VisitExpr_(const VarNode* op) final { | ||
Var var = GetRef<Var>(op); | ||
|
||
auto it = let_var_remap_.find(op); | ||
if (it != let_var_remap_.end()) { | ||
return it->second; | ||
} else { | ||
return ExprMutator::VisitExpr_(op); | ||
} | ||
} | ||
PrimExpr VisitExpr_(const LetNode* op) final { | ||
if (op->value.dtype().lanes() == 1) { | ||
return ExprMutator::VisitExpr_(op); | ||
} | ||
|
||
auto it = let_var_remap_.find(op->var.get()); | ||
ICHECK(it == let_var_remap_.end()) << "Duplicate binding of variable " << op->var; | ||
|
||
Var new_var(op->var->name_hint + "_scalar", op->var.dtype().element_of()); | ||
let_var_remap_[op->var.get()] = new_var; | ||
|
||
PrimExpr value = this->VisitExpr(op->value); | ||
PrimExpr body = this->VisitExpr(op->body); | ||
|
||
let_var_remap_.erase(op->var.get()); | ||
return Let(op->var, value, body); | ||
} | ||
|
||
private: | ||
// The lane to extract | ||
PrimExpr lane_; | ||
|
||
// Let binding | ||
std::unordered_map<const VarNode*, Var> let_var_remap_; | ||
}; | ||
|
||
PrimExpr UnwrapVectorExpr(const PrimExpr& vector_expr, const PrimExpr& lane) { | ||
return Scalarizer(lane)(vector_expr); | ||
} | ||
|
||
} // namespace arith | ||
} // namespace tvm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bug fix? Do we have a test to reproduce if so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not so much as bug as a performance improvement and consistency with some steps in the data-flow portions.
The performance improvement comes from this calling scope, which iterates across pairs of expressions, where
b
is always later in the loop. By placing the simplified expression inb
instead ofa
, this allows later calls toTrySimplifyOr
to further simplify using the simplified expression as an input. As a result, repeated passes across all pairs are avoided.The consistency comes from the
ControlFlowBlock
's use ofExprDeepEqual
to check for convergence. Because the rewrite simplifier doesn't produce a canonical form (e.g. The expressionsi > 0 && j > 0
andj>0 && i>0
are equivalent, but neither will be simplified to the other), there were a few cases where I wanted to preserve a specific order when simplifying.