Files
carbon-lang/toolchain/driver/codegen_options.h
Chandler Carruth 0546b48a7d 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
2026-10-21 00:43:54 +00:00

42 lines
1.3 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_CODEGEN_OPTIONS_H_
#define CARBON_TOOLCHAIN_DRIVER_CODEGEN_OPTIONS_H_
#include <string>
#include "common/command_line.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/TargetParser/Host.h"
namespace Carbon {
// Shared codegen-related options.
//
// See the implementation of `Build` for documentation on members.
struct CodegenOptions {
auto Build(CommandLine::CommandBuilder& b) -> void;
// Appends the Clang driver flags corresponding to `target_cpu`,
// `target_cpu_tune`, and `target_cpu_features`.
auto AppendClangArgs(llvm::SmallVectorImpl<std::string>& args) const -> void;
// Returns the Clang driver flags corresponding to `target_cpu`,
// `target_cpu_tune`, and `target_cpu_features`.
auto GetClangArgs() const -> llvm::SmallVector<std::string>;
std::string host = llvm::sys::getDefaultTargetTriple();
llvm::StringRef target;
llvm::StringRef target_cpu;
llvm::StringRef target_cpu_tune;
llvm::StringRef target_cpu_features;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_CODEGEN_OPTIONS_H_