mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 20:42:39 +00:00
Clang implements the `"+r,m"` constraint in `benchmark::DoNotOptimize` by always choosing memory, so each call stores the value to the stack and loads it back. Most of our benchmarks call it on a loop counter or another value carried to the next iteration, which puts that store and reload on the loop's critical path. On an Apple M1, the cost of that round trip depends on which register the compiler uses to address the stack slot, and unrelated code changes move it. Changing only that register from `x29` to `sp`, with the same address, made `BM_SetLookupHitPtr<Set<int>>` 41.5% to 49.1% slower. Add `Carbon::Testing::DoNotOptimize` in `//testing/base:benchmark_helpers`, and switch every benchmark to it. It only accepts types it can keep in registers: integers, enums, and pointers go into a `"+r"` constraint with a `"memory"` clobber, and containers pass their `data()` pointer to that form, so the compiler must assume the call reads and writes their contents. Any other type is a compile error. The container form doesn't block optimizing based on a container's size; passing the container's address does. Benchmarks that pass a loop counter or carried value no longer measure a store and reload per iteration, so their numbers aren't comparable with earlier runs. For example, the hashing latency benchmarks no longer include one in each hash's latency. Assisted-by: Claude Code
127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
# 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
|
|
#
|
|
# Benchmarking infrastructure as well as comprehensive benchmarks across the
|
|
# tolochain. Infrastructure libraries may be depended on by focused benchmarks
|
|
# in various other parts of the toolchain.
|
|
|
|
load("@rules_shell//shell:sh_test.bzl", "sh_test")
|
|
load("//bazel/cc_rules:defs.bzl", "cc_binary", "cc_library", "cc_test")
|
|
load("//bazel/cc_toolchains:defs.bzl", "cc_env")
|
|
|
|
package(default_visibility = ["//visibility:public"])
|
|
|
|
cc_library(
|
|
name = "source_gen_lib",
|
|
testonly = 1,
|
|
srcs = ["source_gen.cpp"],
|
|
hdrs = ["source_gen.h"],
|
|
deps = [
|
|
"//common:check",
|
|
"//common:map",
|
|
"//common:raw_string_ostream",
|
|
"//common:set",
|
|
"//toolchain/lex:token_kind",
|
|
"@abseil-cpp//absl/random",
|
|
"@llvm-project//llvm:Support",
|
|
],
|
|
)
|
|
|
|
cc_test(
|
|
name = "source_gen_test",
|
|
size = "small",
|
|
srcs = ["source_gen_test.cpp"],
|
|
deps = [
|
|
":source_gen_lib",
|
|
"//common:all_llvm_targets",
|
|
"//common:set",
|
|
"//testing/base:global_exe_path",
|
|
"//testing/base:gtest_main",
|
|
"//toolchain/base:install_paths_test_helpers",
|
|
"//toolchain/driver",
|
|
"@googletest//:gtest",
|
|
"@llvm-project//llvm:Support",
|
|
],
|
|
)
|
|
|
|
cc_binary(
|
|
name = "source_gen",
|
|
testonly = 1,
|
|
srcs = ["source_gen_main.cpp"],
|
|
deps = [
|
|
":source_gen_lib",
|
|
"//common:bazel_working_dir",
|
|
"//common:command_line",
|
|
"//common:init_llvm",
|
|
"//common:ostream",
|
|
"@llvm-project//llvm:Support",
|
|
],
|
|
)
|
|
|
|
cc_binary(
|
|
name = "compile_benchmark",
|
|
testonly = 1,
|
|
srcs = ["compile_benchmark.cpp"],
|
|
# The subprocess benchmarks execute the installed toolchain binaries.
|
|
data = ["//toolchain/install:install_data"],
|
|
deps = [
|
|
":source_gen_lib",
|
|
"//common:all_llvm_targets",
|
|
"//common:filesystem",
|
|
"//testing/base:benchmark_helpers",
|
|
"//testing/base:benchmark_main",
|
|
"//testing/base:global_exe_path",
|
|
"//toolchain/base:install_paths",
|
|
"//toolchain/base:install_paths_test_helpers",
|
|
"//toolchain/driver",
|
|
"//toolchain/driver:clang_runner",
|
|
"//toolchain/testing:compile_helper",
|
|
"@google_benchmark//:benchmark",
|
|
"@llvm-project//llvm:Support",
|
|
],
|
|
)
|
|
|
|
sh_test(
|
|
name = "compile_benchmark_test",
|
|
size = "small",
|
|
srcs = [":compile_benchmark"],
|
|
args = [
|
|
"--benchmark_dry_run",
|
|
# Dry-run the smallest (256-line) case of every benchmark. Also run just
|
|
# the lex benchmark that executes a subprocess to reduce the overhead
|
|
# and reduce timeouts. The `$$` is repeated for Bazel escaping of `$`.
|
|
"--benchmark_filter=^(BM_CompileApi.*/256|BM_CompileExec.*Phase::Lex.*/256/real_time)$$",
|
|
],
|
|
env = cc_env(),
|
|
)
|
|
|
|
cc_binary(
|
|
name = "prelude_benchmark",
|
|
testonly = 1,
|
|
srcs = ["prelude_benchmark.cpp"],
|
|
deps = [
|
|
"//common:all_llvm_targets",
|
|
"//common:check",
|
|
"//testing/base:benchmark_helpers",
|
|
"//testing/base:benchmark_main",
|
|
"//testing/base:global_exe_path",
|
|
"//toolchain/base:install_paths",
|
|
"//toolchain/base:install_paths_test_helpers",
|
|
"//toolchain/base:mem_usage",
|
|
"//toolchain/driver",
|
|
"@google_benchmark//:benchmark",
|
|
"@llvm-project//llvm:Support",
|
|
],
|
|
)
|
|
|
|
sh_test(
|
|
name = "prelude_benchmark_test",
|
|
size = "small",
|
|
srcs = [":prelude_benchmark"],
|
|
# No `--benchmark_filter` is needed (unlike `compile_benchmark_test`): the
|
|
# benchmarks aren't `Range()`-parameterized, so a dry run is already cheap.
|
|
args = ["--benchmark_dry_run"],
|
|
env = cc_env(),
|
|
)
|