Skip to content

Commit f0c374c

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 cb1228f commit f0c374c

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ 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: 0,
143+
// CHECK-NEXT: 9,
144+
// CHECK-NEXT: 9,
145+
// CHECK-NEXT: 9,
146+
// CHECK-NEXT: 9,
147+
// CHECK-NEXT: 5,
148+
// CHECK-NEXT: 6,
149+
// CHECK-NEXT: 5,
150+
// CHECK-NEXT: 5,
151+
// CHECK-NEXT: };
152+
// CHECK-EMPTY:
140153
// CHECK-NEXT: const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::ImplToLibcall[RTLIB::NumLibcallImpls] = {
141154
// CHECK-NEXT: RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported
142155
// CHECK-NEXT: RTLIB::MEMCPY, // RTLIB::___memcpy
@@ -162,11 +175,11 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, (add calloc, LibraryWithCondi
162175
// CHECK-NEXT: }
163176

164177
// 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},
178+
// CHECK: static constexpr uint16_t HashTableNameToEnum[16] = {
179+
// CHECK: 2, // 0x000000705301b8, ___memset
180+
// CHECK: 0,
181+
// CHECK: 6, // 0x0000001417a2af, calloc
182+
// CHECK: 0,
170183
// CHECK: };
171184

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

llvm/utils/TableGen/Basic/RuntimeLibcallsEmitter.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,17 +459,14 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
459459
OS << "iota_range<RTLIB::LibcallImpl> RTLIB::RuntimeLibcallsInfo::"
460460
"lookupLibcallImplNameImpl(StringRef Name) {\n";
461461

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

467466
for (auto [FuncName, Hash, TableVal] : Lookup) {
468-
OS << " {" << TableVal << ", " << FuncName.size() << "},";
469-
470-
if (TableVal != 0) {
467+
OS << " " << TableVal << ',';
468+
if (TableVal != 0)
471469
OS << " // " << format_hex(Hash, 16) << ", " << FuncName;
472-
}
473470

474471
OS << '\n';
475472
}
@@ -480,11 +477,12 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
480477
<< ";\n\n"
481478
" for (int I = 0; I != "
482479
<< Collisions << R"(; ++I) {
483-
auto [Entry, StringSize] = HashTableNameToEnum[Idx + I];
480+
const uint16_t Entry = HashTableNameToEnum[Idx + I];
484481
const uint16_t StrOffset = RuntimeLibcallNameOffsetTable[Entry];
482+
const uint8_t StrSize = RuntimeLibcallNameSizeTable[Entry];
485483
StringRef Str(
486484
&RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTableStorage[StrOffset],
487-
StringSize);
485+
StrSize);
488486
if (Str == Name)
489487
return libcallImplNameHit(Entry, StrOffset);
490488
}
@@ -520,6 +518,15 @@ const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
520518
}
521519
OS << "};\n";
522520

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

0 commit comments

Comments
 (0)