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/Linalg.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(MlirOperation mlirOp) {
19   Operation *op = unwrap(mlirOp);
20   auto linalgOp = cast<LinalgOp>(op);
21   auto *dialect = static_cast<LinalgDialect *>(linalgOp->getDialect());
22   LinalgDialect::RegionBuilderFunType fun =
23       dialect->getRegionBuilder(op->getName().getStringRef());
24 
25   assert(fun && "Expected a builtin named Linalg op.");
26   assert(op->getNumRegions() == 1 && "Expected Linalg op with 1 region");
27   assert(op->getRegion(0).getBlocks().empty() &&
28          "Expected Linalg op with 0 blocks");
29 
30   SmallVector<Type, 8> argTypes;
31   for (OpOperand *opOperand : linalgOp.getInputAndOutputOperands())
32     argTypes.push_back(getElementTypeOrSelf(opOperand->get().getType()));
33 
34   ImplicitLocOpBuilder b(op->getLoc(), op->getContext());
35   Region &region = op->getRegion(0);
36   Block *body = b.createBlock(&region, /*insertPt=*/{}, argTypes);
37   b.setInsertionPointToStart(body);
38   fun(b, *body);
39 }
40 
41 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect)
42