1 //===- FuncToSPIRVPass.cpp - Func 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 // This file implements a pass to convert Func dialect to SPIR-V dialect. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRVPass.h" 14 #include "../PassDetail.h" 15 #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h" 16 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" 17 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" 18 19 using namespace mlir; 20 21 namespace { 22 /// A pass converting MLIR Func operations into the SPIR-V dialect. 23 class ConvertFuncToSPIRVPass 24 : public ConvertFuncToSPIRVBase<ConvertFuncToSPIRVPass> { 25 void runOnOperation() override; 26 }; 27 } // namespace 28 runOnOperation()29void ConvertFuncToSPIRVPass::runOnOperation() { 30 MLIRContext *context = &getContext(); 31 ModuleOp module = getOperation(); 32 33 auto targetAttr = spirv::lookupTargetEnvOrDefault(module); 34 std::unique_ptr<ConversionTarget> target = 35 SPIRVConversionTarget::get(targetAttr); 36 37 SPIRVTypeConverter::Options options; 38 options.emulateNon32BitScalarTypes = this->emulateNon32BitScalarTypes; 39 SPIRVTypeConverter typeConverter(targetAttr, options); 40 41 RewritePatternSet patterns(context); 42 populateFuncToSPIRVPatterns(typeConverter, patterns); 43 populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns); 44 45 if (failed(applyPartialConversion(module, *target, std::move(patterns)))) 46 return signalPassFailure(); 47 } 48 createConvertFuncToSPIRVPass()49std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertFuncToSPIRVPass() { 50 return std::make_unique<ConvertFuncToSPIRVPass>(); 51 } 52