mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 17:42:38 +00:00
Add support to the driver for CLI flags that select which CPU to target, which CPU to tune performance for, and which CPU features to enable. - `--target-cpu` selects the architecture of CPU to target, similar to `-march` on x86. This combines `-march` with `armv*` strings and `-mcpu` on Arm for specific CPU names. Similarly for RISC-V (if/when we support and test it), etc. Notably, `--target-cpu=native` targets the current native CPU. - `--target-cpu-tune` maps to `-mtune`. This is expected to be rarely if ever desirable, and I'm open to removing it, but if folks are relying on this for performance I didn't want to hide the option. - `--target-cpu-features` allows a "standard" (for LLVM) comma-separated list of LLVM subtarget features, including `+feature` and `-feature` syntax. These flags, when provided, establish the default baseline for CPU targeting and control how on-demand runtimes are built. They can be changed or overridden by additional Clang flags when using `--clang-arg`. A critical part of this is building runtimes on demand using these features. For that, we need to do some amount of canonicalization (re-using Clang's canonicalization to ensure consistent behavior), and extend the runtimes cache features to recognize these customizations. Note that only the top-level Carbon flags impact runtimes, any particular Clang arguments are only used by the specific compiles that receive those arguments. There is some nascent RISC-V support here, but it is incomplete and only minimally tested. Still, it seemed better to record the parts we could test than remove them. Assisted-by: Antigravity with Gemini
150 lines
6.8 KiB
C++
150 lines
6.8 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_CLANG_RUNNER_H_
|
|
#define CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_
|
|
|
|
#include <filesystem>
|
|
#include <optional>
|
|
|
|
#include "common/error.h"
|
|
#include "common/ostream.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Support/Allocator.h"
|
|
#include "llvm/Support/ThreadPool.h"
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
|
#include "llvm/TargetParser/Triple.h"
|
|
#include "toolchain/base/install_paths.h"
|
|
#include "toolchain/diagnostics/emitter.h"
|
|
#include "toolchain/driver/codegen_options.h"
|
|
#include "toolchain/driver/runtimes_cache.h"
|
|
#include "toolchain/driver/tool_runner_base.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Runs Clang in a similar fashion to invoking it with the provided arguments on
|
|
// the command line. We use a textual command line interface to allow easily
|
|
// incorporating custom command line flags from user invocations that we don't
|
|
// parse, but will pass transparently along to Clang itself.
|
|
//
|
|
// This class is thread safe, allowing multiple threads to share a single runner
|
|
// and concurrently invoke Clang.
|
|
//
|
|
// This doesn't literally use a subprocess to invoke Clang; it instead tries to
|
|
// directly use the Clang command line driver library. We also work to simplify
|
|
// how that driver operates and invoke it in an opinionated way to get the best
|
|
// behavior for our expected use cases in the Carbon driver:
|
|
//
|
|
// - Ensure thread-safe invocation of Clang to enable concurrent usage.
|
|
// - Minimize canonicalization of file names to try to preserve the paths as
|
|
// users type them.
|
|
// - Minimize the use of subprocess invocations which are expensive on some
|
|
// operating systems. To the extent possible, we try to directly invoke the
|
|
// Clang logic within this process.
|
|
// - Provide programmatic API to control defaults of Clang. For example, causing
|
|
// verbose output.
|
|
//
|
|
// Note that this makes the current process behave like running Clang -- it uses
|
|
// standard output and standard error, and otherwise can only read and write
|
|
// files based on their names described in the arguments. It doesn't provide any
|
|
// higher-level abstraction such as streams for inputs or outputs.
|
|
//
|
|
// TODO: Switch the diagnostic machinery to buffer and do locked output so that
|
|
// concurrent invocations of Clang don't intermingle their diagnostic output.
|
|
//
|
|
// TODO: If support for thread-local overrides of `llvm::errs` and `llvm::outs`
|
|
// becomes available upstream, also buffer and synchronize those streams to
|
|
// further improve the behavior of concurrent invocations.
|
|
class ClangRunner : ToolRunnerBase {
|
|
public:
|
|
// Build a Clang runner that uses the provided installation and filesystem.
|
|
//
|
|
// Optionally accepts a `vlog_stream` to enable verbose logging from Carbon to
|
|
// that stream. The verbose output from Clang goes to stderr regardless.
|
|
ClangRunner(const InstallPaths* install_paths,
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
|
|
llvm::raw_ostream* vlog_stream = nullptr,
|
|
std::optional<std::filesystem::path> override_clang_path = {});
|
|
|
|
// Run Clang with the provided arguments and a runtime cache for on-demand
|
|
// runtime building.
|
|
//
|
|
// This works to support all of the Clang commandline, including commands that
|
|
// use target-dependent resources like linking. When it detects such commands,
|
|
// it will use runtimes from the provided cache. If not available in the
|
|
// cache, it will build the necessary runtimes using the provided thread pool
|
|
// both to use and incorporate into the cache.
|
|
//
|
|
// Returns an error only if unable to successfully run Clang with the
|
|
// arguments. If able to run Clang, no error is returned, and a bool
|
|
// indicating whether than Clang invocation succeeded is returned.
|
|
auto Run(llvm::ArrayRef<llvm::StringRef> args,
|
|
Runtimes::Cache& runtimes_cache,
|
|
llvm::ThreadPoolInterface& runtimes_build_thread_pool,
|
|
bool enable_leaking = false, Runtimes::Cache::Features features = {})
|
|
-> ErrorOr<bool>;
|
|
|
|
// Run Clang with the provided arguments and prebuilt runtimes.
|
|
//
|
|
// Similar to `Run`, but requires and uses pre-built runtimes rather than a
|
|
// cache or building them on demand.
|
|
auto RunWithPrebuiltRuntimes(llvm::ArrayRef<llvm::StringRef> args,
|
|
Runtimes& prebuilt_runtimes,
|
|
bool enable_leaking = false) -> ErrorOr<bool>;
|
|
|
|
// Run Clang with the provided arguments and without any target runtimes.
|
|
//
|
|
// Similar to `Run`, but omits any target runtimes.
|
|
//
|
|
// This method can be used to avoid building target-dependent resources when
|
|
// unnecessary, but not all Clang command lines will work correctly.
|
|
// Specifically, compile-only commands will typically work, while linking will
|
|
// not.
|
|
auto RunWithNoRuntimes(llvm::ArrayRef<llvm::StringRef> args,
|
|
bool enable_leaking = false) -> ErrorOr<bool>;
|
|
|
|
// Computes the canonicalized `Runtimes::Cache::Features` for
|
|
// `codegen_options`, emitting any diagnostics for invalid target flags to
|
|
// `consumer`.
|
|
auto ComputeRuntimesFeatures(const CodegenOptions& codegen_options,
|
|
Diagnostics::Consumer& consumer)
|
|
-> ErrorOr<Runtimes::Cache::Features>;
|
|
|
|
private:
|
|
friend class ClangRuntimesBuilderBase;
|
|
|
|
auto RunClangCC1(llvm::SmallVectorImpl<const char*>& cstr_args,
|
|
bool enable_leaking) -> int;
|
|
|
|
// Handles building the Clang driver and passing the arguments down to it.
|
|
auto RunInternal(llvm::ArrayRef<llvm::StringRef> args, llvm::StringRef target,
|
|
std::optional<llvm::StringRef> target_resource_dir_path,
|
|
std::optional<std::filesystem::path> libunwind_path,
|
|
std::optional<std::filesystem::path> libcxx_path,
|
|
bool link_runtime_libs, bool enable_leaking)
|
|
-> ErrorOr<bool>;
|
|
|
|
// Returns the target-specific source files for the builtins runtime library.
|
|
auto CollectBuiltinsSrcFiles(const llvm::Triple& target_triple)
|
|
-> llvm::SmallVector<llvm::StringRef>;
|
|
|
|
// Builds the builtins runtime library into the provided archive file path,
|
|
// using the provided objects path for intermediate object files.
|
|
auto BuildBuiltinsLib(llvm::StringRef target,
|
|
const llvm::Triple& target_triple,
|
|
const std::filesystem::path& runtimes_path,
|
|
llvm::ThreadPoolTaskGroup& threads) -> ErrorOr<Success>;
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
|
|
|
|
std::filesystem::path clang_path_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_
|