1 //===- ConvertSPIRVToLLVMPass.cpp - Convert SPIR-V ops to LLVM ops --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a pass to convert MLIR SPIR-V ops into LLVM ops
11 //===----------------------------------------------------------------------===//
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/IR/SPIRVDialect.h"
24 /// A pass converting MLIR SPIR-V operations into LLVM dialect.
25 class ConvertSPIRVToLLVMPass
26 : public ConvertSPIRVToLLVMBase
<ConvertSPIRVToLLVMPass
> {
27 void runOnOperation() override
;
31 void ConvertSPIRVToLLVMPass::runOnOperation() {
32 MLIRContext
*context
= &getContext();
33 ModuleOp module
= getOperation();
34 LLVMTypeConverter
converter(&getContext());
36 // Encode global variable's descriptor set and binding if they exist.
37 encodeBindAttribute(module
);
39 OwningRewritePatternList patterns
;
41 populateSPIRVToLLVMTypeConversion(converter
);
43 populateSPIRVToLLVMModuleConversionPatterns(context
, converter
, patterns
);
44 populateSPIRVToLLVMConversionPatterns(context
, converter
, patterns
);
45 populateSPIRVToLLVMFunctionConversionPatterns(context
, converter
, patterns
);
47 ConversionTarget
target(getContext());
48 target
.addIllegalDialect
<spirv::SPIRVDialect
>();
49 target
.addLegalDialect
<LLVM::LLVMDialect
>();
51 // Set `ModuleOp` and `ModuleTerminatorOp` as legal for `spv.module`
53 target
.addLegalOp
<ModuleOp
>();
54 target
.addLegalOp
<ModuleTerminatorOp
>();
55 if (failed(applyPartialConversion(module
, target
, std::move(patterns
))))
59 std::unique_ptr
<OperationPass
<ModuleOp
>> mlir::createConvertSPIRVToLLVMPass() {
60 return std::make_unique
<ConvertSPIRVToLLVMPass
>();