From e041afd98d4bbecfc67c2fc07dbd49ae7884ea02 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Fri, 15 May 2026 13:17:23 -0700 Subject: [PATCH] Centralize benchmarking infrastructure and the toolchain-wide benchmarks (#7212) The benchmarks themselves aren't really specific to `driver`. Keeping the source generation near to the primary use case of benchmarking also seems like a more discoverable location. I feel a little bad doing this reorganization right after I gave a talk with links to a bunch of this code, but seems good to reorganize a bit before doing some work to extend things now that we have full standard library support for C++ benchmarking and other improvements. Assisted-by: Antigravity with Gemini --- testing/base/BUILD | 49 +--------- toolchain/benchmarking/BUILD | 89 +++++++++++++++++++ .../compile_benchmark.cpp | 2 +- .../benchmarking}/source_gen.cpp | 2 +- .../benchmarking}/source_gen.h | 6 +- .../benchmarking}/source_gen_main.cpp | 2 +- .../benchmarking}/source_gen_test.cpp | 2 +- toolchain/driver/BUILD | 33 +------ toolchain/lex/BUILD | 2 +- toolchain/lex/tokenized_buffer_benchmark.cpp | 2 +- 10 files changed, 100 insertions(+), 89 deletions(-) create mode 100644 toolchain/benchmarking/BUILD rename toolchain/{driver => benchmarking}/compile_benchmark.cpp (99%) rename {testing/base => toolchain/benchmarking}/source_gen.cpp (99%) rename {testing/base => toolchain/benchmarking}/source_gen.h (98%) rename {testing/base => toolchain/benchmarking}/source_gen_main.cpp (98%) rename {testing/base => toolchain/benchmarking}/source_gen_test.cpp (99%) diff --git a/testing/base/BUILD b/testing/base/BUILD index 85a9164e2c06..54c2c363769d 100644 --- a/testing/base/BUILD +++ b/testing/base/BUILD @@ -5,7 +5,7 @@ # Trivial, single-file testing libraries. More complex libraries should get # their own directory. -load("//bazel/cc_rules:defs.bzl", "cc_binary", "cc_library", "cc_test") +load("//bazel/cc_rules:defs.bzl", "cc_library", "cc_test") package(default_visibility = ["//visibility:public"]) @@ -56,53 +56,6 @@ cc_library( ], ) -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 = [ - ":global_exe_path", - ":gtest_main", - ":source_gen_lib", - "//common:all_llvm_targets", - "//common:set", - "//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_library( name = "file_helpers", testonly = 1, diff --git a/toolchain/benchmarking/BUILD b/toolchain/benchmarking/BUILD new file mode 100644 index 000000000000..36168bce70c1 --- /dev/null +++ b/toolchain/benchmarking/BUILD @@ -0,0 +1,89 @@ +# 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"], + deps = [ + ":source_gen_lib", + "//common:all_llvm_targets", + "//testing/base:benchmark_main", + "//testing/base:global_exe_path", + "//toolchain/base:install_paths_test_helpers", + "//toolchain/driver", + "//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", + # The `$$` is repeated for Bazel escaping of `$`. + "--benchmark_filter=/256$$", + ], + env = cc_env(), +) diff --git a/toolchain/driver/compile_benchmark.cpp b/toolchain/benchmarking/compile_benchmark.cpp similarity index 99% rename from toolchain/driver/compile_benchmark.cpp rename to toolchain/benchmarking/compile_benchmark.cpp index 1243e6025047..16be6667537e 100644 --- a/toolchain/driver/compile_benchmark.cpp +++ b/toolchain/benchmarking/compile_benchmark.cpp @@ -8,8 +8,8 @@ #include #include "testing/base/global_exe_path.h" -#include "testing/base/source_gen.h" #include "toolchain/base/install_paths_test_helpers.h" +#include "toolchain/benchmarking/source_gen.h" #include "toolchain/driver/driver.h" #include "toolchain/testing/compile_helper.h" diff --git a/testing/base/source_gen.cpp b/toolchain/benchmarking/source_gen.cpp similarity index 99% rename from testing/base/source_gen.cpp rename to toolchain/benchmarking/source_gen.cpp index 207ddb69c5a0..0f6212991a01 100644 --- a/testing/base/source_gen.cpp +++ b/toolchain/benchmarking/source_gen.cpp @@ -2,7 +2,7 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include "testing/base/source_gen.h" +#include "toolchain/benchmarking/source_gen.h" #include #include diff --git a/testing/base/source_gen.h b/toolchain/benchmarking/source_gen.h similarity index 98% rename from testing/base/source_gen.h rename to toolchain/benchmarking/source_gen.h index c7c7301c94e7..595ed02ca8c9 100644 --- a/testing/base/source_gen.h +++ b/toolchain/benchmarking/source_gen.h @@ -2,8 +2,8 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#ifndef CARBON_TESTING_BASE_SOURCE_GEN_H_ -#define CARBON_TESTING_BASE_SOURCE_GEN_H_ +#ifndef CARBON_TOOLCHAIN_BENCHMARKING_SOURCE_GEN_H_ +#define CARBON_TOOLCHAIN_BENCHMARKING_SOURCE_GEN_H_ #include @@ -296,4 +296,4 @@ class SourceGen { } // namespace Carbon::Testing -#endif // CARBON_TESTING_BASE_SOURCE_GEN_H_ +#endif // CARBON_TOOLCHAIN_BENCHMARKING_SOURCE_GEN_H_ diff --git a/testing/base/source_gen_main.cpp b/toolchain/benchmarking/source_gen_main.cpp similarity index 98% rename from testing/base/source_gen_main.cpp rename to toolchain/benchmarking/source_gen_main.cpp index 4c9a3d7fcdd8..b9cf4fbdd0c4 100644 --- a/testing/base/source_gen_main.cpp +++ b/toolchain/benchmarking/source_gen_main.cpp @@ -13,7 +13,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/FileSystem.h" -#include "testing/base/source_gen.h" +#include "toolchain/benchmarking/source_gen.h" namespace Carbon::Testing { namespace { diff --git a/testing/base/source_gen_test.cpp b/toolchain/benchmarking/source_gen_test.cpp similarity index 99% rename from testing/base/source_gen_test.cpp rename to toolchain/benchmarking/source_gen_test.cpp index f03e470e363a..6e11a19655e0 100644 --- a/testing/base/source_gen_test.cpp +++ b/toolchain/benchmarking/source_gen_test.cpp @@ -2,7 +2,7 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include "testing/base/source_gen.h" +#include "toolchain/benchmarking/source_gen.h" #include #include diff --git a/toolchain/driver/BUILD b/toolchain/driver/BUILD index f191ca885a33..3135252f5ee6 100644 --- a/toolchain/driver/BUILD +++ b/toolchain/driver/BUILD @@ -2,9 +2,7 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -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") +load("//bazel/cc_rules:defs.bzl", "cc_library", "cc_test") load("//testing/fuzzing:rules.bzl", "cc_fuzz_test") package(default_visibility = ["//visibility:public"]) @@ -113,35 +111,6 @@ cc_test( ], ) -cc_binary( - name = "compile_benchmark", - testonly = 1, - srcs = ["compile_benchmark.cpp"], - deps = [ - ":driver", - "//common:all_llvm_targets", - "//testing/base:benchmark_main", - "//testing/base:global_exe_path", - "//testing/base:source_gen_lib", - "//toolchain/base:install_paths_test_helpers", - "//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", - # The `$$` is repeated for Bazel escaping of `$`. - "--benchmark_filter=/256$$", - ], - env = cc_env(), -) - cc_library( name = "codegen_options", srcs = [ diff --git a/toolchain/lex/BUILD b/toolchain/lex/BUILD index 4da6d167ac6f..ca913c4d076f 100644 --- a/toolchain/lex/BUILD +++ b/toolchain/lex/BUILD @@ -326,8 +326,8 @@ cc_binary( "//common:check", "//common:raw_string_ostream", "//testing/base:benchmark_main", - "//testing/base:source_gen_lib", "//toolchain/base:shared_value_stores", + "//toolchain/benchmarking:source_gen_lib", "//toolchain/diagnostics:emitter", "//toolchain/diagnostics:null_diagnostics", "@abseil-cpp//absl/random", diff --git a/toolchain/lex/tokenized_buffer_benchmark.cpp b/toolchain/lex/tokenized_buffer_benchmark.cpp index aa9137a9ad75..3998727b1d2a 100644 --- a/toolchain/lex/tokenized_buffer_benchmark.cpp +++ b/toolchain/lex/tokenized_buffer_benchmark.cpp @@ -14,8 +14,8 @@ #include "common/raw_string_ostream.h" #include "llvm/ADT/Sequence.h" #include "llvm/ADT/StringExtras.h" -#include "testing/base/source_gen.h" #include "toolchain/base/shared_value_stores.h" +#include "toolchain/benchmarking/source_gen.h" #include "toolchain/diagnostics/emitter.h" #include "toolchain/diagnostics/null_diagnostics.h" #include "toolchain/lex/lex.h"