• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ConvertSPIRVToLLVMPass.cpp - Convert SPIR-V ops to LLVM ops --------===//
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/ConvertSPIRVToLLVMPass.h"
14 #include "../PassDetail.h"
15 #include "mlir/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.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/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 
runOnOperation()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   OwningRewritePatternList patterns;
40 
41   populateSPIRVToLLVMTypeConversion(converter);
42 
43   populateSPIRVToLLVMModuleConversionPatterns(context, converter, patterns);
44   populateSPIRVToLLVMConversionPatterns(context, converter, patterns);
45   populateSPIRVToLLVMFunctionConversionPatterns(context, converter, patterns);
46 
47   ConversionTarget target(getContext());
48   target.addIllegalDialect<spirv::SPIRVDialect>();
49   target.addLegalDialect<LLVM::LLVMDialect>();
50 
51   // Set `ModuleOp` and `ModuleTerminatorOp` as legal for `spv.module`
52   // conversion.
53   target.addLegalOp<ModuleOp>();
54   target.addLegalOp<ModuleTerminatorOp>();
55   if (failed(applyPartialConversion(module, target, std::move(patterns))))
56     signalPassFailure();
57 }
58 
createConvertSPIRVToLLVMPass()59 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertSPIRVToLLVMPass() {
60   return std::make_unique<ConvertSPIRVToLLVMPass>();
61 }
62