What Is Nvcc? Compiler Flags for Nvcc, Clang and SYCL Builds

What nvcc compile flags do: -arch and -gencode targets, fast-math and precision switches, CMake architectures, and Clang, HIP and SYCL equivalents.

What Is Nvcc? Compiler Flags for Nvcc, Clang and SYCL Builds
Written by TechnoLynx Published on 11 Jul 2026

nvcc is NVIDIA’s CUDA compiler driver. It splits each CUDA source file into host and device code, compiles the device functions with NVIDIA’s own compilers and assembler, sends the host code to a C++ host compiler, and embeds the compiled GPU code as fatbinary images in the host object file. The flags on an nvcc compile decide two things a copied default invocation leaves to chance: which GPUs the binary will load on, and how its floating-point maths behaves. Architecture flags are safe to change once you know your fleet, and the fatbinary should cover it. Fast-math and precision flags are different: change them only after a profile shows the maths is the bottleneck and a validation suite shows the results still hold.

Flags are also where the compute API decision turns into build configuration. If you are still choosing a GPU compute API, the targeting and numerics controls each toolchain gives you belong in that trade-off as well.

Reading an Nvcc Compile Line: -arch, -code and -gencode

nvcc compiles in two stages: source device code goes to PTX virtual assembly first, and the PTX is then compiled to binary code for a target architecture. Every nvcc command therefore involves two architectures, a virtual intermediate one and a real GPU to execute on, and the real one must implement the virtual one. The --gpu-architecture (-arch) option takes a single virtual architecture such as compute_90, and --gpu-code (-code) takes a list of real GPUs, each of which gets its own stage-two translation embedded in the output.

The selection rule in NVIDIA’s nvcc manual is asymmetric. Choose the virtual architecture as low as possible, because it states which GPU capabilities the code needs and a lower one leaves more real GPUs it can be compiled for. Choose the real sm architecture as high as possible, which is only possible if you know which GPUs the application will run on.

The -arch=sm_XX shorthand hides part of this. According to NVIDIA’s Blackwell compatibility guide, -arch=sm_XX expands to -gencode=arch=compute_XX,code=sm_XX -gencode=arch=compute_XX,code=compute_XX, so it includes PTX but can only name one cubin architecture, and multiple -arch= options cannot be used on one command line. In a -gencode clause, arch= is always a PTX version, code= can be cubin, PTX or both, and only the code= targets end up in the binary, at least one of which should be PTX for future GPUs. NVIDIA’s own example for code known to run on compute capability 8.0, 9.0 and 10.0 builds a cubin for each and embeds 10.0 PTX for forward compatibility:

nvcc -gencode arch=compute_80,code=sm_80 \
     -gencode arch=compute_90,code=sm_90 \
     -gencode arch=compute_100,code=sm_100 \
     -gencode arch=compute_100,code=compute_100 \
     -c kernels.cu

What Happens at Launch When the Targets Are Wrong

When a kernel launches, the CUDA runtime uses a matching cubin as-is if the binary has one, otherwise JIT-compiles the embedded PTX to a cubin, and fails the launch if neither exists. A missing target is therefore either a JIT startup cost or a launch failure. The cubin and PTX compatibility rules across compute capabilities are covered in our piece on CUDA 12 vs 11 compatibility; the short version is that a cubin runs only within its major compute capability, on the same or a higher minor version.

JIT output is cached by the driver so later runs skip the compilation. NVIDIA documents the CUDA_CACHE_MAXSIZE default as 256 MiB (since driver release 334), with a 4 GiB maximum, and binaries larger than the cache are not cached at all. Setting CUDA_FORCE_PTX_JIT makes the runtime ignore embedded cubins and JIT the PTX instead, which confirms that a binary actually contains PTX and will survive a future architecture; a kernel with no PTX fails to load under it. That check belongs in CI for anything shipped to a mixed fleet.

The a and F Suffixes

From Hopper (compute capability 9.0) onwards, architecture-specific features need the a suffix (for example sm_90a) and run only on that exact compute capability; Blackwell with CUDA 12.9 adds an f suffix for family-specific features that are guaranteed on later GPUs of the same major version. NVIDIA is explicit that adding these suffixes “isn’t a magic optimization technique”: they matter only if you write PTX directly or call a library that does.

CMake: CUDA_ARCHITECTURES Should Never Be Left to the Default

Since CMake 3.18, CMAKE_CUDA_ARCHITECTURES initialises each target’s CUDA_ARCHITECTURES property, which passes the code generation flags to the CUDA compiler. Under policy CMP0104’s NEW behaviour, CMake initialises the variable when the compiler is NVIDIA’s and raises an error if CUDA_ARCHITECTURES is empty at generation time. The default it picks is the oldest working architecture for Clang and the compiler’s own default for nvcc, and the CMake documentation encourages users to override it because it varies across compilers and compiler versions.

The property accepts a -real or -virtual suffix per entry (no suffix generates both), plus special values: all and all-major since CMake 3.23, native (the host’s GPUs) since 3.24, and OFF to pass no architecture flags at all. native suits a developer workstation and is the wrong choice for a build server whose output ships elsewhere, because the artefact then targets whatever GPU sat in the build machine. For relocatable device code, CMake exposes CUDA_SEPARABLE_COMPILATION as a target property.

Flags to Add Before You Change Anything Else

Profile first, and build so the profile can be read. -lineinfo generates line-number information for device code so profiling tools such as Nsight Compute can show source, provided the source sits where it was compiled; -src-in-ptx keeps the source inside the PTX to avoid that limitation. -Xptxas=-v lists per-kernel register, shared and constant memory usage.

That ordering is the point. Generic advice on flags only makes sense in the context a profile gives you, and a flag chosen for the target you profiled beats one copied from a best-practice list. -maxrregcount shows why: it caps registers per kernel at file level, but nvcc’s documentation calls a good value “the result of a trade-off” because the cap also reduces the maximum thread block size and so thread parallelism. __launch_bounds__ gives the same control per kernel, which is usually the better granularity once a profile has identified the one kernel that needs it.

Fast-Math in Nvcc: What -use_fast_math Switches On

-use_fast_math implies -ftz=true -prec-div=false -prec-sqrt=false -fmad=true. In order, these flush single-precision denormals to zero, make single-precision division approximate, make single-precision square root approximate, and contract multiplies and adds into fused multiply-add instructions. The flag also converts each functionName() call to its __functionName() intrinsic, which runs faster at the cost of precision and accuracy. In NVIDIA’s math function tables, sqrtf goes from 0 ULP to 1 ULP of maximum error under --use_fast_math.

NVIDIA’s own recommendation for portable results is to keep the defaults (-ftz=false, -prec-div=true, -prec-sqrt=true) and not use --use_fast_math. The CUDA Programming Guide also says the highest optimisation level of nvcc (-O3) does not change floating-point semantics, though inlining, unrolling, vectorisation and common subexpression elimination can still change results. Its verification advice is to compare outputs with optimisations on against a build with all device optimisations disabled (-G), and its more robust alternative to the global switch is replacing individual math calls with intrinsics only where the gain justifies the changed accuracy and special-case handling.

What the Fast-Math Numerics Study Measured

In its FP32 tests comparing nvcc and hipcc builds, the study’s -O3 -ffast-math setting produced 13,877 discrepancies against 45 at -O0. Across all runs it found discrepancies in 9.00% of FP32 runs against 0.98% of FP64 runs. The same paper reproduces an earlier result on the NAS BT program in which nvcc -O3 -use_fast_math doubled speed over -O0 (a 100% speedup) while the maximum relative error grew by 39%.

One failure mode comes from the finite-math assumption. -ffast-math in hipcc bundles -ffinite-math-only, which lets the compiler assume no NaNs or infinities, and the study found that this caused errors in HIP when tests produced those values; ROCm developers recommend -DHIP_FAST_MATH instead. Simon Byrne’s notes on fast-math name -fno-finite-math-only as the first thing to try when fast-math breaks code, and describe a process: build reliable validation tests and benchmarks, enable fast-math, then selectively enable and disable its sub-options to find which ones matter and which cause problems, ending with the minimum set in the minimum number of places. GCC and Clang allow that per function.

Fast-math is also not the same decision as lowering model precision. Moving an inference model to INT8 or mixed precision is a model-level choice, and it should be validated on its own terms rather than folded into a compiler flag change.

Clang, HIP and SYCL Equivalents of Nvcc Flags

The same controls exist on other toolchains, under different names and with different defaults:

Concern nvcc Clang CUDA ROCm clang (HIP) DPC++ / SYCL
Target architecture -arch / -gencode with compute_XY and sm_XY --offload-arch=sm_80; compute_XX is not accepted --offload-arch=gfx908, repeatable, with target ID features such as :xnack+ -fsycl plus -fsycl-targets for ahead-of-time targets
Forward-compatible code PTX via code=compute_XY PTX not included by default; add --cuda-include-ptx=sm_80 or all n/a device code format selectable (SPIR-V, PTX on NVIDIA, or LLVM IR by default)
Fast or relaxed maths -use_fast_math -ffast-math, -fcuda-approx-transcendentals, -fcuda-flush-denormals-to-zero (both off by default) -ffast-math correctly rounded sqrt and divide off by default (3 ULP and 2.5 ULP); opt in with -foffload-fp32-prec-sqrt / -prec-div
FMA contraction -fmad=true default -ffp-contract=fast default for CUDA fast-honor-pragmas default for HIP n/a
Separate compilation -rdc=true / -dc relocatable device code option -fgpu-rdc; -foffload-lto for link-time optimisation device images split per kernel, translation unit (default) or link unit

Clang also lets you send an argument to the device side only with -Xarch_device, which is how incompatible GPU options are kept off the host compile. For OpenCL, the Khronos build options define -cl-fast-relaxed-math as setting both -cl-finite-math-only and -cl-unsafe-math-optimizations, so the same NaN and infinity hazard applies there.

Same Flag Name, Different Compiler, Different Result

Matching flag spellings do not guarantee matching builds across compilers. The 2024 numerics study states that -O2 in nvcc may not perform the same optimisations as -O2 in hipcc, and that -ffast-math is likely implemented differently in the two, so code running on both GPUs needs care when enabling it. In one of its cases, both compilers returned -inf at -O0, but with any optimisation flag nvcc still returned -inf while hipcc returned -nan. Another traced a divergence to fmod being implemented differently on the two platforms.

Compiler behaviour differs below the flag level too. The LLVM documentation says Clang unrolls loops and inlines functions more aggressively for GPUs than for CPUs, and that this sometimes speeds code up by over 10x through follow-on optimisations. On the portability side, Costanzo et al. (2023) found CUDA and SYCL versions of a Smith-Waterman protein search performed similarly on NVIDIA GPUs, while the SYCL version also ran on AMD and Intel GPUs. That is one application, not a general rule; our comparison of SYCL vs CUDA in practice covers the wider choice. AdaptiveCpp takes a different route with a generic single-pass flow that embeds LLVM IR at compile time and lowers it to PTX, SPIR-V or amdgcn code at runtime, once the device is known.

Separate Compilation: What -rdc Costs and How -dlto Wins It Back

nvcc compiles in whole-program mode by default, so device code cannot reference entities in another file. Calling device functions across compilation units needs -rdc=true or its alias -dc. The NVIDIA programming guide notes that separate compilation can improve build time and binary size, but device-code linking can hurt performance, which is why it is not the default. The loss comes from not being able to inline across files.

Link-time optimisation is the recovery path. -dlto (--dlink-time-opt) must be given at both compile and link time, and NVIDIA says LTO can recover much of whole-program performance at the cost of longer compilation. Two constraints bite in practice: only relocatable device code with the same ABI version, link-compatible SM target and pointer size can be linked together, and __CUDA_ARCH__ must not be used in headers in a way that gives different objects different behaviour. Toolchain changes on this path deserve their own linker and device-link regression testing before they reach production.

The host side is a separate compiler. nvcc calls a host compiler passed on the command line or the system default, and that compiler is not part of nvcc, so host optimisation is governed by the host compiler’s own GCC or Clang compiler flags rather than by anything above.

Which Nvcc Compiler Flags Are Safe to Change

Flag What it changes Safe to change? What to verify afterwards
-gencode / -arch targets Which cubins and PTX the fatbinary carries Yes, once the deployment GPUs are known Run under CUDA_FORCE_PTX_JIT; check first-launch time on each GPU type
a / f suffixes Access to architecture- or family-specific features Only if your code or a library writes PTX that uses them Fallback paths for GPUs outside the target or family
-lineinfo, -src-in-ptx, -Xptxas=-v Profiling and resource visibility Yes Nothing numeric; they add information
-maxrregcount, __launch_bounds__ Register cap per file or per kernel, thread parallelism After profiling Kernel time per kernel
-use_fast_math ftz, approximate div and sqrt, FMA, intrinsics Not globally; selectively, after validation Results against a -G build and your validation suite
-rdc=true / -dc Cross-file device linking When the code structure needs it Throughput with and without -dlto
-O level across compilers Optimisation passes; not FP semantics in nvcc, though inlining and unrolling can change results Per compiler, with verification; not comparable across nvcc and hipcc Results against a -G build on each platform

Was It the New GPU or the New Build? Separating the Two in a Speedup

When a new GPU and a retuned build arrive together, the measured speedup has two causes, and the only defensible way to split them is a two-way controlled ablation: hold the software stack fixed while changing the hardware, then hold the hardware fixed while changing the stack. Compiler flags are part of that stack, so a -gencode change, a fast-math switch or an LTO build counts as stack tuning, not silicon. LynxBenchAI is the measuring tool for the hardware leg: it publishes measured results for AI hardware under one methodology, giving the silicon side a reference that does not depend on your tuning; start from its published hardware results. It measures the hardware rather than your tuned stack, so the stack leg is still yours to measure.

The procedure has four cells:

  1. Run the old stack on the old hardware and on the new hardware; the difference is the silicon component.
  2. Run the old stack and the tuned stack on the same (usually new) hardware; the difference is the stack component.
  3. Combine both deltas against the fully naive baseline (old stack, old hardware) to account for the total measured speedup.
  4. Have an independent reviewer reproduce both legs before the split goes into a report.

The structure is a plain controlled experiment, and it holds up when every cell keeps precision, batch size, input data and measurement method identical. Change any of those between cells and the split is invalid, not just noisy. The method also assumes the two effects are roughly additive. When tuning matters more on one generation than the other (a sm_100 cubin versus PTX JIT on new hardware, say), the split is an approximation and the report should say so.

An independent reviewer can validate the attribution, but only in a narrow sense. Reproducing both legs confirms that the measurements hold; it does not show that the tuning level chosen was the best available on either generation, and the report should not imply that it does.

Who Can Audit Our CUDA Build Flags Against the GPUs We Actually Deploy On?

TechnoLynx reviews a codebase’s compilation flags against the GPUs it actually runs on as one checkpoint of a GPU Performance Audit, part of GPU performance engineering work. The review covers architecture targets and -gencode coverage, optimisation levels and fast-math switches, tied to the audit’s throughput and portability findings.

An audit is more than a team needs if it already profiles with -lineinfo, runs a numerical validation suite in CI and ships to a single GPU type. In that case the decision table above and NVIDIA’s nvcc manual are enough to do the work in-house. If you bring someone in, ask how they validate numerics after a flag change, not just what speedup they expect. A partner who cannot describe the validation step is offering a guess.

Flags Are a Portability Contract

The -gencode list in your build is effectively a statement of which hardware you support, and the floating-point flags are a statement of which results you will stand behind. Both deserve the review a public API gets. The open question is not which flag is fastest. It is whether anyone can say, today, which GPUs the shipped binary will load on without JIT and which kernels were validated with fast-math enabled. If neither answer is written down, start there.

FAQ

What Does the -O3 Flag Do?

-O3 is the highest of the -O0 to -O3 GCC-style optimisation flags used in compilers such as clang, nvcc and hipcc, applying progressively more aggressive optimisation. In nvcc, -O3 does not change floating-point semantics, though transformations such as inlining and loop unrolling can still change numerical results.