1 //===- SPIRVToLLVMPass.cpp - SPIR-V to LLVM 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 MLIR SPIR-V ops into LLVM ops 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h" 14 #include "../PassDetail.h" 15 #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h" 16 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h" 17 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h" 18 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 19 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" 20 21 using namespace mlir; 22 23 namespace { 24 /// A pass converting MLIR SPIR-V operations into LLVM dialect. 25 class ConvertSPIRVToLLVMPass 26 : public ConvertSPIRVToLLVMBase<ConvertSPIRVToLLVMPass> { 27 void runOnOperation() override; 28 }; 29 } // namespace 30 31 void ConvertSPIRVToLLVMPass::runOnOperation() { 32 MLIRContext *context = &getContext(); 33 ModuleOp module = getOperation(); 34 LLVMTypeConverter converter(&getContext()); 35 36 // Encode global variable's descriptor set and binding if they exist. 37 encodeBindAttribute(module); 38 39 RewritePatternSet patterns(context); 40 41 populateSPIRVToLLVMTypeConversion(converter); 42 43 populateSPIRVToLLVMModuleConversionPatterns(converter, patterns); 44 populateSPIRVToLLVMConversionPatterns(converter, patterns); 45 populateSPIRVToLLVMFunctionConversionPatterns(converter, patterns); 46 47 ConversionTarget target(*context); 48 target.addIllegalDialect<spirv::SPIRVDialect>(); 49 target.addLegalDialect<LLVM::LLVMDialect>(); 50 51 // Set `ModuleOp` as legal for `spv.module` conversion. 52 target.addLegalOp<ModuleOp>(); 53 if (failed(applyPartialConversion(module, target, std::move(patterns)))) 54 signalPassFailure(); 55 } 56 57 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertSPIRVToLLVMPass() { 58 return std::make_unique<ConvertSPIRVToLLVMPass>(); 59 } 60