1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
//===- TestToLLVMIRTranslation.cpp - Translate Test dialect to LLVM IR ----===//
//
// 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 a translation between the MLIR Test dialect and LLVM IR.
//
//===----------------------------------------------------------------------===//
#include "TestDialect.h"
#include "TestOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/DebugProgramInstruction.h"
using namespace mlir;
namespace {
class TestDialectLLVMIRTranslationInterface
: public LLVMTranslationDialectInterface {
public:
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
LogicalResult
amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
NamedAttribute attribute,
LLVM::ModuleTranslation &moduleTranslation) const final;
LogicalResult
convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
};
} // namespace
LogicalResult TestDialectLLVMIRTranslationInterface::amendOperation(
Operation *op, ArrayRef<llvm::Instruction *> instructions,
NamedAttribute attribute,
LLVM::ModuleTranslation &moduleTranslation) const {
return llvm::StringSwitch<llvm::function_ref<LogicalResult(Attribute)>>(
attribute.getName())
// The `test.discardable_mod_attr` attribute, if present and set to
// `true`, results in the addition of a `test.symbol` in the module it is
// attached to with name "sym_from_attr".
.Case("test.discardable_mod_attr",
[&](Attribute attr) {
if (!isa<ModuleOp>(op)) {
op->emitOpError("attribute 'test.discardable_mod_attr' only "
"supported in modules");
return failure();
}
bool createSymbol = false;
if (auto boolAttr = dyn_cast<BoolAttr>(attr))
createSymbol = boolAttr.getValue();
if (createSymbol) {
OpBuilder builder(op->getRegion(0));
test::SymbolOp::create(
builder, op->getLoc(),
StringAttr::get(op->getContext(), "sym_from_attr"),
/*sym_visibility=*/nullptr);
}
return success();
})
.Case("test.add_annotation",
[&](Attribute attr) {
for (llvm::Instruction *instruction : instructions) {
if (auto strAttr = dyn_cast<StringAttr>(attr)) {
instruction->addAnnotationMetadata("annotation_from_test: " +
strAttr.getValue().str());
} else {
instruction->addAnnotationMetadata("annotation_from_test");
}
}
return success();
})
.Default([](Attribute) {
// Skip other discardable dialect attributes.
return success();
})(attribute.getValue());
}
LogicalResult TestDialectLLVMIRTranslationInterface::convertOperation(
Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const {
return llvm::TypeSwitch<Operation *, LogicalResult>(op)
// `test.symbol`s are translated into global integers in LLVM IR, with a
// name equal to the symbol they are translated from.
.Case([&](test::SymbolOp symOp) {
llvm::Module *mod = moduleTranslation.getLLVMModule();
llvm::IntegerType *i32Type =
llvm::IntegerType::get(moduleTranslation.getLLVMContext(), 32);
mod->getOrInsertGlobal(symOp.getSymName(), i32Type);
return success();
})
.Default([&](Operation *) {
return op->emitOpError("unsupported translation of test operation");
});
}
namespace mlir {
void registerTestToLLVMIR() {
TranslateFromMLIRRegistration registration(
"test-to-llvmir", "test dialect to LLVM IR",
[](Operation *op, raw_ostream &output) {
llvm::LLVMContext llvmContext;
auto llvmModule = translateModuleToLLVMIR(op, llvmContext);
if (!llvmModule)
return failure();
llvmModule->removeDebugIntrinsicDeclarations();
llvmModule->print(output, nullptr);
return success();
},
[](DialectRegistry ®istry) {
registry.insert<test::TestDialect>();
registerBuiltinDialectTranslation(registry);
registerLLVMDialectTranslation(registry);
registry.addExtension(
+[](MLIRContext *ctx, test::TestDialect *dialect) {
dialect->addInterfaces<TestDialectLLVMIRTranslationInterface>();
});
});
}
} // namespace mlir
|