-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathOpDescription.cpp
More file actions
224 lines (179 loc) · 8.61 KB
/
OpDescription.cpp
File metadata and controls
224 lines (179 loc) · 8.61 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "llvm-dialects/Dialect/OpDescription.h"
#include "llvm-dialects/Dialect/OpSet.h"
#include "llvm-dialects/Dialect/Dialect.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
using namespace llvm_dialects;
using namespace llvm;
unsigned OpDescription::getOpcode() const {
if (auto *op = std::get_if<unsigned>(&m_op))
return *op;
llvm_unreachable("OpDescription does not contain any opcode!");
}
bool OpDescription::matchInstruction(const Instruction &inst) const {
if (m_kind == Kind::Intrinsic) {
if (auto *intr = dyn_cast<IntrinsicInst>(&inst))
return matchIntrinsic(intr->getIntrinsicID());
return false;
}
if (m_kind == Kind::Core) {
if (auto *op = std::get_if<unsigned>(&m_op))
return inst.getOpcode() == *op;
return false;
}
if (auto *call = dyn_cast<CallInst>(&inst)) {
if (auto *fn = call->getCalledFunction())
return matchDeclaration(*fn);
}
return false;
}
bool OpDescription::matchDeclaration(const Function &decl) const {
if (auto *mnemonic = std::get_if<StringRef>(&m_op)) {
if (m_kind == Kind::DialectWithOverloads)
return llvm_dialects::detail::isOverloadedOperationDecl(&decl, *mnemonic);
assert(m_kind == Kind::Dialect);
return llvm_dialects::detail::isSimpleOperationDecl(&decl, *mnemonic);
}
assert(m_kind == Kind::Intrinsic);
return matchIntrinsic(decl.getIntrinsicID());
}
bool OpDescription::matchIntrinsic(unsigned intrinsicId) const {
assert(m_kind == Kind::Intrinsic);
if (auto *op = std::get_if<unsigned>(&m_op))
return *op == intrinsicId;
return false;
}
// ============================================================================
// Descriptions of core instructions.
template <> const OpSet &OpSet::getClass<UnaryInstruction>() {
static unsigned opcodes[] = {
Instruction::Alloca,
Instruction::Load,
Instruction::VAArg,
Instruction::ExtractValue,
#define HANDLE_UNARY_INST(num, opcode, Class) Instruction::opcode,
#define HANDLE_CAST_INST(num, opcode, Class) Instruction::opcode,
#include "llvm/IR/Instruction.def"
};
static const OpSet set = OpSet::fromCoreOpcodes(opcodes);
return set;
}
template <> const OpSet &OpSet::getClass<BinaryOperator>() {
static unsigned opcodes[] = {
#define HANDLE_BINARY_INST(num, opcode, Class) Instruction::opcode,
#include "llvm/IR/Instruction.def"
};
static const OpSet set = OpSet::fromCoreOpcodes({opcodes});
return set;
}
// Generate OpDescription for all dedicate instruction classes.
#define HANDLE_USER_INST(...)
#define HANDLE_UNARY_INST(...)
#define HANDLE_BINARY_INST(...)
#define HANDLE_INST(num, opcode, Class) \
template <> const OpDescription &OpDescription::get<Class>() { \
static const OpDescription desc{Kind::Core, Instruction::opcode}; \
return desc; \
}
#include "llvm/IR/Instruction.def"
#define HANDLE_INTRINSIC_DESC(Class, opcode) \
template <> const OpDescription &OpDescription::get<Class>() { \
static const OpDescription desc{Kind::Intrinsic, Intrinsic::opcode}; \
return desc; \
}
#define HANDLE_INTRINSIC_DESC_OPCODE_SET(Class, ...) \
template <> const OpSet &OpSet::getClass<Class>() { \
static const OpSet set = OpSet::fromIntrinsicIDs({__VA_ARGS__}); \
return set; \
}
// ============================================================================
// Descriptions of intrinsic facades implemented in LLVM
#if HAVE_LLVM_VERSION_MAJOR >= 16
HANDLE_INTRINSIC_DESC_OPCODE_SET(LifetimeIntrinsic, Intrinsic::lifetime_start,
Intrinsic::lifetime_end)
#endif
// Add Intrinsic::dbg_addr back for sufficiently recent LLVM
#if HAVE_LLVM_VERSION_MAJOR >= 16
HANDLE_INTRINSIC_DESC_OPCODE_SET(DbgInfoIntrinsic, Intrinsic::dbg_declare,
Intrinsic::dbg_value, Intrinsic::dbg_label,
Intrinsic::dbg_assign)
#else
HANDLE_INTRINSIC_DESC_OPCODE_SET(DbgInfoIntrinsic, Intrinsic::dbg_declare,
Intrinsic::dbg_value, Intrinsic::dbg_label)
#endif
// Add Intrinsic::dbg_addr back for sufficiently recent LLVM
#if HAVE_LLVM_VERSION_MAJOR >= 16
HANDLE_INTRINSIC_DESC_OPCODE_SET(DbgVariableIntrinsic, Intrinsic::dbg_declare,
Intrinsic::dbg_value, Intrinsic::dbg_assign)
#else
HANDLE_INTRINSIC_DESC_OPCODE_SET(DbgVariableIntrinsic, Intrinsic::dbg_declare,
Intrinsic::dbg_value)
#endif
HANDLE_INTRINSIC_DESC(DbgDeclareInst, dbg_declare)
HANDLE_INTRINSIC_DESC(DbgValueInst, dbg_value)
// Add this back for sufficiently recent LLVM
// HANDLE_INTRINSIC_DESC(DbgAddrIntrinsic, dbg_addr)
#if HAVE_LLVM_VERSION_MAJOR >= 16
HANDLE_INTRINSIC_DESC(DbgAssignIntrinsic, dbg_assign)
#endif
HANDLE_INTRINSIC_DESC(DbgLabelInst, dbg_label)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AtomicMemIntrinsic,
Intrinsic::memcpy_element_unordered_atomic,
Intrinsic::memmove_element_unordered_atomic,
Intrinsic::memset_element_unordered_atomic)
HANDLE_INTRINSIC_DESC(AtomicMemSetInst, memset_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AtomicMemTransferInst,
Intrinsic::memcpy_element_unordered_atomic,
Intrinsic::memmove_element_unordered_atomic)
HANDLE_INTRINSIC_DESC(AtomicMemCpyInst, memcpy_element_unordered_atomic)
HANDLE_INTRINSIC_DESC(AtomicMemMoveInst, memmove_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(MemIntrinsic, Intrinsic::memcpy,
Intrinsic::memmove, Intrinsic::memset,
Intrinsic::memset_inline,
Intrinsic::memcpy_inline)
HANDLE_INTRINSIC_DESC_OPCODE_SET(MemSetInst, Intrinsic::memset,
Intrinsic::memset_inline)
HANDLE_INTRINSIC_DESC(MemSetInlineInst, memset_inline)
HANDLE_INTRINSIC_DESC_OPCODE_SET(MemTransferInst, Intrinsic::memcpy,
Intrinsic::memmove, Intrinsic::memcpy_inline)
HANDLE_INTRINSIC_DESC_OPCODE_SET(MemCpyInst, Intrinsic::memcpy,
Intrinsic::memcpy_inline)
HANDLE_INTRINSIC_DESC(MemMoveInst, memmove)
HANDLE_INTRINSIC_DESC(MemCpyInlineInst, memcpy_inline)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AnyMemIntrinsic, Intrinsic::memcpy,
Intrinsic::memcpy_inline, Intrinsic::memmove,
Intrinsic::memset, Intrinsic::memset_inline,
Intrinsic::memcpy_element_unordered_atomic,
Intrinsic::memmove_element_unordered_atomic,
Intrinsic::memset_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AnyMemSetInst, Intrinsic::memset,
Intrinsic::memset_inline,
Intrinsic::memset_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AnyMemTransferInst, Intrinsic::memcpy,
Intrinsic::memcpy_inline, Intrinsic::memmove,
Intrinsic::memcpy_element_unordered_atomic,
Intrinsic::memmove_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AnyMemCpyInst, Intrinsic::memcpy,
Intrinsic::memcpy_inline,
Intrinsic::memcpy_element_unordered_atomic)
HANDLE_INTRINSIC_DESC_OPCODE_SET(AnyMemMoveInst, Intrinsic::memmove,
Intrinsic::memmove_element_unordered_atomic)
// TODO: Is completing this list worth it?
#undef HANDLE_INTRINSIC_DESC_OPCODE_SET
#undef HANDLE_INTRINSIC_DESC