tvm
Loading...
Searching...
No Matches
structural_visit_with_constraint_context.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_TIRX_STRUCTURAL_VISIT_WITH_CONSTRAINT_CONTEXT_H_
25#define TVM_TIRX_STRUCTURAL_VISIT_WITH_CONSTRAINT_CONTEXT_H_
26
27#include <tvm/ffi/extra/structural_visit.h>
28#include <tvm/sym/constraint.h>
29
30namespace tvm {
31namespace tirx {
32
38class StructuralVisitorWithConstraintContextObj : public ffi::StructuralVisitorObj {
39 public:
41 using StateTupleType = std::tuple<const sym::ConstraintContext&>;
52
57 void AppendConstraint(const sym::Constraint& constraint) {
58 if (needs_child_context_) {
60 needs_child_context_ = false;
61 }
62 constraints->Append(constraint);
63 }
64
71 template <typename F>
72 auto WithConstraintScope(F&& f) -> decltype(f()) {
73 sym::ConstraintContext saved_context = constraints;
74 int64_t saved_prefix = constraints->LocalSize();
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
80 ? saved_context
81 : sym::ConstraintContext(saved_context, saved_prefix);
82 needs_child_context_ = saved_needs_child;
83 }
84 return result;
85 }
86
110 ffi::Expected<ffi::Optional<ffi::VisitInterrupt>> DefaultVisitExpected(
111 ffi::AnyView value) noexcept;
112 TVM_FFI_DECLARE_OBJECT_INFO("tirx.StructuralVisitorWithConstraintContext",
113 StructuralVisitorWithConstraintContextObj, ffi::StructuralVisitorObj);
114
115 protected:
116 explicit StructuralVisitorWithConstraintContextObj(const ffi::StructuralVisitorVTable* vtable)
117 : ffi::StructuralVisitorObj(vtable) {}
118
119 StateTupleType StateTuple() const noexcept { return {constraints}; }
120
121 private:
122 bool needs_child_context_{false};
123};
124
128class StructuralVisitorWithConstraintContext : public ffi::StructuralVisitor {
129 public:
134 ffi::ObjectPtr<StructuralVisitorWithConstraintContextObj> node)
135 : ffi::StructuralVisitor(std::move(node)) {}
137 ffi::StructuralVisitor,
139};
140
144template <ffi::WalkOrder order, typename... Callbacks>
145ffi::Expected<ffi::Optional<ffi::VisitInterrupt>> StructuralWalkWithConstraintContextExpected(
146 ffi::AnyView root, Callbacks&&... callbacks) noexcept {
147 static_assert(sizeof...(Callbacks) != 0, "At least one callback is required");
148 using Engine = ffi::StructuralWalkEngine<StructuralVisitorWithConstraintContextObj, order,
149 std::decay_t<Callbacks>...>;
150 auto visitor = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
151 return visitor->VisitExpected(root);
152}
153
171template <ffi::WalkOrder order, typename... Callbacks>
172ffi::Optional<ffi::VisitInterrupt> StructuralWalkWithConstraintContext(ffi::AnyView root,
173 Callbacks&&... callbacks) {
174 return StructuralWalkWithConstraintContextExpected<order>(root,
175 std::forward<Callbacks>(callbacks)...)
176 .value();
177}
178
182template <typename... Callbacks>
183ffi::Expected<ffi::Optional<ffi::VisitInterrupt>> StructuralVisitWithConstraintContextExpected(
184 ffi::AnyView root, Callbacks&&... callbacks) noexcept {
185 static_assert(sizeof...(Callbacks) != 0, "At least one callback is required");
186 using Engine = ffi::StructuralVisitEngine<StructuralVisitorWithConstraintContextObj,
187 std::decay_t<Callbacks>...>;
188 auto visitor = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
189 return visitor->VisitExpected(root);
190}
191
198template <typename... Callbacks>
199ffi::Optional<ffi::VisitInterrupt> StructuralVisitWithConstraintContext(ffi::AnyView root,
200 Callbacks&&... callbacks) {
201 return StructuralVisitWithConstraintContextExpected(root, std::forward<Callbacks>(callbacks)...)
202 .value();
203}
204
209 ffi::reflection::NativeFunctionView<ffi::Optional<ffi::VisitInterrupt>(
211namespace type_attr {
215inline constexpr ffi::reflection::AttrKey<FStructuralVisitWithConstraintContext>
216 kStructuralVisitWithConstraintContext{"__s_visit_with_constraint_context__"};
217} // namespace type_attr
218
221 static const ffi::StructuralVisitorVTable table{[](ffi::StructuralVisitorObj* self,
222 ffi::AnyView value) noexcept {
223 return ffi::details::ExpectedUnsafe::MoveToTVMFFIAny(
225 value));
226 }};
227 return &table;
228 }()) {}
229
230inline ffi::Expected<ffi::Optional<ffi::VisitInterrupt>>
232 static ffi::reflection::TypeAttrColumn column(type_attr::kStructuralVisitWithConstraintContext);
233 auto attr = column[value.type_index()];
234 if (attr == nullptr) return ffi::StructuralVisitorObj::DefaultVisitExpected(value);
235 auto hook = attr.try_cast<FStructuralVisitWithConstraintContext>();
236 if (!hook) {
237 return ffi::Unexpected(ffi::Error(
238 "TypeError",
239 "__s_visit_with_constraint_context__ must be an opaque function pointer or ffi.Function",
240 ""));
241 }
242 return hook->CallExpected(this, value);
243}
244
245} // namespace tirx
246} // namespace tvm
247#endif // TVM_TIRX_STRUCTURAL_VISIT_WITH_CONSTRAINT_CONTEXT_H_
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