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 SmallVector<Location, 8> argLocs; 32 for (OpOperand *opOperand : linalgOp.getInputAndOutputOperands()) { 33 argTypes.push_back(getElementTypeOrSelf(opOperand->get().getType())); 34 argLocs.push_back(opOperand->get().getLoc()); 35 } 36 37 ImplicitLocOpBuilder b(op->getLoc(), op->getContext()); 38 Region ®ion = op->getRegion(0); 39 Block *body = b.createBlock(®ion, /*insertPt=*/{}, argTypes, argLocs); 40 b.setInsertionPointToStart(body); 41 fun(b, *body); 42 } 43 44 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect) 45