Files
llvm-project/llvm/benchmarks/RuntimeLibcalls.cpp
Reid Kleckner f3efbce4a7 [llvm] Move data layout string computation to TargetParser (#157612)
Clang and other frontends generally need the LLVM data layout string in
order to generate LLVM IR modules for LLVM. MLIR clients often need it
as well, since MLIR users often lower to LLVM IR.

Before this change, the LLVM datalayout string was computed in the
LLVM${TGT}CodeGen library in the relevant TargetMachine subclass.
However, none of the logic for computing the data layout string requires
any details of code generation. Clients who want to avoid duplicating
this information were forced to link in LLVMCodeGen and all registered
targets, leading to bloated binaries. This happened in PR #145899,
which measurably increased binary size for some of our users.

By moving this information to the TargetParser library, we
can delete the duplicate datalayout strings in Clang, and retain the
ability to generate IR for unregistered targets.

This is intended to be a very mechanical LLVM-only change, but there is
an immediately obvious follow-up to clang, which will be prepared
separately.

The vast majority of data layouts are computable with two inputs: the
triple and the "ABI name". There is only one exception, NVPTX, which has
a cl::opt to enable short device pointers. I invented a "shortptr" ABI
name to pass this option through the target independent interface.
Everything else fits. Mips is a bit awkward because it uses a special
MipsABIInfo abstraction, which includes members with codegen-like
concepts like ABI physical registers that can't live in TargetParser. I
think the string logic of looking for "n32" "n64" etc is reasonable to
duplicate. We have plenty of other minor duplication to preserve
layering.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2025-09-11 11:05:29 -07:00

119 lines
3.9 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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())
Names[LC] = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LC);
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;
}
#ifdef SYMBOL_TEST_DATA_FILE
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);
DataLayout DL(HostTriple.computeDataLayout());
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;
}
#endif
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());
}
}
}
#ifdef SYMBOL_TEST_DATA_FILE
// 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());
}
}
}
#endif
BENCHMARK(BM_LookupRuntimeLibcallByNameKnownCalls);
BENCHMARK(BM_LookupRuntimeLibcallByNameRandomCalls);
#ifdef SYMBOL_TEST_DATA_FILE
BENCHMARK(BM_LookupRuntimeLibcallByNameSampleData);
#endif
BENCHMARK_MAIN();