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/Registration.h"
11 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
12 
13 using namespace mlir;
14 using 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.
18 void mlirLinalgFillBuiltinNamedOpRegion(MlirDialect linalgDialect,
19                                         MlirOperation mlirOp, intptr_t n,
20                                         MlirValue const *mlirCaptures) {
21   Operation *op = unwrap(mlirOp);
22   SmallVector<Value> captures;
23   captures.reserve(n);
24   for (unsigned idx = 0; idx < n; ++idx)
25     captures.push_back(unwrap(mlirCaptures[idx]));
26 
27   LinalgDialect::RegionBuilderFunType fun =
28       static_cast<LinalgDialect *>(unwrap(linalgDialect))
29           ->getRegionBuilder(op->getName().getStringRef());
30   assert(fun && "Expected a builtin named Linalg op.");
31   assert(op->getNumRegions() == 1 && "Expected Linalg op with 1 region");
32   assert(op->getRegion(0).getBlocks().empty() &&
33          "Expected Linalg op with 0 blocks");
34 
35   SmallVector<Type, 8> argTypes;
36   auto linalgOp = cast<LinalgOp>(op);
37   for (OpOperand *opOperand : linalgOp.getInputAndOutputOperands())
38     argTypes.push_back(getElementTypeOrSelf(opOperand->get().getType()));
39 
40   ImplicitLocOpBuilder b(op->getLoc(), op->getContext());
41   Region &region = op->getRegion(0);
42   Block *body = b.createBlock(&region, /*insertPt=*/{}, argTypes);
43   b.setInsertionPointToStart(body);
44   fun(b, *body, captures);
45 }
46 
47 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect)
48