| 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 | |
| 9 | #include "mlir-c/Dialect/Linalg.h" |
| 10 | #include "mlir/CAPI/AffineMap.h" |
| 11 | #include "mlir/CAPI/Registration.h" |
| 12 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 13 | |
| 14 | using namespace mlir; |
| 15 | using 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. |
| 19 | void mlirLinalgFillBuiltinNamedOpRegion(MlirOperation mlirOp) { |
| 20 | Operation *op = unwrap(c: mlirOp); |
| 21 | auto linalgOp = cast<LinalgOp>(Val: op); |
| 22 | auto *dialect = static_cast<LinalgDialect *>(linalgOp->getDialect()); |
| 23 | LinalgDialect::RegionBuilderFunType fun = |
| 24 | dialect->getRegionBuilder(name: 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; |
| 32 | SmallVector<Location, 8> argLocs; |
| 33 | for (OpOperand &opOperand : linalgOp->getOpOperands()) { |
| 34 | argTypes.push_back(Elt: getElementTypeOrSelf(type: opOperand.get().getType())); |
| 35 | argLocs.push_back(Elt: opOperand.get().getLoc()); |
| 36 | } |
| 37 | |
| 38 | ImplicitLocOpBuilder b(op->getLoc(), op->getContext()); |
| 39 | Region ®ion = op->getRegion(index: 0); |
| 40 | Block *body = b.createBlock(parent: ®ion, /*insertPt=*/{}, argTypes, locs: argLocs); |
| 41 | b.setInsertionPointToStart(body); |
| 42 | NamedAttrList attrs; |
| 43 | op->getName().walkInherentAttrs( |
| 44 | op, visitor: [&](StringRef name, Attribute &attr) { attrs.append(name, attr); }); |
| 45 | fun(b, *body, attrs, /*emitError=*/{}); |
| 46 | } |
| 47 | |
| 48 | MLIR_CAPI_EXPORTED bool mlirLinalgIsAContractionOp(MlirOperation op) { |
| 49 | auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(Val: unwrap(c: op)); |
| 50 | // isaContractionOpInterface handles null linalgOp internally. |
| 51 | return linalg::isaContractionOpInterface(linalgOp); |
| 52 | } |
| 53 | |
| 54 | static MlirLinalgContractionDimensions |
| 55 | toContractionDimensions(MLIRContext *ctx, |
| 56 | const linalg::ContractionDimensions &dims) { |
| 57 | auto toAttr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute { |
| 58 | return wrap(cpp: DenseI32ArrayAttr::get(context: ctx, content: llvm::to_vector_of<int32_t>(Range&: vals))); |
| 59 | }; |
| 60 | return {.batch: toAttr(dims.batch), .m: toAttr(dims.m), .n: toAttr(dims.n), .k: toAttr(dims.k)}; |
| 61 | } |
| 62 | |
| 63 | MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions |
| 64 | mlirLinalgInferContractionDimensions(MlirOperation op) { |
| 65 | auto linalgOp = dyn_cast<linalg::LinalgOp>(Val: unwrap(c: op)); |
| 66 | if (!linalgOp) |
| 67 | return {}; |
| 68 | |
| 69 | FailureOr<linalg::ContractionDimensions> maybeDims = |
| 70 | linalg::inferContractionDims(linalgOp); |
| 71 | if (failed(Result: maybeDims)) |
| 72 | return {}; |
| 73 | |
| 74 | const linalg::ContractionDimensions &contractionDims = *maybeDims; |
| 75 | MLIRContext *ctx = linalgOp.getContext(); |
| 76 | return toContractionDimensions(ctx, dims: contractionDims); |
| 77 | } |
| 78 | |
| 79 | MLIR_CAPI_EXPORTED MlirLinalgContractionDimensions |
| 80 | mlirLinalgInferContractionDimensionsFromMaps(const MlirAffineMap *indexingMaps, |
| 81 | size_t numMaps) { |
| 82 | if (!indexingMaps || numMaps != 3) |
| 83 | return {}; |
| 84 | |
| 85 | SmallVector<AffineMap, 3> maps; |
| 86 | for (size_t i = 0; i < numMaps; ++i) { |
| 87 | maps.push_back(Elt: unwrap(c: indexingMaps[i])); |
| 88 | } |
| 89 | |
| 90 | FailureOr<linalg::ContractionDimensions> maybeDims = |
| 91 | linalg::inferContractionDims(indexingMaps: maps); |
| 92 | if (failed(Result: maybeDims)) |
| 93 | return {}; |
| 94 | |
| 95 | MLIRContext *ctx = maps[0].getContext(); |
| 96 | |
| 97 | return toContractionDimensions(ctx, dims: *maybeDims); |
| 98 | } |
| 99 | |
| 100 | MLIR_CAPI_EXPORTED bool mlirLinalgIsAConvolutionOp(MlirOperation op) { |
| 101 | auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(Val: unwrap(c: op)); |
| 102 | if (!linalgOp) |
| 103 | return false; |
| 104 | |
| 105 | return linalg::isaConvolutionOpInterface(linalgOp); |
| 106 | } |
| 107 | |
| 108 | static MlirLinalgConvolutionDimensions |
| 109 | toConvolutionDimensions(MLIRContext *ctx, |
| 110 | const linalg::ConvolutionDimensions &dims) { |
| 111 | auto toI32Attr = [ctx](ArrayRef<unsigned> vals) -> MlirAttribute { |
| 112 | return wrap(cpp: DenseI32ArrayAttr::get(context: ctx, content: llvm::to_vector_of<int32_t>(Range&: vals))); |
| 113 | }; |
| 114 | auto toI64Attr = [ctx](ArrayRef<int64_t> vals) -> MlirAttribute { |
| 115 | return wrap(cpp: DenseI64ArrayAttr::get(context: ctx, content: vals)); |
| 116 | }; |
| 117 | return {.batch: toI32Attr(dims.batch), .outputImage: toI32Attr(dims.outputImage), |
| 118 | .outputChannel: toI32Attr(dims.outputChannel), .filterLoop: toI32Attr(dims.filterLoop), |
| 119 | .inputChannel: toI32Attr(dims.inputChannel), .depth: toI32Attr(dims.depth), |
| 120 | .strides: toI64Attr(dims.strides), .dilations: toI64Attr(dims.dilations)}; |
| 121 | } |
| 122 | |
| 123 | MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions |
| 124 | mlirLinalgInferConvolutionDimensions(MlirOperation op) { |
| 125 | auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(Val: unwrap(c: op)); |
| 126 | if (!linalgOp) |
| 127 | return {}; |
| 128 | |
| 129 | FailureOr<linalg::ConvolutionDimensions> maybeDims = |
| 130 | linalg::inferConvolutionDims(linalgOp); |
| 131 | if (failed(Result: maybeDims)) |
| 132 | return {}; |
| 133 | |
| 134 | return toConvolutionDimensions(ctx: linalgOp.getContext(), dims: *maybeDims); |
| 135 | } |
| 136 | |
| 137 | MLIR_CAPI_EXPORTED MlirLinalgConvolutionDimensions |
| 138 | mlirLinalgInferConvolutionDimensionsFromMaps(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 | |
| 145 | SmallVector<AffineMap, 3> maps; |
| 146 | for (size_t i = 0; i < numMaps; ++i) |
| 147 | maps.push_back(Elt: unwrap(c: indexingMaps[i])); |
| 148 | |
| 149 | FailureOr<linalg::ConvolutionDimensions> maybeDims = |
| 150 | linalg::inferConvolutionDims(indexingMaps: maps); |
| 151 | if (failed(Result: maybeDims)) |
| 152 | return {}; |
| 153 | |
| 154 | return toConvolutionDimensions(ctx: maps[0].getContext(), dims: *maybeDims); |
| 155 | } |
| 156 | |
| 157 | MLIR_CAPI_EXPORTED MlirAttribute |
| 158 | mlirLinalgGetIndexingMapsAttribute(MlirOperation op) { |
| 159 | auto linalgOp = llvm::dyn_cast<mlir::linalg::LinalgOp>(Val: unwrap(c: op)); |
| 160 | if (!linalgOp) |
| 161 | return MlirAttribute{.ptr: nullptr}; |
| 162 | |
| 163 | ArrayAttr attr = linalgOp.getIndexingMaps(); |
| 164 | return wrap(cpp: attr); |
| 165 | } |
| 166 | |
| 167 | MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect) |
| 168 | |