MLIR  21.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 
14 #include "mlir/Dialect/DLTI/DLTI.h"
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 
24 using namespace mlir;
25 
26 namespace 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> 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(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(false));
46 
47  TranslateToMLIRRegistration registration(
48  "import-llvm", "Translate LLVMIR to MLIR",
49  [](llvm::SourceMgr &sourceMgr,
51  llvm::SMDiagnostic err;
52  llvm::LLVMContext llvmContext;
53  std::unique_ptr<llvm::Module> llvmModule =
54  llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()),
55  err, llvmContext);
56  if (!llvmModule) {
57  std::string errStr;
58  llvm::raw_string_ostream errStream(errStr);
59  err.print(/*ProgName=*/"", errStream);
60  emitError(UnknownLoc::get(context)) << errStr;
61  return {};
62  }
63  if (llvm::verifyModule(*llvmModule, &llvm::errs()))
64  return nullptr;
65 
66  // Debug records are not currently supported in the LLVM IR translator.
67  if (llvmModule->IsNewDbgInfoFormat)
68  llvmModule->convertFromNewDbgValues();
69 
71  std::move(llvmModule), context, emitExpensiveWarnings,
72  dropDICompositeTypeElements, /*loadAllDialects=*/true,
73  preferUnregisteredIntrinsics);
74  },
75  [](DialectRegistry &registry) {
76  // Register the DLTI dialect used to express the data layout
77  // specification of the imported module.
78  registry.insert<DLTIDialect>();
79  // Register all dialects that implement the LLVMImportDialectInterface
80  // including the LLVM dialect.
82  });
83 }
84 } // 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:60
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:78
OwningOpRef< ModuleOp > translateLLVMIRToModule(std::unique_ptr< llvm::Module > llvmModule, MLIRContext *context, bool emitExpensiveWarnings=true, bool dropDICompositeTypeElements=false, bool loadAllDialects=true, bool preferUnregisteredIntrinsics=false)
Translates the LLVM module into an MLIR module living in the given context.
void registerFromLLVMIRTranslation()
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Use Translate[ToMLIR|FromMLIR]Registration as an initializer that registers a function and associates...
Definition: Translation.h:110