Files
carbon-lang/toolchain/driver/driver_env.h
T
Chandler CarruthandChristopher Di Bella 361c832713 Add a prelude compilation benchmark (#7368)
Adds `toolchain/benchmarking/prelude_benchmark.cpp`, which measures the
time to compile the Core prelude and reports the most interesting SemIR
memory statistics as benchmark counters.

The prelude is compiled by checking a file that imports it: the implicit
prelude import causes the check phase to lex, parse, and check the full
set of prelude files, so this is a direct measure of prelude compilation
cost. Four input variations exercise increasing amounts of the prelude:
an empty file, a minimal single-type use, an operator-heavy file that
hits many impls, and a compact file that pulls in a wide swath of the
prelude.

Memory usage is queried directly: `Driver::set_mem_usage` takes a
`MemUsage` that a compile merges each file's usage into; the benchmark
passes one, compiles, and sums the entries by label. A compilation unit
collects into its own `MemUsage` whenever usage is dumped or a sink is
provided (decided in `SetMultiUnitCache`); after a file is done it dumps
that `MemUsage` per-file as before and, if a sink was provided, merges
into it via a new `MemUsage::Add(const MemUsage&)` overload. `MemUsage`
also exposes its entries via a public `Entry` type and an `entries()`
accessor.

Also extends `scripts/bench_runner.py` to (1) treat Mem-prefixed
counters as cost metrics (smaller is better) and (2) tolerate metrics
that aren't reported by every benchmark in a binary.

Assisted-by: Claude Code

---------

Co-authored-by: Christopher Di Bella <cjdb.ns@gmail.com>
2026-06-29 05:39:44 +00:00

103 lines
3.4 KiB
C++

// 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_TOOLCHAIN_DRIVER_DRIVER_ENV_H_
#define CARBON_TOOLCHAIN_DRIVER_DRIVER_ENV_H_
#include <cstdio>
#include <utility>
#include "common/ostream.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "toolchain/base/install_paths.h"
#include "toolchain/diagnostics/emitter.h"
#include "toolchain/driver/runtimes_cache.h"
namespace Carbon {
class MemUsage;
// The result of a driver run.
struct DriverResult {
// Overall success result.
bool success;
// Per-file success results. May be empty if files aren't individually
// processed.
llvm::SmallVector<std::pair<std::string, bool>> per_file_success = {};
};
// Driver environment information, encapsulated for easy passing to subcommands.
struct DriverEnv {
explicit DriverEnv(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const InstallPaths* installation, FILE* input_stream,
llvm::raw_pwrite_stream* output_stream,
llvm::raw_pwrite_stream* error_stream, bool fuzzing,
bool enable_leaking, Diagnostics::Consumer* consumer)
: fs(std::move(fs)),
installation(installation),
input_stream(input_stream),
output_stream(output_stream),
error_stream(error_stream),
fuzzing(fuzzing),
enable_leaking(enable_leaking),
consumer(consumer),
emitter(consumer) {}
// The filesystem for source code.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs;
// Helper to locate the toolchain installation's files.
const InstallPaths* installation;
// Standard input; stdin. May be null, to prevent accidental use.
FILE* input_stream;
// Standard output; stdout.
llvm::raw_pwrite_stream* output_stream;
// Error output; stderr.
llvm::raw_pwrite_stream* error_stream;
// Tracks when the driver is being fuzzed. This allows specific commands to
// error rather than perform operations that aren't well behaved during
// fuzzing.
bool fuzzing;
// Tracks whether the driver can leak resources, typically because it is being
// invoked as part of a single command line program execution. Defaults to
// `false` for safe and correct library execution.
bool enable_leaking = false;
// Whether to build runtimes on-demand. Only used when `prebuilt_runtimes` is
// empty.
bool build_runtimes_on_demand = false;
// A diagnostic consumer, to be able to connect output.
Diagnostics::Consumer* consumer;
// A diagnostic emitter that has no locations.
Diagnostics::NoLocEmitter emitter;
// Thread pool available for use when concurrency is needed.
llvm::ThreadPoolInterface* thread_pool;
// For CARBON_VLOG.
llvm::raw_pwrite_stream* vlog_stream = nullptr;
// If set, a compile action collects each file's memory usage into this, so it
// can be queried programmatically (for example, by benchmarks) without being
// dumped. See `Driver::set_mem_usage`.
MemUsage* mem_usage = nullptr;
// Cached runtimes.
Runtimes::Cache runtimes_cache;
// Prebuilt runtimes.
std::optional<Runtimes> prebuilt_runtimes;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_ENV_H_