MLIR 24.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 NamedAttrList attrs;
44 op, [&](StringRef name, Attribute &attr) { attrs.append(name, attr); });
45 fun(b, *body, attrs, /*emitError=*/{});
46}
47
49 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
50 // isaContractionOpInterface handles null linalgOp internally.
51 return linalg::isaContractionOpInterface(linalgOp);
52}
53
57 auto toAttr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
58 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
59 };
60 return {toAttr(dims.batch), toAttr(dims.m), toAttr(dims.n), toAttr(dims.k)};
61}
62
65 auto linalgOp = dyn_cast<linalg::LinalgOp>(unwrap(op));
66 if (!linalgOp)
67 return {};
68
69 FailureOr<linalg::ContractionDimensions> maybeDims =
71 if (failed(maybeDims))
72 return {};
73
74 const linalg::ContractionDimensions &contractionDims = *maybeDims;
75 MLIRContext *ctx = linalgOp.getContext();
76 return toContractionDimensions(ctx, contractionDims);
77}
78
80mlirLinalgInferContractionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
81 size_t numMaps) {
82 if (!indexingMaps || numMaps != 3)
83 return {};
84
86 for (size_t i = 0; i < numMaps; ++i) {
87 maps.push_back(unwrap(indexingMaps[i]));
88 }
89
90 FailureOr<linalg::ContractionDimensions> maybeDims =
92 if (failed(maybeDims))
93 return {};
94
95 MLIRContext *ctx = maps[0].getContext();
96
97 return toContractionDimensions(ctx, *maybeDims);
98}
99
101 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
102 if (!linalgOp)
103 return false;
104
105 return linalg::isaConvolutionOpInterface(linalgOp);
106}
107
110 const linalg::ConvolutionDimensions &dims) {
111 auto toI32Attr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute {
112 return wrap(DenseI32ArrayAttr::get(ctx, llvm::to_vector_of<int32_t>(vals)));
113 };
114 auto toI64Attr = [ctx](ArrayRef<int64_t> vals) -> MlirAttribute {
115 return wrap(DenseI64ArrayAttr::get(ctx, vals));
116 };
117 return {toI32Attr(dims.batch), toI32Attr(dims.outputImage),
118 toI32Attr(dims.outputChannel), toI32Attr(dims.filterLoop),
119 toI32Attr(dims.inputChannel), toI32Attr(dims.depth),
120 toI64Attr(dims.strides), toI64Attr(dims.dilations)};
121}
122
125 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
126 if (!linalgOp)
127 return {};
128
129 FailureOr<linalg::ConvolutionDimensions> maybeDims =
131 if (failed(maybeDims))
132 return {};
133
134 return toConvolutionDimensions(linalgOp.getContext(), *maybeDims);
135}
136
138mlirLinalgInferConvolutionDimensionsFromMaps(const MlirAffineMap *indexingMaps,
139 size_t numMaps) {
140 // inferConvolutionDims requires exactly 3 maps (input, filter, output);
141 // keep this check in sync with its contract
142 if (!indexingMaps || numMaps != 3)
143 return {};
144
146 for (size_t i = 0; i < numMaps; ++i)
147 maps.push_back(unwrap(indexingMaps[i]));
148
149 FailureOr<linalg::ConvolutionDimensions> maybeDims =
151 if (failed(maybeDims))
152 return {};
153
154 return toConvolutionDimensions(maps[0].getContext(), *maybeDims);
155}
156
157MLIR_CAPI_EXPORTED MlirAttribute
159 auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(unwrap(op));
160 if (!linalgOp)
161 return MlirAttribute{nullptr};
162
163 ArrayAttr attr = linalgOp.getIndexingMaps();
164 return wrap(attr);
165}
166
167MLIR_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:109
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions mlirLinalgInferConvolutionDimensions(MlirOperation op)
Definition Linalg.cpp:124
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:80
static MlirLinalgContractionDimensions toContractionDimensions(MLIRContext *ctx, const linalg::ContractionDimensions &dims)
Definition Linalg.cpp:55
MLIR_CAPI_EXPORTED bool mlirLinalgIsAContractionOp(MlirOperation op)
Definition Linalg.cpp:48
MLIR_CAPI_EXPORTED MlirAttribute mlirLinalgGetIndexingMapsAttribute(MlirOperation op)
Definition Linalg.cpp:158
MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions mlirLinalgInferConvolutionDimensionsFromMaps(const MlirAffineMap *indexingMaps, size_t numMaps)
Definition Linalg.cpp:138
MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op)
Definition Linalg.cpp:100
MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions mlirLinalgInferContractionDimensions(MlirOperation op)
Definition Linalg.cpp:64
b getContext())
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
Attributes are known-constant values of operations.
Definition Attributes.h:25
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
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
This class represents an operand of an operation.
Definition Value.h:254
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
void walkInherentAttrs(Operation *op, InherentAttrVisitor visitor) const
Visit the inherent attributes stored in the properties of op.
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:738
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:726
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