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