Files
Schrodinger ZHU Yifan aca3f14c0d [libc][benchmark] allow benchmark to be built without llvm support (#200951)
Update benchmark to allow it to be built in runtimes driven mode. 

It is still useful to have an in-tree benchmark suite. On my side, I am
working on some allocators related benchmarks. Due to the broken state
of this in-tree benchmark suite, I have to frequently port changes
between two workspace. Also, having benchmark suite can make it easier
to track performance change in a consistent view.

Assisted-by: AI tools, checked manually
2026-06-25 19:16:08 +00:00

82 lines
2.4 KiB
C++

//===-- Benchmark function -----------------------------------------------===//
//
// 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 "LibcBenchmark.h"
#include "llvm/ADT/StringRef.h"
#ifdef LIBC_BENCHMARKS_HAS_LLVM_SUPPORT
#include "llvm/TargetParser/Host.h"
#endif
namespace llvm {
namespace libc_benchmarks {
void checkRequirements() {
const auto &CpuInfo = benchmark::CPUInfo::Get();
if (CpuInfo.scaling == benchmark::CPUInfo::ENABLED)
report_fatal_error(
"CPU scaling is enabled, the benchmark real time measurements may be "
"noisy and will incur extra overhead.");
}
HostState HostState::get() {
const auto &cpu_info = benchmark::CPUInfo::Get();
HostState h;
h.cpu_frequency = cpu_info.cycles_per_second;
#ifdef LIBC_BENCHMARKS_HAS_LLVM_SUPPORT
h.cpu_name = llvm::sys::getHostCPUName().str();
#else
h.cpu_name = "";
#endif
for (const auto &benchmark_cache_info : cpu_info.caches) {
CacheInfo ci;
ci.type = benchmark_cache_info.type;
ci.level = benchmark_cache_info.level;
ci.size = benchmark_cache_info.size;
ci.num_sharing = benchmark_cache_info.num_sharing;
h.caches.push_back(std::move(ci));
}
return h;
}
} // namespace libc_benchmarks
} // namespace llvm
#ifndef LIBC_BENCHMARKS_HAS_LLVM_SUPPORT
#include "llvm/ADT/Twine.h"
#include <cstdlib>
#include <iostream>
#include <string_view>
#if __has_include(<execinfo.h>)
#include <execinfo.h>
#include <unistd.h>
#endif
namespace llvm {
[[noreturn]] void report_fatal_error(std::string_view reason,
bool gen_crash_diag) {
std::cerr << "Fatal error: " << reason << std::endl;
#if __has_include(<execinfo.h>)
if (gen_crash_diag) {
void *buffer[64]{};
int nptrs = ::backtrace(buffer, 64);
::backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
}
#endif
std::abort();
}
[[noreturn]] void report_fatal_error(StringRef reason, bool gen_crash_diag) {
report_fatal_error(std::string_view(reason.data(), reason.size()),
gen_crash_diag);
}
[[noreturn]] void report_fatal_error(const Twine &reason, bool gen_crash_diag) {
report_fatal_error("(unknown twine error)", gen_crash_diag);
}
} // namespace llvm
#endif