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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
//===- RuntimeLibcallEmitter.cpp - Properties from RuntimeLibcalls.td -----===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
using namespace llvm;
namespace {
class RuntimeLibcall {
const Record *TheDef = nullptr;
public:
RuntimeLibcall() = delete;
RuntimeLibcall(const Record *Def) : TheDef(Def) { assert(Def); }
~RuntimeLibcall() { assert(TheDef); }
const Record *getDef() const { return TheDef; }
StringRef getName() const { return TheDef->getName(); }
void emitEnumEntry(raw_ostream &OS) const {
OS << "RTLIB::" << TheDef->getValueAsString("Name");
}
};
class RuntimeLibcallImpl {
const Record *TheDef;
const RuntimeLibcall *Provides = nullptr;
public:
RuntimeLibcallImpl(
const Record *Def,
const DenseMap<const Record *, const RuntimeLibcall *> &ProvideMap)
: TheDef(Def) {
if (const Record *ProvidesDef = Def->getValueAsDef("Provides"))
Provides = ProvideMap.lookup(ProvidesDef);
}
~RuntimeLibcallImpl() {}
const Record *getDef() const { return TheDef; }
StringRef getName() const { return TheDef->getName(); }
const RuntimeLibcall *getProvides() const { return Provides; }
StringRef getLibcallFuncName() const {
return TheDef->getValueAsString("LibCallFuncName");
}
void emitQuotedLibcallFuncName(raw_ostream &OS) const {
OS << '\"' << getLibcallFuncName() << '\"';
}
bool isDefault() const { return TheDef->getValueAsBit("IsDefault"); }
void emitEnumEntry(raw_ostream &OS) const {
OS << "RTLIB::" << TheDef->getName();
}
};
class RuntimeLibcallEmitter {
private:
const RecordKeeper &Records;
DenseMap<const Record *, const RuntimeLibcall *> Def2RuntimeLibcall;
const RuntimeLibcall *getRuntimeLibcall(const Record *Def) const {
return Def2RuntimeLibcall.lookup(Def);
}
std::vector<RuntimeLibcall> RuntimeLibcallDefList;
std::vector<RuntimeLibcallImpl> RuntimeLibcallImplDefList;
DenseMap<const RuntimeLibcall *, const RuntimeLibcallImpl *>
LibCallToDefaultImpl;
void
emitTargetOverrideFunc(raw_ostream &OS, StringRef FuncName,
ArrayRef<RuntimeLibcallImpl> LibCallImplList) const;
void emitGetRuntimeLibcallEnum(raw_ostream &OS) const;
void emitWindowsArm64LibCallNameOverrides(raw_ostream &OS) const;
void emitGetInitRuntimeLibcallNames(raw_ostream &OS) const;
void emitGetInitRuntimeLibcallUtils(raw_ostream &OS) const;
public:
RuntimeLibcallEmitter(const RecordKeeper &R) : Records(R) {
ArrayRef<const Record *> AllRuntimeLibcalls =
Records.getAllDerivedDefinitions("RuntimeLibcall");
RuntimeLibcallDefList.reserve(AllRuntimeLibcalls.size());
for (const Record *RuntimeLibcallDef : AllRuntimeLibcalls) {
RuntimeLibcallDefList.emplace_back(RuntimeLibcallDef);
Def2RuntimeLibcall[RuntimeLibcallDef] = &RuntimeLibcallDefList.back();
}
for (RuntimeLibcall &LibCall : RuntimeLibcallDefList)
Def2RuntimeLibcall[LibCall.getDef()] = &LibCall;
ArrayRef<const Record *> AllRuntimeLibcallImpls =
Records.getAllDerivedDefinitions("RuntimeLibcallImpl");
RuntimeLibcallImplDefList.reserve(AllRuntimeLibcallImpls.size());
for (const Record *LibCallImplDef : AllRuntimeLibcallImpls) {
RuntimeLibcallImplDefList.emplace_back(LibCallImplDef,
Def2RuntimeLibcall);
RuntimeLibcallImpl &LibCallImpl = RuntimeLibcallImplDefList.back();
// const RuntimeLibcallImpl &LibCallImpl =
// RuntimeLibcallImplDefList.back();
if (LibCallImpl.isDefault()) {
const RuntimeLibcall *Provides = LibCallImpl.getProvides();
if (!Provides)
PrintFatalError(LibCallImplDef->getLoc(),
"default implementations must provide a libcall");
LibCallToDefaultImpl[Provides] = &LibCallImpl;
}
}
}
std::vector<RuntimeLibcallImpl>
getRuntimeLibcallImplSet(StringRef Name) const {
std::vector<RuntimeLibcallImpl> Result;
ArrayRef<const Record *> ImplSet =
Records.getAllDerivedDefinitionsIfDefined(Name);
Result.reserve(ImplSet.size());
for (const Record *LibCallImplDef : ImplSet)
Result.emplace_back(LibCallImplDef, Def2RuntimeLibcall);
return Result;
}
void run(raw_ostream &OS);
};
} // End anonymous namespace.
/// Emit a method \p FuncName of RTLIB::RuntimeLibcallsInfo to override the
/// libcall names in \p LibCallImplList.
void RuntimeLibcallEmitter::emitTargetOverrideFunc(
raw_ostream &OS, StringRef FuncName,
ArrayRef<RuntimeLibcallImpl> LibCallImplList) const {
OS << "void llvm::RTLIB::RuntimeLibcallsInfo::" << FuncName << "() {\n";
if (LibCallImplList.empty()) {
OS << " llvm_unreachable(\"override set not defined\");\n";
} else {
// for (const Record *LibCallImpl : LibCallImplList) {
for (const RuntimeLibcallImpl &LibCallImpl : LibCallImplList) {
const RuntimeLibcall *Provides = LibCallImpl.getProvides();
OS << " LibcallImpls[";
Provides->emitEnumEntry(OS);
OS << "] = ";
LibCallImpl.emitEnumEntry(OS);
OS << ";\n";
}
}
OS << "}\n\n";
}
void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
OS << "#ifdef GET_RUNTIME_LIBCALL_ENUM\n"
"namespace llvm {\n"
"namespace RTLIB {\n"
"enum Libcall : unsigned short {\n";
size_t CallTypeEnumVal = 0;
for (const RuntimeLibcall &LibCall : RuntimeLibcallDefList) {
StringRef Name = LibCall.getName();
OS << " " << Name << " = " << CallTypeEnumVal++ << ",\n";
}
// TODO: Emit libcall names as string offset table.
OS << " UNKNOWN_LIBCALL = " << CallTypeEnumVal
<< "\n};\n\n"
"enum LibcallImpl : unsigned short {\n"
" Unsupported = 0,\n";
// FIXME: Emit this in a different namespace. And maybe use enum class.
size_t LibCallImplEnumVal = 1;
for (const RuntimeLibcallImpl &LibCall : RuntimeLibcallImplDefList) {
OS << " " << LibCall.getName() << " = " << LibCallImplEnumVal++ << ", // "
<< LibCall.getLibcallFuncName() << '\n';
}
OS << " NumLibcallImpls = " << LibCallImplEnumVal
<< "\n};\n"
"} // End namespace RTLIB\n"
"} // End namespace llvm\n"
"#endif\n\n";
}
void RuntimeLibcallEmitter::emitWindowsArm64LibCallNameOverrides(
raw_ostream &OS) const {
// FIXME: Stop treating this as a special case
OS << "void "
"llvm::RTLIB::RuntimeLibcallsInfo::setWindowsArm64LibCallNameOverrides("
") {\n"
" static const RTLIB::LibcallImpl "
"WindowsArm64RoutineImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {\n";
for (const RuntimeLibcall &LibCall : RuntimeLibcallDefList) {
auto I = LibCallToDefaultImpl.find(&LibCall);
if (I == LibCallToDefaultImpl.end())
OS << " RTLIB::Unsupported,";
else {
const RuntimeLibcallImpl *LibCallImpl = I->second;
assert(LibCallImpl);
OS << " RTLIB::arm64ec_" << LibCallImpl->getName() << ',';
}
OS << " // ";
LibCall.emitEnumEntry(OS);
OS << '\n';
}
OS << " RTLIB::Unsupported // RTLIB::UNKNOWN_LIBCALL\n"
" };\n\n"
" std::memcpy(LibcallImpls, WindowsArm64RoutineImpls,\n"
" sizeof(LibcallImpls));\n"
" static_assert(sizeof(LibcallImpls) == "
"sizeof(WindowsArm64RoutineImpls),\n"
" \"libcall array size should match\");\n"
"}\n#endif\n\n";
}
void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
raw_ostream &OS) const {
// TODO: Emit libcall names as string offset table.
OS << "#ifdef GET_INIT_RUNTIME_LIBCALL_NAMES\n"
"const RTLIB::LibcallImpl "
"llvm::RTLIB::RuntimeLibcallsInfo::"
"DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {\n";
for (const RuntimeLibcall &LibCall : RuntimeLibcallDefList) {
auto I = LibCallToDefaultImpl.find(&LibCall);
if (I == LibCallToDefaultImpl.end()) {
OS << " RTLIB::Unsupported,";
} else {
const RuntimeLibcallImpl *LibCallImpl = I->second;
OS << " ";
LibCallImpl->emitEnumEntry(OS);
OS << ",";
}
OS << " // ";
LibCall.emitEnumEntry(OS);
OS << '\n';
}
OS << " RTLIB::Unsupported\n"
"};\n\n";
// Emit the implementation names
OS << "const char *const llvm::RTLIB::RuntimeLibcallsInfo::"
"LibCallImplNames[RTLIB::NumLibcallImpls] = {\n"
" nullptr, // RTLIB::Unsupported\n";
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
OS << " \"" << LibCallImpl.getLibcallFuncName() << "\", // ";
LibCallImpl.emitEnumEntry(OS);
OS << '\n';
}
OS << "};\n\n";
// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"
"ImplToLibcall[RTLIB::NumLibcallImpls] = {\n"
" RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported\n";
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
const RuntimeLibcall *Provides = LibCallImpl.getProvides();
OS << " ";
Provides->emitEnumEntry(OS);
OS << ", // ";
LibCallImpl.emitEnumEntry(OS);
OS << '\n';
}
OS << "};\n\n";
std::vector<RuntimeLibcallImpl> ZOSRuntimeLibcallImplList =
getRuntimeLibcallImplSet("ZOSRuntimeLibcallImpl");
emitTargetOverrideFunc(OS, "setZOSLibCallNameOverrides",
ZOSRuntimeLibcallImplList);
std::vector<RuntimeLibcallImpl> PPCRuntimeLibcallImplList =
getRuntimeLibcallImplSet("PPCRuntimeLibcallImpl");
emitTargetOverrideFunc(OS, "setPPCLibCallNameOverrides",
PPCRuntimeLibcallImplList);
emitWindowsArm64LibCallNameOverrides(OS);
}
void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallUtils(
raw_ostream &OS) const {
// FIXME: Hack we shouldn't really need
OS << "#ifdef GET_INIT_RUNTIME_LIBCALL_UTILS\n"
"static inline bool isAtomicLibCall(llvm::RTLIB::Libcall LC) {\n"
" switch (LC) {\n";
for (const RuntimeLibcall &LibCall : RuntimeLibcallDefList) {
StringRef Name = LibCall.getName();
if (Name.contains("ATOMIC")) {
OS << " case ";
LibCall.emitEnumEntry(OS);
OS << ":\n";
}
}
OS << " return true;\n"
" default:\n"
" return false;\n"
" }\n\n"
" llvm_unreachable(\"covered switch over libcalls\");\n"
"}\n#endif\n\n";
}
void RuntimeLibcallEmitter::run(raw_ostream &OS) {
emitSourceFileHeader("Runtime LibCalls Source Fragment", OS, Records);
emitGetRuntimeLibcallEnum(OS);
emitGetInitRuntimeLibcallNames(OS);
emitGetInitRuntimeLibcallUtils(OS);
}
static TableGen::Emitter::OptClass<RuntimeLibcallEmitter>
X("gen-runtime-libcalls", "Generate RuntimeLibcalls");
|