Skip to content

Commit b6af40f

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 b6af40f

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
@@ -461,15 +461,13 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
461461

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

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

474472
OS << '\n';
475473
}
@@ -480,11 +478,12 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
480478
<< ";\n\n"
481479
" for (int I = 0; I != "
482480
<< Collisions << R"(; ++I) {
483-
auto [Entry, StringSize] = HashTableNameToEnum[Idx + I];
481+
const uint16_t Entry = HashTableNameToEnum[Idx + I];
484482
const uint16_t StrOffset = RuntimeLibcallNameOffsetTable[Entry];
483+
const uint8_t StrSize = RuntimeLibcallNameSizeTable[Entry];
485484
StringRef Str(
486485
&RTLIB::RuntimeLibcallsInfo::RuntimeLibcallImplNameTableStorage[StrOffset],
487-
StringSize);
486+
StrSize);
488487
if (Str == Name)
489488
return libcallImplNameHit(Entry, StrOffset);
490489
}
@@ -520,6 +519,15 @@ const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = {
520519
}
521520
OS << "};\n";
522521

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

0 commit comments

Comments
 (0)