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