tvm
Loading...
Searching...
No Matches
structural_mutate_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_MUTATE_WITH_CONSTRAINT_CONTEXT_H_
25#define TVM_TIRX_STRUCTURAL_MUTATE_WITH_CONSTRAINT_CONTEXT_H_
26
27#include <tvm/ffi/extra/structural_mutate.h>
29
30namespace tvm {
31namespace tirx {
32
40class StructuralMutatorWithConstraintContextObj : public ffi::StructuralMutatorObj {
41 public:
43 using StateTupleType = std::tuple<const sym::ConstraintContext&>;
50
55 void AppendConstraint(const sym::Constraint& constraint) {
56 if (needs_child_context_) {
58 needs_child_context_ = false;
59 }
60 constraints->Append(constraint);
61 }
62
69 template <typename F>
70 auto WithConstraintScope(F&& f) -> decltype(f()) {
71 sym::ConstraintContext saved_context = constraints;
72 int64_t saved_prefix = constraints->LocalSize();
73 bool saved_needs_child = needs_child_context_;
74 needs_child_context_ = true;
75 auto result = std::forward<F>(f)();
76 if (!result.is_err()) {
77 constraints = saved_context->LocalSize() == saved_prefix
78 ? saved_context
79 : sym::ConstraintContext(saved_context, saved_prefix);
80 needs_child_context_ = saved_needs_child;
81 }
82 return result;
83 }
84
88 ffi::Expected<ffi::UnchangedOr<ffi::Any>> DefaultMutateExpected(
89 ffi::AnyView value, ffi::InplaceMode mode = ffi::InplaceMode::kDisallow) noexcept;
90 TVM_FFI_DECLARE_OBJECT_INFO("tirx.StructuralMutatorWithConstraintContext",
91 StructuralMutatorWithConstraintContextObj, ffi::StructuralMutatorObj);
92
93 protected:
94 explicit StructuralMutatorWithConstraintContextObj(const ffi::StructuralMutatorVTable* vtable)
95 : ffi::StructuralMutatorObj(vtable) {}
96
97 StateTupleType StateTuple() const noexcept { return {constraints}; }
98
99 private:
100 bool needs_child_context_{false};
101};
105class StructuralMutatorWithConstraintContext : public ffi::StructuralMutator {
106 public:
111 ffi::ObjectPtr<StructuralMutatorWithConstraintContextObj> node)
112 : ffi::StructuralMutator(std::move(node)) {}
114 ffi::StructuralMutator,
116};
117
121template <ffi::WalkOrder order, typename... Callbacks>
123 ffi::Any root, Callbacks&&... callbacks) noexcept {
124 static_assert(sizeof...(Callbacks) != 0, "At least one callback is required");
125 using Engine = ffi::StructuralMapEngine<StructuralMutatorWithConstraintContextObj, order,
126 std::decay_t<Callbacks>...>;
127 auto mutator = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
128 auto result = mutator->MutateExpected(root, ffi::InplaceMode::kAllow);
129 if (result.is_err()) return ffi::Unexpected(std::move(result).error());
130 return std::move(result).value().ValueOrUnchanged(std::move(root));
131}
132
140template <ffi::WalkOrder order, typename... Callbacks>
141ffi::Any StructuralMapWithConstraintContext(ffi::Any root, Callbacks&&... callbacks) {
142 return StructuralMapWithConstraintContextExpected<order>(std::move(root),
143 std::forward<Callbacks>(callbacks)...)
144 .value();
145}
146
150template <typename... Callbacks>
152 ffi::Any root, Callbacks&&... callbacks) noexcept {
153 static_assert(sizeof...(Callbacks) != 0, "At least one callback is required");
154 using Engine = ffi::StructuralMutateEngine<StructuralMutatorWithConstraintContextObj,
155 std::decay_t<Callbacks>...>;
156 auto mutator = ffi::make_object<Engine>(std::forward<Callbacks>(callbacks)...);
157 auto result = mutator->MutateExpected(root, ffi::InplaceMode::kAllow);
158 if (result.is_err()) return ffi::Unexpected(std::move(result).error());
159 return std::move(result).value().ValueOrUnchanged(std::move(root));
160}
161
178template <typename... Callbacks>
179ffi::Any StructuralMutateWithConstraintContext(ffi::Any root, Callbacks&&... callbacks) {
181 std::forward<Callbacks>(callbacks)...)
182 .value();
183}
184
189 ffi::reflection::NativeFunctionView<ffi::UnchangedOr<ffi::Any>(
191
192namespace type_attr {
196inline constexpr ffi::reflection::AttrKey<FStructuralMutateWithConstraintContext>
197 kStructuralMutateWithConstraintContext{"__s_mutate_with_constraint_context__"};
201inline constexpr ffi::reflection::AttrKey<FStructuralMutateWithConstraintContext>
203 "__s_maybe_inplace_mutate_with_constraint_context__"};
204} // namespace type_attr
205
206inline ffi::Expected<ffi::UnchangedOr<ffi::Any>>
208 ffi::InplaceMode mode) noexcept {
209 static ffi::reflection::TypeAttrColumn copying(type_attr::kStructuralMutateWithConstraintContext);
210 static ffi::reflection::TypeAttrColumn inplace(
212 auto attr = mode == ffi::InplaceMode::kAllow ? inplace[value.type_index()] : ffi::AnyView();
213 if (attr == nullptr) attr = copying[value.type_index()];
214 if (attr == nullptr) return ffi::StructuralMutatorObj::DefaultMutateExpected(value, mode);
215 if (!attr.try_cast<FStructuralMutateWithConstraintContext>()) {
216 return ffi::Unexpected(ffi::Error(
217 "TypeError",
218 "Constraint-aware mutation hook must be an opaque function pointer or ffi.Function", ""));
219 }
220 using ABI = TVMFFIAny (*)(StructuralMutatorWithConstraintContextObj*, ffi::AnyView) noexcept;
221 TVMFFIAny raw;
222 if (attr.type_index() == ffi::TypeIndex::kTVMFFIOpaquePtr) {
223 raw = reinterpret_cast<ABI>(attr.cast<void*>())(this, value);
224 } else {
225 raw = ffi::details::ExpectedUnsafe::MoveToTVMFFIAny(
226 attr.cast<ffi::Function>().CallExpected<ffi::Any>(this, value));
227 }
228 auto result = ffi::details::ExpectedUnsafe::MoveFromTVMFFIAny<ffi::UnchangedOr<ffi::Any>>(raw);
229 if (result.is_err()) {
230 ffi::detail::UpdateVisitErrorContext(&result, value);
231 }
232 return result;
233}
234
235} // namespace tirx
236} // namespace tvm
237#endif // TVM_TIRX_STRUCTURAL_MUTATE_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
Mutates values using their structural mutation behavior while maintaining the constraint context rele...
Definition structural_mutate_with_constraint_context.h:40
TVM_FFI_DECLARE_OBJECT_INFO("tirx.StructuralMutatorWithConstraintContext", StructuralMutatorWithConstraintContextObj, ffi::StructuralMutatorObj)
std::tuple< const sym::ConstraintContext & > StateTupleType
Definition structural_mutate_with_constraint_context.h:43
StructuralMutatorWithConstraintContextObj(const ffi::StructuralMutatorVTable *vtable)
Definition structural_mutate_with_constraint_context.h:94
ffi::Expected< ffi::UnchangedOr< ffi::Any > > DefaultMutateExpected(ffi::AnyView value, ffi::InplaceMode mode=ffi::InplaceMode::kDisallow) noexcept
Default path, bypassing this node's engine callback.
Definition structural_mutate_with_constraint_context.h:207
StateTupleType StateTuple() const noexcept
Definition structural_mutate_with_constraint_context.h:97
void AppendConstraint(const sym::Constraint &constraint)
Add a fact to the current scope, creating its child context on the first append.
Definition structural_mutate_with_constraint_context.h:55
sym::ConstraintContext constraints
Active effective premises. Consumers must not append during traversal. Retain this handle and constra...
Definition structural_mutate_with_constraint_context.h:49
auto WithConstraintScope(F &&f) -> decltype(f())
Run an Expected-returning callback in a constraint scope. Use AppendConstraint to add facts....
Definition structural_mutate_with_constraint_context.h:70
Owning reference to the specialized mutator.
Definition structural_mutate_with_constraint_context.h:105
StructuralMutatorWithConstraintContext(ffi::ObjectPtr< StructuralMutatorWithConstraintContextObj > node)
Adopt an active traversal object.
Definition structural_mutate_with_constraint_context.h:110
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StructuralMutatorWithConstraintContext, ffi::StructuralMutator, StructuralMutatorWithConstraintContextObj)
constexpr ffi::reflection::AttrKey< FStructuralMutateWithConstraintContext > kStructuralMaybeInplaceMutateWithConstraintContext
In-place descent after the engine establishes permission and uniqueness.
Definition structural_mutate_with_constraint_context.h:202
constexpr ffi::reflection::AttrKey< FStructuralMutateWithConstraintContext > kStructuralMutateWithConstraintContext
Copying descent for types with constraint scope behavior.
Definition structural_mutate_with_constraint_context.h:197
ffi::Expected< ffi::Any > StructuralMapWithConstraintContextExpected(ffi::Any root, Callbacks &&... callbacks) noexcept
Expected form of StructuralMapWithConstraintContext.
Definition structural_mutate_with_constraint_context.h:122
ffi::Any StructuralMutateWithConstraintContext(ffi::Any root, Callbacks &&... callbacks)
Mutate with (value, StructuralMutatorWithConstraintContextObj* [, InplaceMode]). The first matching c...
Definition structural_mutate_with_constraint_context.h:179
ffi::Expected< ffi::Any > StructuralMutateWithConstraintContextExpected(ffi::Any root, Callbacks &&... callbacks) noexcept
Expected form of StructuralMutateWithConstraintContext.
Definition structural_mutate_with_constraint_context.h:151
ffi::Any StructuralMapWithConstraintContext(ffi::Any root, Callbacks &&... callbacks)
Map with (value, const sym::ConstraintContext& [, def_region_kind]) callbacks. Callbacks return a rep...
Definition structural_mutate_with_constraint_context.h:141
ffi::reflection::NativeFunctionView< ffi::UnchangedOr< ffi::Any >(StructuralMutatorWithConstraintContextObj *, ffi::AnyView)> FStructuralMutateWithConstraintContext
Sparse constraint mutation hook, sharing the structural mutation ABI contract.
Definition structural_mutate_with_constraint_context.h:190
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Structural traversal with ordered lexical symbolic premises.