MLIR 23.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 toAttr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
55 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
56 };
57 return {toAttr(dims.batch), toAttr(dims.m), toAttr(dims.n), toAttr(dims.k)};
58}
59
62 auto linalgOp = dyn_cast<linalg::LinalgOp>(unwrap(op));
63 if (!linalgOp)
64 return {};
65
66 FailureOr<linalg::ContractionDimensions> maybeDims =
68 if (failed(maybeDims))
69 return {};
70
71 const linalg::ContractionDimensions &contractionDims = *maybeDims;
72 MLIRContext *ctx = linalgOp.getContext();
73 return toContractionDimensions(ctx, contractionDims);
74}
75
77mlirLinalgInferContractionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
78 size_t numMaps) {
79 if (!indexingMaps || numMaps != 3)
80 return {};
81
83 for (size_t i = 0; i < numMaps; ++i) {
84 maps.push_back(unwrap(indexingMaps[i]));
85 }
86
87 FailureOr<linalg::ContractionDimensions> maybeDims =
89 if (failed(maybeDims))
90 return {};
91
92 MLIRContext *ctx = maps[0].getContext();
93
94 return toContractionDimensions(ctx, *maybeDims);
95}
96
98 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
99 if (!linalgOp)
100 return false;
101
102 return linalg::isaConvolutionOpInterface(linalgOp);
103}
104
107 const linalg::ConvolutionDimensions &dims) {
108 auto toI32Attr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
109 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
110 };
111 auto toI64Attr = [ctx](ArrayRef<int64_t> vals) -> MlirAttribute {
112 return wrap(DenseI64ArrayAttr::get(ctx, vals));
113 };
114 return {toI32Attr(dims.batch), toI32Attr(dims.outputImage),
115 toI32Attr(dims.outputChannel), toI32Attr(dims.filterLoop),
116 toI32Attr(dims.inputChannel), toI32Attr(dims.depth),
117 toI64Attr(dims.strides), toI64Attr(dims.dilations)};
118}
119
122 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
123 if (!linalgOp)
124 return {};
125
126 FailureOr<linalg::ConvolutionDimensions> maybeDims =
128 if (failed(maybeDims))
129 return {};
130
131 return toConvolutionDimensions(linalgOp.getContext(), *maybeDims);
132}
133
135mlirLinalgInferConvolutionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
136 size_t numMaps) {
137 // inferConvolutionDims requires exactly 3 maps (input, filter, output);
138 // keep this check in sync with its contract
139 if (!indexingMaps || numMaps != 3)
140 return {};
141
143 for (size_t i = 0; i < numMaps; ++i)
144 maps.push_back(unwrap(indexingMaps[i]));
145
146 FailureOr<linalg::ConvolutionDimensions> maybeDims =
148 if (failed(maybeDims))
149 return {};
150
151 return toConvolutionDimensions(maps[0].getContext(), *maybeDims);
152}
153
154MLIR_CAPI_EXPORTED MlirAttribute
156 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
157 if (!linalgOp)
158 return MlirAttribute{nullptr};
159
160 ArrayAttr attr = linalgOp.getIndexingMaps();
161 return wrap(attr);
162}
163
164MLIR_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()
static MlirLinalgConvolutionDimensions toConvolutionDimensions(MLIRContext *ctx, const linalg::ConvolutionDimensions &dims)
Definition Linalg.cpp:106
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions mlirLinalgInferConvolutionDimensions(MlirOperation op)
Definition Linalg.cpp:121
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:77
static MlirLinalgContractionDimensions toContractionDimensions(MLIRContext *ctx, const linalg::ContractionDimensions &dims)
Definition Linalg.cpp:52
MLIR_CAPI_EXPORTED bool mlirLinalgIsAContractionOp(MlirOperation op)
Definition Linalg.cpp:45
MLIR_CAPI_EXPORTED MlirAttribute mlirLinalgGetIndexingMapsAttribute(MlirOperation op)
Definition Linalg.cpp:155
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions mlirLinalgInferConvolutionDimensionsFromMaps(const MlirAffineMap *indexingMaps, size_t numMaps)
Definition Linalg.cpp:135
MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op)
Definition Linalg.cpp:97
MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions mlirLinalgInferContractionDimensions(MlirOperation op)
Definition Linalg.cpp:61
b getContext())
#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:632
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:254
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:711
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition Operation.h:537
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:699
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
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