24#ifndef TVM_TIRX_STRUCTURAL_VISIT_WITH_CONSTRAINT_CONTEXT_H_
25#define TVM_TIRX_STRUCTURAL_VISIT_WITH_CONSTRAINT_CONTEXT_H_
27#include <tvm/ffi/extra/structural_visit.h>
58 if (needs_child_context_) {
60 needs_child_context_ =
false;
75 bool saved_needs_child = needs_child_context_;
76 needs_child_context_ =
true;
77 auto result = std::forward<F>(f)();
78 if (!result.is_err()) {
79 constraints = saved_context->LocalSize() == saved_prefix
82 needs_child_context_ = saved_needs_child;
111 ffi::AnyView value)
noexcept;
117 : ffi::StructuralVisitorObj(vtable) {}
122 bool needs_child_context_{
false};
134 ffi::ObjectPtr<StructuralVisitorWithConstraintContextObj> node)
135 : ffi::StructuralVisitor(std::move(node)) {}
137 ffi::StructuralVisitor,
144template <ffi::WalkOrder order,
typename... Callbacks>
146 ffi::AnyView root, Callbacks&&... callbacks)
noexcept {
147 static_assert(
sizeof...(Callbacks) != 0,
"At least one callback is required");
149 std::decay_t<Callbacks>...>;
150 auto visitor = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
151 return visitor->VisitExpected(root);
171template <ffi::WalkOrder order,
typename... Callbacks>
173 Callbacks&&... callbacks) {
174 return StructuralWalkWithConstraintContextExpected<order>(root,
175 std::forward<Callbacks>(callbacks)...)
182template <
typename... Callbacks>
184 ffi::AnyView root, Callbacks&&... callbacks)
noexcept {
185 static_assert(
sizeof...(Callbacks) != 0,
"At least one callback is required");
187 std::decay_t<Callbacks>...>;
188 auto visitor = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
189 return visitor->VisitExpected(root);
198template <
typename... Callbacks>
200 Callbacks&&... callbacks) {
209 ffi::reflection::NativeFunctionView<ffi::Optional<ffi::VisitInterrupt>(
215inline constexpr ffi::reflection::AttrKey<FStructuralVisitWithConstraintContext>
221 static const ffi::StructuralVisitorVTable table{[](ffi::StructuralVisitorObj* self,
222 ffi::AnyView value)
noexcept {
223 return ffi::details::ExpectedUnsafe::MoveToTVMFFIAny(
230inline ffi::Expected<ffi::Optional<ffi::VisitInterrupt>>
233 auto attr = column[value.type_index()];
234 if (attr ==
nullptr)
return ffi::StructuralVisitorObj::DefaultVisitExpected(value);
237 return ffi::Unexpected(ffi::Error(
239 "__s_visit_with_constraint_context__ must be an opaque function pointer or ffi.Function",
242 return hook->CallExpected(
this, value);
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
Visits values using their structural visit behavior while maintaining the constraint context relevant...
Definition structural_visit_with_constraint_context.h:38
TVM_FFI_DECLARE_OBJECT_INFO("tirx.StructuralVisitorWithConstraintContext", StructuralVisitorWithConstraintContextObj, ffi::StructuralVisitorObj)
StructuralVisitorWithConstraintContextObj()
Construct a visitor using constraint-aware default descent.
Definition structural_visit_with_constraint_context.h:219
StateTupleType StateTuple() const noexcept
Definition structural_visit_with_constraint_context.h:119
std::tuple< const sym::ConstraintContext & > StateTupleType
Definition structural_visit_with_constraint_context.h:41
void AppendConstraint(const sym::Constraint &constraint)
Add a fact to the current scope, creating its child context on the first append.
Definition structural_visit_with_constraint_context.h:57
ffi::Expected< ffi::Optional< ffi::VisitInterrupt > > DefaultVisitExpected(ffi::AnyView value) noexcept
Descend with the sparse specialized hook, or the ordinary fallback. Call this from a Visit callback t...
Definition structural_visit_with_constraint_context.h:231
StructuralVisitorWithConstraintContextObj(const ffi::StructuralVisitorVTable *vtable)
Definition structural_visit_with_constraint_context.h:116
auto WithConstraintScope(F &&f) -> decltype(f())
Run an Expected-returning callback in a constraint scope. Use AppendConstraint to add facts....
Definition structural_visit_with_constraint_context.h:72
sym::ConstraintContext constraints
Active effective premises. Consumers must not append during traversal. Retain this handle and constra...
Definition structural_visit_with_constraint_context.h:51
Owning reference to the active specialized visitor.
Definition structural_visit_with_constraint_context.h:128
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StructuralVisitorWithConstraintContext, ffi::StructuralVisitor, StructuralVisitorWithConstraintContextObj)
StructuralVisitorWithConstraintContext(ffi::ObjectPtr< StructuralVisitorWithConstraintContextObj > node)
Adopt an active traversal object.
Definition structural_visit_with_constraint_context.h:133
Data-only symbolic premises collected during traversal.
constexpr ffi::reflection::AttrKey< FStructuralVisitWithConstraintContext > kStructuralVisitWithConstraintContext
Optional constraint-aware descent; missing hooks use ordinary structural descent.
Definition structural_visit_with_constraint_context.h:216
ffi::Optional< ffi::VisitInterrupt > StructuralWalkWithConstraintContext(ffi::AnyView root, Callbacks &&... callbacks)
Walk every occurrence with (value, const sym::ConstraintContext&) callbacks. A trailing TVMFFIDefRegi...
Definition structural_visit_with_constraint_context.h:172
ffi::reflection::NativeFunctionView< ffi::Optional< ffi::VisitInterrupt >(StructuralVisitorWithConstraintContextObj *, ffi::AnyView)> FStructuralVisitWithConstraintContext
Sparse hook with the same result/error contract as FStructuralVisit.
Definition structural_visit_with_constraint_context.h:210
ffi::Optional< ffi::VisitInterrupt > StructuralVisitWithConstraintContext(ffi::AnyView root, Callbacks &&... callbacks)
Visit with callbacks (value, StructuralVisitorWithConstraintContextObj*). The first matching callback...
Definition structural_visit_with_constraint_context.h:199
ffi::Expected< ffi::Optional< ffi::VisitInterrupt > > StructuralWalkWithConstraintContextExpected(ffi::AnyView root, Callbacks &&... callbacks) noexcept
Expected form of StructuralWalkWithConstraintContext.
Definition structural_visit_with_constraint_context.h:145
ffi::Expected< ffi::Optional< ffi::VisitInterrupt > > StructuralVisitWithConstraintContextExpected(ffi::AnyView root, Callbacks &&... callbacks) noexcept
Expected form of StructuralVisitWithConstraintContext.
Definition structural_visit_with_constraint_context.h:183
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41