Skip to content

Commit b74801a

Browse files
authored
TableGen: Use IfDefEmitter (#164209)
1 parent f3a60cf commit b74801a

File tree

4 files changed

+64
-57
lines changed

4 files changed

+64
-57
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
/// implementation, which includes a name and type signature.
3232
#define GET_RUNTIME_LIBCALL_ENUM
3333
#include "llvm/IR/RuntimeLibcalls.inc"
34-
#undef GET_RUNTIME_LIBCALL_ENUM
3534

3635
namespace llvm {
3736

@@ -170,7 +169,6 @@ struct RuntimeLibcallsInfo {
170169
// querying a real set of symbols
171170
#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
172171
#include "llvm/IR/RuntimeLibcalls.inc"
173-
#undef GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
174172
}
175173

176174
/// Check if this is valid libcall for the current module, otherwise

llvm/lib/IR/RuntimeLibcalls.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ using namespace RTLIB;
2121
#define GET_SET_TARGET_RUNTIME_LIBCALL_SETS
2222
#define DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
2323
#include "llvm/IR/RuntimeLibcalls.inc"
24-
#undef GET_INIT_RUNTIME_LIBCALL_NAMES
25-
#undef GET_SET_TARGET_RUNTIME_LIBCALL_SETS
26-
#undef DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
2724

2825
/// Set default libcall names. If a target wants to opt-out of a libcall it
2926
/// should be placed here.

llvm/test/TableGen/RuntimeLibcallEmitter.td

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
7373

7474
// All entries should be emitted in Libcall enum.
7575
// CHECK: #ifdef GET_RUNTIME_LIBCALL_ENUM
76+
// CHECK-NEXT: #undef GET_RUNTIME_LIBCALL_ENUM
77+
// CHECK-EMPTY:
7678
// CHECK-NEXT: namespace llvm {
7779
// CHECK-NEXT: namespace RTLIB {
7880
// CHECK-NEXT: enum Libcall : unsigned short {
@@ -101,9 +103,12 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
101103
// CHECK-NEXT: constexpr size_t NumLibcallImpls = 9;
102104
// CHECK-NEXT: } // End namespace RTLIB
103105
// CHECK-NEXT: } // End namespace llvm
104-
// CHECK-NEXT: #endif
106+
// CHECK-EMPTY:
107+
// CHECK-NEXT: #endif // GET_RUNTIME_LIBCALL_ENUM
105108

106109
// CHECK: #ifdef GET_INIT_RUNTIME_LIBCALL_NAMES
110+
// CHECK-NEXT: #undef GET_INIT_RUNTIME_LIBCALL_NAMES
111+
// CHECK-EMPTY:
107112
// CHECK-EMPTY:
108113
// CHECK-NEXT: #ifdef __GNUC__
109114
// CHECK-NEXT: #pragma GCC diagnostic push
@@ -163,13 +168,18 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
163168
// CHECK-NEXT: };
164169

165170
// CHECK: #ifdef GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
171+
// CHECK-NEXT: #undef GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
172+
// CHECK-EMPTY:
166173
// CHECK-NEXT: size_t Size = Name.size();
167174
// CHECK-NEXT: if (Size == 0 || Size > 9)
168175
// CHECK-NEXT: return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);
169176
// CHECK-NEXT: return lookupLibcallImplNameImpl(Name);
170-
// CHECK-NEXT: #endif
177+
// CHECK-EMPTY:
178+
// CHECK-NEXT: #endif // GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
171179

172180
// CHECK: #ifdef DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
181+
// CHECK-NEXT: #undef DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
182+
// CHECK-EMPTY:
173183
// CHECK-NEXT: static inline uint64_t hash(StringRef Str) {
174184
// CHECK-NEXT: return static_cast<uint32_t>(xxh3_64bits(Str));
175185
// CHECK-NEXT: }

llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/Support/FormatVariadic.h"
1616
#include "llvm/Support/raw_ostream.h"
1717
#include "llvm/Support/xxhash.h"
18+
#include "llvm/TableGen/CodeGenHelpers.h"
1819
#include "llvm/TableGen/Error.h"
1920
#include "llvm/TableGen/Record.h"
2021
#include "llvm/TableGen/SetTheory.h"
@@ -290,8 +291,9 @@ class RuntimeLibcallEmitter {
290291
} // End anonymous namespace.
291292

292293
void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
293-
OS << "#ifdef GET_RUNTIME_LIBCALL_ENUM\n"
294-
"namespace llvm {\n"
294+
IfDefEmitter IfDef(OS, "GET_RUNTIME_LIBCALL_ENUM");
295+
296+
OS << "namespace llvm {\n"
295297
"namespace RTLIB {\n"
296298
"enum Libcall : unsigned short {\n";
297299

@@ -315,8 +317,7 @@ void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
315317
<< RuntimeLibcallImplDefList.size() + 1
316318
<< ";\n"
317319
"} // End namespace RTLIB\n"
318-
"} // End namespace llvm\n"
319-
"#endif\n\n";
320+
"} // End namespace llvm\n";
320321
}
321322

322323
// StringMap uses xxh3_64bits, truncated to uint32_t.
@@ -432,14 +433,16 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
432433
//
433434
// TODO: It may make more sense to split the search by string size more. There
434435
// are a few outliers, most call names are small.
435-
OS << "#ifdef GET_LOOKUP_LIBCALL_IMPL_NAME_BODY\n"
436-
" size_t Size = Name.size();\n"
437-
" if (Size == 0 || Size > "
438-
<< MaxFuncNameSize
439-
<< ")\n"
440-
" return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);\n"
441-
" return lookupLibcallImplNameImpl(Name);\n"
442-
"#endif\n";
436+
{
437+
IfDefEmitter IfDef(OS, "GET_LOOKUP_LIBCALL_IMPL_NAME_BODY");
438+
439+
OS << " size_t Size = Name.size();\n"
440+
" if (Size == 0 || Size > "
441+
<< MaxFuncNameSize
442+
<< ")\n"
443+
" return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);\n"
444+
" return lookupLibcallImplNameImpl(Name);\n";
445+
}
443446

444447
auto [Size, Collisions] = computePerfectHashParameters(Hashes);
445448
std::vector<unsigned> Lookup =
@@ -449,7 +452,7 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
449452
LLVM_DEBUG(dbgs() << "Runtime libcall perfect hashing parameters: Size = "
450453
<< Size << ", maximum collisions = " << Collisions << '\n');
451454

452-
OS << "#ifdef DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME\n";
455+
IfDefEmitter IfDef(OS, "DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME");
453456
emitHashFunction(OS);
454457

455458
OS << "iota_range<RTLIB::LibcallImpl> RTLIB::RuntimeLibcallsInfo::"
@@ -481,59 +484,57 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
481484
return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);
482485
}
483486
)";
484-
485-
OS << "#endif\n\n";
486487
}
487488

488489
void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
489490
raw_ostream &OS) const {
490-
OS << "#ifdef GET_INIT_RUNTIME_LIBCALL_NAMES\n";
491-
492491
// Emit the implementation names
493492
StringToOffsetTable Table(/*AppendZero=*/true,
494493
"RTLIB::RuntimeLibcallsInfo::");
495494

496-
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
497-
Table.GetOrAddStringOffset(LibCallImpl.getLibcallFuncName());
495+
{
496+
IfDefEmitter IfDef(OS, "GET_INIT_RUNTIME_LIBCALL_NAMES");
497+
498+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
499+
Table.GetOrAddStringOffset(LibCallImpl.getLibcallFuncName());
498500

499-
Table.EmitStringTableDef(OS, "RuntimeLibcallImplNameTable");
500-
OS << R"(
501+
Table.EmitStringTableDef(OS, "RuntimeLibcallImplNameTable");
502+
OS << R"(
501503
const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
502504
)";
503505

504-
OS << formatv(" {}, // {}\n", Table.GetStringOffset(""),
505-
""); // Unsupported entry
506-
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
507-
StringRef ImplName = LibCallImpl.getLibcallFuncName();
508-
OS << formatv(" {}, // {}\n", Table.GetStringOffset(ImplName), ImplName);
509-
}
510-
OS << "};\n";
506+
OS << formatv(" {}, // {}\n", Table.GetStringOffset(""),
507+
""); // Unsupported entry
508+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
509+
StringRef ImplName = LibCallImpl.getLibcallFuncName();
510+
OS << formatv(" {}, // {}\n", Table.GetStringOffset(ImplName), ImplName);
511+
}
512+
OS << "};\n";
511513

512-
OS << R"(
514+
OS << R"(
513515
const uint8_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameSizeTable[] = {
514516
)";
515517

516-
OS << " 0,\n";
517-
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
518-
OS << " " << LibCallImpl.getLibcallFuncName().size() << ",\n";
519-
OS << "};\n\n";
518+
OS << " 0,\n";
519+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
520+
OS << " " << LibCallImpl.getLibcallFuncName().size() << ",\n";
521+
OS << "};\n\n";
520522

521-
// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
522-
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"
523-
"ImplToLibcall[RTLIB::NumLibcallImpls] = {\n"
524-
" RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported\n";
523+
// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
524+
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"
525+
"ImplToLibcall[RTLIB::NumLibcallImpls] = {\n"
526+
" RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported\n";
525527

526-
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
527-
const RuntimeLibcall *Provides = LibCallImpl.getProvides();
528-
OS << " ";
529-
Provides->emitEnumEntry(OS);
530-
OS << ", // ";
531-
LibCallImpl.emitEnumEntry(OS);
532-
OS << '\n';
528+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
529+
const RuntimeLibcall *Provides = LibCallImpl.getProvides();
530+
OS << " ";
531+
Provides->emitEnumEntry(OS);
532+
OS << ", // ";
533+
LibCallImpl.emitEnumEntry(OS);
534+
OS << '\n';
535+
}
536+
OS << "};\n\n";
533537
}
534-
OS << "};\n\n";
535-
536-
OS << "#endif\n\n";
537538

538539
emitNameMatchHashTable(OS, Table);
539540
}
@@ -757,9 +758,10 @@ void RuntimeLibcallEmitter::run(raw_ostream &OS) {
757758

758759
emitGetInitRuntimeLibcallNames(OS);
759760

760-
OS << "#ifdef GET_SET_TARGET_RUNTIME_LIBCALL_SETS\n";
761-
emitSystemRuntimeLibrarySetCalls(OS);
762-
OS << "#endif\n\n";
761+
{
762+
IfDefEmitter IfDef(OS, "GET_SET_TARGET_RUNTIME_LIBCALL_SETS");
763+
emitSystemRuntimeLibrarySetCalls(OS);
764+
}
763765
}
764766

765767
void LibcallPredicateExpander::expand(SetTheory &ST, const Record *Def,

0 commit comments

Comments
 (0)