Skip to content

Use GPUToolbox's Float64-free math overrides on devices without fp64 - #640

Merged
maleadt merged 1 commit into
mainfrom
tb/gputoolbox-math
Sep 23, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/gputoolbox-math

Conversation

@maleadt

@maleadt maleadt commented Sep 23, 2026

Copy link
Copy Markdown
Member

Several Base methods compute single-precision results in double precision, so they fail to compile for devices without Float64 support (e.g. Intel Arc): div, fld and cld of Float32 values on Julia 1.12 and 1.13, sind and friends, inversion and division of ComplexF32, sincospi/cispi, and comparisons between Float32/Float16 values and 32-bit integers. On such devices, this stacks GPUToolbox.Overlays.float64_overrides (JuliaGPU/GPUToolbox.jl#21, GPUToolbox 3.1) underneath SPIRVIntrinsics' method table, which replaces them with Float64-free implementations. Devices that support Float64 keep using Base's more accurate implementations.

This also removes the div(::Float32, ::Float32) = trunc(x / y) quirk, which truncated the rounded quotient and thus returned incorrect results on all devices, e.g. div(1f0, 0.1f0) == 10 instead of 9. Devices with Float64 now use Base's implementation, and devices without use the override above, which is exact whenever the quotient is.

I couldn't run this on hardware; on macOS, I checked that kernels calling div, fld, sind, inv// of ComplexF32 and hypot compile without any Float64 when targeting a device without Float64 support, and that they keep Base's implementations otherwise. The same stacking is used by OpenCL.jl, where the tests compile these functions for a device without Float64.

Base computes some single-precision math in double precision, e.g.,
`div` on Float32 (on Julia 1.12 and 1.13), `sind` and `cosd`, or
inversion of ComplexF32. On devices that don't support Float64, stack
GPUToolbox.Overlays.float64_overrides underneath SPIRVIntrinsics'
method table to keep these computations in single precision.

This replaces the `div(::Float32, ::Float32)` quirk, which truncated
the rounded quotient and thus returned incorrect results (e.g.,
`div(1f0, 0.1f0) == 10`), for all devices.
@github-actions

Copy link
Copy Markdown
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic main) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl
index f147240..7e36726 100644
--- a/src/compiler/compilation.jl
+++ b/src/compiler/compilation.jl
@@ -37,10 +37,12 @@ function GPUCompiler.method_table_view(job::oneAPICompilerJob)
         parent = SPIRVIntrinsics.method_table
     else
         # keep single-precision math that Base computes in Float64 out of double precision
-        parent = GPUCompiler.StackedMethodTable(job.world, SPIRVIntrinsics.method_table,
-                                                GPUToolbox.Overlays.float64_overrides)
+        parent = GPUCompiler.StackedMethodTable(
+            job.world, SPIRVIntrinsics.method_table,
+            GPUToolbox.Overlays.float64_overrides
+        )
     end
-    GPUCompiler.StackedMethodTable(job.world, method_table, parent)
+    return GPUCompiler.StackedMethodTable(job.world, method_table, parent)
 end
 
 # filter out OpenCL built-ins

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.66%. Comparing base (d2e06ce) to head (7cff0f8).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #640      +/-   ##
==========================================
+ Coverage   81.53%   81.66%   +0.13%     
==========================================
  Files          50       50              
  Lines        3607     3611       +4     
==========================================
+ Hits         2941     2949       +8     
+ Misses        666      662       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt merged commit f110df6 into main Sep 23, 2026
5 checks passed
@maleadt
maleadt deleted the tb/gputoolbox-math branch September 23, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant