Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/device/gcn/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ for jltype in (Float64, Float32, Float16)
end
end

# Julia's min/max propagate NaNs and order -0 before +0, like llvm.minimum/maximum. LLVM only
# expands those intrinsics for every AMDGPU target since version 19, so older versions of
# Julia's LLVM get an explicit implementation.
@static if LLVM.version() < v"19"
@device_override Base.min(x::T, y::T) where {T<:Union{Float16, Float32, Float64}} =
ifelse((x < y) | isnan(x) | ((x == y) & signbit(x)), x, y)
@device_override Base.max(x::T, y::T) where {T<:Union{Float16, Float32, Float64}} =
ifelse((x > y) | isnan(x) | ((x == y) & signbit(y)), x, y)
end

@device_override @inline function Base.:(^)(x::Float32, y::Int64)
y == -1 && return inv(x)
y == 0 && return one(x)
Expand Down
84 changes: 59 additions & 25 deletions test/core/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ using AMDGPU: Device, ROCArray, @roc, UnsafeAtomics
using AMDGPU.Device: sync_workgroup, workitemIdx, workgroupIdx, workgroupDim
using KernelAbstractions: @atomic

# compile for a GPU other than the local one
function compile_offline(f, tt, format; dev_isa="gfx90a", backend=:external,
unsafe_fp_atomics=true, atomic_memory_assumptions=true,
validate=true)
wf64 = !startswith(dev_isa, "gfx1")
features = wf64 ? "-wavefrontsize32,+wavefrontsize64" : "+wavefrontsize32,-wavefrontsize64"
target = GPUCompiler.GCNCompilerTarget(; dev_isa, features, backend)
params = AMDGPU.Compiler.HIPCompilerParams(wf64, unsafe_fp_atomics,
atomic_memory_assumptions)
config = GPUCompiler.CompilerConfig(target, params; kernel=true, always_inline=true,
validate)
job = GPUCompiler.CompilerJob(GPUCompiler.methodinstance(typeof(f), tt), config)
GPUCompiler.JuliaContext() do _
if format === :llvm
mod, _ = GPUCompiler.compile(:llvm, job)
ir = string(mod)
LLVM.dispose(mod)
ir
else
asm, meta = GPUCompiler.compile(:asm, job)
LLVM.dispose(meta.ir)
asm
end
end
end

@testset "Synchronization" begin
function synckern()
sync_workgroup()
Expand Down Expand Up @@ -179,31 +205,6 @@ end
return
end

# compile for a GPU other than the local one, e.g. one whose atomics depend on the metadata
function compile_offline(f, tt, format; dev_isa="gfx90a", backend=:external,
unsafe_fp_atomics=true, atomic_memory_assumptions=true,
validate=true)
wf64 = !startswith(dev_isa, "gfx1")
features = wf64 ? "-wavefrontsize32,+wavefrontsize64" : "+wavefrontsize32,-wavefrontsize64"
target = GPUCompiler.GCNCompilerTarget(; dev_isa, features, backend)
params = AMDGPU.Compiler.HIPCompilerParams(wf64, unsafe_fp_atomics,
atomic_memory_assumptions)
config = GPUCompiler.CompilerConfig(target, params; kernel=true, always_inline=true,
validate)
job = GPUCompiler.CompilerJob(GPUCompiler.methodinstance(typeof(f), tt), config)
GPUCompiler.JuliaContext() do _
if format === :llvm
mod, _ = GPUCompiler.compile(:llvm, job)
ir = string(mod)
LLVM.dispose(mod)
ir
else
asm, meta = GPUCompiler.compile(:asm, job)
LLVM.dispose(meta.ir)
asm
end
end
end
rmw_tt(T, op, scope; as=AMDGPU.Device.AS.Global) =
Tuple{Core.LLVMPtr{T,as}, T, typeof(op), typeof(scope)}

Expand Down Expand Up @@ -326,3 +327,36 @@ end
k(; groupsize=256)
AMDGPU.synchronize()
end

@testset "Float min/max" begin
function minmax_kernel(p, x, y)
unsafe_store!(p, min(x, y), 1)
unsafe_store!(p, max(x, y), 2)
return
end

# never ocml's NaN-ignoring llvm.minnum/maxnum, and llvm.minimum/maximum only
# where Julia's LLVM expands them for every target (LLVM 19+)
configs = Tuple{Symbol, String}[]
if AMDGPU.Compiler.AMDGPU_LLVM_Backend_jll.is_available()
append!(configs, (:external, isa) for isa in ("gfx90a", "gfx942", "gfx1200"))
end
if :AMDGPU in LLVM.backends()
push!(configs, (:inprocess, "gfx90a"))
LLVM.version() >= v"17" && push!(configs, (:inprocess, "gfx942"))
LLVM.version() >= v"18" && push!(configs, (:inprocess, "gfx1200"))
end
for T in (Float16, Float32, Float64), (backend, dev_isa) in configs
tt = Tuple{Core.LLVMPtr{T, AMDGPU.Device.AS.Global}, T, T}
ir = compile_offline(minmax_kernel, tt, :llvm; dev_isa, backend)
@test !occursin("llvm.minnum", ir) && !occursin("llvm.maxnum", ir)
has_intrinsics = occursin("llvm.minimum", ir) && occursin("llvm.maximum", ir)
@test has_intrinsics == (LLVM.version() >= v"19")

asm = compile_offline(minmax_kernel, tt, :asm; dev_isa, backend)
if T == Float32 && dev_isa == "gfx1200" && has_intrinsics
# natively supported (on the SALU here, since the arguments are uniform)
@test occursin(r"[sv]_minimum_f32", asm) && occursin(r"[sv]_maximum_f32", asm)
end
end
end
33 changes: 27 additions & 6 deletions test/device/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,34 @@ using Base.FastMath
end

@testset "min/max" begin
# Julia's semantics: NaN propagates, and -0.0 < 0.0
a = [NaN, 1, -0.0, 0.0, 2, NaN, Inf, -Inf]
b = [1, NaN, 0.0, -0.0, 3, NaN, NaN, 1]
# NaNs propagate and -0 < +0, as on the host
for T in (Float16, Float32, Float64)
x, y = T.(a), T.(b)
@test isequal(Array(max.(ROCArray(x), ROCArray(y))), max.(x, y))
@test isequal(Array(min.(ROCArray(x), ROCArray(y))), min.(x, y))
vals = T[NaN, -Inf, Inf, -floatmax(T), -1, -0.0, 0.0, 1, floatmax(T),
nextfloat(zero(T)), -nextfloat(zero(T)), floatmin(T) / 2, floatmin(T)]
xs = repeat(vals; outer=length(vals))
ys = repeat(vals; inner=length(vals))
dxs, dys = ROCArray(xs), ROCArray(ys)
@test isequal(Array(min.(dxs, dys)), min.(xs, ys))
@test isequal(Array(max.(dxs, dys)), max.(xs, ys))
end

# Atomix implements these with a CAS loop around `min`/`max`, or with
# `atomicrmw fminimum`/`fmaximum` on recent Julia versions
function atomic_minmax!(a, xs)
x = @inbounds xs[AMDGPU.workitemIdx().x]
@inbounds AMDGPU.@atomic max(a[1], x)
@inbounds AMDGPU.@atomic min(a[2], x)
return
end
for T in (Float16, Float32, Float64)
for (xs, expected) in ((T[1, NaN, 2], (T(NaN), T(NaN))),
(T[NaN, 2, 1], (T(NaN), T(NaN))),
(T[-0.0, 0.0], (T(0.0), T(-0.0))),
(T[0.0, -0.0], (T(0.0), T(-0.0))))
a = ROCArray(T[xs[1], xs[1]])
@roc groupsize=length(xs) atomic_minmax!(a, ROCArray(xs))
@test isequal(Tuple(Array(a)), expected)
end
end
end

Expand Down