1//===- ConvertFromLLVMIR.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 the function that registers the translation between
10// LLVM IR and the MLIR LLVM dialect.
11//
12//===----------------------------------------------------------------------===//
13
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"
23
24using namespace mlir;
25
26namespace mlir {
27void 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(Val: false));
33 static llvm::cl::opt<bool> dropDICompositeTypeElements(
34 "drop-di-composite-type-elements",
35 llvm::cl::desc(
36 "Avoid translating the elements of DICompositeTypes during "
37 "the LLVM IR import (discouraged: testing only!)"),
38 llvm::cl::init(Val: false));
39
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(Buffer: *sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID()),
48 Err&: err, Context&: llvmContext);
49 if (!llvmModule) {
50 std::string errStr;
51 llvm::raw_string_ostream errStream(errStr);
52 err.print(/*ProgName=*/"", S&: errStream);
53 emitError(UnknownLoc::get(context)) << errStream.str();
54 return {};
55 }
56 if (llvm::verifyModule(M: *llvmModule, OS: &llvm::errs()))
57 return nullptr;
58
59 // Debug records are not currently supported in the LLVM IR translator.
60 if (llvmModule->IsNewDbgInfoFormat)
61 llvmModule->convertFromNewDbgValues();
62
63 return translateLLVMIRToModule(std::move(llvmModule), context,
64 emitExpensiveWarnings,
65 dropDICompositeTypeElements);
66 },
67 [](DialectRegistry &registry) {
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);
74 });
75}
76} // namespace mlir
77

source code of mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp