Support --target-cpu, --target-cpu-tune, and --target-cpu-features (#7903)

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
This commit is contained in:
Chandler Carruth authored and GitHub committed 2026-10-21 00:43:54 +00:00
1 parent 4441c53404
commit 0546b48a7d
25 files changed
+1084 -92

No files matched your search

+15 -7
View File
@@ -75,14 +75,22 @@ struct ConfigDataEntry {
// If there are any errors setting up Clang, this will diagnose them using
// `driver_env.consumer` and return `false`. If successful, returns `true`.
static auto ComputeClangConfig(DriverEnv& driver_env,
llvm::StringRef target_str,
const CodegenOptions& codegen_options,
llvm::SmallVectorImpl<ConfigDataEntry>& data)
-> bool {
llvm::SmallVector<std::string> codegen_clang_args =
codegen_options.GetClangArgs();
llvm::SmallVector<llvm::StringRef> extra_args(codegen_clang_args.begin(),
codegen_clang_args.end());
// Build a library invocation of Clang in order to query its header search
// paths.
std::shared_ptr clang_invocation =
BuildClangInvocation(*driver_env.consumer, driver_env.fs,
*driver_env.installation, target_str, {});
std::shared_ptr clang_invocation = BuildClangInvocation(
*driver_env.consumer, driver_env.fs, *driver_env.installation,
codegen_options.target, extra_args);
if (!clang_invocation) {
return false;
}
clang_invocation->getFrontendOpts().DisableFree = false;
// Setup up a driver-style diagnostic engine for the compiler invocation and
@@ -106,7 +114,8 @@ static auto ComputeClangConfig(DriverEnv& driver_env,
CARBON_DIAGNOSTIC(ConfigFailedToSetupTarget, Error,
"unable to setup the requested target `{0}`",
std::string);
driver_env.emitter.Emit(ConfigFailedToSetupTarget, target_str.str());
driver_env.emitter.Emit(ConfigFailedToSetupTarget,
codegen_options.target.str());
return false;
}
@@ -215,8 +224,7 @@ auto ConfigSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
// Compute and print Clang's config entries if we can. This will have been
// diagnosed while computing, so just track if we hit errors.
result &=
ComputeClangConfig(driver_env, options_.codegen_options.target, data);
result &= ComputeClangConfig(driver_env, options_.codegen_options, data);
llvm::sort(data, [](const ConfigDataEntry& lhs, const ConfigDataEntry& rhs) {
return lhs.key < rhs.key;