Skip to content

Commit 7545fb4

Browse files
committed
TableGen: Use StringOffsetTable for RuntimeLibcall names
1 parent 20b47bf commit 7545fb4

File tree

6 files changed

+72
-31
lines changed

6 files changed

+72
-31
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/ArrayRef.h"
1818
#include "llvm/ADT/Sequence.h"
19+
#include "llvm/ADT/StringTable.h"
1920
#include "llvm/IR/CallingConv.h"
2021
#include "llvm/IR/InstrTypes.h"
2122
#include "llvm/Support/AtomicOrdering.h"
@@ -77,12 +78,16 @@ struct RuntimeLibcallsInfo {
7778
/// Get the libcall routine name for the specified libcall.
7879
// FIXME: This should be removed. Only LibcallImpl should have a name.
7980
const char *getLibcallName(RTLIB::Libcall Call) const {
80-
return LibCallImplNames[LibcallImpls[Call]];
81+
return getLibcallImplName(LibcallImpls[Call]);
8182
}
8283

8384
/// Get the libcall routine name for the specified libcall implementation.
85+
// FIXME: Change to return StringRef
8486
static const char *getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
85-
return LibCallImplNames[CallImpl];
87+
if (CallImpl == RTLIB::Unsupported)
88+
return nullptr;
89+
return RuntimeLibcallImplNameTable[RuntimeLibcallNameOffsetTable[CallImpl]]
90+
.data();
8691
}
8792

8893
/// Return the lowering's selection of implementation call for \p Call
@@ -144,7 +149,9 @@ struct RuntimeLibcallsInfo {
144149

145150
/// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
146151
/// SHL_I32
147-
LLVM_ABI static const char *const LibCallImplNames[RTLIB::NumLibcallImpls];
152+
LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
153+
LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
154+
LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
148155

149156
/// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
150157
LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];

llvm/include/llvm/TableGen/StringToOffsetTable.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ namespace llvm {
2323
class StringToOffsetTable {
2424
StringMap<unsigned> StringOffset;
2525
std::string AggregateString;
26+
27+
/// If this is to be a static class member, the prefix to use (i.e. class name
28+
/// plus ::)
29+
const StringRef ClassPrefix;
2630
const bool AppendZero;
2731

2832
public:
29-
StringToOffsetTable(bool AppendZero = true) : AppendZero(AppendZero) {
33+
StringToOffsetTable(bool AppendZero = true, StringRef ClassPrefix = "")
34+
: ClassPrefix(ClassPrefix), AppendZero(AppendZero) {
3035
// Ensure we always put the empty string at offset zero. That lets empty
3136
// initialization also be zero initialization for offsets into the table.
3237
GetOrAddStringOffset("");

llvm/lib/IR/RuntimeLibcalls.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/IR/RuntimeLibcalls.h"
10+
#include "llvm/ADT/StringTable.h"
1011

1112
using namespace llvm;
1213
using namespace RTLIB;

llvm/lib/TableGen/StringToOffsetTable.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS,
3838
#pragma GCC diagnostic push
3939
#pragma GCC diagnostic ignored "-Woverlength-strings"
4040
#endif
41-
static constexpr char {}Storage[] = )",
42-
Name);
41+
{} constexpr char {}{}Storage[] = )",
42+
ClassPrefix.empty() ? "static" : "", ClassPrefix, Name);
4343

4444
// MSVC silently miscompiles string literals longer than 64k in some
4545
// circumstances. The build system sets EmitLongStrLiterals to false when it
@@ -83,10 +83,10 @@ static constexpr char {}Storage[] = )",
8383
#pragma GCC diagnostic pop
8484
#endif
8585
86-
static constexpr llvm::StringTable
87-
{0} = {0}Storage;
86+
{1}constexpr llvm::StringTable
87+
{2}{0} = {0}Storage;
8888
)",
89-
Name);
89+
Name, ClassPrefix.empty() ? "static " : "", ClassPrefix);
9090
}
9191

9292
void StringToOffsetTable::EmitString(raw_ostream &O) const {

llvm/test/TableGen/RuntimeLibcallEmitter.td

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,40 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
116116
// CHECK-NEXT: RTLIB::Unsupported
117117
// CHECK-NEXT: };
118118
// CHECK-EMPTY:
119-
// CHECK-NEXT: const char *const llvm::RTLIB::RuntimeLibcallsInfo::LibCallImplNames[RTLIB::NumLibcallImpls] = {
120-
// CHECK-NEXT: nullptr, // RTLIB::Unsupported
121-
// CHECK-NEXT: "___memcpy", // RTLIB::___memcpy
122-
// CHECK-NEXT: "___memset", // RTLIB::___memset
123-
// CHECK-NEXT: "__ashlsi3", // RTLIB::__ashlsi3
124-
// CHECK-NEXT: "__lshrdi3", // RTLIB::__lshrdi3
125-
// CHECK-NEXT: "bzero", // RTLIB::bzero
126-
// CHECK-NEXT: "calloc", // RTLIB::calloc
127-
// CHECK-NEXT: "sqrtl", // RTLIB::sqrtl_f80
128-
// CHECK-NEXT: "sqrtl", // RTLIB::sqrtl_f128
119+
// CHECK-EMPTY:
120+
// CHECK-NEXT: #ifdef __GNUC__
121+
// CHECK-NEXT: #pragma GCC diagnostic push
122+
// CHECK-NEXT: #pragma GCC diagnostic ignored "-Woverlength-strings"
123+
// CHECK-NEXT: #endif
124+
// CHECK-NEXT: constexpr char RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTableStorage[] =
125+
// CHECK-NEXT: "\0"
126+
// CHECK-NEXT: "___memcpy\0"
127+
// CHECK-NEXT: "___memset\0"
128+
// CHECK-NEXT: "__ashlsi3\0"
129+
// CHECK-NEXT: "__lshrdi3\0"
130+
// CHECK-NEXT: "bzero\0"
131+
// CHECK-NEXT: "calloc\0"
132+
// CHECK-NEXT: "sqrtl\0"
133+
// CHECK-NEXT: ;
134+
// CHECK-NEXT: #ifdef __GNUC__
135+
// CHECK-NEXT: #pragma GCC diagnostic pop
136+
// CHECK-NEXT: #endif
137+
// CHECK-EMPTY:
138+
// CHECK-NEXT: constexpr llvm::StringTable
139+
// CHECK-NEXT: RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTable = RuntimeLibcallImplNameTableStorage;
140+
// CHECK-EMPTY:
141+
// CHECK-NEXT: const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
142+
// CHECK-NEXT: 0, //
143+
// CHECK-NEXT: 1, // ___memcpy
144+
// CHECK-NEXT: 11, // ___memset
145+
// CHECK-NEXT: 21, // __ashlsi3
146+
// CHECK-NEXT: 31, // __lshrdi3
147+
// CHECK-NEXT: 41, // bzero
148+
// CHECK-NEXT: 47, // calloc
149+
// CHECK-NEXT: 54, // sqrtl
150+
// CHECK-NEXT: 54, // sqrtl
129151
// CHECK-NEXT: };
130-
131-
// CHECK: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
152+
// CHECK-NEXT: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
132153
// CHECK-NEXT: RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported
133154
// CHECK-NEXT: RTLIB::MEMCPY, // RTLIB::___memcpy
134155
// CHECK-NEXT: RTLIB::MEMSET, // RTLIB::___memset

llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
#include "llvm/ADT/StringRef.h"
1010
#include "llvm/Support/Debug.h"
11+
#include "llvm/Support/FormatVariadic.h"
1112
#include "llvm/Support/raw_ostream.h"
1213
#include "llvm/TableGen/Error.h"
1314
#include "llvm/TableGen/Record.h"
1415
#include "llvm/TableGen/SetTheory.h"
16+
#include "llvm/TableGen/StringToOffsetTable.h"
1517
#include "llvm/TableGen/TableGenBackend.h"
1618

1719
using namespace llvm;
@@ -305,8 +307,6 @@ void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
305307

306308
void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
307309
raw_ostream &OS) const {
308-
// TODO: Emit libcall names as string offset table.
309-
310310
OS << "const RTLIB::LibcallImpl "
311311
"llvm::RTLIB::RuntimeLibcallsInfo::"
312312
"DefaultLibcallImpls[RTLIB::UNKNOWN_LIBCALL + 1] = {\n";
@@ -331,17 +331,24 @@ void RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames(
331331
"};\n\n";
332332

333333
// Emit the implementation names
334-
OS << "const char *const llvm::RTLIB::RuntimeLibcallsInfo::"
335-
"LibCallImplNames[RTLIB::NumLibcallImpls] = {\n"
336-
" nullptr, // RTLIB::Unsupported\n";
334+
StringToOffsetTable Table(/*AppendZero=*/true,
335+
"RTLIB::RuntimeLibcallsInfo::");
336+
337+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
338+
Table.GetOrAddStringOffset(LibCallImpl.getLibcallFuncName());
339+
340+
Table.EmitStringTableDef(OS, "RuntimeLibcallImplNameTable");
341+
OS << R"(
342+
const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
343+
)";
337344

345+
OS << formatv(" {}, // {}\n", Table.GetStringOffset(""),
346+
""); // Unsupported entry
338347
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
339-
OS << " \"" << LibCallImpl.getLibcallFuncName() << "\", // ";
340-
LibCallImpl.emitEnumEntry(OS);
341-
OS << '\n';
348+
StringRef ImplName = LibCallImpl.getLibcallFuncName();
349+
OS << formatv(" {}, // {}\n", Table.GetStringOffset(ImplName), ImplName);
342350
}
343-
344-
OS << "};\n\n";
351+
OS << "};\n";
345352

346353
// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
347354
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"

0 commit comments

Comments
 (0)