MLIR  20.0.0git
Classes | Namespaces | Macros | Typedefs | Functions
LinalgOps.cpp File Reference
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/AsmParser/AsmParser.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"
#include "mlir/Dialect/Utils/StaticValueUtils.h"
#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/LogicalResult.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <optional>
#include "mlir/Dialect/Linalg/IR/LinalgNamedStructuredOps.yamlgen.cpp.inc"
#include "mlir/Dialect/Linalg/IR/LinalgOps.cpp.inc"
#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"

Go to the source code of this file.

Classes

struct  FoldTransposeWithTranspose
 Fold transpose with transpose. More...
 
struct  SwapTransposeWithBroadcast
 This pattern canonicalize transpose by swapping the order of broadcast and transpose: transpose(broadcast(input)) -> broadcast(transpose(input)) More...
 

Namespaces

 mlir
 Include the generated interface declarations.
 
 mlir::linalg
 

Macros

#define GET_OP_CLASSES
 
#define GET_OP_CLASSES
 

Typedefs

using RegionBuilderFn = llvm::function_ref< void(ImplicitLocOpBuilder &, Block &, ArrayRef< NamedAttribute >)>
 

Functions

static OpFoldResult getDimValue (OpBuilder &builder, Location loc, Value v, int64_t dim)
 Return a memref.dim or tensor.dim for the shape of v at dim. More...
 
static OperationgetSlice (OpBuilder &b, Location loc, Value source, ArrayRef< OpFoldResult > offsets, ArrayRef< OpFoldResult > sizes, ArrayRef< OpFoldResult > strides)
 Returns a memref.subview or a tensor.extract_slice based on the type of the source. More...
 
static void fillStructuredOpRegion (OpBuilder &opBuilder, Region &region, TypeRange inputTypes, TypeRange outputTypes, ArrayRef< NamedAttribute > attrs, RegionBuilderFn regionBuilder)
 Fills the region of a structured operation using the provided regionBuilder. More...
 
static SmallVector< AffineMap, 3 > getDefaultIndexingMapsForMatmul (MLIRContext *context)
 Helper to create a typical indexing map for MatmulOp. More...
 
static SmallVector< AttributegetDefaultMatmulIndexingMapAttr (MLIRContext *context)
 Wrapper to return the typical indexing map array attribute for MatmulOp. More...
 
static void buildStructuredOp (OpBuilder &b, OperationState &state, std::optional< TypeRange > resultTensorTypes, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes, RegionBuilderFn regionBuilder)
 Creates a structured operation given inputs, outputs, and attributes. More...
 
static void buildMatmulOp (OpBuilder &b, OperationState &state, std::optional< TypeRange > resultTensorTypes, ValueRange inputs, ValueRange outputs, ArrayRef< NamedAttribute > attributes, RegionBuilderFn regionBuilder, std::optional< ArrayRef< AffineMap >> indexingMaps=std::nullopt)
 
static ParseResult parseCommonStructuredOpParts (OpAsmParser &parser, OperationState &result, SmallVectorImpl< Type > &inputTypes, SmallVectorImpl< Type > &outputTypes, bool addOperandSegmentSizes=true)
 Common parsing used for both named structured ops created by ods-gen and by manually defined C++ ops. More...
 
static void printCommonStructuredOpParts (OpAsmPrinter &p, ValueRange inputs, ValueRange outputs)
 
static ParseResult parseNamedStructuredOpRegion (OpAsmParser &parser, Region &region, unsigned numRegionArgs, TypeRange inputTypes, TypeRange outputTypes, ArrayRef< NamedAttribute > attrs, RegionBuilderFn regionBuilder)
 
static ParseResult parseNamedStructuredOpResults (OpAsmParser &parser, SmallVectorImpl< Type > &resultTypes)
 
static ParseResult parseNamedStructuredOp (OpAsmParser &parser, OperationState &result, unsigned numRegionArgs, RegionBuilderFn regionBuilder)
 
static void printNamedStructuredOpResults (OpAsmPrinter &p, TypeRange resultTypes)
 
static void printNamedStructuredOp (OpAsmPrinter &p, Operation *op, ValueRange inputs, ValueRange outputs, ArrayRef< StringRef > elidedAttrs={})
 
static void buildGenericRegion (OpBuilder &builder, Location loc, Region &region, ValueRange inputs, ValueRange outputs, function_ref< void(OpBuilder &, Location, ValueRange)> bodyBuild)
 
static void getGenericEffectsImpl (SmallVectorImpl< SideEffects::EffectInstance< MemoryEffects::Effect >> &effects, LinalgOp linalgOp)
 
static Speculation::Speculatability getGenericSpeculatabilityImpl (LinalgOp linalgOp)
 
static ParseResult parseDstStyleOp (OpAsmParser &parser, OperationState &result, function_ref< ParseResult(OpAsmParser &, NamedAttrList &)> parseAttrsFn=nullptr)
 
static void addBodyWithPayloadOp (OpAsmParser &parser, OperationState &result, const OperationName &payloadOpName, const NamedAttrList &payloadOpAttrs, ArrayRef< Value > operands, bool initFirst=false)
 
static OperationfindPayloadOp (Block *body, bool initFirst=false)
 
void printShortForm (OpAsmPrinter &p, Operation *payloadOp)
 
static ParseResult parseDenseI64ArrayAttr (OpAsmParser &parser, NamedAttrList &attributes, StringRef attributeName)
 
static void printDenseI64ArrayAttr (OpAsmPrinter &p, StringRef attributeName, ArrayRef< int64_t > attributeValue)
 
static void buildIdentityRegion (OpBuilder &builder, Location loc, Region &region, ValueRange inputs, ValueRange outputs)
 
static LogicalResult verifyYield (linalg::YieldOp op, LinalgOp linalgOp)
 
static LogicalResult appendMangledType (llvm::raw_string_ostream &ss, Type t)
 
static std::tuple< SmallVector< utils::IteratorType >, SmallVector< AffineMap > > computeIteratorTypesAndIndexingMaps (OpBuilder &builder, int64_t inputRank, int64_t dim, bool allParallel=false)
 
template<typename T >
static Value reduce (OpBuilder &builder, Location loc, Value input, Value output, int64_t dim)
 
static Value buildSubAndExpOp (OpBuilder &builder, Location loc, Value input, Value max, Value output, int64_t dim)
 Produce a linalg generic that computes the second step of the softmax decomposition: res = exp(input - max), where max is the max of input on dimension dim. More...
 
static Value buildDivOp (OpBuilder &builder, Location loc, Value numerator, Value denominator, Value output, int64_t dim)
 Produce a linalg generic that computes the final step of the softmax decomposition. More...
 
static bool isValidResultDimExprs (AffineMap explictMap, AffineMap defaultMap)
 Returns true if the result AffineExpr of the explicitMap is same as defaultMap. More...
 
static bool isBroadcasted (AffineMap explictMap, AffineMap defaultMap)
 Returns true if the explictMap is broadcasted with respect to the defaultMap. More...
 
static LogicalResult verifyExtendedMatmulSemantic (MatmulOp matmulOp, unsigned opIndex)
 Verifies the broadcast and transpose semantic sepecified by the explicit indexing map for the MatmulOp op for each operand specified by opIndex. More...
 

Macro Definition Documentation

◆ GET_OP_CLASSES [1/2]

#define GET_OP_CLASSES

Definition at line 2303 of file LinalgOps.cpp.

◆ GET_OP_CLASSES [2/2]

#define GET_OP_CLASSES

Definition at line 2303 of file LinalgOps.cpp.

Typedef Documentation

◆ RegionBuilderFn

Definition at line 117 of file LinalgOps.cpp.

Function Documentation

◆ addBodyWithPayloadOp()

static void addBodyWithPayloadOp ( OpAsmParser parser,
OperationState result,
const OperationName payloadOpName,
const NamedAttrList payloadOpAttrs,
ArrayRef< Value operands,
bool  initFirst = false 
)
static

◆ appendMangledType()

static LogicalResult appendMangledType ( llvm::raw_string_ostream &  ss,
Type  t 
)
static

◆ buildDivOp()

static Value buildDivOp ( OpBuilder builder,
Location  loc,
Value  numerator,
Value  denominator,
Value  output,
int64_t  dim 
)
static

Produce a linalg generic that computes the final step of the softmax decomposition.

Returns
linalg.generic ins(numerator, denominator) outs(output) { yield n / d }

Definition at line 2866 of file LinalgOps.cpp.

References computeIteratorTypesAndIndexingMaps(), mlir::OpBuilder::create(), mlir::Operation::getResult(), and mlir::Value::getType().

◆ buildGenericRegion()

static void buildGenericRegion ( OpBuilder builder,
Location  loc,
Region region,
ValueRange  inputs,
ValueRange  outputs,
function_ref< void(OpBuilder &, Location, ValueRange)>  bodyBuild 
)
static

◆ buildIdentityRegion()

static void buildIdentityRegion ( OpBuilder builder,
Location  loc,
Region region,
ValueRange  inputs,
ValueRange  outputs 
)
static

Definition at line 1864 of file LinalgOps.cpp.

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

◆ buildMatmulOp()

static void buildMatmulOp ( OpBuilder b,
OperationState state,
std::optional< TypeRange resultTensorTypes,
ValueRange  inputs,
ValueRange  outputs,
ArrayRef< NamedAttribute attributes,
RegionBuilderFn  regionBuilder,
std::optional< ArrayRef< AffineMap >>  indexingMaps = std::nullopt 
)
static

Definition at line 212 of file LinalgOps.cpp.

◆ buildStructuredOp()

static void buildStructuredOp ( OpBuilder b,
OperationState state,
std::optional< TypeRange resultTensorTypes,
ValueRange  inputs,
ValueRange  outputs,
ArrayRef< NamedAttribute attributes,
RegionBuilderFn  regionBuilder 
)
static

Creates a structured operation given inputs, outputs, and attributes.

The result types are derived automatically if resultTensorTypes is none. The body of the operation is filled using regionBuilder. All ods-gen created structured operations use the method to implement their builders.

Definition at line 183 of file LinalgOps.cpp.

◆ buildSubAndExpOp()

static Value buildSubAndExpOp ( OpBuilder builder,
Location  loc,
Value  input,
Value  max,
Value  output,
int64_t  dim 
)
static

Produce a linalg generic that computes the second step of the softmax decomposition: res = exp(input - max), where max is the max of input on dimension dim.

Definition at line 2840 of file LinalgOps.cpp.

References computeIteratorTypesAndIndexingMaps(), mlir::OpBuilder::create(), mlir::Operation::getResult(), mlir::Value::getType(), and max().

◆ computeIteratorTypesAndIndexingMaps()

static std::tuple<SmallVector<utils::IteratorType>, SmallVector<AffineMap> > computeIteratorTypesAndIndexingMaps ( OpBuilder builder,
int64_t  inputRank,
int64_t  dim,
bool  allParallel = false 
)
static

◆ fillStructuredOpRegion()

static void fillStructuredOpRegion ( OpBuilder opBuilder,
Region region,
TypeRange  inputTypes,
TypeRange  outputTypes,
ArrayRef< NamedAttribute attrs,
RegionBuilderFn  regionBuilder 
)
static

Fills the region of a structured operation using the provided regionBuilder.

The method is used by both named structured ops created by ods-gen and by manually defined C++ ops. It is called by both builders and parsers and creates a block with arguments corresponding to the elemental types of inputTypes and outputTypes. All output types are asserted to be ShapedType.

Definition at line 126 of file LinalgOps.cpp.

References mlir::OpBuilder::createBlock(), mlir::getElementTypeOrSelf(), mlir::Builder::getUnknownLoc(), and mlir::OpBuilder::setInsertionPointToStart().

Referenced by parseNamedStructuredOpRegion().

◆ findPayloadOp()

static Operation* findPayloadOp ( Block body,
bool  initFirst = false 
)
static

◆ getDefaultIndexingMapsForMatmul()

static SmallVector<AffineMap, 3> getDefaultIndexingMapsForMatmul ( MLIRContext context)
static

Helper to create a typical indexing map for MatmulOp.

Returns a list of AffineMap.

Definition at line 161 of file LinalgOps.cpp.

References mlir::bindDims(), and mlir::AffineMap::get().

Referenced by getDefaultMatmulIndexingMapAttr().

◆ getDefaultMatmulIndexingMapAttr()

static SmallVector<Attribute> getDefaultMatmulIndexingMapAttr ( MLIRContext context)
static

Wrapper to return the typical indexing map array attribute for MatmulOp.

Definition at line 173 of file LinalgOps.cpp.

References mlir::get(), and getDefaultIndexingMapsForMatmul().

◆ getDimValue()

static OpFoldResult getDimValue ( OpBuilder builder,
Location  loc,
Value  v,
int64_t  dim 
)
static

Return a memref.dim or tensor.dim for the shape of v at dim.

Definition at line 58 of file LinalgOps.cpp.

References mlir::getAsOpFoldResult(), mlir::Builder::getIndexAttr(), and mlir::Value::getType().

◆ getGenericEffectsImpl()

static void getGenericEffectsImpl ( SmallVectorImpl< SideEffects::EffectInstance< MemoryEffects::Effect >> &  effects,
LinalgOp  linalgOp 
)
static

◆ getGenericSpeculatabilityImpl()

static Speculation::Speculatability getGenericSpeculatabilityImpl ( LinalgOp  linalgOp)
static

◆ getSlice()

static Operation* getSlice ( OpBuilder b,
Location  loc,
Value  source,
ArrayRef< OpFoldResult offsets,
ArrayRef< OpFoldResult sizes,
ArrayRef< OpFoldResult strides 
)
static

Returns a memref.subview or a tensor.extract_slice based on the type of the source.

Definition at line 76 of file LinalgOps.cpp.

References mlir::OpBuilder::create(), and mlir::Value::getType().

◆ isBroadcasted()

static bool isBroadcasted ( AffineMap  explictMap,
AffineMap  defaultMap 
)
static

Returns true if the explictMap is broadcasted with respect to the defaultMap.

Definition at line 3449 of file LinalgOps.cpp.

References mlir::AffineMap::getNumResults().

Referenced by verifyExtendedMatmulSemantic().

◆ isValidResultDimExprs()

static bool isValidResultDimExprs ( AffineMap  explictMap,
AffineMap  defaultMap 
)
static

Returns true if the result AffineExpr of the explicitMap is same as defaultMap.

Definition at line 3438 of file LinalgOps.cpp.

References mlir::AffineMap::getResults().

Referenced by verifyExtendedMatmulSemantic().

◆ parseCommonStructuredOpParts()

static ParseResult parseCommonStructuredOpParts ( OpAsmParser parser,
OperationState result,
SmallVectorImpl< Type > &  inputTypes,
SmallVectorImpl< Type > &  outputTypes,
bool  addOperandSegmentSizes = true 
)
static

◆ parseDenseI64ArrayAttr()

static ParseResult parseDenseI64ArrayAttr ( OpAsmParser parser,
NamedAttrList attributes,
StringRef  attributeName 
)
static

◆ parseDstStyleOp()

static ParseResult parseDstStyleOp ( OpAsmParser parser,
OperationState result,
function_ref< ParseResult(OpAsmParser &, NamedAttrList &)>  parseAttrsFn = nullptr 
)
static

◆ parseNamedStructuredOp()

static ParseResult parseNamedStructuredOp ( OpAsmParser parser,
OperationState result,
unsigned  numRegionArgs,
RegionBuilderFn  regionBuilder 
)
static

◆ parseNamedStructuredOpRegion()

static ParseResult parseNamedStructuredOpRegion ( OpAsmParser parser,
Region region,
unsigned  numRegionArgs,
TypeRange  inputTypes,
TypeRange  outputTypes,
ArrayRef< NamedAttribute attrs,
RegionBuilderFn  regionBuilder 
)
static

◆ parseNamedStructuredOpResults()

static ParseResult parseNamedStructuredOpResults ( OpAsmParser parser,
SmallVectorImpl< Type > &  resultTypes 
)
static

Definition at line 341 of file LinalgOps.cpp.

References mlir::AsmParser::parseOptionalArrowTypeList().

Referenced by parseNamedStructuredOp().

◆ printCommonStructuredOpParts()

static void printCommonStructuredOpParts ( OpAsmPrinter p,
ValueRange  inputs,
ValueRange  outputs 
)
static

Definition at line 310 of file LinalgOps.cpp.

References mlir::ValueRange::getTypes().

◆ printDenseI64ArrayAttr()

static void printDenseI64ArrayAttr ( OpAsmPrinter p,
StringRef  attributeName,
ArrayRef< int64_t >  attributeValue 
)
static

Definition at line 1745 of file LinalgOps.cpp.

◆ printNamedStructuredOp()

static void printNamedStructuredOp ( OpAsmPrinter p,
Operation op,
ValueRange  inputs,
ValueRange  outputs,
ArrayRef< StringRef >  elidedAttrs = {} 
)
static

Definition at line 385 of file LinalgOps.cpp.

◆ printNamedStructuredOpResults()

static void printNamedStructuredOpResults ( OpAsmPrinter p,
TypeRange  resultTypes 
)
static

Definition at line 378 of file LinalgOps.cpp.

References mlir::AsmPrinter::printOptionalArrowTypeList().

◆ printShortForm()

void printShortForm ( OpAsmPrinter p,
Operation payloadOp 
)

◆ reduce()

template<typename T >
static Value reduce ( OpBuilder builder,
Location  loc,
Value  input,
Value  output,
int64_t  dim 
)
static

◆ verifyExtendedMatmulSemantic()

static LogicalResult verifyExtendedMatmulSemantic ( MatmulOp  matmulOp,
unsigned  opIndex 
)
static

Verifies the broadcast and transpose semantic sepecified by the explicit indexing map for the MatmulOp op for each operand specified by opIndex.

Definition at line 3456 of file LinalgOps.cpp.

References isBroadcasted(), and isValidResultDimExprs().

◆ verifyYield()

static LogicalResult verifyYield ( linalg::YieldOp  op,
LinalgOp  linalgOp 
)
static