24#ifndef TVM_TIRX_STRUCTURAL_MUTATE_WITH_CONSTRAINT_CONTEXT_H_
25#define TVM_TIRX_STRUCTURAL_MUTATE_WITH_CONSTRAINT_CONTEXT_H_
27#include <tvm/ffi/extra/structural_mutate.h>
56 if (needs_child_context_) {
58 needs_child_context_ =
false;
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
80 needs_child_context_ = saved_needs_child;
89 ffi::AnyView value, ffi::InplaceMode mode = ffi::InplaceMode::kDisallow)
noexcept;
95 : ffi::StructuralMutatorObj(vtable) {}
100 bool needs_child_context_{
false};
111 ffi::ObjectPtr<StructuralMutatorWithConstraintContextObj> node)
112 : ffi::StructuralMutator(std::move(node)) {}
114 ffi::StructuralMutator,
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");
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));
140template <ffi::WalkOrder order,
typename... Callbacks>
142 return StructuralMapWithConstraintContextExpected<order>(std::move(root),
143 std::forward<Callbacks>(callbacks)...)
150template <
typename... Callbacks>
152 ffi::Any root, Callbacks&&... callbacks)
noexcept {
153 static_assert(
sizeof...(Callbacks) != 0,
"At least one callback is required");
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));
178template <
typename... Callbacks>
181 std::forward<Callbacks>(callbacks)...)
189 ffi::reflection::NativeFunctionView<ffi::UnchangedOr<ffi::Any>(
196inline constexpr ffi::reflection::AttrKey<FStructuralMutateWithConstraintContext>
201inline constexpr ffi::reflection::AttrKey<FStructuralMutateWithConstraintContext>
203 "__s_maybe_inplace_mutate_with_constraint_context__"};
206inline ffi::Expected<ffi::UnchangedOr<ffi::Any>>
208 ffi::InplaceMode mode)
noexcept {
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);
216 return ffi::Unexpected(ffi::Error(
218 "Constraint-aware mutation hook must be an opaque function pointer or ffi.Function",
""));
222 if (attr.type_index() == ffi::TypeIndex::kTVMFFIOpaquePtr) {
223 raw =
reinterpret_cast<ABI
>(attr.cast<
void*>())(
this, value);
225 raw = ffi::details::ExpectedUnsafe::MoveToTVMFFIAny(
226 attr.cast<ffi::Function>().CallExpected<ffi::Any>(
this, value));
228 auto result = ffi::details::ExpectedUnsafe::MoveFromTVMFFIAny<ffi::UnchangedOr<ffi::Any>>(raw);
229 if (result.is_err()) {
230 ffi::detail::UpdateVisitErrorContext(&result, 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
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.