[TIR][Utility] More flexible tir::Substitute arguments #14251
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, the
tir::Substitutemethod had overloads that supported a few ways of providing the variable map (e.g.const Map<Var,PrimExpr>&,std::unordered_map<const VarNode*, PrimExpr>&, etc.), delegating out to the overload that usesstd::function<Optional<PrimExpr>(const Var&)>. However, the types supported for the variable map depended on the type being substituted (e.g. only supportingconst Map<Var,PrimExpr>&with substituting into aArray<Range>), which would be unexpected to new developers.This PR makes the
tir::Substituteutility more uniform in the arguments that it accepts.For any type that is supported by
tir::Substitute,Array<T>is also supported.Any variable mapping type can be used with any substitution type. All variable mapping types are normalized to
std::function<Optional<PrimExpr>(const Var&)>.For
Mapandstd::unordered_maparguments, the value type may be any subclass ofPrimExpr(e.g.Map<Var, Var>instead ofMap<Var, PrimExpr>). Previously, the calling scope needed to either construct a temporary map that returnedPrimExpr, or to use a broader value type in the map than otherwise required.The initial and primary goal was to allow a
Map<Var, Var>to be used as an argument totir::Substitute, rather than aMap<Var, PrimExpr>, and making the utility more general was more straightforward than adding multiple overloads specificall forMap<Var, Var>.