MLIR 22.0.0git
ConvertFromLLVMIR.cpp
Go to the documentation of this file.
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
15#include "mlir/IR/BuiltinOps.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 {
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> convertDebugRecToIntrinsics(
34 "convert-debug-rec-to-intrinsics",
35 llvm::cl::desc("Change the input LLVM module to use old debug intrinsics "
36 "instead of records "
37 "via convertFromNewDbgValues, this happens "
38 "before importing the debug information"
39 "(discouraged: to be removed soon!)"),
40 llvm::cl::init(false));
41 static llvm::cl::opt<bool> dropDICompositeTypeElements(
42 "drop-di-composite-type-elements",
43 llvm::cl::desc(
44 "Avoid translating the elements of DICompositeTypes during "
45 "the LLVM IR import (discouraged: testing only!)"),
46 llvm::cl::init(false));
47
48 static llvm::cl::opt<bool> preferUnregisteredIntrinsics(
49 "prefer-unregistered-intrinsics",
50 llvm::cl::desc(
51 "Prefer translating all intrinsics into llvm.call_intrinsic instead "
52 "of using dialect supported intrinsics"),
53 llvm::cl::init(false));
54
55 static llvm::cl::opt<bool> importStructsAsLiterals(
56 "import-structs-as-literals",
57 llvm::cl::desc("Controls if structs should be imported as literal "
58 "structs, i.e., nameless structs."),
59 llvm::cl::init(false));
60
61 TranslateToMLIRRegistration registration(
62 "import-llvm", "Translate LLVMIR to MLIR",
63 [](llvm::SourceMgr &sourceMgr,
65 llvm::SMDiagnostic err;
66 llvm::LLVMContext llvmContext;
67 std::unique_ptr<llvm::Module> llvmModule =
68 llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()),
69 err, llvmContext);
70 if (!llvmModule) {
71 std::string errStr;
72 llvm::raw_string_ostream errStream(errStr);
73 err.print(/*ProgName=*/"", errStream);
74 emitError(UnknownLoc::get(context)) << errStr;
75 return {};
76 }
77 if (llvm::verifyModule(*llvmModule, &llvm::errs()))
78 return nullptr;
79
80 // Now that the translation supports importing debug records directly,
81 // make it the default, but allow the user to override to old behavior.
82 if (convertDebugRecToIntrinsics)
83 llvmModule->convertFromNewDbgValues();
84
86 std::move(llvmModule), context, emitExpensiveWarnings,
87 dropDICompositeTypeElements, /*loadAllDialects=*/true,
88 preferUnregisteredIntrinsics, importStructsAsLiterals);
89 },
90 [](DialectRegistry &registry) {
91 // Register the DLTI dialect used to express the data layout
92 // specification of the imported module.
93 registry.insert<DLTIDialect>();
94 // Register all dialects that implement the LLVMImportDialectInterface
95 // including the LLVM dialect.
97 });
98}
99} // namespace mlir
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
Definition OwningOpRef.h:29
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
static void registerAllFromLLVMIRTranslations(DialectRegistry &registry)
Registers all dialects that can be translated from LLVM IR and the corresponding translation interfac...
Definition All.h:79
void registerFromLLVMIRTranslation()
OwningOpRef< ModuleOp > translateLLVMIRToModule(std::unique_ptr< llvm::Module > llvmModule, MLIRContext *context, bool emitExpensiveWarnings=true, bool dropDICompositeTypeElements=false, bool loadAllDialects=true, bool preferUnregisteredIntrinsics=false, bool importStructsAsLiterals=false)
Translates the LLVM module into an MLIR module living in the given context.
Use Translate[ToMLIR|FromMLIR]Registration as an initializer that registers a function and associates...