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) { 20 Operation *op = unwrap(mlirOp); 21 LinalgDialect::RegionBuilderFunType fun = 22 static_cast<LinalgDialect *>(unwrap(linalgDialect)) 23 ->getRegionBuilder(op->getName().getStringRef()); 24 assert(fun && "Expected a builtin named Linalg op."); 25 assert(op->getNumRegions() == 1 && "Expected Linalg op with 1 region"); 26 assert(op->getRegion(0).getBlocks().empty() && 27 "Expected Linalg op with 0 blocks"); 28 SmallVector<Type, 8> argTypes; 29 auto linalgOp = cast<LinalgOp>(op); 30 for (auto t : linalgOp.getShapedOperandTypes()) 31 argTypes.push_back(getElementTypeOrSelf(t)); 32 OpBuilder b(op->getContext()); 33 Region ®ion = op->getRegion(0); 34 Block *body = b.createBlock(®ion, /*insertPt=*/{}, argTypes); 35 // TODO: allow captures. 36 fun(*body, ValueRange{}); 37 } 38 39 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect) 40