1 //===- LinalgToSPIRVPass.cpp - Linalg to SPIR-V Passes --------------------===// 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/Conversion/LinalgToSPIRV/LinalgToSPIRVPass.h" 10 #include "../PassDetail.h" 11 #include "mlir/Conversion/LinalgToSPIRV/LinalgToSPIRV.h" 12 #include "mlir/Dialect/Func/IR/FuncOps.h" 13 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" 14 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" 15 16 using namespace mlir; 17 18 namespace { 19 /// A pass converting MLIR Linalg ops into SPIR-V ops. 20 class LinalgToSPIRVPass : public ConvertLinalgToSPIRVBase<LinalgToSPIRVPass> { 21 void runOnOperation() override; 22 }; 23 } // namespace 24 runOnOperation()25void LinalgToSPIRVPass::runOnOperation() { 26 MLIRContext *context = &getContext(); 27 ModuleOp module = getOperation(); 28 29 auto targetAttr = spirv::lookupTargetEnvOrDefault(module); 30 std::unique_ptr<ConversionTarget> target = 31 SPIRVConversionTarget::get(targetAttr); 32 33 SPIRVTypeConverter typeConverter(targetAttr); 34 RewritePatternSet patterns(context); 35 populateLinalgToSPIRVPatterns(typeConverter, patterns); 36 populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns); 37 38 // Allow builtin ops. 39 target->addLegalOp<ModuleOp>(); 40 target->addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) { 41 return typeConverter.isSignatureLegal(op.getFunctionType()) && 42 typeConverter.isLegal(&op.getBody()); 43 }); 44 45 if (failed(applyFullConversion(module, *target, std::move(patterns)))) 46 return signalPassFailure(); 47 } 48 createLinalgToSPIRVPass()49std::unique_ptr<OperationPass<ModuleOp>> mlir::createLinalgToSPIRVPass() { 50 return std::make_unique<LinalgToSPIRVPass>(); 51 } 52