From 6e400b317a91bf8ec9a2d6c9307395d6b7eaa161 Mon Sep 17 00:00:00 2001 From: Gilles Colling Date: Sun, 29 Nov 2026 14:55:46 +0100 Subject: [PATCH] PTX: make FMA contraction a PTXCompilerTarget option PTX code generation always let the back-end contract multiply-add pairs, as nvcc does, so kernels could not reproduce CPU results bit for bit. The new `fma_contraction` field (default true) controls this for both the NVPTX and NVVM back-ends. It is part of the target's hash and therefore of the cache owner, so kernels compiled with and without contraction don't share results. --- src/ptx.jl | 12 ++++++++++-- test/helpers/ptx.jl | 5 +++-- test/ptx.jl | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/ptx.jl b/src/ptx.jl index f13350db..b7b68f5f 100644 --- a/src/ptx.jl +++ b/src/ptx.jl @@ -45,6 +45,11 @@ Base.@kwdef struct PTXCompilerTarget <: AbstractCompilerTarget fastmath::Bool = Base.JLOptions().fast_math == 1 + # may the back-end contract any multiply-add pair into a fused multiply-add, as nvcc + # does? if not, only operations the IR marks contractable (`muladd`, `@fastmath`) are + # fused, as on the CPU, so results can match the CPU's bit for bit. + fma_contraction::Bool = true + # can system-scope atomics be used? the ISA supports them from sm_60 on (which # `validate_ir` checks separately), but some platforms reject them, e.g. Pascal GPUs # under Windows, or Tegra GPUs before sm_72. @@ -68,6 +73,7 @@ function Base.hash(target::PTXCompilerTarget, h::UInt) h = hash(target.blocks_per_sm, h) h = hash(target.maxregs, h) h = hash(target.fastmath, h) + h = hash(target.fma_contraction, h) h = hash(target.system_atomics, h) h @@ -164,6 +170,7 @@ function Base.show(io::IO, @nospecialize(job::CompilerJob{PTXCompilerTarget})) job.config.target.blocks_per_sm !== nothing && print(io, ", blocks_per_sm=$(job.config.target.blocks_per_sm)") job.config.target.maxregs !== nothing && print(io, ", maxregs=$(job.config.target.maxregs)") job.config.target.fastmath && print(io, ", fast math enabled") + job.config.target.fma_contraction || print(io, ", FMA contraction disabled") end const ptx_intrinsics = ("vprintf", "__assertfail", "malloc", "free") @@ -377,13 +384,13 @@ end # the back-end derives the triple and datalayout from these options; unlike `llc`, # it rejects unknown processors and PTX versions instead of silently falling back. # FMA contraction is requested as `-nvptx-fma-level=1` was: match nvcc by leaving - # eligible multiply-add pairs contractible for ptxas. + # eligible multiply-add pairs contractible for ptxas, unless the target disables it. cpu = cpu_name(target) ptx = GC.@preserve cpu begin options = Ref(NVPTXCompileOptions(Base.unsafe_convert(Cstring, cpu), target.ptx.major, target.ptx.minor, Int === Int64, #=opt_level=# 2, - #=fma_contraction=# true, #=verbose=# true)) + target.fma_contraction, #=verbose=# true)) external_compile(backend, bitcode(mod), options, "Failed to compile to PTX with the NVPTX back-end") end @@ -423,6 +430,7 @@ function nvvm_options(@nospecialize(job::CompilerJob{PTXCompilerTarget})) if target.fastmath append!(options, ["-ftz=1", "-prec-div=0", "-prec-sqrt=0"]) end + target.fma_contraction || push!(options, "-fma=0") # the debug info itself comes from the module's metadata (see `llvm_debug_info`), so # these flags only mirror what `nvcc -lineinfo` and `nvcc -G` pass to libNVVM's `cicc`. diff --git a/test/helpers/ptx.jl b/test/helpers/ptx.jl index 1e24c1ad..e4aa1986 100644 --- a/test/helpers/ptx.jl +++ b/test/helpers/ptx.jl @@ -65,14 +65,15 @@ function create_job(@nospecialize(func), @nospecialize(types); feature_set=:baseline, minthreads=nothing, maxthreads=nothing, blocks_per_sm=nothing, maxregs=nothing, - fastmath=false, system_atomics=true, target_debuginfo::Bool=false, + fastmath=false, fma_contraction=true, system_atomics=true, + target_debuginfo::Bool=false, patch::Bool=false, exception_sites::Bool=false, kwargs...) config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = PTXCompilerTarget(; cap, ptx, feature_set, backend, minthreads, maxthreads, blocks_per_sm, maxregs, - fastmath, system_atomics, + fastmath, fma_contraction, system_atomics, # `debuginfo` is the reflection keyword (`code_llvm`) debuginfo=target_debuginfo) params = CompilerParams(patch, exception_sites) diff --git a/test/ptx.jl b/test/ptx.jl index e7cf84c2..da1a84ef 100644 --- a/test/ptx.jl +++ b/test/ptx.jl @@ -1150,6 +1150,30 @@ end @test !occursin("sub.rn.f32", ptx) end +@testset "FMA contraction disabled" begin + function contract(x) + for _ in 1:8 + x = 2.001f0 * x - x + end + return x + end + + ptx = sprint(io->PTX.code_native(io, contract, Tuple{Float32}; backend, fma_contraction=false)) + @test occursin("mul.rn.f32", ptx) + @test occursin("sub.rn.f32", ptx) + @test !occursin("fma", ptx) + + # explicitly contractable operations are still fused + explicit(x) = muladd(2.001f0, x, -x) + ptx = sprint(io->PTX.code_native(io, explicit, Tuple{Float32}; backend, fma_contraction=false)) + @test occursin("fma.rn.f32", ptx) + + job, _ = PTX.create_job(contract, Tuple{Float32}; backend, fma_contraction=false) + fused, _ = PTX.create_job(contract, Tuple{Float32}; backend) + @test hash(job.config.target) != hash(fused.config.target) + @test GPUCompiler.cache_owner(job) != GPUCompiler.cache_owner(fused) +end + @testset "fastmath" begin # `fastmath=true` on the target calls `apply_fastmath!` from # `finish_linked_module!`, stamping `unsafe-fp-math` + fast-math flags on