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 static llvm::cl::opt<bool> preferUnregisteredIntrinsics(
41 "prefer-unregistered-intrinsics",
42 llvm::cl::desc(
43 "Prefer translating all intrinsics into llvm.call_intrinsic instead "
44 "of using dialect supported intrinsics"),
45 llvm::cl::init(Val: false));
46
47 static llvm::cl::opt<bool> importStructsAsLiterals(
48 "import-structs-as-literals",
49 llvm::cl::desc("Controls if structs should be imported as literal "
50 "structs, i.e., nameless structs."),
51 llvm::cl::init(Val: false));
52
53 TranslateToMLIRRegistration registration(
54 "import-llvm", "Translate LLVMIR to MLIR",
55 [](llvm::SourceMgr &sourceMgr,
56 MLIRContext *context) -> OwningOpRef<Operation *> {
57 llvm::SMDiagnostic err;
58 llvm::LLVMContext llvmContext;
59 std::unique_ptr<llvm::Module> llvmModule =
60 llvm::parseIR(Buffer: *sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID()),
61 Err&: err, Context&: llvmContext);
62 if (!llvmModule) {
63 std::string errStr;
64 llvm::raw_string_ostream errStream(errStr);
65 err.print(/*ProgName=*/"", S&: errStream);
66 emitError(UnknownLoc::get(context)) << errStr;
67 return {};
68 }
69 if (llvm::verifyModule(M: *llvmModule, OS: &llvm::errs()))
70 return nullptr;
71
72 // Debug records are not currently supported in the LLVM IR translator.
73 if (llvmModule->IsNewDbgInfoFormat)
74 llvmModule->convertFromNewDbgValues();
75
76 return translateLLVMIRToModule(
77 std::move(llvmModule), context, emitExpensiveWarnings,
78 dropDICompositeTypeElements, /*loadAllDialects=*/true,
79 preferUnregisteredIntrinsics, importStructsAsLiterals);
80 },
81 [](DialectRegistry &registry) {
82 // Register the DLTI dialect used to express the data layout
83 // specification of the imported module.
84 registry.insert<DLTIDialect>();
85 // Register all dialects that implement the LLVMImportDialectInterface
86 // including the LLVM dialect.
87 registerAllFromLLVMIRTranslations(registry);
88 });
89}
90} // namespace mlir
91

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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