| 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/LibraryOptionsContext.h" |
| 23 | #include "llvm/Support/SourceMgr.h" |
| 24 | |
| 25 | using namespace mlir; |
| 26 | |
| 27 | namespace mlir { |
| 28 | void registerFromLLVMIRTranslation() { |
| 29 | static llvm::cl::opt<bool> emitExpensiveWarnings( |
| 30 | "emit-expensive-warnings" , |
| 31 | llvm::cl::desc("Emit expensive warnings during LLVM IR import " |
| 32 | "(discouraged: testing only!)" ), |
| 33 | llvm::cl::init(Val: false)); |
| 34 | static llvm::cl::opt<bool> convertDebugRecToIntrinsics( |
| 35 | "convert-debug-rec-to-intrinsics" , |
| 36 | llvm::cl::desc("Change the input LLVM module to use old debug intrinsics " |
| 37 | "instead of records " |
| 38 | "via convertFromNewDbgValues, this happens " |
| 39 | "before importing the debug information" |
| 40 | "(discouraged: to be removed soon!)" ), |
| 41 | llvm::cl::init(Val: false)); |
| 42 | static llvm::cl::opt<bool> dropDICompositeTypeElements( |
| 43 | "drop-di-composite-type-elements" , |
| 44 | llvm::cl::desc( |
| 45 | "Avoid translating the elements of DICompositeTypes during " |
| 46 | "the LLVM IR import (discouraged: testing only!)" ), |
| 47 | llvm::cl::init(Val: false)); |
| 48 | |
| 49 | static llvm::cl::opt<bool> preferUnregisteredIntrinsics( |
| 50 | "prefer-unregistered-intrinsics" , |
| 51 | llvm::cl::desc( |
| 52 | "Prefer translating all intrinsics into llvm.call_intrinsic instead " |
| 53 | "of using dialect supported intrinsics" ), |
| 54 | llvm::cl::init(Val: false)); |
| 55 | |
| 56 | static llvm::cl::opt<bool> importStructsAsLiterals( |
| 57 | "import-structs-as-literals" , |
| 58 | llvm::cl::desc("Controls if structs should be imported as literal " |
| 59 | "structs, i.e., nameless structs." ), |
| 60 | llvm::cl::init(Val: false)); |
| 61 | |
| 62 | TranslateToMLIRRegistration registration( |
| 63 | "import-llvm" , "Translate LLVMIR to MLIR" , |
| 64 | [](llvm::SourceMgr &sourceMgr, |
| 65 | MLIRContext *context) -> OwningOpRef<Operation *> { |
| 66 | llvm::SMDiagnostic err; |
| 67 | llvm::LLVMContext llvmContext; |
| 68 | llvmContext.getLibraryOptions().assign(Other: getTranslationLibraryOptions()); |
| 69 | std::unique_ptr<llvm::Module> llvmModule = |
| 70 | llvm::parseIR(Buffer: *sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID()), |
| 71 | Err&: err, Context&: llvmContext); |
| 72 | if (!llvmModule) { |
| 73 | std::string errStr; |
| 74 | llvm::raw_string_ostream errStream(errStr); |
| 75 | err.print(/*ProgName=*/"" , S&: errStream); |
| 76 | emitError(loc: UnknownLoc::get(context)) << errStr; |
| 77 | return {}; |
| 78 | } |
| 79 | if (llvm::verifyModule(M: *llvmModule, OS: &llvm::errs())) |
| 80 | return nullptr; |
| 81 | |
| 82 | // Now that the translation supports importing debug records directly, |
| 83 | // make it the default, but allow the user to override to old behavior. |
| 84 | if (convertDebugRecToIntrinsics) |
| 85 | llvmModule->convertFromNewDbgValues(); |
| 86 | |
| 87 | return translateLLVMIRToModule( |
| 88 | llvmModule: std::move(llvmModule), context, emitExpensiveWarnings, |
| 89 | dropDICompositeTypeElements, /*loadAllDialects=*/true, |
| 90 | preferUnregisteredIntrinsics, importStructsAsLiterals); |
| 91 | }, |
| 92 | [](DialectRegistry ®istry) { |
| 93 | // Register the DLTI dialect used to express the data layout |
| 94 | // specification of the imported module. |
| 95 | registry.insert<DLTIDialect>(); |
| 96 | // Register all dialects that implement the LLVMImportDialectInterface |
| 97 | // including the LLVM dialect. |
| 98 | registerAllFromLLVMIRTranslations(registry); |
| 99 | }); |
| 100 | } |
| 101 | } // namespace mlir |
| 102 | |