24#ifndef TVM_SYM_ANALYZER_H_
25#define TVM_SYM_ANALYZER_H_
27#include <tvm/ffi/cast.h>
28#include <tvm/ffi/object.h>
29#include <tvm/ffi/reflection/registry.h>
30#include <tvm/ffi/string.h>
37#include <unordered_map>
58class ConstraintContext;
95 namespace refl = tvm::ffi::reflection;
96 refl::ObjectDef<ConstIntBoundObj>()
102 static const constexpr int64_t
kPosInf = std::numeric_limits<int64_t>::max();
137 std::unordered_map<PrimExpr, ConstIntBound, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>;
190 std::function<void()> EnterConstraint(
const PrimExpr& constraint);
194 std::unique_ptr<Impl> impl_;
217 namespace refl = tvm::ffi::reflection;
218 refl::ObjectDef<ModularSetObj>()
270 std::function<void()> EnterConstraint(
const PrimExpr& constraint);
274 std::unique_ptr<Impl> impl_;
306 bool is_assume =
false);
423 ffi::ObjectPtr<Impl> impl_;
455 ffi::ObjectPtr<Impl> impl_;
475 return CompareResult(
static_cast<int>(lhs) &
static_cast<int>(rhs));
478 return CompareResult(
static_cast<int>(lhs) |
static_cast<int>(rhs));
507 bool propagate_inequalities =
true);
541 std::unique_ptr<Impl> impl_;
577 bool allow_override =
false);
693 int64_t min_consecutive = 1);
700 void CopyFrom(
const Z3Prover& other);
702 std::unique_ptr<Impl> impl_;
778 void Bind(
const Var& var,
const Range& range,
bool allow_override =
false);
787 void Bind(
const ffi::Map<Var, Range>& variables,
bool allow_override =
false);
899 static constexpr bool _type_mutable =
true;
920 explicit Analyzer(ffi::ObjectPtr<AnalyzerObj> n) : ffi::ObjectRef(std::move(n)) {
921 TVM_FFI_ICHECK(this->get() !=
nullptr);
962 : analyzer_(analyzer), constraint_(std::move(constraint)), is_assume_(is_assume) {}
971 :
ConstraintContext(ffi::GetRef<Analyzer>(analyzer), std::move(constraint),
false) {}
979 :
ConstraintContext(ffi::GetRef<Analyzer>(analyzer), std::move(constraint), is_assume) {}
989 std::vector<std::function<void()>> recovery_functions_;
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Range container
Definition expr.h:1025
Managed reference to VarNode.
Definition expr.h:714
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:715
RewriteSimplifier rewrite_simplify
sub-analyzer rewrite simplify
Definition analyzer.h:722
bool CanProve(const PrimExpr &cond, ProofStrength strength=ProofStrength::kDefault)
Whether can we prove condition.
Analyzer Clone() const
Deep-copy this analyzer into a new, independent Analyzer.
bool CanProveGreaterEqual(const PrimExpr &expr, int64_t lower_bound)
Whether can we prove expr >= val.
IntSetAnalyzer int_set
sub-analyzer: int set
Definition analyzer.h:726
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.
Z3Prover z3_prover
sub-analyzer using Z3
Definition analyzer.h:730
TransitiveComparisonAnalyzer transitive_comparisons
sub-analyzer transitive comparisons
Definition analyzer.h:728
void Bind(const ffi::Map< Var, Range > &variables, bool allow_override=false)
Bind all the vars in the Map.
CanonicalSimplifier canonical_simplify
sub-analyzer canonical simplify
Definition analyzer.h:724
bool CanProveEqual(const PrimExpr &lhs, const PrimExpr &rhs)
Whether can we prove lhs == rhs.
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.
PrimExpr Simplify(const PrimExpr &expr, int steps=2)
Simplify expr.
ConstIntBoundAnalyzer const_int_bound
sub-analyzer: const integer bound
Definition analyzer.h:718
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.Analyzer", AnalyzerObj, ffi::Object)
bool CanProveLessEqualThanSymbolicShapeValue(const PrimExpr &lhs, const PrimExpr &shape)
Whether we can prove lhs is smaller than possibly symbolic shape.
bool CanProveLess(const PrimExpr &expr, int64_t upper_bound)
Whether can we prove expr < val.
ModularSetAnalyzer modular_set
sub-analyzer: modular set
Definition analyzer.h:720
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Analyzer()
Default-construct a fresh analyzer (allocates an AnalyzerObj).
Definition analyzer.h:919
Analyzer(ffi::ObjectPtr< AnalyzerObj > n)
Definition analyzer.h:920
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Analyzer, ffi::ObjectRef, AnalyzerObj)
Canonical-form based simplifier.
Definition analyzer.h:429
TVM_TIRX_DLL void Update(const Var &var, const PrimExpr &new_expr, bool allow_override=false)
Update binding of var to a new expression.
TVM_TIRX_DLL PrimExpr operator()(const PrimExpr &expr)
analyze the expr
Analyzer to get constant integer bound over expression.
Definition analyzer.h:134
TVM_TIRX_DLL bool IsBound(const Var &var) const
Check if a variable is bound to a range.
TVM_TIRX_DLL ConstIntBound operator()(const PrimExpr &expr) const
analyze the expr
TVM_TIRX_DLL void Bind(const Var &var, const Range &range, bool allow_override=false)
Bind variable to a range.
TVM_TIRX_DLL ConstIntBound operator()(const PrimExpr &expr, BoundMapType *bound)
analyze the expr with the intermediate memorized to avoid redundant computation
TVM_TIRX_DLL 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:137
Constant integer up and lower bound(inclusive). Useful for value bound analysis.
Definition analyzer.h:89
static const constexpr int64_t kPosInf
Number to represent +inf.
Definition analyzer.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ConstIntBound", ConstIntBoundObj, ffi::Object)
static void RegisterReflection()
Definition analyzer.h:94
int64_t min_value
Definition analyzer.h:91
int64_t max_value
Definition analyzer.h:92
static const constexpr int64_t kNegInf
Number to represent -inf.
Definition analyzer.h:107
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition analyzer.h:109
reference class to ConstIntBoundObj
Definition analyzer.h:117
TVM_TIRX_DLL ConstIntBound(int64_t min_value, int64_t max_value)
constructor by fields.
static const constexpr int64_t kNegInf
Definition analyzer.h:127
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ConstIntBound, ffi::ObjectRef, ConstIntBoundObj)
static const constexpr int64_t kPosInf
Definition analyzer.h:126
Constraint context.
Definition analyzer.h:942
Integer set analyzer.
Definition analyzer.h:547
TVM_TIRX_DLL void Bind(const Var &var, const Range &new_range, bool allow_override=false)
Update binding of var to a new expression.
std::function< void()> EnterConstraint(const PrimExpr &constraint)
TVM_TIRX_DLL 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...
TVM_TIRX_DLL IntSet operator()(const PrimExpr &expr)
Find a symbolic integer set that contains all possible values of expr given the domain of each variab...
TVM_TIRX_DLL void Update(const Var &var, const IntSet &new_interval_set, bool allow_override=false)
Update binding of var to a new expression.
Managed reference to IntSetObj.
Definition int_set.h:63
Analyzer to get modular information over expression.
Definition analyzer.h:241
TVM_TIRX_DLL void Update(const Var &var, const ModularSet &info, bool allow_override=false)
Update constant int bound information of var.
TVM_TIRX_DLL 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:209
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ModularSet", ModularSetObj, ffi::Object)
int64_t base
The base.
Definition analyzer.h:214
int64_t coeff
linear co-efficient
Definition analyzer.h:212
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition analyzer.h:223
static void RegisterReflection()
Definition analyzer.h:216
reference of ModularSetObj
Definition analyzer.h:231
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ModularSet, ffi::ObjectRef, ModularSetObj)
TVM_TIRX_DLL ModularSet(int64_t coeff, int64_t base)
Rewrite-rule based simplifier.
Definition analyzer.h:280
TVM_TIRX_DLL PrimExpr operator()(const PrimExpr &expr)
analyze the expr
Extension
Flags to enable more computationally-intensive simplifications.
Definition analyzer.h:320
@ kComparisonOfProductAndSum
Definition analyzer.h:379
@ kConvertBooleanToAndOfOrs
Definition analyzer.h:337
@ kApplyConstraintsToBooleanBranches
Definition analyzer.h:350
@ kTransitivelyProveInequalities
Definition analyzer.h:329
@ kNone
Definition analyzer.h:322
TVM_TIRX_DLL void Update(const Var &var, const PrimExpr &new_expr, bool allow_override=false)
Update binding of var to a new expression.
TVM_TIRX_DLL std::function< void()> EnterConstraint(const PrimExpr &constraint, bool is_assume=false)
Update the internal state to enter constraint.
TVM_TIRX_DLL void SetEnabledExtensions(Extension flags)
Enable an optional extension or extensions.
TVM_TIRX_DLL void SetMaximumRewriteSteps(int64_t maximum)
Set the maximum allowed number of rewrite steps.
TVM_TIRX_DLL Extension GetEnabledExtensions() const
Return the currently enabled extensions.
TVM_TIRX_DLL void ResetStatsCounters()
Reset the statistics counters.
TVM_TIRX_DLL ffi::ObjectRef GetStatsCounters() const
Return the statistics counters.
Using previously specified knowns, compare the expressions provided.
Definition analyzer.h:487
TVM_TIRX_DLL void Bind(const Var &var, const Range &range, bool allow_override=false)
Bind a variable as being within a specified range.
TVM_TIRX_DLL std::function< void()> EnterConstraint(const PrimExpr &constraint)
Update the internal state to enter constraint.
TVM_TIRX_DLL void Bind(const Var &var, const PrimExpr &expr, bool allow_override=false)
Bind a variable as being equal to a known expression.
TVM_TIRX_DLL CompareResult TryCompare(const PrimExpr &lhs, const PrimExpr &rhs, bool propagate_inequalities=true)
Definition analyzer.h:600
ffi::String GetStats()
Get statistics about Z3 prover.
ffi::String GetSMTLIB2(const ffi::Optional< PrimExpr > expr)
Get the SMTLIB2 representation of the current context.
TVM_TIRX_DLL bool IsEnabled() const
Whether the Z3 backend is compiled into this build (USE_Z3=ON).
ffi::String GetModel(const PrimExpr &expr)
Get the Z3 model for the given expression if satisfiable.
std::function< void()> EnterConstraint(const PrimExpr &constraint, bool is_assume=false)
Update the internal state to enter constraint.
void SetTimeoutMs(unsigned timeout_ms)
Set timeout in milliseconds for Z3 prover.
TVM_TIRX_DLL void Bind(const Var &var, const PrimExpr &expr, bool allow_override=false)
Update binding of var to a new expression.
TVM_TIRX_DLL bool CanProve(const PrimExpr &expr)
Whether can we prove expr is always true.
void SetRLimit(unsigned rlimit)
Set resource limitation for Z3 prover.
TVM_TIRX_DLL void Bind(const Var &var, const Range &new_range, bool allow_override=false)
Update binding of var to a new expression.
TVM_TIRX_DLL 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.
@ kUnknown
Definition int_set.h:47
CompareResult
Structure for representing result of known.
Definition analyzer.h:463
DivMode
Definition analyzer.h:60
@ kFloorDiv
Floor division.
Definition analyzer.h:64
@ kTruncDiv
Truncated division.
Definition analyzer.h:62
constexpr CompareResult operator|(CompareResult lhs, CompareResult rhs)
Definition analyzer.h:477
ProofStrength
The strength used in top-level condition proves.
Definition analyzer.h:74
@ kSymbolicBound
Prove using symbolic bound analysis.
@ kDefault
default strength, can be used in.
constexpr CompareResult operator&(CompareResult lhs, CompareResult rhs)
Definition analyzer.h:474
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83
RAII wrapper function to enter and exit a context object similar to python's with syntax.