14 #ifndef MLIR_IR_AFFINEMAP_H
15 #define MLIR_IR_AFFINEMAP_H
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMapInfo.h"
22 #include "llvm/ADT/SmallBitVector.h"
23 #include "llvm/ADT/SmallVectorExtras.h"
33 struct AffineMapStorage;
58 static AffineMap get(
unsigned dimCount,
unsigned symbolCount,
63 static AffineMap get(
unsigned dimCount,
unsigned symbolCount,
68 static AffineMap get(
unsigned dimCount,
unsigned symbolCount,
132 explicit operator bool()
const {
return map !=
nullptr; }
200 void print(raw_ostream &os)
const;
245 unsigned numResultDims,
246 unsigned numResultSyms)
const;
252 unsigned numResultDims,
unsigned numResultSyms)
const;
263 unsigned numResultDims,
unsigned numResultSyms)
const;
270 llvm::map_to_vector<4>(
301 llvm::sort(reverse_sorted_positions, std::greater<int64_t>());
303 auto exprs = llvm::to_vector<4>(
getResults());
304 for (int64_t pos : reverse_sorted_positions)
305 exprs.erase(exprs.begin() + pos);
316 auto exprs = llvm::to_vector<4>(
getResults());
317 exprs.insert(exprs.begin() + pos, expr);
325 bool *hasPoison =
nullptr)
const;
335 bool *hasPoison =
nullptr)
const;
411 return static_cast<const void *
>(map);
420 static AffineMap getImpl(
unsigned dimCount,
unsigned symbolCount,
421 ArrayRef<AffineExpr> results, MLIRContext *context);
462 unsigned numDims = 0;
463 unsigned numSymbols = 0;
473 AffineMap
compressDims(AffineMap map,
const llvm::SmallBitVector &unusedDims);
485 const llvm::SmallBitVector &unusedSymbols);
498 ArrayRef<OpFoldResult> operands,
499 SmallVector<Value> &remainingValues);
636 const llvm::SmallBitVector &projectedDimensions,
637 bool compressDimsFlag =
false);
641 const llvm::SmallBitVector &projectedSymbols,
642 bool compressSymbolsFlag =
false);
646 const llvm::SmallBitVector &projectedDimensions,
647 bool compressDimsFlag =
true,
648 bool compressSymbolsFlag =
true);
662 const llvm::SmallBitVector &projectedDimensions);
674 template <
typename T>
681 if (
auto dimExpr = dyn_cast<AffineDimExpr>(expr)) {
682 result.push_back(source[dimExpr.getPosition()]);
683 }
else if (
auto constExpr = dyn_cast<AffineConstantExpr>(expr)) {
684 assert(constExpr.getValue() == 0 &&
685 "Unexpected constant in projected permutation map");
688 llvm_unreachable(
"Unexpected result in projected permutation map");
696 template <
typename AffineExprContainer>
698 int64_t &maxDim, int64_t &maxSym) {
699 for (
const auto &exprs : exprsList) {
700 for (
auto expr : exprs) {
702 if (
auto d = dyn_cast<AffineDimExpr>(e))
703 maxDim =
std::max(maxDim,
static_cast<int64_t
>(d.getPosition()));
704 if (
auto s = dyn_cast<AffineSymbolExpr>(e))
705 maxSym =
std::max(maxSym,
static_cast<int64_t
>(s.getPosition()));
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
A dimensional identifier appearing in an affine expression.
Base type for affine expression.
AffineExpr shiftDims(unsigned numDims, unsigned shift, unsigned offset=0) const
Replace dims[offset ...
AffineExpr shiftSymbols(unsigned numSymbols, unsigned shift, unsigned offset=0) const
Replace symbols[offset ...
bool isFunctionOfDim(unsigned position) const
Return true if the affine expression involves AffineDimExpr position.
bool isFunctionOfSymbol(unsigned position) const
Return true if the affine expression involves AffineSymbolExpr position.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
int64_t getSingleConstantResult() const
Returns the constant result of this map.
static AffineMap getMinorIdentityMap(unsigned dims, unsigned results, MLIRContext *context)
Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most minor dimensions.
AffineMap dropResults(ArrayRef< int64_t > positions) const
AffineMap getSliceMap(unsigned start, unsigned length) const
Returns the map consisting of length expressions starting from start.
AffineMap getMajorSubMap(unsigned numResults) const
Returns the map consisting of the most major numResults results.
MLIRContext * getContext() const
friend ::llvm::hash_code hash_value(AffineMap arg)
bool isFunctionOfDim(unsigned position) const
Return true if any affine expression involves AffineDimExpr position.
bool isMinorIdentity() const
Returns true if this affine map is a minor identity, i.e.
unsigned getDimPosition(unsigned idx) const
Extracts the position of the dimensional expression at the given result, when the caller knows it is ...
bool isConstant() const
Returns true if this affine map has only constant results.
static AffineMap getMultiDimIdentityMap(unsigned numDims, MLIRContext *context)
Returns an AffineMap with 'numDims' identity result dim exprs.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
AffineMap shiftDims(unsigned shift, unsigned offset=0) const
Replace dims[offset ...
bool isSingleConstant() const
Returns true if this affine map is a single result constant function.
AffineMap insertResult(AffineExpr expr, unsigned pos) const
Returns a new AffineMap with the same number of dims and symbols and an extra result inserted at pos.
bool isProjectedPermutation(bool allowZeroInResults=false) const
Returns true if the AffineMap represents a subset (i.e.
AffineMap getMinorSubMap(unsigned numResults) const
Returns the map consisting of the most minor numResults results.
detail::AffineMapStorage ImplType
uint64_t getLargestKnownDivisorOfMapExprs()
Get the largest known divisor of all map expressions.
constexpr AffineMap()=default
bool isEmpty() const
Returns true if this affine map is an empty map, i.e., () -> ().
AffineMap dropResult(int64_t pos) const
Returns a new AffineMap with the same number of dims and symbols and one less result at pos,...
std::optional< unsigned > getResultPosition(AffineExpr input) const
Extracts the first result position where input dimension resides.
bool operator==(AffineMap other) const
unsigned getNumSymbols() const
bool isMinorIdentityWithBroadcasting(SmallVectorImpl< unsigned > *broadcastedDims=nullptr) const
Returns true if this affine map is a minor identity up to broadcasted dimensions which are indicated ...
unsigned getNumDims() const
ArrayRef< AffineExpr > getResults() const
SmallVector< int64_t > getConstantResults() const
Returns the constant results of this map.
bool isFunctionOfSymbol(unsigned position) const
Return true if any affine expression involves AffineSymbolExpr position.
bool isPermutationOfMinorIdentityWithBroadcasting(SmallVectorImpl< unsigned > &permutedDims) const
Return true if this affine map can be converted to a minor identity with broadcast by doing a permute...
size_t getNumOfZeroResults() const
Returns the number of "zero" results (constant values == 0) in this map.
bool isSymbolIdentity() const
Returns true if this affine map is an identity affine map on the symbol identifiers.
unsigned getNumResults() const
AffineMap replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements, unsigned numResultDims, unsigned numResultSyms) const
This method substitutes any uses of dimensions and symbols (e.g.
unsigned getNumInputs() const
AffineMap shiftSymbols(unsigned shift, unsigned offset=0) const
Replace symbols[offset ...
static AffineMap getFromOpaquePointer(const void *pointer)
const void * getAsOpaquePointer() const
Methods supporting C API.
AffineExpr getResult(unsigned idx) const
static AffineMap getFilteredIdentityMap(MLIRContext *ctx, unsigned numDims, llvm::function_ref< bool(AffineDimExpr)> keepDimFilter)
Returns an identity affine map with numDims input dimensions and filtered results using keepDimFilter...
AffineMap replace(AffineExpr expr, AffineExpr replacement, unsigned numResultDims, unsigned numResultSyms) const
Sparse replace method.
AffineMap dropZeroResults()
Returns the AffineMap resulting from removing "zero" results (constant values == 0) from this map.
static AffineMap getPermutationMap(ArrayRef< unsigned > permutation, MLIRContext *context)
Returns an AffineMap representing a permutation.
SmallVector< unsigned > getBroadcastDims() const
Returns the list of broadcast dimensions (i.e.
bool operator!=(AffineMap other) const
void walkExprs(llvm::function_ref< void(AffineExpr)> callback) const
Walk all of the AffineExpr's in this mapping.
AffineMap partialConstantFold(ArrayRef< Attribute > operandConstants, SmallVectorImpl< int64_t > *results=nullptr, bool *hasPoison=nullptr) const
Propagates the constant operands into this affine map.
static AffineMap getConstantMap(int64_t val, MLIRContext *context)
Returns a single constant result affine map.
static AffineMap getMultiDimMapWithTargets(unsigned numDims, ArrayRef< unsigned > targets, MLIRContext *context)
Returns an affine map with numDims input dimensions and results specified by targets.
AffineMap getSubMap(ArrayRef< unsigned > resultPos) const
Returns the map consisting of the resultPos subset.
LogicalResult constantFold(ArrayRef< Attribute > operandConstants, SmallVectorImpl< Attribute > &results, bool *hasPoison=nullptr) const
Folds the results of the application of an affine map on the provided operands to a constant if possi...
void print(raw_ostream &os) const
AffineMap compose(AffineMap map) const
Returns the AffineMap resulting from composing this with map.
bool isIdentity() const
Returns true if this affine map is an identity affine map.
bool isPermutation() const
Returns true if the AffineMap represents a symbol-less permutation map.
static SmallVector< AffineMap, 4 > inferFromExprList(ArrayRef< ArrayRef< AffineExpr >> exprsList, MLIRContext *context)
Returns a vector of AffineMaps; each with as many results as exprs.size(), as many dims as the larges...
MLIRContext is the top-level object for a collection of MLIR operations.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
AffineMap simplifyAffineMap(AffineMap map)
Simplifies an affine map by simplifying its underlying AffineExpr results.
inline ::llvm::hash_code hash_value(AffineMap arg)
AffineMap concatAffineMaps(ArrayRef< AffineMap > maps, MLIRContext *context)
Concatenates a list of maps into a single AffineMap, stepping over potentially empty maps.
AffineMap expandDimsToRank(AffineMap map, int64_t rank, const llvm::SmallBitVector &projectedDimensions)
Expand map to operate on rank dims while projecting out the dims in projectedDimensions.
AffineMap removeDuplicateExprs(AffineMap map)
Returns a map with the same dimension and symbol count as map, but whose results are the unique affin...
llvm::SmallBitVector getUnusedSymbolsBitVector(ArrayRef< AffineMap > maps)
SmallVector< T > applyPermutationMap(AffineMap map, llvm::ArrayRef< T > source)
Apply a permutation from map to source and return the result.
AffineMap inverseAndBroadcastProjectedPermutation(AffineMap map)
Return the reverse map of a projected permutation where the projected dimensions are transformed into...
AffineMap inversePermutation(AffineMap map)
Returns a map of codomain to domain dimensions such that the first codomain dimension for a particula...
AffineMap compressSymbols(AffineMap map, const llvm::SmallBitVector &unusedSymbols)
Drop the symbols that are listed in unusedSymbols.
static void getMaxDimAndSymbol(ArrayRef< AffineExprContainer > exprsList, int64_t &maxDim, int64_t &maxSym)
Calculates maximum dimension and symbol positions from the expressions in exprsLists and stores them ...
AffineMap compressUnusedDims(AffineMap map)
Drop the dims that are not used.
AffineMap compressDims(AffineMap map, const llvm::SmallBitVector &unusedDims)
Drop the dims that are listed in unusedDims.
AffineMap getProjectedMap(AffineMap map, const llvm::SmallBitVector &projectedDimensions, bool compressDimsFlag=true, bool compressSymbolsFlag=true)
Calls projectDims(map, projectedDimensions, compressDimsFlag).
llvm::SmallBitVector getUnusedDimsBitVector(ArrayRef< AffineMap > maps)
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
AffineMap projectDims(AffineMap map, const llvm::SmallBitVector &projectedDimensions, bool compressDimsFlag=false)
Returns the map that results from projecting out the dimensions specified in projectedDimensions.
AffineMap compressUnusedSymbols(AffineMap map)
Drop the symbols that are not used.
AffineMap projectSymbols(AffineMap map, const llvm::SmallBitVector &projectedSymbols, bool compressSymbolsFlag=false)
Symbol counterpart of projectDims.
AffineMap foldAttributesIntoMap(Builder &b, AffineMap map, ArrayRef< OpFoldResult > operands, SmallVector< Value > &remainingValues)
Fold all attributes among the given operands into the affine map.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
static unsigned getHashValue(mlir::AffineMap val)
static mlir::AffineMap getEmptyKey()
static bool isEqual(mlir::AffineMap LHS, mlir::AffineMap RHS)
static mlir::AffineMap getTombstoneKey()
A mutable affine map. Its affine expressions are however unique.
void setNumDims(unsigned d)
void reset(AffineMap map)
Resets this MutableAffineMap with 'map'.
MutableAffineMap()=default
void setResult(unsigned idx, AffineExpr result)
AffineMap getAffineMap() const
Get the AffineMap corresponding to this MutableAffineMap.
void setNumSymbols(unsigned d)
AffineExpr getResult(unsigned idx) const
unsigned getNumSymbols() const
ArrayRef< AffineExpr > getResults() const
bool isMultipleOf(unsigned idx, int64_t factor) const
Returns true if the idx'th result expression is a multiple of factor.
MLIRContext * getContext() const
unsigned getNumResults() const
unsigned getNumDims() const
void simplify()
Simplify the (result) expressions in this map using analysis (used by.