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 22 LinalgDialect::RegionBuilderFunType fun = 23 static_cast<LinalgDialect *>(unwrap(linalgDialect)) 24 ->getRegionBuilder(op->getName().getStringRef()); 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 auto linalgOp = cast<LinalgOp>(op); 32 for (OpOperand *opOperand : linalgOp.getInputAndOutputOperands()) 33 argTypes.push_back(getElementTypeOrSelf(opOperand->get().getType())); 34 35 ImplicitLocOpBuilder b(op->getLoc(), op->getContext()); 36 Region ®ion = op->getRegion(0); 37 Block *body = b.createBlock(®ion, /*insertPt=*/{}, argTypes); 38 b.setInsertionPointToStart(body); 39 fun(b, *body); 40 } 41 42 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Linalg, linalg, LinalgDialect) 43