From a83b6441d78105616c6aecb7d9bab4421e9a2c9a Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 4 Oct 2026 15:51:41 +0200 Subject: [PATCH] Use Julia's min and max for floats The device overrides of `Base.min` and `Base.max` for floats called OCML's `__ocml_min`/`__ocml_max`, which, like C's `fmin`/`fmax`, return the other argument for a NaN, while Julia's `min` and `max` return NaN. Drop them: Julia's own definitions (`llvm.minimum`/`llvm.maximum` on Julia 1.12+, arithmetic before) compile for AMDGPU. Assisted-by: Claude Code (Opus 5.5) --- src/device/gcn/math.jl | 6 ------ test/device/math.jl | 11 +++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/device/gcn/math.jl b/src/device/gcn/math.jl index df1cc231f..d45b2e6f5 100644 --- a/src/device/gcn/math.jl +++ b/src/device/gcn/math.jl @@ -60,12 +60,6 @@ for jltype in (Float64, Float32, Float16) @eval @device_override Base.fma(x::$jltype, y::$jltype, z::$jltype) = ccall( $("extern __ocml_fma_$(fntypes[jltype])"), llvmcall, $jltype, ($jltype, $jltype, $jltype), x, y, z) - @eval @device_override Base.min(x::$jltype, y::$jltype) = ccall( - $("extern __ocml_min_$(fntypes[jltype])"), llvmcall, $jltype, ($jltype, $jltype), x, y) - - @eval @device_override Base.max(x::$jltype, y::$jltype) = ccall( - $("extern __ocml_max_$(fntypes[jltype])"), llvmcall, $jltype, ($jltype, $jltype), x, y) - @eval @device_override Base.copysign(x::$jltype, y::$jltype) = ccall( $("extern __ocml_copysign_$(fntypes[jltype])"), llvmcall, $jltype, ($jltype, $jltype), x, y) diff --git a/test/device/math.jl b/test/device/math.jl index 618eada48..a2e433e85 100644 --- a/test/device/math.jl +++ b/test/device/math.jl @@ -20,6 +20,17 @@ using Base.FastMath end 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] + 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)) + end +end + @testset "Fast min/max" begin function ker!(x) x[1] = @fastmath max(x[1], zero(eltype(x)))