25 #include "llvm/ADT/ScopeExit.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <type_traits>
30 #define DEBUG_TYPE "linalg-interchange"
39 if (interchangeVector.empty() ||
40 genericOp.getNumLoops() != interchangeVector.size())
44 genericOp.getContext())))
60 assert(permutationMap &&
"unexpected null map");
64 auto guard = llvm::make_scope_exit(
69 for (
OpOperand &opOperand : genericOp->getOpOperands()) {
70 AffineMap m = genericOp.getMatchingIndexingMap(&opOperand);
72 m = m.compose(permutationMap);
73 newIndexingMaps.push_back(m);
75 genericOp.setIndexingMapsAttr(
81 llvm::append_range(itTypesVector, itTypes);
84 genericOp.setIteratorTypesAttr(rewriter.
getArrayAttr(itTypesVector));
87 if (genericOp.hasIndexSemantics()) {
89 for (IndexOp indexOp :
90 llvm::make_early_inc_range(genericOp.getBody()->getOps<IndexOp>())) {
93 allIndices.reserve(genericOp.getNumLoops());
94 llvm::transform(llvm::seq<uint64_t>(0, genericOp.getNumLoops()),
95 std::back_inserter(allIndices), [&](uint64_t dim) {
96 return rewriter.create<IndexOp>(indexOp->getLoc(), dim);
99 indexOp, permutationMap.
getSubMap(indexOp.getDim()), allIndices);
static LogicalResult interchangeGenericOpPrecondition(GenericOp genericOp, ArrayRef< unsigned > interchangeVector)
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
bool isEmpty() const
Returns true if this affine map is an empty map, i.e., () -> ().
static AffineMap getPermutationMap(ArrayRef< unsigned > permutation, MLIRContext *context)
Returns an AffineMap representing a permutation.
AffineMap getSubMap(ArrayRef< unsigned > resultPos) const
Returns the map consisting of the resultPos subset.
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
MLIRContext is the top-level object for a collection of MLIR operations.
RAII guard to reset the insertion point of the builder when destroyed.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
This class represents an operand of an operation.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void startOpModification(Operation *op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
FailureOr< GenericOp > interchangeGenericOp(RewriterBase &rewriter, GenericOp genericOp, ArrayRef< unsigned > interchangeVector)
Interchange the iterator_types and iterator_maps dimensions and adapts the index accesses of op.
Include the generated interface declarations.
AffineMap inversePermutation(AffineMap map)
Returns a map of codomain to domain dimensions such that the first codomain dimension for a particula...
void applyPermutationToVector(SmallVector< T, N > &inVec, ArrayRef< int64_t > permutation)
Apply the permutation defined by permutation to inVec.