MLIR  16.0.0git
Classes | Namespaces | Macros | Functions
MemRefOps.cpp File Reference
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/MemRef/Utils/MemRefUtils.h"
#include "mlir/Dialect/Utils/StaticValueUtils.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Interfaces/ViewLikeInterface.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
+ Include dependency graph for MemRefOps.cpp:

Go to the source code of this file.

Classes

struct  AllocaScopeInliner
 Inline an AllocaScopeOp if either the direct parent is an allocation scope or it contains no allocation. More...
 
struct  AllocaScopeHoister
 Move allocations into an allocation scope, if it is legal to move them (e.g. More...
 
struct  CollapseShapeOpMemRefCastFolder
 
struct  SubViewReturnTypeCanonicalizer
 Return the canonical type of the result of a subview. More...
 
struct  SubViewCanonicalizer
 A canonicalizer wrapper to replace SubViewOps. More...
 

Namespaces

 saturated_arith
 Idiomatic saturated operations on offsets, sizes and strides.
 

Macros

#define GET_OP_CLASSES
 

Functions

static void constifyIndexValues (SmallVectorImpl< OpFoldResult > &values, MemRefType memRefTy, MLIRContext *ctxt, llvm::function_ref< SmallVector< int64_t >(MemRefType)> getAttributes, llvm::function_ref< bool(int64_t)> isDynamic)
 Helper function that infers the constant values from a list of values, a memRefTy, and another helper function getAttributes. More...
 
static SmallVector< int64_t > getConstantSizes (MemRefType memRefTy)
 Wrapper around getShape that conforms to the function signature expected for getAttributes in constifyIndexValues. More...
 
static SmallVector< int64_t > getConstantOffset (MemRefType memrefType)
 Wrapper around getStridesAndOffset that returns only the offset and conforms to the function signature expected for getAttributes in constifyIndexValues. More...
 
static SmallVector< int64_t > getConstantStrides (MemRefType memrefType)
 Wrapper around getStridesAndOffset that returns only the strides and conforms to the function signature expected for getAttributes in constifyIndexValues. More...
 
template<typename AllocLikeOp >
static LogicalResult verifyAllocLikeOp (AllocLikeOp op)
 
static bool isGuaranteedAutomaticAllocation (Operation *op)
 Given an operation, return whether this op is guaranteed to allocate an AutomaticAllocationScopeResource. More...
 
static bool isOpItselfPotentialAutomaticAllocation (Operation *op)
 Given an operation, return whether this op itself could allocate an AutomaticAllocationScopeResource. More...
 
static bool lastNonTerminatorInRegion (Operation *op)
 Return whether this op is the last non terminating op in a region. More...
 
static std::map< int64_t, unsigned > getNumOccurences (ArrayRef< int64_t > vals)
 Return a map with key being elements in vals and data being number of occurences of it. More...
 
static llvm::Optional< llvm::SmallBitVector > computeMemRefRankReductionMask (MemRefType originalType, MemRefType reducedType, ArrayRef< OpFoldResult > sizes)
 Given the originalType and a candidateReducedType whose shape is assumed to be a subset of originalType with some 1 entries erased, return the set of indices that specifies which of the entries of originalShape are dropped to obtain reducedShape. More...
 
template<typename Container >
static bool replaceConstantUsesOf (OpBuilder &rewriter, Location loc, Container values, ArrayRef< OpFoldResult > maybeConstants)
 Helper function to perform the replacement of all constant uses of values by a materialized constant extracted from maybeConstants. More...
 
static void printGlobalMemrefOpTypeAndInitialValue (OpAsmPrinter &p, GlobalOp op, TypeAttr type, Attribute initialValue)
 
static ParseResult parseGlobalMemrefOpTypeAndInitialValue (OpAsmParser &parser, TypeAttr &typeAttr, Attribute &initialValue)
 
static LogicalResult verifyCollapsedShape (Operation *op, ArrayRef< int64_t > collapsedShape, ArrayRef< int64_t > expandedShape, ArrayRef< ReassociationIndices > reassociation, bool allowMultipleDynamicDimsPerGroup)
 Helper function for verifying the shape of ExpandShapeOp and ResultShapeOp result and operand. More...
 
static FailureOr< StridedLayoutAttr > computeExpandedLayoutMap (MemRefType srcType, ArrayRef< int64_t > resultShape, ArrayRef< ReassociationIndices > reassociation)
 Compute the layout map after expanding a given source MemRef type with the specified reassociation indices. More...
 
static FailureOr< StridedLayoutAttr > computeCollapsedLayoutMap (MemRefType srcType, ArrayRef< ReassociationIndices > reassociation, bool strict=false)
 Compute the layout map after collapsing a given source MemRef type with the specified reassociation indices. More...
 
static bool haveCompatibleOffsets (MemRefType t1, MemRefType t2)
 Return true if t1 and t2 have equal offsets (both dynamic or of same static value). More...
 
static SliceVerificationResult isRankReducedMemRefType (MemRefType originalType, MemRefType candidateRankReducedType, ArrayRef< OpFoldResult > sizes)
 Checks if original Type type can be rank reduced to reduced type. More...
 
template<typename OpTy >
static LogicalResult produceSubViewErrorMsg (SliceVerificationResult result, OpTy op, Type expectedType)
 
static MemRefType getCanonicalSubViewResultType (MemRefType currentResultType, MemRefType currentSourceType, MemRefType sourceType, ArrayRef< OpFoldResult > mixedOffsets, ArrayRef< OpFoldResult > mixedSizes, ArrayRef< OpFoldResult > mixedStrides)
 Compute the canonical result type of a SubViewOp. More...
 
static MemRefType getCanonicalSubViewResultType (MemRefType currentResultType, MemRefType sourceType, ArrayRef< OpFoldResult > mixedOffsets, ArrayRef< OpFoldResult > mixedSizes, ArrayRef< OpFoldResult > mixedStrides)
 Compute the canonical result type of a SubViewOp. More...
 
static bool isTrivialSubViewOp (SubViewOp subViewOp)
 Helper method to check if a subview operation is trivially a no-op. More...
 
static MemRefType inferTransposeResultType (MemRefType memRefType, AffineMap permutationMap)
 Build a strided memref type by applying permutationMap tp memRefType. More...
 

Macro Definition Documentation

◆ GET_OP_CLASSES

#define GET_OP_CLASSES

Definition at line 3364 of file MemRefOps.cpp.

Function Documentation

◆ computeCollapsedLayoutMap()

static FailureOr<StridedLayoutAttr> computeCollapsedLayoutMap ( MemRefType  srcType,
ArrayRef< ReassociationIndices reassociation,
bool  strict = false 
)
static

Compute the layout map after collapsing a given source MemRef type with the specified reassociation indices.

Note: All collapsed dims in a reassociation group must be contiguous. It is not possible to check this by inspecting a MemRefType in the general case. If non-contiguity cannot be checked statically, the collapse is assumed to be valid (and thus accepted by this function) unless strict = true.

Definition at line 2263 of file MemRefOps.cpp.

References mlir::failed(), mlir::failure(), and mlir::getStridesAndOffset().

◆ computeExpandedLayoutMap()

static FailureOr<StridedLayoutAttr> computeExpandedLayoutMap ( MemRefType  srcType,
ArrayRef< int64_t >  resultShape,
ArrayRef< ReassociationIndices reassociation 
)
static

Compute the layout map after expanding a given source MemRef type with the specified reassociation indices.

Definition at line 2147 of file MemRefOps.cpp.

References mlir::failed(), mlir::failure(), and mlir::getStridesAndOffset().

◆ computeMemRefRankReductionMask()

static llvm::Optional<llvm::SmallBitVector> computeMemRefRankReductionMask ( MemRefType  originalType,
MemRefType  reducedType,
ArrayRef< OpFoldResult sizes 
)
static

Given the originalType and a candidateReducedType whose shape is assumed to be a subset of originalType with some 1 entries erased, return the set of indices that specifies which of the entries of originalShape are dropped to obtain reducedShape.

This accounts for cases where there are multiple unit-dims, but only a subset of those are dropped. For MemRefTypes these can be disambiguated using the strides. If a dimension is dropped the stride must be dropped too.

Definition at line 966 of file MemRefOps.cpp.

References mlir::detail::enumerate(), mlir::failed(), getNumOccurences(), and mlir::getStridesAndOffset().

Referenced by getCanonicalSubViewResultType(), and isRankReducedMemRefType().

◆ constifyIndexValues()

static void constifyIndexValues ( SmallVectorImpl< OpFoldResult > &  values,
MemRefType  memRefTy,
MLIRContext ctxt,
llvm::function_ref< SmallVector< int64_t >(MemRefType)>  getAttributes,
llvm::function_ref< bool(int64_t)>  isDynamic 
)
static

Helper function that infers the constant values from a list of values, a memRefTy, and another helper function getAttributes.

The inferred constant values replace the related OpFoldResult in values.

Note
This function shouldn't be used directly, instead, use the getConstifiedMixedXXX methods from the related operations.

getAttributes retuns a list of potentially constant values, as determined by isDynamic, from the given memRefTy. The returned list must have as many elements as values or be empty.

E.g., consider the following example:

memref.reinterpret_cast %base to <...> strides: [2, %dyn_stride] :
memref<f32> to memref<?x?xf32, strided<[?, 1], offset: ?>>

ReinterpretCastOp::getMixedStrides() will return [2, dyn_stride]. Now using this helper function with:

  • values == [2, dyn_stride],
  • memRefTy == memref<?x?xf32, strided<[?, 1], offset: ?>>
  • getAttributes == getConstantStrides (i.e., a wrapper around getStridesAndOffset), and
  • isDynamic == ShapedType::isDynamic Will yield: values == [2, 1]

Definition at line 141 of file MemRefOps.cpp.

References mlir::Attribute::cast(), mlir::detail::enumerate(), mlir::getConstantIntValue(), and mlir::Builder::getIndexAttr().

◆ getCanonicalSubViewResultType() [1/2]

static MemRefType getCanonicalSubViewResultType ( MemRefType  currentResultType,
MemRefType  currentSourceType,
MemRefType  sourceType,
ArrayRef< OpFoldResult mixedOffsets,
ArrayRef< OpFoldResult mixedSizes,
ArrayRef< OpFoldResult mixedStrides 
)
static

Compute the canonical result type of a SubViewOp.

Call inferResultType to deduce the result type for the given sourceType. Additionally, reduce the rank of the inferred result type if currentResultType is lower rank than currentSourceType. Use this signature if sourceType is updated together with the result type. In this case, it is important to compute the dropped dimensions using currentSourceType whose strides align with currentResultType.

Definition at line 2884 of file MemRefOps.cpp.

References computeMemRefRankReductionMask().

Referenced by getCanonicalSubViewResultType(), and SubViewReturnTypeCanonicalizer::operator()().

◆ getCanonicalSubViewResultType() [2/2]

static MemRefType getCanonicalSubViewResultType ( MemRefType  currentResultType,
MemRefType  sourceType,
ArrayRef< OpFoldResult mixedOffsets,
ArrayRef< OpFoldResult mixedSizes,
ArrayRef< OpFoldResult mixedStrides 
)
static

Compute the canonical result type of a SubViewOp.

Call inferResultType to deduce the result type. Additionally, reduce the rank of the inferred result type if currentResultType is lower rank than sourceType.

Definition at line 2922 of file MemRefOps.cpp.

References getCanonicalSubViewResultType().

◆ getConstantOffset()

static SmallVector<int64_t> getConstantOffset ( MemRefType  memrefType)
static

Wrapper around getStridesAndOffset that returns only the offset and conforms to the function signature expected for getAttributes in constifyIndexValues.

Definition at line 188 of file MemRefOps.cpp.

References mlir::failed(), and mlir::getStridesAndOffset().

◆ getConstantSizes()

static SmallVector<int64_t> getConstantSizes ( MemRefType  memRefTy)
static

Wrapper around getShape that conforms to the function signature expected for getAttributes in constifyIndexValues.

Definition at line 180 of file MemRefOps.cpp.

◆ getConstantStrides()

static SmallVector<int64_t> getConstantStrides ( MemRefType  memrefType)
static

Wrapper around getStridesAndOffset that returns only the strides and conforms to the function signature expected for getAttributes in constifyIndexValues.

Definition at line 201 of file MemRefOps.cpp.

References mlir::failed(), and mlir::getStridesAndOffset().

◆ getNumOccurences()

static std::map<int64_t, unsigned> getNumOccurences ( ArrayRef< int64_t >  vals)
static

Return a map with key being elements in vals and data being number of occurences of it.

Use std::map, since the vals here are strides and the dynamic stride value is the same as the tombstone value for DenseMap<int64_t>.

Definition at line 951 of file MemRefOps.cpp.

Referenced by computeMemRefRankReductionMask().

◆ haveCompatibleOffsets()

static bool haveCompatibleOffsets ( MemRefType  t1,
MemRefType  t2 
)
static

Return true if t1 and t2 have equal offsets (both dynamic or of same static value).

Definition at line 2755 of file MemRefOps.cpp.

References mlir::getStridesAndOffset(), and mlir::succeeded().

Referenced by isRankReducedMemRefType().

◆ inferTransposeResultType()

static MemRefType inferTransposeResultType ( MemRefType  memRefType,
AffineMap  permutationMap 
)
static

Build a strided memref type by applying permutationMap tp memRefType.

Definition at line 3097 of file MemRefOps.cpp.

◆ isGuaranteedAutomaticAllocation()

static bool isGuaranteedAutomaticAllocation ( Operation op)
static

Given an operation, return whether this op is guaranteed to allocate an AutomaticAllocationScopeResource.

Definition at line 458 of file MemRefOps.cpp.

Referenced by AllocaScopeHoister::matchAndRewrite().

◆ isOpItselfPotentialAutomaticAllocation()

static bool isOpItselfPotentialAutomaticAllocation ( Operation op)
static

Given an operation, return whether this op itself could allocate an AutomaticAllocationScopeResource.

Note that this will not check whether an operation contained within the op can allocate.

Definition at line 477 of file MemRefOps.cpp.

References mlir::Operation::hasTrait().

Referenced by AllocaScopeInliner::matchAndRewrite().

◆ isRankReducedMemRefType()

static SliceVerificationResult isRankReducedMemRefType ( MemRefType  originalType,
MemRefType  candidateRankReducedType,
ArrayRef< OpFoldResult sizes 
)
static

Checks if original Type type can be rank reduced to reduced type.

This function is slight variant of is subsequence algorithm where not matching dimension must be 1.

Definition at line 2767 of file MemRefOps.cpp.

References computeMemRefRankReductionMask(), haveCompatibleOffsets(), mlir::isRankReducedType(), mlir::LayoutMismatch, mlir::MemSpaceMismatch, and mlir::Success.

◆ isTrivialSubViewOp()

static bool isTrivialSubViewOp ( SubViewOp  subViewOp)
static

Helper method to check if a subview operation is trivially a no-op.

This is the case if the all offsets are zero, all strides are 1, and the source shape is same as the size of the subview. In such cases, the subview can be folded into its source.

Definition at line 2935 of file MemRefOps.cpp.

References mlir::detail::enumerate(), and mlir::getConstantIntValue().

◆ lastNonTerminatorInRegion()

static bool lastNonTerminatorInRegion ( Operation op)
static

Return whether this op is the last non terminating op in a region.

That is to say, it is in a one-block region and is only followed by a terminator. This prevents extending the lifetime of allocations.

Definition at line 500 of file MemRefOps.cpp.

References mlir::Operation::getBlock(), mlir::Region::getBlocks(), mlir::Operation::getParentRegion(), and mlir::Block::getTerminator().

Referenced by AllocaScopeInliner::matchAndRewrite(), and AllocaScopeHoister::matchAndRewrite().

◆ parseGlobalMemrefOpTypeAndInitialValue()

static ParseResult parseGlobalMemrefOpTypeAndInitialValue ( OpAsmParser parser,
TypeAttr &  typeAttr,
Attribute initialValue 
)
static

◆ printGlobalMemrefOpTypeAndInitialValue()

static void printGlobalMemrefOpTypeAndInitialValue ( OpAsmPrinter p,
GlobalOp  op,
TypeAttr  type,
Attribute  initialValue 
)
static

Definition at line 1544 of file MemRefOps.cpp.

References mlir::AsmPrinter::printAttributeWithoutType().

◆ produceSubViewErrorMsg()

template<typename OpTy >
static LogicalResult produceSubViewErrorMsg ( SliceVerificationResult  result,
OpTy  op,
Type  expectedType 
)
static

◆ replaceConstantUsesOf()

template<typename Container >
static bool replaceConstantUsesOf ( OpBuilder rewriter,
Location  loc,
Container  values,
ArrayRef< OpFoldResult maybeConstants 
)
static

Helper function to perform the replacement of all constant uses of values by a materialized constant extracted from maybeConstants.

values and maybeConstants are expected to have the same size.

Definition at line 1391 of file MemRefOps.cpp.

References mlir::OpBuilder::create(), mlir::getAsOpFoldResult(), and mlir::Operation::replaceUsesOfWith().

◆ verifyAllocLikeOp()

template<typename AllocLikeOp >
static LogicalResult verifyAllocLikeOp ( AllocLikeOp  op)
static

Definition at line 226 of file MemRefOps.cpp.

References mlir::success(), and value.

◆ verifyCollapsedShape()

static LogicalResult verifyCollapsedShape ( Operation op,
ArrayRef< int64_t >  collapsedShape,
ArrayRef< int64_t >  expandedShape,
ArrayRef< ReassociationIndices reassociation,
bool  allowMultipleDynamicDimsPerGroup 
)
static

Helper function for verifying the shape of ExpandShapeOp and ResultShapeOp result and operand.

Layout maps are verified separately.

If allowMultipleDynamicDimsPerGroup, multiple dynamic dimensions are allowed in a reassocation group.

Definition at line 2054 of file MemRefOps.cpp.

References mlir::Operation::emitOpError(), mlir::detail::enumerate(), and mlir::success().