100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the function that registers the translation between10// 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(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,64 MLIRContext *context) -> OwningOpRef<Operation *> {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 85 return translateLLVMIRToModule(86 std::move(llvmModule), context, emitExpensiveWarnings,87 dropDICompositeTypeElements, /*loadAllDialects=*/true,88 preferUnregisteredIntrinsics, importStructsAsLiterals);89 },90 [](DialectRegistry ®istry) {91 // Register the DLTI dialect used to express the data layout92 // specification of the imported module.93 registry.insert<DLTIDialect>();94 // Register all dialects that implement the LLVMImportDialectInterface95 // including the LLVM dialect.96 registerAllFromLLVMIRTranslations(registry);97 });98}99} // namespace mlir100