diff --git a/src/device/gcn/math.jl b/src/device/gcn/math.jl index d45b2e6f5..87247180c 100644 --- a/src/device/gcn/math.jl +++ b/src/device/gcn/math.jl @@ -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) diff --git a/test/core/codegen.jl b/test/core/codegen.jl index 9ab1065a7..319705d3d 100644 --- a/test/core/codegen.jl +++ b/test/core/codegen.jl @@ -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() @@ -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)} @@ -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 diff --git a/test/device/math.jl b/test/device/math.jl index a2e433e85..2b84e7d9b 100644 --- a/test/device/math.jl +++ b/test/device/math.jl @@ -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