-
Notifications
You must be signed in to change notification settings - Fork 15.5k
TableGen: Emit static hash table for runtime libcalls #150192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arsenm
merged 7 commits into
main
from
users/arsenm/tablegen/emit-perfect-hash-table-runtime-libcalls
Aug 15, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
904e7a4
TableGen: Emit perfect hash function for runtime libcalls
arsenm bb16958
Use StringLiteral and constexpr
arsenm 96c279f
loop suggestion
arsenm 67e1dbd
Fix bad constexpr
arsenm 51e3a1e
Remove llvm_unreachable
arsenm 8896713
RuntimeLibcalls: Improve unit test
arsenm 1490210
fix clang-format
arsenm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/IR/RuntimeLibcalls.h" | ||
| #include "benchmark/benchmark.h" | ||
| #include "llvm/IR/DataLayout.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "llvm/Support/LineIterator.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
| #include "llvm/TargetParser/Triple.h" | ||
| #include <random> | ||
| #include <string> | ||
| using namespace llvm; | ||
|
|
||
| static constexpr unsigned MaxFuncNameSize = 53; | ||
|
|
||
| static std::vector<StringRef> getLibcallNameStringRefs() { | ||
| std::vector<StringRef> Names(RTLIB::NumLibcallImpls); | ||
| // Keep the strlens on the StringRef construction out of the benchmark loop. | ||
| for (RTLIB::LibcallImpl LC : RTLIB::libcall_impls()) { | ||
| const char *Name = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC); | ||
| Names[LC] = StringRef(Name); | ||
| } | ||
|
|
||
| return Names; | ||
| } | ||
|
|
||
| static std::vector<std::string> getRandomFuncNames() { | ||
| std::mt19937_64 Rng; | ||
| std::uniform_int_distribution<> StringLengthDistribution(1, MaxFuncNameSize); | ||
| std::uniform_int_distribution<> CharDistribution(1, 255); | ||
| int NumTestFuncs = 1 << 10; | ||
| std::vector<std::string> TestFuncNames(NumTestFuncs); | ||
|
|
||
| for (std::string &TestFuncName : TestFuncNames) { | ||
| for (int I = 0, E = StringLengthDistribution(Rng); I != E; ++I) | ||
| TestFuncName += static_cast<char>(CharDistribution(Rng)); | ||
| } | ||
|
|
||
| return TestFuncNames; | ||
| } | ||
|
|
||
| static std::vector<std::string> readSymbolsFromFile(StringRef InputFile) { | ||
| auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFile, /*IsText=*/true); | ||
| if (!BufOrError) { | ||
| reportFatalUsageError("failed to open \'" + Twine(InputFile) + | ||
| "\': " + BufOrError.getError().message()); | ||
| } | ||
|
|
||
| // Hackily figure out if there's a prefix on the symbol names - llvm-nm | ||
| // appears to not have a flag to skip this. | ||
| llvm::Triple HostTriple(LLVM_HOST_TRIPLE); | ||
| std::string DummyDatalayout = "e"; | ||
| DummyDatalayout += DataLayout::getManglingComponent(HostTriple); | ||
|
|
||
| DataLayout DL(DummyDatalayout); | ||
| char GlobalPrefix = DL.getGlobalPrefix(); | ||
|
|
||
| std::vector<std::string> Lines; | ||
| for (line_iterator LineIt(**BufOrError, /*SkipBlanks=*/true); | ||
| !LineIt.is_at_eof(); ++LineIt) { | ||
| StringRef SymbolName = *LineIt; | ||
| SymbolName.consume_front(StringRef(&GlobalPrefix, 1)); | ||
|
|
||
| Lines.push_back(SymbolName.str()); | ||
| } | ||
| return Lines; | ||
| } | ||
|
|
||
| static void BM_LookupRuntimeLibcallByNameKnownCalls(benchmark::State &State) { | ||
| std::vector<StringRef> Names = getLibcallNameStringRefs(); | ||
|
|
||
| for (auto _ : State) { | ||
| for (StringRef Name : Names) { | ||
| benchmark::DoNotOptimize( | ||
| RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName(Name).empty()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void BM_LookupRuntimeLibcallByNameRandomCalls(benchmark::State &State) { | ||
| std::vector<std::string> TestFuncNames = getRandomFuncNames(); | ||
|
|
||
| for (auto _ : State) { | ||
| for (const std::string &Name : TestFuncNames) { | ||
| benchmark::DoNotOptimize( | ||
| RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName(StringRef(Name)) | ||
| .empty()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // This isn't fully representative, it doesn't include any anonymous functions. | ||
| // nm -n --no-demangle --format=just-symbols sample-binary > sample.txt | ||
| static void BM_LookupRuntimeLibcallByNameSampleData(benchmark::State &State) { | ||
| std::vector<std::string> TestFuncNames = | ||
| readSymbolsFromFile(SYMBOL_TEST_DATA_FILE); | ||
| for (auto _ : State) { | ||
| for (const std::string &Name : TestFuncNames) { | ||
| benchmark::DoNotOptimize( | ||
| RTLIB::RuntimeLibcallsInfo::lookupLibcallImplName(StringRef(Name)) | ||
| .empty()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(BM_LookupRuntimeLibcallByNameKnownCalls); | ||
| BENCHMARK(BM_LookupRuntimeLibcallByNameRandomCalls); | ||
| BENCHMARK(BM_LookupRuntimeLibcallByNameSampleData); | ||
|
|
||
| BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File dependencies don't work well in CMake. I think this should be
and
DEPENDS llvm-nm llcCMake will recognize the names if they are existing targets and add proper dependencies (COMMAND should add target-level dependencies, DEPENDS should add file-level dependencies). Not sure what llc dependency is for here.
It might still need a check if the target exists
if (TARGET llvm-nm AND TARGET llc).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the command part still needs to be TARGET_FILE, but yes the depends should be in target terms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a CMake guru, so I blindly trust the documentation: