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
10#include "mlir/CAPI/AffineMap.h"
13
14using namespace mlir;
15using namespace mlir::linalg;
16
17/// Apply the special region builder for the builtin named Linalg op.
18/// Assert that `op` is a builtin named Linalg op.
19void mlirLinalgFillBuiltinNamedOpRegion(MlirOperation mlirOp) {
20 Operation *op = unwrap(mlirOp);
21 auto linalgOp = cast<LinalgOp>(op);
22 auto *dialect = static_cast<LinalgDialect *>(linalgOp->getDialect());
23 LinalgDialect::RegionBuilderFunType fun =
24 dialect->getRegionBuilder(op->getName().getStringRef());
25
26 assert(fun && "Expected a builtin named Linalg op.");
27 assert(op->getNumRegions() == 1 && "Expected Linalg op with 1 region");
28 assert(op->getRegion(0).getBlocks().empty() &&
29 "Expected Linalg op with 0 blocks");
30
31 SmallVector<Type, 8> argTypes;
33 for (OpOperand &opOperand : linalgOp->getOpOperands()) {
34 argTypes.push_back(getElementTypeOrSelf(opOperand.get().getType()));
35 argLocs.push_back(opOperand.get().getLoc());
36 }
37
39 Region &region = op->getRegion(0);
40 Block *body = b.createBlock(&region, /*insertPt=*/{}, argTypes, argLocs);
41 b.setInsertionPointToStart(body);
42 fun(b, *body, op->getAttrs(), /*emitError=*/{});
43}
44
46 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
47 // isaContractionOpInterface handles null linalgOp internally.
48 return linalg::isaContractionOpInterface(linalgOp);
49}
50
54 auto linalgOp = dyn_cast<linalg::LinalgOp>(unwrap(op));
55 if (!linalgOp)
56 return result;
57
58 FailureOr<linalg::ContractionDimensions> maybeDims =
60 if (failed(maybeDims))
61 return result;
62
63 const linalg::ContractionDimensions &contractionDims = *maybeDims;
64 MLIRContext *ctx = linalgOp.getContext();
65
66 auto toAttr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
67 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(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
79mlirLinalgInferContractionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
80 size_t numMaps) {
82 if (!indexingMaps || numMaps == 0)
83 return result;
84
86 maps.reserve(numMaps);
87 for (size_t i = 0; i < numMaps; ++i) {
88 maps.push_back(unwrap(indexingMaps[i]));
89 }
90
91 FailureOr<linalg::ContractionDimensions> maybeDims =
93 if (failed(maybeDims))
94 return result;
95
96 MLIRContext *ctx = maps[0].getContext();
97
98 auto toAttr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
99 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
100 };
101
102 result.batch = toAttr(maybeDims->batch);
103 result.m = toAttr(maybeDims->m);
104 result.n = toAttr(maybeDims->n);
105 result.k = toAttr(maybeDims->k);
106
107 return result;
108}
109
111 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
112 if (!linalgOp)
113 return false;
114
115 return linalg::isaConvolutionOpInterface(linalgOp);
116}
117
121 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
122 if (!linalgOp)
123 return result;
124
125 FailureOr<linalg::ConvolutionDimensions> maybeDims =
127 if (failed(maybeDims))
128 return result;
129
130 const linalg::ConvolutionDimensions &dims = *maybeDims;
131 MLIRContext *ctx = linalgOp.getContext();
132
133 auto toI32Attr =
134 [&ctx](const SmallVector<unsigned, 2> &vals) -> MlirAttribute {
135 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
136 };
137
138 auto toI64Attr =
139 [&ctx](const SmallVector<int64_t, 2> &vals) -> MlirAttribute {
140 return wrap(DenseI64ArrayAttr::get(ctx, vals));
141 };
142
143 result.batch = toI32Attr(dims.batch);
144 result.outputImage = toI32Attr(dims.outputImage);
145 result.outputChannel = toI32Attr(dims.outputChannel);
146 result.filterLoop = toI32Attr(dims.filterLoop);
147 result.inputChannel = toI32Attr(dims.inputChannel);
148 result.depth = toI32Attr(dims.depth);
149 result.strides = toI64Attr(dims.strides);
150 result.dilations = toI64Attr(dims.dilations);
151
152 return result;
153}
154
155MLIR_CAPI_EXPORTED MlirAttribute
157 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
158 if (!linalgOp)
159 return MlirAttribute{nullptr};
160
161 ArrayAttr attr = linalgOp.getIndexingMaps();
162 return wrap(attr);
163}
164
165MLIR_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:119
void mlirLinalgFillBuiltinNamedOpRegion(MlirOperation mlirOp)
Apply the special region builder for the builtin named Linalg op.
Definition Linalg.cpp:19
MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions mlirLinalgInferContractionDimensionsFromMaps(const MlirAffineMap *indexingMaps, size_t numMaps)
Definition Linalg.cpp:79
MLIR_CAPI_EXPORTED bool mlirLinalgIsAContractionOp(MlirOperation op)
Definition Linalg.cpp:45
MLIR_CAPI_EXPORTED MlirAttribute mlirLinalgGetIndexingMapsAttribute(MlirOperation op)
Definition Linalg.cpp:156
MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op)
Definition Linalg.cpp:110
MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions mlirLinalgInferContractionDimensions(MlirOperation op)
Definition Linalg.cpp:52
#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