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

@ -122,7 +122,7 @@ Beyond the rules:
- There is no host-platform preprocessor split: the host is Linux. Target differences (mingw, wasm) branch on `cfg.target` strings at runtime, in the library as well as in `project.cpp` and tests.
- A build step fails when its command exits nonzero, never because it printed. Use `RunBuildCommand` or `RunCommandChecked`; warnings go to `Progress::Diagnostic`.
- Every output path goes through `Configuration::BinDir()`, `BuildDir()`, `PcmDir()` and `ProgramDir()`. Hand-rolled `path / "bin" / ...` clobbered variants before; do not add new ones. `ProgramDir()` is where an executable and the files it reads beside it land, its shaders, `cfg.files`, assets and what its dependencies forward, and the directory `-r` runs a host executable in and serves a browser build from (a Flatpak's `cfg.flatpak.share` entries go to `files/share` instead); `ExpectedOutput()` names the executable in it. A Flatpak's bundle and the directory `install` and `bundle` work in are `FlatpakBundlePath()` and `FlatpakAppDir()`.
- Anything that changes what gets compiled or bundled belongs in `VariantId()`. It already hashes type, debug, sysroot, defines, compile flags, the wasm baseline and variant flags, the std module the units import (`StdPcmKey`), uninterpreted project args, the toolchain (`Toolchain::Key()`) and, only when it is one, that the executable is packaged as a Flatpak. What a Flatpak is installed as (`FlatpakApp`) is not hashed: it changes `files/share`, which the writer rewrites in place, and what `install` and `bundle` finish the app with, never what is compiled. Its modules are not hashed either: flatpak-builder builds them for `install` and `bundle` in `FlatpakAppDir()`, one per app, outside any variant's directory.
- Anything that changes what gets compiled or bundled belongs in `VariantId()`. It already hashes type, debug, sysroot, defines, compile flags, the wasm baseline and variant flags, the std module the units import (`StdPcmKey`), uninterpreted project args, the toolchain (`Toolchain::Key()`), the GPU architecture (`cudaArch`), for `native` crafter-build's reading of the GPUs it compiles for, the NVIDIA display devices sysfs lists with `CUDA_VISIBLE_DEVICES` and `CUDA_DEVICE_ORDER` (`Toolchain::cudaNativeKey`, which is not what nvcc detects), and the nvcc (`Toolchain::nvccStamp`; `Toolchain::Key()` leaves both out) of a configuration with CUDA sources and, only when it is one, that the executable is packaged as a Flatpak. What a Flatpak is installed as (`FlatpakApp`) is not hashed: it changes `files/share`, which the writer rewrites in place, and what `install` and `bundle` finish the app with, never what is compiled. Its modules are not hashed either: flatpak-builder builds them for `install` and `bundle` in `FlatpakAppDir()`, one per app, outside any variant's directory.
## Design rules