tvm
Loading...
Searching...
No Matches
constraint.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_SYM_CONSTRAINT_H_
25#define TVM_SYM_CONSTRAINT_H_
26
27#include <tvm/ffi/cast.h>
28#include <tvm/ffi/container/array.h>
29#include <tvm/ir/expr.h>
30#include <tvm/ir/prim/expr.h>
31
32#include <utility>
33
34namespace tvm {
35namespace sym {
36
40class ConstraintObj : public ffi::Object {
41 public:
42 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
43 TVM_FFI_DECLARE_OBJECT_INFO("sym.Constraint", ConstraintObj, ffi::Object);
44};
45
49class Constraint : public ffi::ObjectRef {
50 public:
54 explicit Constraint(ffi::ObjectPtr<ConstraintObj> node) : ffi::ObjectRef(std::move(node)) {}
56};
57
63 public:
64 BindingConstraintObj(Var var, PrimExpr value) : var(std::move(var)), value(std::move(value)) {}
65 explicit BindingConstraintObj(ffi::UnsafeInit tag) : var(tag), value(tag) {}
74
75 static void RegisterReflection() {
76 namespace refl = ffi::reflection;
77 refl::ObjectDef<BindingConstraintObj>()
78 .def_ro("var", &BindingConstraintObj::var)
79 .def_ro("value", &BindingConstraintObj::value);
80 }
82};
83
88 public:
93 : Constraint(ffi::make_object<BindingConstraintObj>(std::move(var), std::move(value))) {}
96};
97
103 public:
104 RangeConstraintObj(Var var, Range range) : var(std::move(var)), range(std::move(range)) {}
105 explicit RangeConstraintObj(ffi::UnsafeInit tag) : var(tag), range(tag) {}
114
115 static void RegisterReflection() {
116 namespace refl = ffi::reflection;
117 refl::ObjectDef<RangeConstraintObj>()
118 .def_ro("var", &RangeConstraintObj::var)
119 .def_ro("range", &RangeConstraintObj::range);
120 }
122};
123
128 public:
133 : Constraint(ffi::make_object<RangeConstraintObj>(std::move(var), std::move(range))) {}
135};
136
141 public:
144 explicit PredicateConstraintObj(ffi::UnsafeInit tag) : condition(tag) {}
155 bool assume_always{false};
156
157 static void RegisterReflection() {
158 namespace refl = ffi::reflection;
159 refl::ObjectDef<PredicateConstraintObj>()
160 .def_ro("condition", &PredicateConstraintObj::condition)
161 .def_ro("assume_always", &PredicateConstraintObj::assume_always, refl::DefaultValue(false));
162 }
165};
166
171 public:
175 explicit PredicateConstraint(PrimExpr condition, bool assume_always = false)
176 : Constraint(ffi::make_object<PredicateConstraintObj>(std::move(condition), assume_always)) {}
179};
180
182
191class ConstraintContextObj : public ffi::Object {
192 public:
201 ConstraintContextObj(ffi::ObjectPtr<ConstraintContextObj> parent, int64_t parent_prefix_size)
202 : parent_(std::move(parent)), parent_prefix_size_(parent_prefix_size) {
203 TVM_FFI_CHECK(parent_ && 0 <= parent_prefix_size && parent_prefix_size <= parent_->LocalSize(),
204 ValueError)
205 << "Constraint context parent local prefix is out of bounds";
206 }
207 // Context nodes have stable identity; use Snapshot to capture their current position.
210
216 void Append(const Constraint& constraint) { constraints_.push_back(constraint); }
217
222 int64_t LocalSize() const { return constraints_.size(); }
223
239
247 template <typename F>
248 void ForEach(F&& f) const {
249 // Keep this object alive if a callback replaces the caller's last handle.
250 auto context = ffi::GetObjectPtr<ConstraintContextObj>(const_cast<ConstraintContextObj*>(this));
251 context->ForEach(context->LocalSize(), std::forward<F>(f));
252 }
253
261 ffi::Array<Constraint> ToArray() const { return ToArray(LocalSize()); }
262
264 static constexpr bool _type_mutable = true;
266 static void RegisterReflection();
267 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ConstraintContext", ConstraintContextObj, ffi::Object);
268
269 private:
270 friend class AnalyzerObj;
271 // Materialize the fixed inherited sequence and the requested local prefix.
272 ffi::Array<Constraint> ToArray(int64_t local_prefix) const {
273 if (!parent_) {
274 if (local_prefix == LocalSize()) return constraints_;
275 return ffi::Array<Constraint>(constraints_.begin(), constraints_.begin() + local_prefix);
276 }
277 auto result = parent_->ToArray(parent_prefix_size_);
278 if (local_prefix != 0) {
279 result.insert(result.end(), constraints_.begin(), constraints_.begin() + local_prefix);
280 }
281 return result;
282 }
283
284 // Traverse a fixed prefix even if callbacks append more local constraints.
285 template <typename F>
286 void ForEach(int64_t local_prefix, F&& f) const {
287 if (parent_) parent_->ForEach(parent_prefix_size_, f);
288 for (int64_t i = 0; i < local_prefix; ++i) {
289 // Read by value: a callback may append and reallocate the local array.
290 const Constraint constraint = constraints_[i];
291 f(constraint);
292 }
293 }
294
295 // Fixed parent context; null for a root. Parent links are acyclic.
296 const ffi::ObjectPtr<ConstraintContextObj> parent_;
297 // Fixed prefix length in the parent's LOCAL sequence, not its effective count.
298 const int64_t parent_prefix_size_{0};
299 // Append-only local sequence; captured entries cannot be replaced or mutated through aliases.
300 ffi::Array<Constraint> constraints_;
301};
302
306class ConstraintContext : public ffi::ObjectRef {
307 public:
309 ConstraintContext() : ffi::ObjectRef(ffi::make_object<ConstraintContextObj>()) {}
318 ConstraintContext(ConstraintContext parent, int64_t parent_prefix_size)
319 : ffi::ObjectRef(ffi::make_object<ConstraintContextObj>(
320 ffi::GetObjectPtr<ConstraintContextObj>(parent.get()), parent_prefix_size)) {}
322 explicit ConstraintContext(ffi::ObjectPtr<ConstraintContextObj> node)
323 : ffi::ObjectRef(std::move(node)) {}
326};
327
329 return ConstraintContext(ffi::GetRef<ConstraintContext>(this), LocalSize());
330}
331
332} // namespace sym
333} // namespace tvm
334#endif // TVM_SYM_CONSTRAINT_H_
Base expr nodes in TVM.
TIR expressions.
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
Coordinated symbolic analyses over an explicitly synchronized premise environment.
Definition analyzer.h:556
The value captured when a variable is bound. A mutable-read RHS is retained as data; it must not be r...
Definition constraint.h:62
BindingConstraintObj(Var var, PrimExpr value)
Definition constraint.h:64
BindingConstraintObj(ffi::UnsafeInit tag)
Definition constraint.h:65
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.BindingConstraint", BindingConstraintObj, ConstraintObj)
Var var
The variable whose bound value is recorded.
Definition constraint.h:69
static void RegisterReflection()
Definition constraint.h:75
PrimExpr value
The RHS expression at the binding site, including any mutable reads.
Definition constraint.h:73
Managed reference to BindingConstraintObj.
Definition constraint.h:87
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BindingConstraint, Constraint, BindingConstraintObj)
BindingConstraint(Var var, PrimExpr value)
Construct a premise from its fields, without inference.
Definition constraint.h:92
Tracks the constraints relevant to each point in a traversal.
Definition constraint.h:191
ConstraintContext Snapshot() const
Captures the constraints currently visible in this context. Later additions to this context do not af...
Definition constraint.h:328
void Append(const Constraint &constraint)
Append a premise locally, preserving every previously captured prefix.
Definition constraint.h:216
ConstraintContextObj()=default
Construct an empty root context.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.ConstraintContext", ConstraintContextObj, ffi::Object)
static constexpr bool _type_mutable
Allow appending through a const context handle.
Definition constraint.h:264
ffi::Array< Constraint > ToArray() const
Returns an independent array of the constraints in this context. Includes inherited constraints in or...
Definition constraint.h:261
ConstraintContextObj(ffi::ObjectPtr< ConstraintContextObj > parent, int64_t parent_prefix_size)
Capture an inherited prefix and start an empty local sequence.
Definition constraint.h:201
ConstraintContextObj & operator=(const ConstraintContextObj &)=delete
static void RegisterReflection()
Register the context methods exposed through FFI reflection.
ConstraintContextObj(const ConstraintContextObj &)=delete
int64_t LocalSize() const
Count the local entries.
Definition constraint.h:222
void ForEach(F &&f) const
Read constraints in inherited/local order. Later appends do not enter this iteration....
Definition constraint.h:248
Managed reference to ConstraintContextObj. Copies share the same context.
Definition constraint.h:306
ConstraintContext(ffi::ObjectPtr< ConstraintContextObj > node)
Adopt a reference to a non-null context object.
Definition constraint.h:322
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ConstraintContext, ffi::ObjectRef, ConstraintContextObj)
ConstraintContext(ConstraintContext parent, int64_t parent_prefix_size)
Construct a child capturing [0, parent_prefix_size) of its parent's local entries....
Definition constraint.h:318
ConstraintContext()
Construct an empty root.
Definition constraint.h:309
Base node for a symbolic premise; it owns no analyzer state.
Definition constraint.h:40
TVM_FFI_DECLARE_OBJECT_INFO("sym.Constraint", ConstraintObj, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition constraint.h:42
A binding, range, or predicate, without inference or normalization.
Definition constraint.h:49
Constraint(ffi::ObjectPtr< ConstraintObj > node)
Adopt an existing premise node.
Definition constraint.h:54
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Constraint, ffi::ObjectRef, ConstraintObj)
A boolean premise. Its validity and lifetime are supplied by the collector.
Definition constraint.h:140
PredicateConstraintObj(ffi::UnsafeInit tag)
Definition constraint.h:144
PredicateConstraintObj(PrimExpr condition, bool assume_always=false)
Definition constraint.h:142
bool assume_always
Whether to assume the predicate remains true throughout the active constraint scope,...
Definition constraint.h:155
PrimExpr condition
The boolean predicate supplied by the collector.
Definition constraint.h:148
static void RegisterReflection()
Definition constraint.h:157
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.PredicateConstraint", PredicateConstraintObj, ConstraintObj)
Managed reference to PredicateConstraintObj.
Definition constraint.h:170
PredicateConstraint(PrimExpr condition, bool assume_always=false)
Construct a premise from its fields, without inference.
Definition constraint.h:175
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PredicateConstraint, Constraint, PredicateConstraintObj)
A variable lies in a half-open range. The range need not enumerate every attainable value.
Definition constraint.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.RangeConstraint", RangeConstraintObj, ConstraintObj)
Range range
A half-open superset of the variable's possible values.
Definition constraint.h:113
RangeConstraintObj(ffi::UnsafeInit tag)
Definition constraint.h:105
static void RegisterReflection()
Definition constraint.h:115
Var var
The variable whose possible values lie in the range.
Definition constraint.h:109
RangeConstraintObj(Var var, Range range)
Definition constraint.h:104
Managed reference to RangeConstraintObj.
Definition constraint.h:127
RangeConstraint(Var var, Range range)
Construct a premise from its fields, without inference.
Definition constraint.h:132
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RangeConstraint, Constraint, RangeConstraintObj)
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41