1//===- TestFromLLVMIRTranslation.cpp - Import Test dialect from LLVM IR ---===//
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 a translation between LLVM IR and the MLIR Test dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#include "TestOpsTraits.h"
14#include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h"
15#include "mlir/IR/Builders.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/IR/BuiltinOps.h"
18#include "mlir/Support/LLVM.h"
19#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h"
20#include "mlir/Target/LLVMIR/Import.h"
21#include "mlir/Target/LLVMIR/ModuleImport.h"
22#include "mlir/Tools/mlir-translate/Translation.h"
23
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Verifier.h"
27#include "llvm/IRReader/IRReader.h"
28#include "llvm/Support/SourceMgr.h"
29
30using namespace mlir;
31using namespace test;
32
33static ArrayRef<unsigned> getSupportedInstructionsImpl() {
34 static unsigned instructions[] = {llvm::Instruction::Load};
35 return instructions;
36}
37
38static LogicalResult convertLoad(OpBuilder &builder, llvm::Instruction *inst,
39 ArrayRef<llvm::Value *> llvmOperands,
40 LLVM::ModuleImport &moduleImport) {
41 FailureOr<Value> addr = moduleImport.convertValue(value: llvmOperands[0]);
42 if (failed(Result: addr))
43 return failure();
44 // Create the LoadOp
45 Value loadOp = LLVM::LoadOp::create(
46 builder, location: moduleImport.translateLoc(loc: inst->getDebugLoc()),
47 type: moduleImport.convertType(type: inst->getType()), addr: *addr);
48 moduleImport.mapValue(inst) = SameOperandElementTypeOp::create(
49 builder, loadOp.getLoc(), loadOp.getType(), loadOp, loadOp);
50 return success();
51}
52
53namespace {
54class TestDialectLLVMImportDialectInterface
55 : public LLVMImportDialectInterface {
56public:
57 using LLVMImportDialectInterface::LLVMImportDialectInterface;
58
59 LogicalResult
60 convertInstruction(OpBuilder &builder, llvm::Instruction *inst,
61 ArrayRef<llvm::Value *> llvmOperands,
62 LLVM::ModuleImport &moduleImport) const override {
63 switch (inst->getOpcode()) {
64 case llvm::Instruction::Load:
65 return convertLoad(builder, inst, llvmOperands, moduleImport);
66 default:
67 break;
68 }
69 return failure();
70 }
71
72 ArrayRef<unsigned> getSupportedInstructions() const override {
73 return getSupportedInstructionsImpl();
74 }
75};
76} // namespace
77
78namespace mlir {
79void registerTestFromLLVMIR() {
80 TranslateToMLIRRegistration registration(
81 "test-import-llvmir", "test dialect from LLVM IR",
82 [](llvm::SourceMgr &sourceMgr,
83 MLIRContext *context) -> OwningOpRef<Operation *> {
84 llvm::SMDiagnostic err;
85 llvm::LLVMContext llvmContext;
86 std::unique_ptr<llvm::Module> llvmModule =
87 llvm::parseIR(Buffer: *sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID()),
88 Err&: err, Context&: llvmContext);
89 if (!llvmModule) {
90 std::string errStr;
91 llvm::raw_string_ostream errStream(errStr);
92 err.print(/*ProgName=*/"", S&: errStream);
93 emitError(loc: UnknownLoc::get(context)) << errStream.str();
94 return {};
95 }
96 if (llvm::verifyModule(M: *llvmModule, OS: &llvm::errs()))
97 return nullptr;
98
99 return translateLLVMIRToModule(llvmModule: std::move(llvmModule), context, emitExpensiveWarnings: false);
100 },
101 [](DialectRegistry &registry) {
102 registry.insert<DLTIDialect>();
103 registry.insert<test::TestDialect>();
104 registerLLVMDialectImport(registry);
105 registry.addExtension(
106 extensionFn: +[](MLIRContext *ctx, test::TestDialect *dialect) {
107 dialect->addInterfaces<TestDialectLLVMImportDialectInterface>();
108 });
109 });
110}
111} // namespace mlir
112