MLIR 22.0.0git
Linalg.cpp
Go to the documentation of this file.
1//===- Linalg.cpp - C Interface for Linalg dialect ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12
13using namespace mlir;
14using namespace mlir::linalg;
15
16/// Apply the special region builder for the builtin named Linalg op.
17/// Assert that `op` is a builtin named Linalg op.
18void mlirLinalgFillBuiltinNamedOpRegion(MlirOperation mlirOp) {
19 Operation *op = unwrap(mlirOp);
20 auto linalgOp = cast<LinalgOp>(op);
21 auto *dialect = static_cast<LinalgDialect *>(linalgOp->getDialect());
22 LinalgDialect::RegionBuilderFunType fun =
23 dialect->getRegionBuilder(op->getName().getStringRef());
24
25 assert(fun && "Expected a builtin named Linalg op.");
26 assert(op->getNumRegions() == 1 && "Expected Linalg op with 1 region");
27 assert(op->getRegion(0).getBlocks().empty() &&
28 "Expected Linalg op with 0 blocks");
29
30 SmallVector<Type, 8> argTypes;
32 for (OpOperand &opOperand : linalgOp->getOpOperands()) {
33 argTypes.push_back(getElementTypeOrSelf(opOperand.get().getType()));
34 argLocs.push_back(opOperand.get().getLoc());
35 }
36
38 Region &region = op->getRegion(0);
39 Block *body = b.createBlock(&region, /*insertPt=*/{}, argTypes, argLocs);
40 b.setInsertionPointToStart(body);
41 fun(b, *body, op->getAttrs(), /*emitError=*/{});
42}
43
45 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
46 // isaContractionOpInterface handles null linalgOp internally.
47 return linalg::isaContractionOpInterface(linalgOp);
48}
49
53 auto linalgOp = dyn_cast<linalg::LinalgOp>(unwrap(op));
54 if (!linalgOp)
55 return result;
56
57 FailureOr<linalg::ContractionDimensions> maybeDims =
59 if (failed(maybeDims))
60 return result;
61
62 const linalg::ContractionDimensions &contractionDims = *maybeDims;
63 MLIRContext *ctx = linalgOp.getContext();
64
65 auto toAttr = [&ctx](const SmallVector<unsigned, 2> &vals) -> MlirAttribute {
66 return wrap(
67 DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t, 2>(vals)));
68 };
69
70 result.batch = toAttr(contractionDims.batch);
71 result.m = toAttr(contractionDims.m);
72 result.n = toAttr(contractionDims.n);
73 result.k = toAttr(contractionDims.k);
74
75 return result;
76}
77
79 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
80 if (!linalgOp)
81 return false;
82
83 return linalg::isaConvolutionOpInterface(linalgOp);
84}
85
89 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
90 if (!linalgOp)
91 return result;
92
93 FailureOr<linalg::ConvolutionDimensions> maybeDims =
95 if (failed(maybeDims))
96 return result;
97
98 const linalg::ConvolutionDimensions &dims = *maybeDims;
99 MLIRContext *ctx = linalgOp.getContext();
100
101 auto toI32Attr =
102 [&ctx](const SmallVector<unsigned, 2> &vals) -> MlirAttribute {
103 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
104 };
105
106 auto toI64Attr =
107 [&ctx](const SmallVector<int64_t, 2> &vals) -> MlirAttribute {
108 return wrap(DenseI64ArrayAttr::get(ctx, vals));
109 };
110
111 result.batch = toI32Attr(dims.batch);
112 result.outputImage = toI32Attr(dims.outputImage);
113 result.outputChannel = toI32Attr(dims.outputChannel);
114 result.filterLoop = toI32Attr(dims.filterLoop);
115 result.inputChannel = toI32Attr(dims.inputChannel);
116 result.depth = toI32Attr(dims.depth);
117 result.strides = toI64Attr(dims.strides);
118 result.dilations = toI64Attr(dims.dilations);
119
120 return result;
121}
122
123MLIR_CAPI_EXPORTED MlirAttribute
125 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
126 if (!linalgOp)
127 return MlirAttribute{nullptr};
128
129 ArrayAttr attr = linalgOp.getIndexingMaps();
130 return wrap(attr);
131}
132
133MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect)
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
ArrayAttr()
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions mlirLinalgInferConvolutionDimensions(MlirOperation op)
Definition Linalg.cpp:87
void mlirLinalgFillBuiltinNamedOpRegion(MlirOperation mlirOp)
Apply the special region builder for the builtin named Linalg op.
Definition Linalg.cpp:18
MLIR_CAPI_EXPORTED bool mlirLinalgIsAContractionOp(MlirOperation op)
Definition Linalg.cpp:44
MLIR_CAPI_EXPORTED MlirAttribute mlirLinalgGetIndexingMapsAttribute(MlirOperation op)
Definition Linalg.cpp:124
MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op)
Definition Linalg.cpp:78
MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions mlirLinalgInferContractionDimensions(MlirOperation op)
Definition Linalg.cpp:51
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
Block represents an ordered list of Operations.
Definition Block.h:33
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
Definition Builders.h:630
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class represents an operand of an operation.
Definition Value.h:257
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:686
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition Operation.h:512
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:674
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:119
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:216
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
BlockListType & getBlocks()
Definition Region.h:45
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< int32_t > content)
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
FailureOr< ConvolutionDimensions > inferConvolutionDims(LinalgOp linalgOp)
Find at least 1 parallel (output_image) and reduction (filter_loop) dimension candidates that form a ...
bool isaConvolutionOpInterface(LinalgOp linalgOp, bool allowEmptyConvolvedDims=false)
Checks whether linalgOp conforms to ConvolutionOpInterface.
FailureOr< ContractionDimensions > inferContractionDims(LinalgOp linalgOp)
Find at least 2 parallel (m and n) and 1 reduction (k) dimension candidates that form a matmul subcom...
bool isaContractionOpInterface(LinalgOp linalgOp)
Checks whether linalgOp conforms to ContractionOpInterface.
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
Positions of a Linalg op loops that correspond to different kinds of a contraction dimension.
SmallVector< unsigned, 2 > batch
Positions of a Linalg op loops that correspond to different kinds of a convolution dimension.
SmallVector< unsigned, 2 > depth
SmallVector< unsigned, 2 > outputImage
SmallVector< unsigned, 2 > outputChannel
SmallVector< int64_t, 2 > dilations
SmallVector< int64_t, 2 > strides
SmallVector< unsigned, 2 > inputChannel
SmallVector< unsigned, 2 > batch
SmallVector< unsigned, 2 > filterLoop