Files
clang-p2996/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
Tobias Gysi cf487cce6f [mlir][llvm] Make the import of LLVM IR intrinsics extensible.
The revision introduces the LLVMImportDialectInterface to make the
import of LLVM IR intrinsics extensible. It uses a dialect interface
that enables external projects to provide their own conversion functions
for custom intrinsics. These conversion functions can rely on the
ModuleImport class to perform support tasks such as mapping LLVM
values to MLIR values or for converting types between the two worlds.

The implementation largely mirrors the export implementation. One major
difference is the dispatch to the appropriate dialect interface, since
LLVM IR intrinsics have no direct association to an MLIR dialect. The
dialect interfaces thus have to publish the supported intrinsics to
ensure incoming conversion calls are dispatched to the right dialect
interface.

The revision implements the extensible intrinsic import discussed as
part of the "extensible llvm ir import" rfc:
https://discourse.llvm.org/t/rfc-extensible-llvm-ir-import/67256/6

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D140374
2023-01-02 11:35:44 +01:00

55 lines
2.1 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/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 {};
}
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