// Part of the Carbon Language project, under the Apache License v2.0 with LLVM // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #ifndef CARBON_TESTING_BASE_BENCHMARK_HELPERS_H_ #define CARBON_TESTING_BASE_BENCHMARK_HELPERS_H_ #include namespace Carbon::Testing { namespace Internal { // A value that can go directly into a `"+r"` inline assembly constraint. template concept RegisterValue = !std::is_const_v && (std::is_integral_v || std::is_enum_v || std::is_pointer_v) && sizeof(T) <= sizeof(void*); // A container whose `data()` returns a pointer to its contents. template concept DataContainer = requires(T& container) { requires std::is_pointer_v; }; } // namespace Internal // Keeps the compiler from optimizing based on `value`, without forcing it out // of registers. // // Use this instead of `benchmark::DoNotOptimize`, whose `"+r,m"` constraint // Clang implements by storing the value to the stack and loading it back. For // a value carried between loop iterations, that puts a store and a reload on // the loop's critical path. // // Only types that can be handled entirely in registers are accepted, and any // other type is a compile error: // // - Integers, enums, and pointers go directly into a `"+r"` constraint, so the // compiler must assume the call reads and may change `value`. // - Containers pass their `data()` pointer to the first form. // // Both forms also clobber `"memory"`, so the compiler must assume the call // reads and writes any memory reachable through a pointer passed to it. That // holds even for a copy of a pointer, such as the result of `data()`: the copy // itself is discarded, but the memory it points to must be computed before the // call and can't be assumed unchanged after it. // // The container form doesn't keep the compiler from optimizing based on the // container's size. To block that too, pass the container's address instead, // which makes the whole container reachable. // // TODO: Add forms for more types, such as floating-point values, when // benchmarks need them, keeping each one in registers. template requires Internal::RegisterValue> [[clang::always_inline]] inline auto DoNotOptimize(T&& value) -> void { __asm__ volatile("" : "+r"(value) : : "memory"); } template requires Internal::DataContainer> [[clang::always_inline]] inline auto DoNotOptimize(T&& container) -> void { DoNotOptimize(container.data()); } // Returns true if the benchmark binary was invoked in dry-run mode // (`--benchmark_dry_run` or `BENCHMARK_DRY_RUN`). // // Google Benchmark's `--benchmark_dry_run` caps `state.max_iterations` to `1`, // so benchmarks that use `state.KeepRunningBatch(n)` or multi-input setup still // execute a full batch of `n` unless they also clamp `n` on dry runs. auto IsBenchmarkDryRun() -> bool; // Sets whether the benchmark binary is running in dry-run mode. // // This function must only be called once for an execution, and before any // callers to `IsBenchmarkDryRun`. Typically, it is called from within `main`. auto SetBenchmarkDryRun(bool is_dry_run) -> void; } // namespace Carbon::Testing #endif // CARBON_TESTING_BASE_BENCHMARK_HELPERS_H_