Files
carbon-lang/toolchain/driver/driver_env.h
T
Chandler CarruthandDana Jansens 35fb000536 Use a thread pool when building runtimes (#6133)
This parallelizes the compilations and dramatically reduces the time to
build runtimes.

As part of this, teach the driver infrastructure to have an option to
control the use of threads and to build the relevant thread pool and
thread it into the various APIs.

However, it requires our `ClangRunner` to become thread-safe and to
invoke Clang in a way that is thread-safe. This is somewhat challenging
as the code in `clang_main` is distinctly _not_ thread-safe.

To address this, the relevant logic of `clang_main`, especially the CC1
execution, is extracted into our runner and cleaned up to be much more
appropriate in a multithreaded context. Much of this code should
eventually be factored back into Clang, but that will be a follow-up
patch to upstream.

Last but not least, this rearranges the `ClangRunner` API to make a bit
more sense out of the different options for building runtimes, and have
a clean model for which things need to be passed in at which points.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-09-30 12:54:51 +00:00

83 lines
2.7 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/diagnostics/diagnostic_emitter.h"
#include "toolchain/driver/runtimes_cache.h"
#include "toolchain/install/install_paths.h"
namespace Carbon {
// 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)
: 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(error_stream),
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;
// A diagnostic consumer, to be able to connect output.
Diagnostics::StreamConsumer 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;
// Cached runtimes.
Runtimes::Cache runtimes_cache;
// Prebuilt runtimes.
std::optional<Runtimes> prebuilt_runtimes;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_ENV_H_