Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/ptx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down
5 changes: 3 additions & 2 deletions test/helpers/ptx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions test/ptx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading