Repository navigation
Conversation
… FMA SPIR-V can only carry the `contract` fast-math flag with SPV_KHR_float_controls2, which few drivers support. Without it, the back-ends drop the flag and mark every kernel `ContractionOff`, so Julia's `muladd` reaches the driver as a separate multiply and add, and is never fused: on PoCL, `y[i] = muladd(a[i], x[i], y[i])` compiles to `vmulps` + `vaddps`. Add a `fma` field to `SPIRVCompilerTarget`, for front-ends to set when the device has a fused multiply-add (OpenCL's `CL_FP_FMA`). Then `contract_muladd!` fuses an `fadd`/`fsub` and an `fmul` that both allow contraction, if the `fmul` has no other use, into `llvm.fmuladd`, which the back-ends translate to OpenCL's `fma`. On PoCL, that compiles to a vectorized `vfmadd231ps`, bit-identical to `fma`. Additions that may be reassociated are left alone: the back-ends emit those as `FPFastMathMode Fast`, which already lets the driver contract them, and fusing them would keep the driver from vectorizing `@simd` reductions, as `fma` can't carry the flag. Assisted-by: Claude Code (Opus 5.5)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1010 +/- ##
==========================================
- Coverage 89.50% 87.44% -2.07%
==========================================
Files 31 31
Lines 6905 7118 +213
==========================================
+ Hits 6180 6224 +44
- Misses 725 894 +169 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SPIR-V can only carry LLVM's
contractfast-math flag withSPV_KHR_float_controls2, which few drivers support. Without it, the back-ends drop the flag and mark every kernelContractionOff, so Julia'smuladdreaches the driver as a separateOpFMul+OpFAddand is never fused. On PoCL,y[i] = muladd(a[i], x[i], y[i])compiles tovmulps+vaddps(JuliaGPU/KernelAbstractions.jl#865).Julia's own contraction can't help here: since JuliaLang/julia#55802 (1.12),
LowerSIMDLooponly marks thefmulascontractand leaves the fusing to the backend. On older Julia,CombineMulAdddid the same.This PR:
fma::Bool = falsefield toSPIRVCompilerTarget, for front-ends to set when the device has a fused multiply-add (OpenCL'sCL_FP_FMA);contract_muladd!(infinish_ir!) rewrites anfadd/fsuband anfmulthat both allow contraction intollvm.fmuladd, if thefmulhas no other use (as LLVM's instruction selection does). Both back-ends translatellvm.fmuladdto OpenCL'sfma:fadd (fmul a, b), c→fmuladd(a, b, c)fsub (fmul a, b), c→fmuladd(a, b, -c)fsub c, (fmul a, b)→fmuladd(-a, b, c)Additions that may be reassociated are left alone. The back-ends emit
reassocasFPFastMathMode Fast, which already lets the driver contract them (PoCL fuses@fastmath x * y + zeither way). Fusing them would also keep the driver from vectorizing@simdreductions, sincefmacan't carry the flag: a@simddot product on PoCL went from 8-wide vector code to a scalarvfmadd231sschain before this exclusion.Why opt-in: OpenCL's
fmamust be correctly rounded, so a device without a fused multiply-add has to emulate it, which is slow.Results on PoCL
AMD Ryzen 9 5950X (AVX2 + FMA), via JuliaGPU/KernelAbstractions.jl#892:
fma = truefma = falsemuladd(a[i], x[i], y[i])vfmadd231ps(ymm), bit-identical tofmavmulps+vaddpsmuladd(a, x, -y),muladd(-y, x, a)vfmsub213ps/vfnmadd213ps, bit-identicala * x + y(nocontract)@simddot productfma = falseTesting
A new "contraction" testset, for both back-ends, checks:
fma;contract, and@fastmathcode.The SPIR-V tests pass locally (733/733).
Related:
SPV_KHR_float_controls2) are the alternative once drivers accept the extension. That route needs PoCL with LLVM 23, while this one works with today'spocl_standalone_jll.muladdintofmaon devices with a fused multiply-add KernelAbstractions.jl#892 setsfmafrom the device.🤖 Generated with Claude Code