1 //===- ConvertFromLLVMIR.cpp - MLIR to LLVM IR conversion -----------------===//
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 the function that registers the translation between
10 // LLVM IR and the MLIR LLVM dialect.
12 //===----------------------------------------------------------------------===//
14 #include "mlir/Dialect/DLTI/DLTI.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/Target/LLVMIR/Dialect/All.h"
17 #include "mlir/Target/LLVMIR/Import.h"
18 #include "mlir/Tools/mlir-translate/Translation.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/IRReader/IRReader.h"
22 #include "llvm/Support/SourceMgr.h"
27 void registerFromLLVMIRTranslation() {
28 static llvm::cl::opt
<bool> emitExpensiveWarnings(
29 "emit-expensive-warnings",
30 llvm::cl::desc("Emit expensive warnings during LLVM IR import "
31 "(discouraged: testing only!)"),
32 llvm::cl::init(false));
33 static llvm::cl::opt
<bool> dropDICompositeTypeElements(
34 "drop-di-composite-type-elements",
36 "Avoid translating the elements of DICompositeTypes during "
37 "the LLVM IR import (discouraged: testing only!)"),
38 llvm::cl::init(false));
40 TranslateToMLIRRegistration
registration(
41 "import-llvm", "Translate LLVMIR to MLIR",
42 [](llvm::SourceMgr
&sourceMgr
,
43 MLIRContext
*context
) -> OwningOpRef
<Operation
*> {
44 llvm::SMDiagnostic err
;
45 llvm::LLVMContext llvmContext
;
46 std::unique_ptr
<llvm::Module
> llvmModule
=
47 llvm::parseIR(*sourceMgr
.getMemoryBuffer(sourceMgr
.getMainFileID()),
51 llvm::raw_string_ostream
errStream(errStr
);
52 err
.print(/*ProgName=*/"", errStream
);
53 emitError(UnknownLoc::get(context
)) << errStream
.str();
56 if (llvm::verifyModule(*llvmModule
, &llvm::errs()))
59 // Debug records are not currently supported in the LLVM IR translator.
60 if (llvmModule
->IsNewDbgInfoFormat
)
61 llvmModule
->convertFromNewDbgValues();
63 return translateLLVMIRToModule(std::move(llvmModule
), context
,
64 emitExpensiveWarnings
,
65 dropDICompositeTypeElements
);
67 [](DialectRegistry
®istry
) {
68 // Register the DLTI dialect used to express the data layout
69 // specification of the imported module.
70 registry
.insert
<DLTIDialect
>();
71 // Register all dialects that implement the LLVMImportDialectInterface
72 // including the LLVM dialect.
73 registerAllFromLLVMIRTranslations(registry
);