Template Function tvm::ffi::StructuralWalkExpected#
Defined in File structural_visit.h
Function Documentation#
-
template<WalkOrder order, typename ...Callbacks>
Expected<Optional<VisitInterrupt>> tvm::ffi::StructuralWalkExpected(AnyView root, Callbacks&&... callbacks) noexcept# Walk a structured value graph and invoke typed callbacks on selected values.
The callbacks are invoked only for values matching the first argument type of one of the callbacks. The first callback argument may be
AnyView,Any, an object reference type, an object pointer type, or another FFI-convertible POD type. A callback may also optionally take a secondTVMFFIDefRegionKindargument to inspect whether the value is being visited in a definition region. Callbacks are tested in order, and the first match is used.Each callback should return
Expected<WalkResult>; seeWalkResult.WalkResult::Interrupt(...)halts traversal.WalkResult::Advance()continues traversal.WalkResult::Skip()skips children traversal.Errorindicates traversal failure.
Example:
See also
int num_adds = 0; Expected<Optional<VisitInterrupt>> result = StructuralWalkExpected<WalkOrder::kPreOrder>( root, [&](const Add& add) -> Expected<WalkResult> { ++num_adds; return WalkResult::Advance(); }, [&](const Mul& mul) -> Expected<WalkResult> { return WalkResult::Skip(); });
Note
Return type of each callback should be
Expected<WalkResult>.- Template Parameters:
order – Whether to invoke the callback before or after visiting children.
Callbacks – Callback types.
- Parameters:
root – The root value to visit.
callbacks – Callbacks invoked for matching nodes. Each callback may take either
(value)or(value, def_region_kind)and should returnExpected<WalkResult>.
- Returns:
std::nulloptif traversal completed, or the interrupt returned by a callback.