Skip to content

ILGPU V2.0: Fused Multiply-Add Setting for All Backends - #1694

Open
m4rs-mt wants to merge 4 commits into
atomic_read_back_orderfrom
fused_multiply_add
Open

m4rs-mt wants to merge 4 commits into
atomic_read_back_orderfrom
fused_multiply_add

Conversation

@m4rs-mt

@m4rs-mt m4rs-mt commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Builds on #1693. A new compilation property, FusedMultiplyAddMode, controls whether the native
compilers may fuse multiplications and additions into multiply-adds. By default (Fast), every GPU backend keeps its
fused multiply-adds for the best performance. With Off, no backend fuses, so every operation rounds like it does on
the host, and Metal divides Half values by constants with correct rounding.

<ILGPUCompilationOptions>--fma Off</ILGPUCompilationOptions>
data[index] = a * b - c * d;   // with --fma Off: the products are rounded before the subtraction

A note on authorship

This PR was done with AI-assistant pair programming (Claude via Claude Code). The commits carry a
Co-Authored-By: Claude trailer.

What's in this PR

1. The FusedMultiplyAddMode property and the --fma option (Src/ILGPUC/CompilationProperties.cs, Src/ILGPUC/ProgramOptions/Program.CompilationPropertiesOptions.cs, Src/ILGPUC/IR/ModuleValues/Module.cs)

  • FusedMultiplyAddMode.Fast (the default) and FusedMultiplyAddMode.Off, selected on the command line and in
    ILGPUCompilationOptions with --fma fast|off. The module carries the mode to code generation.

2. Fusion off on every backend (Src/ILGPUC/Backends/)

  • CUDA: nvcc runs with --fmad=false (CUDA/CUDABackend.cs).
  • ROCm: hipcc runs with -ffp-contract=off (ROCm/ROCmBackend.cs).
  • OpenCL: the source starts with #pragma OPENCL FP_CONTRACT OFF, which applies to the offline compilers and to
    sources built at runtime (OpenCL/CLLanguageConfiguration.cs).
  • Metal: the source starts with #pragma STDC FP_CONTRACT OFF, which also applies to the runtime source compilation,
    and the Metal compiler runs with -ffp-contract=off (Metal/MetalLanguageConfiguration.cs, Metal/MetalBackend.cs).
  • Backend.GetFusionFlag adds the native flag; LanguageConfiguration.GetPragmas receives the fusion mode
    (Backend.cs, LanguageConfiguration.cs, CodeGenerator.cs).
  • CPU kernels are C# code that the .NET JIT never fuses, so they match the host in both modes.

3. Correctly rounded Half divisions by constants on Metal (Src/ILGPUC/Backends/ExpressionEmitter.cs, IntrinsicEmitter.cs, Metal/MetalIntrinsicEmitter.cs, Metal/MetalLanguageConfiguration.cs)

  • With fusion off and no fast math mode, a Half division by a constant that is not a power of two divides
    through ilgpu_div_half, which corrects the single-precision quotient by its exact remainder and rounds to the
    correctly rounded Half quotient, matching System.Half for every input.

4. Documentation (Docs/03_Advanced/04_Math-Functions.md, Src/ILGPUC/CompilationProperties.cs)

  • The new "Fused Multiply-Adds" section explains the default, how to turn fusion off, the per-backend
    mechanisms, the CPU behaviour and the Metal Half divisions; the XML docs describe both modes and the Metal fast
    math.

5. Tests (Src/ILGPUC.Tests/)

  • HalfPrecisionExecutionTests.HalfDivision_ProducesCorrectOutput (TestPrograms/HalfPrecision/HalfDivision.cs):
    Half and float divisions and differences of quotients and products run on every backend with fusion
    allowed; CPU kernels match the host in every line.
  • HalfPrecisionExecutionTests.HalfDivisionWithoutFusion_ProducesCorrectOutput: with fusion off, every
    result is bit-exact with System.Half and float on the host.
  • HalfPrecisionExecutionTests.HalfDivisionByConstants_ProducesCorrectOutput
    (TestPrograms/HalfPrecision/HalfDivisionByConstants.cs): every Half value divided by ten constants matches the
    host with fusion off; HalfDivisionByConstantsNative_ProducesOutput runs the native division in the default
    and fast math modes.
  • FusedMultiplyAddSourceTests: --fmad=false for CUDA, -ffp-contract=off for ROCm and Metal, the OpenCL and Metal
    pragmas in the sources and in the sources embedded for runtime compilation, all only with fusion off; unchanged
    CUDA, ROCm and CPU sources; the --fma option and its default.
  • MetalSourceTests.HalfDivision_EmitsPreciseMath and MetalSourceTests.MathMode_SelectsCompileOptions: the Metal
    division helper, pragma and compile options for every combination of math and fusion modes.

Depends on

#1693 (based on its head; the base is retargeted once it merges).

Testing

Suite This PR
Full test suite, Apple Silicon (CPU, Metal, compile-only for all backends) 11050 passed, 2056 skipped, 0 failed
CUDA backend tests with nvcc, NVIDIA GB10 1131 passed, 0 failed
CUDA execution tests, NVIDIA GB10 498 passed, 3 skipped, 0 failed
Release build with TreatWarningsAsErrors 0 errors
Every commit of the PR builds

🤖 Generated with Claude Code

m4rs-mt and others added 4 commits October 24, 2026 21:35
…ion, which allow multiply-add fusion on all backends by default and turn it off with --fmad=false on CUDA, -ffp-contract=off on ROCm and Metal, the FP_CONTRACT pragmas in OpenCL and Metal sources and correctly rounded half divisions by constants on Metal.

Co-Authored-By: Claude <noreply@anthropic.com>
… fusion allowed and turned off, and Metal tests for the correctly rounded half divisions by constants and the fusion and math compile options.

Co-Authored-By: Claude <noreply@anthropic.com>
…ich fuse multiply-adds on all GPU backends by default and compute every operation like the host when turned off, and the fast math of Metal.

Co-Authored-By: Claude <noreply@anthropic.com>
…urces embedded for runtime compilation and the --fma option.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
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