Template Function tvm::ffi::StructuralWalkExpected

Template Function tvm::ffi::StructuralWalkExpected#

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 second TVMFFIDefRegionKind argument 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>; see WalkResult.

Example:

See also

WalkOrder, WalkResult

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 return Expected<WalkResult>.

Returns:

std::nullopt if traversal completed, or the interrupt returned by a callback.