Files
clang-p2996/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
Christian Ulmann bf91cd6ea9 [mlir][llvm] Verify LLVM module before import
This commit ensures that the importing of LLVM modules first verifies
that the module is even valid. As many tests did not work with valid
LLVM IR, they were fixed as part of this commit.

Some error messages were only reachable with invalid input IR, thus they
were replaced with a failures.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D144186
2023-02-17 10:25:23 +01:00

58 lines
2.2 KiB
C++

//===- ConvertFromLLVMIR.cpp - MLIR to LLVM IR conversion -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the function that registers the translation between
// LLVM IR and the MLIR LLVM dialect.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Target/LLVMIR/Dialect/All.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
using namespace mlir;
namespace mlir {
void registerFromLLVMIRTranslation() {
TranslateToMLIRRegistration registration(
"import-llvm", "translate llvmir to mlir",
[](llvm::SourceMgr &sourceMgr,
MLIRContext *context) -> OwningOpRef<Operation *> {
llvm::SMDiagnostic err;
llvm::LLVMContext llvmContext;
std::unique_ptr<llvm::Module> llvmModule =
llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()),
err, llvmContext);
if (!llvmModule) {
std::string errStr;
llvm::raw_string_ostream errStream(errStr);
err.print(/*ProgName=*/"", errStream);
emitError(UnknownLoc::get(context)) << errStream.str();
return {};
}
if (llvm::verifyModule(*llvmModule, &llvm::errs()))
return nullptr;
return translateLLVMIRToModule(std::move(llvmModule), context);
},
[](DialectRegistry &registry) {
// Register the DLTI dialect used to express the data layout
// specification of the imported module.
registry.insert<DLTIDialect>();
// Register all dialects that implement the LLVMImportDialectInterface
// including the LLVM dialect.
registerAllFromLLVMIRTranslations(registry);
});
}
} // namespace mlir