24 #ifndef TVM_ARITH_ANALYZER_H_
25 #define TVM_ARITH_ANALYZER_H_
28 #include <tvm/ffi/cast.h>
29 #include <tvm/ffi/reflection/registry.h>
30 #include <tvm/ffi/string.h>
36 #include <unordered_map>
96 namespace refl = tvm::ffi::reflection;
97 refl::ObjectDef<ConstIntBoundNode>()
138 std::unordered_map<PrimExpr, ConstIntBound, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>;
191 std::function<void()> EnterConstraint(
const PrimExpr& constraint);
218 namespace refl = tvm::ffi::reflection;
219 refl::ObjectDef<ModularSetNode>()
271 std::function<void()> EnterConstraint(
const PrimExpr& constraint);
474 return CompareResult(
static_cast<int>(lhs) &
static_cast<int>(rhs));
477 return CompareResult(
static_cast<int>(lhs) |
static_cast<int>(rhs));
506 bool propagate_inequalities =
true);
540 std::unique_ptr<Impl> impl_;
584 TVM_DLL
void Bind(
const Var&
var,
const Range& new_range,
bool allow_override =
false);
607 TVM_DLL
void Bind(
const Var&
var,
const Range& new_range,
bool allow_override =
false);
690 int64_t min_consecutive = 1);
697 void CopyFrom(
const Z3Prover& other);
784 void Bind(
const ffi::Map<Var, Range>& variables,
bool allow_override =
false);
896 static constexpr
bool _type_mutable =
true;
917 explicit Analyzer(ffi::ObjectPtr<AnalyzerObj> n) : ffi::ObjectRef(std::move(n)) {
918 TVM_FFI_ICHECK(this->get() !=
nullptr);
959 : analyzer_(analyzer), constraint_(std::move(constraint)), is_assume_(is_assume) {}
968 :
ConstraintContext(ffi::GetRef<Analyzer>(analyzer), std::move(constraint),
false) {}
976 :
ConstraintContext(ffi::GetRef<Analyzer>(analyzer), std::move(constraint), is_assume) {}
978 void EnterWithScope();
980 void ExitWithScope();
986 std::vector<std::function<void()>> recovery_functions_;
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Range container
Definition: expr.h:484
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with_context.h:59
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:712
PrimExpr Simplify(const PrimExpr &expr, int steps=2)
Simplify expr.
bool CanProveGreaterEqual(const PrimExpr &expr, int64_t lower_bound)
Whether can we prove expr >= val.
bool CanProveLessEqualThanSymbolicShapeValue(const PrimExpr &lhs, const PrimExpr &shape)
Whether we can prove lhs is smaller than possibly symbolic shape.
Analyzer Clone() const
Deep-copy this analyzer into a new, independent Analyzer.
bool CanProveLess(const PrimExpr &expr, int64_t upper_bound)
Whether can we prove expr < val.
bool CanProve(const PrimExpr &cond, ProofStrength strength=ProofStrength::kDefault)
Whether can we prove condition.
ConstIntBoundAnalyzer const_int_bound
sub-analyzer: const integer bound
Definition: analyzer.h:715
Z3Prover z3_prover
sub-analyzer using Z3
Definition: analyzer.h:727
void Bind(const ffi::Map< Var, Range > &variables, bool allow_override=false)
Bind all the vars in the Map.
void Bind(const Var &var, const Range &range, bool allow_override=false)
Notify all the sub-analyzers that var is created and bound to a range.
void MarkGlobalNonNegValue(const PrimExpr &value)
Mark the value as non-negative value globally in analyzer.
RewriteSimplifier rewrite_simplify
sub-analyzer rewrite simplify
Definition: analyzer.h:719
IntSetAnalyzer int_set
sub-analyzer: int set
Definition: analyzer.h:723
void Bind(const Var &var, const PrimExpr &expr, bool allow_override=false)
Notify all the sub-analyzers that var is created and binded to expr.
TransitiveComparisonAnalyzer transitive_comparisons
sub-analyzer transitive comparisons
Definition: analyzer.h:725
bool CanProveEqual(const PrimExpr &lhs, const PrimExpr &rhs)
Whether can we prove lhs == rhs.
ModularSetAnalyzer modular_set
sub-analyzer: modular set
Definition: analyzer.h:717
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.Analyzer", AnalyzerObj, ffi::Object)
CanonicalSimplifier canonical_simplify
sub-analyzer canonical simplify
Definition: analyzer.h:721
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Analyzer(ffi::ObjectPtr< AnalyzerObj > n)
Definition: analyzer.h:917
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Analyzer, ffi::ObjectRef, AnalyzerObj)
Analyzer()
Default-construct a fresh analyzer (allocates an AnalyzerObj).
Definition: analyzer.h:916
Canonical-form based simplifier.
Definition: analyzer.h:428
PrimExpr operator()(const PrimExpr &expr)
analyze the expr
void Update(const Var &var, const PrimExpr &new_expr, bool allow_override=false)
Update binding of var to a new expression.
Analyzer to get constant integer bound over expression.
Definition: analyzer.h:135
bool IsBound(const Var &var) const
Check if a variable is bound to a range.
void Update(const Var &var, const ConstIntBound &info, bool allow_override=false)
Update constant int bound information of var.
std::unordered_map< PrimExpr, ConstIntBound, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > BoundMapType
Definition: analyzer.h:138
void Bind(const Var &var, const Range &range, bool allow_override=false)
Bind variable to a range.
ConstIntBound operator()(const PrimExpr &expr, BoundMapType *bound)
analyze the expr with the intermediate memorized to avoid redundant computation
ConstIntBound operator()(const PrimExpr &expr) const
analyze the expr
Constant integer up and lower bound(inclusive). Useful for value bound analysis.
Definition: analyzer.h:90
int64_t min_value
Definition: analyzer.h:92
static constexpr const int64_t kNegInf
Number to represent -inf.
Definition: analyzer.h:108
int64_t max_value
Definition: analyzer.h:93
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: analyzer.h:110
static constexpr const int64_t kPosInf
Number to represent +inf.
Definition: analyzer.h:103
static void RegisterReflection()
Definition: analyzer.h:95
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.ConstIntBound", ConstIntBoundNode, ffi::Object)
reference class to ConstIntBoundNode
Definition: analyzer.h:118
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ConstIntBound, ffi::ObjectRef, ConstIntBoundNode)
static constexpr const int64_t kNegInf
Definition: analyzer.h:128
ConstIntBound(int64_t min_value, int64_t max_value)
constructor by fields.
static constexpr const int64_t kPosInf
Definition: analyzer.h:127
Constraint context.
Definition: analyzer.h:939
Integer set analyzer.
Definition: analyzer.h:546
std::function< void()> EnterConstraint(const PrimExpr &constraint)
void Update(const Var &var, const IntSet &new_interval_set, bool allow_override=false)
Update binding of var to a new expression.
void Bind(const Var &var, const Range &new_range, bool allow_override=false)
Update binding of var to a new expression.
IntSet operator()(const PrimExpr &expr, const ffi::Map< Var, IntSet > &dom_map)
Find a symbolic integer set that contains all possible values of expr given the domain of each variab...
IntSet operator()(const PrimExpr &expr)
Find a symbolic integer set that contains all possible values of expr given the domain of each variab...
Managed reference to IntSetNode.
Definition: int_set.h:67
Analyzer to get modular information over expression.
Definition: analyzer.h:242
void Update(const Var &var, const ModularSet &info, bool allow_override=false)
Update constant int bound information of var.
ModularSet operator()(const PrimExpr &expr)
analyze the expr
Range of a linear integer function. Use to do specify the possible index values.
Definition: analyzer.h:210
int64_t coeff
linear co-efficient
Definition: analyzer.h:213
static void RegisterReflection()
Definition: analyzer.h:217
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.ModularSet", ModularSetNode, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: analyzer.h:224
int64_t base
The base.
Definition: analyzer.h:215
reference of ModularSetNode
Definition: analyzer.h:232
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ModularSet, ffi::ObjectRef, ModularSetNode)
ModularSet(int64_t coeff, int64_t base)
Rewrite-rule based simplifier.
Definition: analyzer.h:281
std::function< void()> EnterConstraint(const PrimExpr &constraint)
Update the internal state to enter constraint.
Extension GetEnabledExtensions() const
Return the currently enabled extensions.
ffi::ObjectRef GetStatsCounters() const
Return the statistics counters.
void SetEnabledExtensions(Extension flags)
Enable an optional extension or extensions.
void Update(const Var &var, const PrimExpr &new_expr, bool allow_override=false)
Update binding of var to a new expression.
Extension
Flags to enable more computationally-intensive simplifications.
Definition: analyzer.h:319
@ kNone
Definition: analyzer.h:321
@ kApplyConstraintsToBooleanBranches
Definition: analyzer.h:349
@ kTransitivelyProveInequalities
Definition: analyzer.h:328
@ kComparisonOfProductAndSum
Definition: analyzer.h:378
@ kConvertBooleanToAndOfOrs
Definition: analyzer.h:336
void SetMaximumRewriteSteps(int64_t maximum)
Set the maximum allowed number of rewrite steps.
void ResetStatsCounters()
Reset the statistics counters.
PrimExpr operator()(const PrimExpr &expr)
analyze the expr
Using previously specified knowns, compare the expressions provided.
Definition: analyzer.h:486
void Bind(const Var &var, const Range &range, bool allow_override=false)
Bind a variable as being within a specified range.
std::function< void()> EnterConstraint(const PrimExpr &constraint)
Update the internal state to enter constraint.
void Bind(const Var &var, const PrimExpr &expr, bool allow_override=false)
Bind a variable as being equal to a known expression.
CompareResult TryCompare(const PrimExpr &lhs, const PrimExpr &rhs, bool propagate_inequalities=true)
Definition: analyzer.h:598
ffi::String GetModel(const PrimExpr &expr)
Get the Z3 model for the given expression if satisfiable.
void SetTimeoutMs(unsigned timeout_ms)
Set timeout in milliseconds for Z3 prover.
void Bind(const Var &var, const Range &new_range, bool allow_override=false)
Update binding of var to a new expression.
bool CanProve(const PrimExpr &expr)
Whether can we prove expr is always true.
bool IsEnabled() const
Whether the Z3 backend is compiled into this build (USE_Z3=ON).
ffi::String GetStats()
Get statistics about Z3 prover.
int64_t CountSatisfyingValues(const Var &var, int64_t max_count=2048, int64_t min_consecutive=1)
Count the number of integer values that satisfy the current constraints.
ffi::String GetSMTLIB2(const ffi::Optional< PrimExpr > expr)
Get the SMTLIB2 representation of the current context.
void Bind(const Var &var, const PrimExpr &expr, bool allow_override=false)
Update binding of var to a new expression.
std::function< void()> EnterConstraint(const PrimExpr &constraint)
Update the internal state to enter constraint.
void SetRLimit(unsigned rlimit)
Set resource limitation for Z3 prover.
a named variable in TIR
Definition: var.h:68
ProofStrength
The strength used in top-level condition proves.
Definition: analyzer.h:75
@ kSymbolicBound
Prove using symbolic bound analysis.
@ kDefault
default strength, can be used in.
CompareResult
Structure for representing result of known.
Definition: analyzer.h:462
constexpr CompareResult operator|(CompareResult lhs, CompareResult rhs)
Definition: analyzer.h:476
DivMode
Definition: analyzer.h:61
@ kTruncDiv
Truncated division.
Definition: analyzer.h:63
@ kFloorDiv
Floor division.
Definition: analyzer.h:65
@ kUnknown
Definition: int_set.h:51
constexpr CompareResult operator&(CompareResult lhs, CompareResult rhs)
Definition: analyzer.h:473
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr max_value(PrimType dtype, Span span=Span())
PrimExpr min_value(PrimType dtype, Span span=Span())
RAII wrapper function to enter and exit a context object similar to python's with syntax.