MLIR  15.0.0git
Classes | Macros | Enumerations | Functions
AffineOps.cpp File Reference
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/IR/AffineValueMap.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/BlockAndValueMapping.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/InliningUtils.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Debug.h"
#include "mlir/Dialect/Affine/IR/AffineOpsDialect.cpp.inc"
#include "mlir/Dialect/Affine/IR/AffineOps.cpp.inc"
+ Include dependency graph for AffineOps.cpp:

Go to the source code of this file.

Classes

struct  DeduplicateAffineMinMaxExpressions< T >
 Remove duplicated expressions in affine min/max ops. More...
 
struct  MergeAffineMinMaxOp< T >
 Merge an affine min/max op to its consumers if its consumer is also an affine min/max op. More...
 
struct  CanonicalizeAffineMinMaxOpExprAndTermOrder< T >
 Canonicalize the affine map result expression order of an affine min/max operation. More...
 
struct  CanonicalizeSingleResultAffineMinMaxOp< T >
 

Macros

#define DEBUG_TYPE   "affine-analysis"
 
#define GET_OP_LIST
 
#define GET_OP_CLASSES
 

Enumerations

enum  MinMaxKind
 

Functions

static bool remainsLegalAfterInline (Value value, Region *src, Region *dest, const BlockAndValueMapping &mapping, function_ref< bool(Value, Region *)> legalityCheck)
 Checks if value known to be a legal affine dimension or symbol in src region remains legal if the operation that uses it is inlined into dest with the given value mapping. More...
 
static bool remainsLegalAfterInline (ValueRange values, Region *src, Region *dest, const BlockAndValueMapping &mapping, function_ref< bool(Value, Region *)> legalityCheck)
 Checks if all values known to be legal affine dimensions or symbols in src remain so if their respective users are inlined into dest. More...
 
template<typename OpTy >
static bool remainsLegalAfterInline (OpTy op, Region *src, Region *dest, const BlockAndValueMapping &mapping)
 Checks if an affine read or write operation remains legal after inlining from src to dest. More...
 
template<>
bool LLVM_ATTRIBUTE_UNUSED remainsLegalAfterInline (AffineApplyOp op, Region *src, Region *dest, const BlockAndValueMapping &mapping)
 Checks if an affine apply operation remains legal after inlining from src to dest. More...
 
template<typename AnyMemRefDefOp >
static bool isMemRefSizeValidSymbol (AnyMemRefDefOp memrefDefOp, unsigned index, Region *region)
 Returns true if the 'index' dimension of the memref defined by memrefDefOp is a statically shaped one or defined using a valid symbol for region. More...
 
template<typename OpTy >
static bool isDimOpValidSymbol (OpTy dimOp, Region *region)
 Returns true if the result of the dim op is a valid symbol for region. More...
 
static bool isValidAffineIndexOperand (Value value, Region *region)
 
static void printDimAndSymbolList (Operation::operand_iterator begin, Operation::operand_iterator end, unsigned numDims, OpAsmPrinter &printer)
 Prints dimension and symbol list. More...
 
template<typename OpTy >
static LogicalResult verifyDimAndSymbolIdentifiers (OpTy &op, Operation::operand_range operands, unsigned numDims)
 Utility function to verify that a set of operands are valid dimension and symbol identifiers. More...
 
static LogicalResult replaceDimOrSym (AffineMap *map, unsigned dimOrSymbolPosition, SmallVectorImpl< Value > &dims, SmallVectorImpl< Value > &syms)
 Replace all occurrences of AffineExpr at position pos in map by the defining AffineApplyOp expression and operands. More...
 
static void composeAffineMapAndOperands (AffineMap *map, SmallVectorImpl< Value > *operands)
 Iterate over operands and fold away all those produced by an AffineApplyOp iteratively. More...
 
static Value createFoldedComposedAffineApply (OpBuilder &b, Location loc, AffineMap map, ValueRange operandsRef)
 Fully compose map with operands and canonicalize the result. More...
 
template<class MapOrSet >
static void canonicalizePromotedSymbols (MapOrSet *mapOrSet, SmallVectorImpl< Value > *operands)
 
template<class MapOrSet >
static void canonicalizeMapOrSetAndOperands (MapOrSet *mapOrSet, SmallVectorImpl< Value > *operands)
 
static LogicalResult foldMemRefCast (Operation *op, Value ignore=nullptr)
 This is a common class used for patterns of the form "someop(memrefcast) -> someop". More...
 
static ParseResult parseBound (bool isLower, OperationState &result, OpAsmParser &p)
 Parse a for operation loop bounds. More...
 
static void printBound (AffineMapAttr boundMap, Operation::operand_range boundOperands, const char *prefix, OpAsmPrinter &p)
 
static LogicalResult foldLoopBounds (AffineForOp forOp)
 Fold the constant bounds of a loop. More...
 
static LogicalResult canonicalizeLoopBounds (AffineForOp forOp)
 Canonicalize the bounds of the given loop. More...
 
static bool hasTrivialZeroTripCount (AffineForOp op)
 Returns true if the affine.for has zero iterations in trivial cases. More...
 
template<typename BoundListTy , typename LoopCreatorTy >
static void buildAffineLoopNestImpl (OpBuilder &builder, Location loc, BoundListTy lbs, BoundListTy ubs, ArrayRef< int64_t > steps, function_ref< void(OpBuilder &, Location, ValueRange)> bodyBuilderFn, LoopCreatorTy &&loopCreatorFn)
 Builds an affine loop nest, using "loopCreatorFn" to create individual loop operations. More...
 
static AffineForOp buildAffineLoopFromConstants (OpBuilder &builder, Location loc, int64_t lb, int64_t ub, int64_t step, AffineForOp::BodyBuilderFn bodyBuilderFn)
 Creates an affine loop from the bounds known to be constants. More...
 
static AffineForOp buildAffineLoopFromValues (OpBuilder &builder, Location loc, Value lb, Value ub, int64_t step, AffineForOp::BodyBuilderFn bodyBuilderFn)
 Creates an affine loop from the bounds that may or may not be constants. More...
 
static LogicalResult verifyMemoryOpIndexing (Operation *op, AffineMapAttr mapAttr, Operation::operand_range mapOperands, MemRefType memrefType, unsigned numIndexOperands)
 Verify common indexing invariants of affine.load, affine.store, affine.vector_load and affine.vector_store. More...
 
template<typename T >
static LogicalResult verifyAffineMinMaxOp (T op)
 
template<typename T >
static void printAffineMinMaxOp (OpAsmPrinter &p, T op)
 
template<typename T >
static ParseResult parseAffineMinMaxOp (OpAsmParser &parser, OperationState &result)
 
template<typename T >
static OpFoldResult foldMinMaxOp (T op, ArrayRef< Attribute > operands)
 Fold an affine min or max operation with the given operands. More...
 
static LogicalResult canonicalizeMapExprAndTermOrder (AffineMap &map)
 Canonicalize the result expression order of an affine map and return success if the order changed. More...
 
static LogicalResult canonicalizeLoopBounds (AffineParallelOp op)
 Canonicalize the bounds of the given loop. More...
 
static void printMinMaxBound (OpAsmPrinter &p, AffineMapAttr mapAttr, DenseIntElementsAttr group, ValueRange operands, StringRef keyword)
 Prints a lower(upper) bound of an affine parallel loop with max(min) conditions in it. More...
 
static ParseResult deduplicateAndResolveOperands (OpAsmParser &parser, ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand >> operands, SmallVectorImpl< Value > &uniqueOperands, SmallVectorImpl< AffineExpr > &replacements, AffineExprKind kind)
 Given a list of lists of parsed operands, populates uniqueOperands with unique operands. More...
 
static ParseResult parseAffineMapWithMinMax (OpAsmParser &parser, OperationState &result, MinMaxKind kind)
 Parses an affine map that can contain a min/max for groups of its results, e.g., max(expr-1, expr-2), expr-3, max(expr-4, expr-5, expr-6). More...
 
static LogicalResult verifyVectorMemoryOp (Operation *op, MemRefType memrefType, VectorType vectorType)
 Verify common invariants of affine.vector_load and affine.vector_store. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "affine-analysis"

Definition at line 25 of file AffineOps.cpp.

◆ GET_OP_CLASSES

#define GET_OP_CLASSES

Definition at line 3827 of file AffineOps.cpp.

◆ GET_OP_LIST

#define GET_OP_LIST

Enumeration Type Documentation

◆ MinMaxKind

enum MinMaxKind
strong

Definition at line 3413 of file AffineOps.cpp.

Function Documentation

◆ buildAffineLoopFromConstants()

static AffineForOp buildAffineLoopFromConstants ( OpBuilder builder,
Location  loc,
int64_t  lb,
int64_t  ub,
int64_t  step,
AffineForOp::BodyBuilderFn  bodyBuilderFn 
)
static

Creates an affine loop from the bounds known to be constants.

Definition at line 2010 of file AffineOps.cpp.

References mlir::OpBuilder::create().

Referenced by buildAffineLoopFromValues(), and mlir::buildAffineLoopNest().

◆ buildAffineLoopFromValues()

static AffineForOp buildAffineLoopFromValues ( OpBuilder builder,
Location  loc,
Value  lb,
Value  ub,
int64_t  step,
AffineForOp::BodyBuilderFn  bodyBuilderFn 
)
static

Creates an affine loop from the bounds that may or may not be constants.

Definition at line 2019 of file AffineOps.cpp.

References buildAffineLoopFromConstants(), mlir::OpBuilder::create(), mlir::Value::getDefiningOp(), mlir::Builder::getDimIdentityMap(), and mlir::arith::ConstantIndexOp::value().

Referenced by mlir::buildAffineLoopNest().

◆ buildAffineLoopNestImpl()

template<typename BoundListTy , typename LoopCreatorTy >
static void buildAffineLoopNestImpl ( OpBuilder builder,
Location  loc,
BoundListTy  lbs,
BoundListTy  ubs,
ArrayRef< int64_t >  steps,
function_ref< void(OpBuilder &, Location, ValueRange)>  bodyBuilderFn,
LoopCreatorTy &&  loopCreatorFn 
)
static

Builds an affine loop nest, using "loopCreatorFn" to create individual loop operations.

Definition at line 1969 of file AffineOps.cpp.

References mlir::OpBuilder::create(), and mlir::OpBuilder::setInsertionPointToStart().

Referenced by mlir::buildAffineLoopNest().

◆ canonicalizeLoopBounds() [1/2]

static LogicalResult canonicalizeLoopBounds ( AffineForOp  forOp)
static

◆ canonicalizeLoopBounds() [2/2]

static LogicalResult canonicalizeLoopBounds ( AffineParallelOp  op)
static

◆ canonicalizeMapExprAndTermOrder()

static LogicalResult canonicalizeMapExprAndTermOrder ( AffineMap map)
static

Canonicalize the result expression order of an affine map and return success if the order changed.

The function flattens the map's affine expressions to coefficient arrays and sorts them in lexicographic order. A coefficient array contains a multiplier for every dimension/symbol and a constant term. The canonicalization fails if a result expression is not pure or if the flattening requires local variables that, unlike dimensions and symbols, have no global order.

Definition at line 2775 of file AffineOps.cpp.

References mlir::failure(), mlir::AffineMap::getNumDims(), mlir::AffineMap::getNumResults(), mlir::AffineMap::getNumSymbols(), mlir::AffineMap::getResults(), and mlir::AffineExprVisitor< SubClass, RetTy >::walkPostOrder().

Referenced by CanonicalizeAffineMinMaxOpExprAndTermOrder< T >::matchAndRewrite().

◆ canonicalizeMapOrSetAndOperands()

template<class MapOrSet >
static void canonicalizeMapOrSetAndOperands ( MapOrSet *  mapOrSet,
SmallVectorImpl< Value > *  operands 
)
static

◆ canonicalizePromotedSymbols()

template<class MapOrSet >
static void canonicalizePromotedSymbols ( MapOrSet *  mapOrSet,
SmallVectorImpl< Value > *  operands 
)
static

◆ composeAffineMapAndOperands()

static void composeAffineMapAndOperands ( AffineMap map,
SmallVectorImpl< Value > *  operands 
)
static

◆ createFoldedComposedAffineApply()

static Value createFoldedComposedAffineApply ( OpBuilder b,
Location  loc,
AffineMap  map,
ValueRange  operandsRef 
)
static

Fully compose map with operands and canonicalize the result.

Return the createOrFold'ed AffineApply op.

Definition at line 727 of file AffineOps.cpp.

References mlir::canonicalizeMapAndOperands(), mlir::OpBuilder::createOrFold(), and mlir::fullyComposeAffineMapAndOperands().

◆ deduplicateAndResolveOperands()

static ParseResult deduplicateAndResolveOperands ( OpAsmParser parser,
ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand >>  operands,
SmallVectorImpl< Value > &  uniqueOperands,
SmallVectorImpl< AffineExpr > &  replacements,
AffineExprKind  kind 
)
static

Given a list of lists of parsed operands, populates uniqueOperands with unique operands.

Also populates replacements with affine expressions of kindthat can be used to update affine maps previously accepting a operandsto acceptuniqueOperands` instead.

Definition at line 3385 of file AffineOps.cpp.

◆ foldLoopBounds()

static LogicalResult foldLoopBounds ( AffineForOp  forOp)
static

◆ foldMemRefCast()

static LogicalResult foldMemRefCast ( Operation op,
Value  ignore = nullptr 
)
static

This is a common class used for patterns of the form "someop(memrefcast) -> someop".

It folds the source of any memref.cast into the root operation directly.

Definition at line 981 of file AffineOps.cpp.

References mlir::Operation::getOpOperands(), and mlir::success().

Referenced by mlir::AffineDmaStartOp::fold(), mlir::AffineDmaWaitOp::fold(), CanonicalizeSingleResultAffineMinMaxOp< T >::matchAndRewrite(), and verifyMemoryOpIndexing().

◆ foldMinMaxOp()

template<typename T >
static OpFoldResult foldMinMaxOp ( op,
ArrayRef< Attribute operands 
)
static

Fold an affine min or max operation with the given operands.

The operand list may contain nulls, which are interpreted as the operand not being a constant.

Definition at line 2616 of file AffineOps.cpp.

References value.

Referenced by CanonicalizeSingleResultAffineMinMaxOp< T >::matchAndRewrite().

◆ hasTrivialZeroTripCount()

static bool hasTrivialZeroTripCount ( AffineForOp  op)
static

◆ isDimOpValidSymbol()

template<typename OpTy >
static bool isDimOpValidSymbol ( OpTy  dimOp,
Region region 
)
static

Returns true if the result of the dim op is a valid symbol for region.

Definition at line 326 of file AffineOps.cpp.

References isMemRefSizeValidSymbol(), and mlir::isTopLevelValue().

Referenced by mlir::isValidSymbol().

◆ isMemRefSizeValidSymbol()

template<typename AnyMemRefDefOp >
static bool isMemRefSizeValidSymbol ( AnyMemRefDefOp  memrefDefOp,
unsigned  index,
Region region 
)
static

Returns true if the 'index' dimension of the memref defined by memrefDefOp is a statically shaped one or defined using a valid symbol for region.

Definition at line 312 of file AffineOps.cpp.

References mlir::isValidSymbol().

Referenced by isDimOpValidSymbol().

◆ isValidAffineIndexOperand()

static bool isValidAffineIndexOperand ( Value  value,
Region region 
)
static

◆ parseAffineMapWithMinMax()

static ParseResult parseAffineMapWithMinMax ( OpAsmParser parser,
OperationState result,
MinMaxKind  kind 
)
static

Parses an affine map that can contain a min/max for groups of its results, e.g., max(expr-1, expr-2), expr-3, max(expr-4, expr-5, expr-6).

Populates result attributes with the map (flat list of expressions) and the grouping (list of integers that specify how many expressions to put into each min/max) attributes. Deduplicates repeated operands.

parallel-bound ::= ( parallel-group-list ) parallel-group-list ::= parallel-group (, parallel-group-list)? parallel-group ::= simple-group | min-max-group simple-group ::= expr-of-ssa-ids min-max-group ::= ( min | max ) ( expr-of-ssa-ids-list ) expr-of-ssa-ids-list ::= expr-of-ssa-ids (, expr-of-ssa-id-list)?

Examples: (%0, min(%1 + %2, %3), %4, min(%5 floordiv 32, %6)) (%0, max(%1 - 2 * %2))

Definition at line 3432 of file AffineOps.cpp.

◆ parseAffineMinMaxOp()

template<typename T >
static ParseResult parseAffineMinMaxOp ( OpAsmParser parser,
OperationState result 
)
static

◆ parseBound()

static ParseResult parseBound ( bool  isLower,
OperationState result,
OpAsmParser p 
)
static

◆ printAffineMinMaxOp()

template<typename T >
static void printAffineMinMaxOp ( OpAsmPrinter p,
op 
)
static

◆ printBound()

static void printBound ( AffineMapAttr  boundMap,
Operation::operand_range  boundOperands,
const char *  prefix,
OpAsmPrinter p 
)
static

◆ printDimAndSymbolList()

static void printDimAndSymbolList ( Operation::operand_iterator  begin,
Operation::operand_iterator  end,
unsigned  numDims,
OpAsmPrinter printer 
)
static

Prints dimension and symbol list.

Definition at line 437 of file AffineOps.cpp.

Referenced by printBound(), mlir::replaceForOpWithNewYields(), and verifyDimAndSymbolIdentifiers().

◆ printMinMaxBound()

static void printMinMaxBound ( OpAsmPrinter p,
AffineMapAttr  mapAttr,
DenseIntElementsAttr  group,
ValueRange  operands,
StringRef  keyword 
)
static

Prints a lower(upper) bound of an affine parallel loop with max(min) conditions in it.

mapAttr is a flat list of affine expressions and group identifies which of the those expressions form max/min groups. operands are the SSA values of dimensions and symbols and keyword is either "min" or "max".

Definition at line 3317 of file AffineOps.cpp.

References mlir::AffineMap::getNumDims(), mlir::AffineMap::getResult(), mlir::AffineMap::getSliceMap(), print(), mlir::OpAsmPrinter::printAffineExprOfSSAIds(), mlir::OpAsmPrinter::printAffineMapOfSSAIds(), mlir::OpAsmPrinter::printOptionalAttrDict(), and mlir::OpAsmPrinter::printRegion().

◆ remainsLegalAfterInline() [1/4]

static bool remainsLegalAfterInline ( Value  value,
Region src,
Region dest,
const BlockAndValueMapping mapping,
function_ref< bool(Value, Region *)>  legalityCheck 
)
static

Checks if value known to be a legal affine dimension or symbol in src region remains legal if the operation that uses it is inlined into dest with the given value mapping.

legalityCheck is either isValidDim or isValidSymbol, depending on the value being required to remain a valid dimension or symbol.

Definition at line 45 of file AffineOps.cpp.

References mlir::Value::getDefiningOp(), mlir::Value::isa(), mlir::isTopLevelValue(), mlir::BlockAndValueMapping::lookup(), mlir::m_Constant(), and mlir::matchPattern().

Referenced by remainsLegalAfterInline().

◆ remainsLegalAfterInline() [2/4]

static bool remainsLegalAfterInline ( ValueRange  values,
Region src,
Region dest,
const BlockAndValueMapping mapping,
function_ref< bool(Value, Region *)>  legalityCheck 
)
static

Checks if all values known to be legal affine dimensions or symbols in src remain so if their respective users are inlined into dest.

Definition at line 74 of file AffineOps.cpp.

References remainsLegalAfterInline().

◆ remainsLegalAfterInline() [3/4]

template<typename OpTy >
static bool remainsLegalAfterInline ( OpTy  op,
Region src,
Region dest,
const BlockAndValueMapping mapping 
)
static

Checks if an affine read or write operation remains legal after inlining from src to dest.

Definition at line 85 of file AffineOps.cpp.

References mlir::AffineMap::getNumDims(), mlir::AffineMap::getNumSymbols(), mlir::isValidDim(), mlir::isValidSymbol(), and remainsLegalAfterInline().

◆ remainsLegalAfterInline() [4/4]

template<>
bool LLVM_ATTRIBUTE_UNUSED remainsLegalAfterInline ( AffineApplyOp  op,
Region src,
Region dest,
const BlockAndValueMapping mapping 
)

◆ replaceDimOrSym()

static LogicalResult replaceDimOrSym ( AffineMap map,
unsigned  dimOrSymbolPosition,
SmallVectorImpl< Value > &  dims,
SmallVectorImpl< Value > &  syms 
)
static

Replace all occurrences of AffineExpr at position pos in map by the defining AffineApplyOp expression and operands.

When dimOrSymbolPosition < dims.size(), AffineDimExpr@[pos] is replaced. When dimOrSymbolPosition >= dims.size(), AffineSymbolExpr@[pos - dims.size()] is replaced. Mutate map,dims and syms in place as follows:

  1. dims and syms are only appended to.
  2. map dim and symbols are gradually shifted to higer positions.
  3. Old dim and sym entries are replaced by nullptr This avoids the need for any bookkeeping.

Definition at line 594 of file AffineOps.cpp.

References mlir::failure(), mlir::getAffineDimExpr(), mlir::getAffineSymbolExpr(), mlir::AffineMap::getContext(), mlir::Value::getDefiningOp(), mlir::AffineMap::getNumDims(), mlir::AffineMap::getNumResults(), mlir::AffineMap::getNumSymbols(), mlir::AffineMap::replace(), mlir::AffineMap::shiftDims(), and mlir::success().

Referenced by composeAffineMapAndOperands().

◆ verifyAffineMinMaxOp()

template<typename T >
static LogicalResult verifyAffineMinMaxOp ( op)
static

◆ verifyDimAndSymbolIdentifiers()

template<typename OpTy >
static LogicalResult verifyDimAndSymbolIdentifiers ( OpTy &  op,
Operation::operand_range  operands,
unsigned  numDims 
)
static

◆ verifyMemoryOpIndexing()

static LogicalResult verifyMemoryOpIndexing ( Operation op,
AffineMapAttr  mapAttr,
Operation::operand_range  mapOperands,
MemRefType  memrefType,
unsigned  numIndexOperands 
)
static

◆ verifyVectorMemoryOp()

static LogicalResult verifyVectorMemoryOp ( Operation op,
MemRefType  memrefType,
VectorType  vectorType 
)
static