xref: /llvm-project/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp (revision 48f8d95f337a9c97c5d00eb15eb43fdaa9b23235)
1 //===- ConvertToLLVMIR.cpp - MLIR to LLVM IR conversion -------------------===//
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 translation between the MLIR LLVM dialect and LLVM IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/DLTI/DLTI.h"
14 #include "mlir/Dialect/Func/IR/FuncOps.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/Target/LLVMIR/Dialect/All.h"
17 #include "mlir/Target/LLVMIR/Export.h"
18 #include "mlir/Tools/mlir-translate/Translation.h"
19 #include "llvm/IR/DebugProgramInstruction.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 
23 extern llvm::cl::opt<bool> WriteNewDbgInfoFormat;
24 
25 using namespace mlir;
26 
27 namespace mlir {
registerToLLVMIRTranslation()28 void registerToLLVMIRTranslation() {
29   TranslateFromMLIRRegistration registration(
30       "mlir-to-llvmir", "Translate MLIR to LLVMIR",
31       [](Operation *op, raw_ostream &output) {
32         llvm::LLVMContext llvmContext;
33         auto llvmModule = translateModuleToLLVMIR(op, llvmContext);
34         if (!llvmModule)
35           return failure();
36 
37         // When printing LLVM IR, we should convert the module to the debug info
38         // format that LLVM expects us to print.
39         // See https://llvm.org/docs/RemoveDIsDebugInfo.html
40         llvm::ScopedDbgInfoFormatSetter formatSetter(*llvmModule,
41                                                      WriteNewDbgInfoFormat);
42         if (WriteNewDbgInfoFormat)
43           llvmModule->removeDebugIntrinsicDeclarations();
44         llvmModule->print(output, nullptr);
45         return success();
46       },
47       [](DialectRegistry &registry) {
48         registry.insert<DLTIDialect, func::FuncDialect>();
49         registerAllToLLVMIRTranslations(registry);
50       });
51 }
52 } // namespace mlir
53