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>
39#include <unordered_map>
62class TempConstraintScope;
103 namespace refl = tvm::ffi::reflection;
104 refl::ObjectDef<ConstIntBoundObj>()
110 static const constexpr int64_t
kPosInf = std::numeric_limits<int64_t>::max();
145 std::unordered_map<PrimExpr, ConstIntBound, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>;
174 void IngestBinding(
const Var& var,
const PrimExpr& expr);
176 void IngestBinding(
const Var& var,
const Range& range);
178 void IngestPredicate(
const PrimExpr& constraint);
188 std::unique_ptr<Impl> impl_;
207 namespace refl = tvm::ffi::reflection;
208 refl::ObjectDef<ModularSetObj>()
247 void IngestBinding(
const Var& var,
const PrimExpr& expr);
249 void IngestPredicate(
const PrimExpr& constraint);
259 std::unique_ptr<Impl> impl_;
317 void IngestBinding(
const Var& var,
const Range& range);
319 void IngestPredicate(
const PrimExpr& constraint);
329 ffi::ObjectPtr<Impl> impl_;
349 void IngestBinding(
const Var& var,
const PrimExpr& expr);
358 ffi::ObjectPtr<Impl> impl_;
393 return CompareResult(
static_cast<int>(lhs) &
static_cast<int>(rhs));
402 return CompareResult(
static_cast<int>(lhs) |
static_cast<int>(rhs));
434 void IngestBinding(
const Var& var,
const PrimExpr& expr);
436 void IngestBinding(
const Var& var,
const Range& range);
438 void IngestPredicate(
const PrimExpr& constraint);
525 int64_t min_consecutive = 1);
532 void IngestBinding(
const Var& var,
const PrimExpr& expr);
534 void IngestBinding(
const Var& var,
const Range& range);
536 void IngestPredicate(
const PrimExpr& constraint);
543 void CopyFrom(
const Z3Prover& other);
545 std::unique_ptr<Impl> impl_;
712 static constexpr bool _type_mutable =
true;
716 friend class detail::TempConstraintScope;
718 bool z3_default_fallback_{
true};
720 void IngestBinding(
const Var& var,
const PrimExpr& expr);
722 void IngestBinding(
const Var& var,
const Range& range);
724 void IngestConstraint(
const Constraint& constraint);
726 std::vector<PrimExpr> PreparePredicate(
const PrimExpr& condition);
730 std::vector<Constraint> temp_constraints_;
732 bool synchronizing_{
false};
734 int active_queries_{0};
755 explicit Analyzer(ffi::ObjectPtr<AnalyzerObj> n) : ffi::ObjectRef(std::move(n)) {
756 TVM_FFI_ICHECK(this->get() !=
nullptr);
785 static thread_local int depth_;
#define TVM_TIRX_DLL
Definition base.h:55
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Range container
Definition expr.h:883
Managed reference to VarObj.
Definition expr.h:571
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:58
Scoped no-overflow assumption for unsigned index arithmetic.
Definition analyzer.h:770
static TVM_TIRX_DLL bool Enabled()
Check the current thread's unsigned-index assumption.
Coordinated symbolic analyses over an explicitly synchronized premise environment.
Definition analyzer.h:556
RewriteSimplifier rewrite_simplify
sub-analyzer rewrite simplify
Definition analyzer.h:563
bool CanProve(const PrimExpr &cond, ProofStrength strength=ProofStrength::kDefault)
Attempt to prove a condition under the current premises.
Analyzer Clone() const
Copy accepted facts and query settings into an independent analyzer.
bool CanProveGreaterEqual(const PrimExpr &expr, int64_t lower_bound)
Attempt to prove expr >= lower_bound.
IntSetAnalyzer int_set
sub-analyzer: int set
Definition analyzer.h:567
void SetZ3DefaultFallback(bool enabled)
Set whether DEFAULT-strength queries try Z3 after native analysis fails.
Z3Prover z3_prover
sub-analyzer using Z3
Definition analyzer.h:569
CanonicalSimplifier canonical_simplify
sub-analyzer canonical simplify
Definition analyzer.h:565
ConstraintContext GetConstraintContext() const
Snapshot the accepted premise environment.
bool CanProveEqual(const PrimExpr &lhs, const PrimExpr &rhs)
Attempt to prove lhs == rhs.
PrimExpr Simplify(const PrimExpr &expr, int steps=2)
Simplify expr.
ConstIntBoundAnalyzer const_int_bound
sub-analyzer: const integer bound
Definition analyzer.h:559
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.Analyzer", AnalyzerObj, ffi::Object)
AnalyzerObj()
Construct an analyzer with no accepted premises and default query settings.
bool CanProveLessEqualThanSymbolicShapeValue(const PrimExpr &lhs, const PrimExpr &shape)
Attempt to prove lhs <= a possibly symbolic shape.
void SyncConstraintContext(const ConstraintContext &context)
Synchronize all derived facts to the supplied premise environment.
bool CanProveLess(const PrimExpr &expr, int64_t upper_bound)
Attempt to prove expr < upper_bound.
ModularSetAnalyzer modular_set
sub-analyzer: modular set
Definition analyzer.h:561
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Analyzer()
Construct a fresh analyzer.
Definition analyzer.h:753
Analyzer(ffi::ObjectPtr< AnalyzerObj > n)
Adopt a reference to a non-null analyzer object.
Definition analyzer.h:755
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Analyzer, ffi::ObjectRef, AnalyzerObj)
Canonical-form based simplifier.
Definition analyzer.h:335
TVM_TIRX_DLL PrimExpr operator()(const PrimExpr &expr)
Simplify an expression under the current premises.
Analyzer to get constant integer bound over expression.
Definition analyzer.h:141
TVM_TIRX_DLL bool IsBound(const Var &var) const
Check whether a binding or recognized predicate supplies bounds for a variable.
TVM_TIRX_DLL ConstIntBound operator()(const PrimExpr &expr) const
Compute conservative constant bounds under the current premises.
TVM_TIRX_DLL ConstIntBound operator()(const PrimExpr &expr, BoundMapType *bound)
Compute bounds and optionally collect intermediate query results.
std::unordered_map< PrimExpr, ConstIntBound, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > BoundMapType
Query results keyed by expression object identity.
Definition analyzer.h:145
Inclusive constant integer bounds, representing [min_value, max_value].
Definition analyzer.h:94
static const constexpr int64_t kPosInf
Number to represent +inf.
Definition analyzer.h:110
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ConstIntBound", ConstIntBoundObj, ffi::Object)
static void RegisterReflection()
Register the fields exposed through FFI reflection.
Definition analyzer.h:102
int64_t min_value
Inclusive lower endpoint, or kNegInf when unbounded.
Definition analyzer.h:97
int64_t max_value
Inclusive upper endpoint, or kPosInf when unbounded.
Definition analyzer.h:99
static const constexpr int64_t kNegInf
Number to represent -inf.
Definition analyzer.h:115
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Compare and hash these value objects structurally.
Definition analyzer.h:118
Managed reference to ConstIntBoundObj.
Definition analyzer.h:126
TVM_TIRX_DLL ConstIntBound(int64_t min_value, int64_t max_value)
Construct inclusive bounds; endpoints may use the infinity sentinels.
static const constexpr int64_t kNegInf
Sentinel for an unbounded lower endpoint.
Definition analyzer.h:134
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ConstIntBound, ffi::ObjectRef, ConstIntBoundObj)
static const constexpr int64_t kPosInf
Sentinel for an unbounded upper endpoint.
Definition analyzer.h:132
Managed reference to ConstraintContextObj. Copies share the same context.
Definition constraint.h:306
A binding, range, or predicate, without inference or normalization.
Definition constraint.h:49
Integer set analyzer.
Definition analyzer.h:408
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 supplied variable dom...
TVM_TIRX_DLL IntSet operator()(const PrimExpr &expr)
Find a symbolic integer set that contains all possible values of expr using current bindings,...
Managed reference to IntSetObj.
Definition int_set.h:63
Analyzer to get modular information over expression.
Definition analyzer.h:233
TVM_TIRX_DLL ModularSet operator()(const PrimExpr &expr)
Compute conservative congruence information under the current premises.
Integer congruence class {coeff * x + base | x in Z}.
Definition analyzer.h:198
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ModularSet", ModularSetObj, ffi::Object)
int64_t base
Constant offset of the congruence class.
Definition analyzer.h:203
int64_t coeff
Coefficient of the arbitrary integer multiplier.
Definition analyzer.h:201
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Compare and hash these value objects structurally.
Definition analyzer.h:214
static void RegisterReflection()
Register the fields exposed through FFI reflection.
Definition analyzer.h:206
Managed reference to ModularSetObj.
Definition analyzer.h:222
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ModularSet, ffi::ObjectRef, ModularSetObj)
TVM_TIRX_DLL ModularSet(int64_t coeff, int64_t base)
Store coeff and base as supplied, without normalization.
Rewrite-rule based simplifier.
Definition analyzer.h:265
TVM_TIRX_DLL PrimExpr operator()(const PrimExpr &expr)
Simplify an expression under the current premises.
TVM_TIRX_DLL CompareResult TryCompare(const PrimExpr &lhs, const PrimExpr &rhs)
Compare signed integer expressions using active premises and constant offsets.
TVM_TIRX_DLL void SetMaximumRewriteSteps(int64_t maximum)
Set the maximum allowed cumulative number of successful rewrites.
TVM_TIRX_DLL void ResetStatsCounters()
Clear cumulative rewrite counters without changing facts or the rewrite limit.
TVM_TIRX_DLL ffi::ObjectRef GetStatsCounters() const
Read cumulative rewrite statistics.
Optional Z3-backed prover over the analyzer's accepted premises.
Definition analyzer.h:456
TVM_TIRX_DLL ffi::String GetModel(const PrimExpr &expr)
Get a counterexample model for a boolean expression.
TVM_TIRX_DLL bool IsEnabled() const
Whether the Z3 backend is compiled into this build (TVM_TIRX_USE_Z3=ON).
TVM_TIRX_DLL void SetTimeoutMs(unsigned timeout_ms)
Set timeout in milliseconds for Z3 prover.
TVM_TIRX_DLL bool CanProve(const PrimExpr &expr)
Attempt to prove that expr holds under the current premises.
TVM_TIRX_DLL void SetRLimit(unsigned rlimit)
Set resource limitation for Z3 prover.
TVM_TIRX_DLL ffi::String GetSMTLIB2(const ffi::Optional< PrimExpr > expr)
Get the SMTLIB2 representation of the current context.
TVM_TIRX_DLL ffi::String GetStats()
Get statistics about Z3 prover.
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.
Data-only symbolic premises collected during traversal.
@ kUnknown
Definition int_set.h:47
CompareResult
Possible orderings of two expressions, represented as a bit set.
Definition analyzer.h:367
@ kGE
Greater than or equal.
@ kInconsistent
No ordering is possible under the known facts.
DivMode
Integer division semantics used by symbolic analysis.
Definition analyzer.h:66
@ kFloorDiv
Floor division.
Definition analyzer.h:70
@ kTruncDiv
Truncated division.
Definition analyzer.h:68
constexpr CompareResult operator|(CompareResult lhs, CompareResult rhs)
Unite the possible orderings.
Definition analyzer.h:401
ProofStrength
The strength used for top-level condition proofs.
Definition analyzer.h:80
@ kSymbolicBound
Prove using symbolic bound analysis.
@ kDefault
Default analysis followed by the optional Z3 fallback.
constexpr CompareResult operator&(CompareResult lhs, CompareResult rhs)
Intersect the possible orderings.
Definition analyzer.h:392
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
RAII wrapper function to enter and exit a context object similar to python's with syntax.