Skip to content

Commit 1d38b18

Browse files
committed
RuntimeLibcalls: Generate table of libcall name lengths
Avoids strlen when constructing the returned StringRef. We were emitting these in the libcall name lookup anyway, so split out the offsets for general use. Currently emitted as a separate table, not sure if it would be better to change the string offset table to store pairs of offset and width instead.
1 parent b38597a commit 1d38b18

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ struct RuntimeLibcallsInfo {
8585
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
8686
if (CallImpl == RTLIB::Unsupported)
8787
return StringRef();
88-
return RuntimeLibcallImplNameTable[RuntimeLibcallNameOffsetTable[CallImpl]];
88+
return StringRef(RuntimeLibcallImplNameTable.getCString(
89+
RuntimeLibcallNameOffsetTable[CallImpl]),
90+
RuntimeLibcallNameSizeTable[CallImpl]);
8991
}
9092

9193
/// Return the lowering's selection of implementation call for \p Call
@@ -182,6 +184,7 @@ struct RuntimeLibcallsInfo {
182184
LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
183185
LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
184186
LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
187+
LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
185188

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

llvm/test/TableGen/RuntimeLibcallEmitter.td

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
137137
// CHECK-NEXT: 54, // sqrtl
138138
// CHECK-NEXT: 54, // sqrtl
139139
// CHECK-NEXT: };
140+
// CHECK-EMPTY:
141+
// CHECK-NEXT: const uint8_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameSizeTable[] = {
142+
// CHECK-NEXT: 9,
143+
// CHECK-NEXT: 9,
144+
// CHECK-NEXT: 9,
145+
// CHECK-NEXT: 9,
146+
// CHECK-NEXT: 5,
147+
// CHECK-NEXT: 6,
148+
// CHECK-NEXT: 5,
149+
// CHECK-NEXT: 5,
150+
// CHECK-NEXT: };
151+
// CHECK-EMPTY:
140152
// CHECK-NEXT: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
141153
// CHECK-NEXT: RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported
142154
// CHECK-NEXT: RTLIB::MEMCPY, // RTLIB::___memcpy
@@ -162,11 +174,11 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
162174
// CHECK-NEXT: }
163175

164176
// CHECK: iota_range<RTLIB::LibcallImpl> RTLIB::RuntimeLibcallsInfo::lookupLibcallImplNameImpl(StringRef Name) {
165-
// CHECK: static constexpr std::pair<uint16_t, uint16_t> HashTableNameToEnum[16] = {
166-
// CHECK: {2, 9}, // 0x000000705301b8, ___memset
167-
// CHECK: {0, 0},
168-
// CHECK: {6, 6}, // 0x0000001417a2af, calloc
169-
// CHECK: {0, 0},
177+
// CHECK: static constexpr uint16_t HashTableNameToEnum[16] = {
178+
// CHECK: 2, // 0x000000705301b8, ___memset
179+
// CHECK: 0,
180+
// CHECK: 6, // 0x0000001417a2af, calloc
181+
// CHECK: 0,
170182
// CHECK: };
171183

172184
// CHECK: unsigned Idx = (hash(Name) % 8) * 2;

llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,13 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
463463

464464
// Emit pair of RTLIB::LibcallImpl, size of the string name. It's important to
465465
// avoid strlen on the string table entries.
466-
OS << " static constexpr std::pair<uint16_t, uint16_t> HashTableNameToEnum["
467-
<< Lookup.size() << "] = {\n";
466+
OS << " static constexpr uint16_t HashTableNameToEnum[" << Lookup.size()
467+
<< "] = {\n";
468468

469469
for (auto [FuncName, Hash, TableVal] : Lookup) {
470-
OS << " {" << TableVal << ", " << FuncName.size() << "},";
471-
472-
if (TableVal != 0) {
470+
OS << " " << TableVal << ',';
471+
if (TableVal != 0)
473472
OS << " // " << format_hex(Hash, 16) << ", " << FuncName;
474-
}
475473

476474
OS << '\n';
477475
}
@@ -482,11 +480,12 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
482480
<< ";\n\n"
483481
" for (int I = 0; I != "
484482
<< Collisions << R"(; ++I) {
485-
auto [Entry, StringSize] = HashTableNameToEnum[Idx + I];
483+
const uint16_t Entry = HashTableNameToEnum[Idx + I];
486484
const uint16_t StrOffset = RuntimeLibcallNameOffsetTable[Entry];
485+
const uint8_t StrSize = RuntimeLibcallNameSizeTable[Entry];
487486
StringRef Str(
488487
&RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTableStorage[StrOffset],
489-
StringSize);
488+
StrSize);
490489
if (Str == Name)
491490
return libcallImplNameHit(Entry, StrOffset);
492491
}
@@ -522,6 +521,15 @@ const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
522521
}
523522
OS << "};\n";
524523

524+
OS << R"(
525+
const uint8_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameSizeTable[] = {
526+
)";
527+
528+
OS << " 0,\n";
529+
for (const RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
530+
OS << " " << LibCallImpl.getLibcallFuncName().size() << ",\n";
531+
OS << "};\n\n";
532+
525533
// Emit the reverse mapping from implementation libraries to RTLIB::Libcall
526534
OS << "const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::"
527535
"ImplToLibcall[RTLIB::NumLibcallImpls] = {\n"

0 commit comments

Comments
 (0)