feat(cuda): make the GPU architecture configurable, native by default

nvcc ran with -arch=sm_89 for every build, which is code for that one
architecture and its PTX, whatever GPU the program was for. Jorijn asked
for the architecture to be configurable, and native when nothing sets
it, as the CPU's is.

cfg.cudaArch is nvcc's -arch now, and it defaults to "native": nvcc
compiles for the GPUs it sees, and where it sees none it warns and uses
its own default architecture. Like -march=native it is for a program
that runs where it is built, since it carries no PTX, and
`crafter-build bundle` warns of a bundle whose CUDA code, in the program
or a library it is built on, is compiled for native, as it warns of
-march=native. ApplyStandardArgs sets it from --cuda-arch= and
CRAFTER_BUILD_CUDA_ARCH as it sets march from --march= and
CRAFTER_BUILD_MARCH; AddTest and AddMarchVariants give their tests the
parent's, as they give them its sysroot and debug flag; and
ForceHostTooling gives host tooling the host's, as it gives it the
host's march, since project.so runs it on the machine that loads it.

A .cu is compiled again only when it is newer than its object, so the
architecture keys VariantId, and so does the nvcc that compiles it, by
what it is rather than where: Toolchain::nvccStamp, its canonical path,
size and time, read without a process, changes when CUDA_PATH names
another toolkit and when an update replaces the one at the same path, as
a package does at /opt/cuda. native is keyed as well, by
Toolchain::cudaNativeKey: the NVIDIA display devices sysfs lists, by PCI
address and device ID, and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER,
which choose and number them, each empty value apart from an unset one,
read without a process. It is crafter-build's reading of the machine,
not nvcc's. The word alone would keep an object compiled for a GPU the
machine no longer has, after a GPU change or on another machine that
shares the checkout, and that object carries no PTX for the GPU it has
now, where sm_89's carried its PTX. Only a configuration with CUDA
sources is keyed, so every other one keeps the directory it had, but for
one built with --cuda-arch=, which was a project argument, hashed as
one, and is crafter-build's own now.
This commit is contained in:
Jorijn van der Graaf 2026-11-11 19:34:52 +01:00
commit 967dd12b06
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
10 changed files with 262 additions and 15 deletions

View file

@ -504,6 +504,16 @@ export namespace Crafter {
std::vector<Implementation> implementations;
std::vector<fs::path> cFiles;
std::vector<fs::path> cuda;
// The GPU architecture nvcc compiles `cuda` for, as its -arch takes
// one. "native", the default, compiles for the GPUs of the machine
// that builds, as march's default does for its CPU; with none to be
// seen, nvcc warns and uses its own default. Like -march=native it is
// for a program that runs where it is built: it carries no PTX, which
// a newer GPU could compile when it loads the program. "sm_89" is one
// architecture with its PTX; "all-major" or "all" is for a program
// that runs elsewhere. ApplyStandardArgs sets it from
// CRAFTER_BUILD_CUDA_ARCH and --cuda-arch=.
std::string cudaArch = "native";
std::vector<Configuration*> dependencies;
std::vector<fs::path> files;
// Build-time-only source files this configuration exposes to its own
@ -593,9 +603,9 @@ export namespace Crafter {
void ResolvePendingImports();
// Declare a test. Sources default to `tests/<name>/main.cpp` resolved
// against this Configuration's path; target/march/mtune/sysroot/debug
// are inherited from this Configuration so cross-arch projects don't
// have to re-specify. Returns a builder for chaining defines, deps,
// etc. Defined in Crafter.Build:Test.
// and cudaArch are inherited from this Configuration so cross-arch
// projects don't have to re-specify. Returns a builder for chaining
// defines, deps, etc. Defined in Crafter.Build:Test.
struct TestBuilder AddTest(std::string_view name);
// Same as AddTest, but compiles the parent's `interfaces` directly
// into this test's Configuration (rather than going through a dep).
@ -625,7 +635,7 @@ export namespace Crafter {
// Suffix that uniquely identifies this Configuration's compile state.
// target+march+mtune are spelled out for readability; the rest
// (type, debug, sysroot, defines, compileFlags, the std module, the
// toolchain)
// toolchain, the GPU architecture and the nvcc of CUDA sources)
// collapse into a short hash so two Configurations sharing a path but
// with different compile state can't clobber each other's outputs.
std::string VariantId() const { return VariantId(GetToolchain()); }
@ -690,6 +700,25 @@ export namespace Crafter {
}
compileKey += "|T:";
compileKey += toolchain.Key();
// The GPU architecture decides what a .cu compiles into, and so
// does the toolkit whose nvcc compiles it, which CUDA_PATH can
// switch and an update can replace at the same path, so the nvcc
// is keyed by what it is (Toolchain::nvccStamp), and native by the
// GPUs it resolves to (Toolchain::cudaNativeKey). None of them
// need touch the files whose times decide whether a .cu is
// compiled again, so each needs a directory of its own. Only keyed
// with CUDA sources, so every other configuration keeps the
// directory it had.
if (!cuda.empty()) {
compileKey += "|C:";
compileKey += cudaArch;
compileKey += '|';
compileKey += toolchain.nvccStamp;
if (cudaArch == "native") {
compileKey += '|';
compileKey += toolchain.cudaNativeKey;
}
}
// A Flatpak lays its bin dir out unlike the executable as linked,
// so the two never share one. Only keyed for a Flatpak, so every
// other configuration keeps the directory it had.
@ -867,6 +896,7 @@ export namespace Crafter {
// --target=<triple> cfg.target = <triple>
// --march=<value> cfg.march = <value>
// --mtune=<value> cfg.mtune = <value>
// --cuda-arch=<value> cfg.cudaArch = <value>
// --lib cfg.type promoted Executable → LibraryStatic
// --shared cfg.type promoted LibraryStatic → LibraryDynamic
// --packaging=<kind> cfg.packaging = None or Flatpak (none, flatpak),
@ -878,7 +908,8 @@ export namespace Crafter {
// LibraryDynamic regardless of arg order; each is a no-op when the
// baseline doesn't match (e.g. --shared on an Executable, or --lib on a
// pre-set library).
// $CRAFTER_BUILD_MARCH / $CRAFTER_BUILD_MTUNE seed march/mtune.
// $CRAFTER_BUILD_MARCH / $CRAFTER_BUILD_MTUNE / $CRAFTER_BUILD_CUDA_ARCH
// seed march/mtune/cudaArch.
// Env applies first, then args, so CLI wins over env wins over caller's
// pre-set defaults. Returns an ArgQuery over the same span so projects
// can query their own flags (`--timing`, ...) without re-rolling the

View file

@ -67,6 +67,17 @@ export namespace Crafter {
// for want of a CUDA toolkit.
fs::path nvcc;
std::string nvccError;
// What that nvcc is, for VariantId: its canonical path, size and
// modification time, which change when the toolkit is updated or
// replaced at the same path, as the path alone does not. Empty
// without an nvcc.
std::string nvccStamp;
// What -arch=native compiles for on this machine, for VariantId: the
// NVIDIA display devices the PCI bus lists, by address and device ID,
// and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER, which choose among
// them and number them, each unset apart from empty. Empty without an
// nvcc.
std::string cudaNativeKey;
// The Flatpak runtime crafter-build runs on, from the
// runtime=runtime/<id>/<arch>/<branch> line of /.flatpak-info:
// "x86_64" and "26.08" in the Flatpak, both empty outside one. A
@ -125,6 +136,19 @@ export namespace Crafter {
// order they are tried and as the host names them: GetToolchain()
// passes /opt/cuda, Arch's, and /usr/local/cuda, NVIDIA's installers'.
std::vector<fs::path> cudaRoots;
// The PCI devices sysfs lists, one directory each with its vendor,
// device and class: GetToolchain() passes /sys/bus/pci/devices, which
// the sandbox shows too. What -arch=native compiles for is keyed by
// the NVIDIA display devices among them.
fs::path pciDevices;
// CUDA_VISIBLE_DEVICES, which chooses the GPUs -arch=native compiles
// for, when it is set: set but empty hides them all, which is not the
// same as unset, so it is kept apart from env, which holds non-empty
// values alone.
std::optional<std::string> cudaVisibleDevices;
// CUDA_DEVICE_ORDER, when it is set: it decides which GPU a number in
// CUDA_VISIBLE_DEVICES means, by PCI address or by speed.
std::optional<std::string> cudaDeviceOrder;
};
// Pure path resolution: an env override wins, then a toolchain installed
// beside crafter-build (<crafterPrefix>/bin/clang++), then bare names on