Skip to content

Guard unsupported FP64 native math builtins and finalize FP64 tests/docs - #1753

Merged
rjodinchr merged 1 commit into
google:mainfrom
cyrne1-7208:fix-native-math-fp64
Nov 20, 2026
Merged

rjodinchr merged 1 commit into
google:mainfrom
cyrne1-7208:fix-native-math-fp64

Conversation

@cyrne1-7208

@cyrne1-7208 cyrne1-7208 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Ref #321.

Series follow-up PR (4/4) for #321 following #1750, #1751, and #1752 (remaining pre-existing FP64 edge cases are tracked in #321).

Summary

Keep libclc software implementations for FP64 math builtins that lack 64-bit GLSL.std.450 support in Vulkan SPIR-V under --cl-native-math / --use-native-builtins, add dedicated double operator tests, and remove the stale incomplete-FP64 note from docs/OpenCLCOnVulkan.md.

Problem

In SPIR-V GLSL.std.450, angle-conversion, transcendental, and power/logarithmic instructions (Radians, Degrees, Acos, Acosh, Asin, Asinh, Atan, Atanh, Atan2, Sin, Sinh, Cos, Cosh, Tan, Tanh, Exp, Exp2, Log, Log2, Pow) only support 16-bit and 32-bit floats. Under --cl-native-math or --use-native-builtins, NativeMathPass deleted the libclc bodies of double math builtins regardless of type, either emitting invalid 64-bit GLSL.std.450 instructions (spirv-val failure) or lowering via ReplaceOpenCLBuiltinPass into unlowered llvm.sincos (sincos, tanpi), which also broke transitive libclc callers such as erf, erfc, lgamma, lgamma_r, tgamma, and rootn (and relied on downstream ReplaceOpenCLBuiltinPass::replaceDegreesOrRadians for FP64 degrees / radians). In addition, the 64-Bit Floating-Point Types documentation still carried the note pointing to issue 321, and there was no standalone double operator lit test.

Change

  • In lib/NativeMathPass.cpp, add is_unsupported_fp64_glsl_op and keep_fp64_libclc_body to retain libclc bodies (!F.isDeclaration() && F.getReturnType()->getScalarType()->isDoubleTy()) for:
    • builtins mapping via Builtins::getExtInstEnum to the 20 16/32-bit-only GLSL.std.450 ops above (covering radians, degrees, sin..powr, and acospi..atan2pi), and
    • ReplaceableBuiltins math lowerings that expand to them (exp10, expm1, log10, log1p, pown, cospi, sinpi, tanpi, sincos).
  • Guard both body deletion (have_native_impl) and the --use-native-builtins unsupported-builtin error with !keep_libclc_body, leaving float native math and 64-bit native-capable GLSL/IR builtins (sqrt, rsqrt, floor, ceil, trunc, rint, fma, fmin, fmax, mix, step, smoothstep, sign, ldexp, frexp, fract, length, distance, normalize, cross, etc.) untouched (while FP64 degrees / radians lower to FMul via libclc / ReplaceOpenCLBuiltinPass).
  • Add test/double_operators.cl covering scalar and vector double arithmetic, negation, comparisons, and ternary selection (default and -vec3-to-vec4).
  • Add test/NativeBuiltins/MathBuiltins/double_native_math.cl, double_mixed_float.cl (capturing fp64_double, fp64_v3double where OpTypeVector <double> 3 is emitted, and fp64_v4double), and double_native_capable.cl.
  • Remove the obsolete incomplete-FP64 note referencing issue 321 in docs/OpenCLCOnVulkan.md.

Testing

  • Verified lit.py --filter="(NativeBuiltins|double_operators|double_degrees_radians)" passes 195/195 tests on both -arch=spirv32 and -arch=spirv64 (3,788 unrelated tests excluded), including --cl-native-math, --use-native-builtins, and -vec3-to-vec4 with spirv-val --target-env vulkan1.0.

@rjodinchr

Copy link
Copy Markdown
Collaborator

This is looking good, but as the doc part removes the fp64 incomplete, I'll wait for #1752 to land first.

@rjodinchr

Copy link
Copy Markdown
Collaborator

Could you rebase it on top-of-tree? I want to make sure that we have everything here, so the CI feedback is relevant.
Thank you

@cyrne1-7208

Copy link
Copy Markdown
Contributor Author

Got it, I'll get right on it!

@rjodinchr rjodinchr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing! Could you change Fixes #321 to Ref #321 (in both the commit message and PR description)? A few pre-existing FP64 issues remain that I'll document on #321 for follow-up PRs, so we shouldn't close the issue yet.

Other than that, this PR is good to land once the comments below are resolved.

// FP64-DAG: [[fp64_v4double:%[^ ]+]] = OpTypeVector [[fp64_double]] 4
// FP64-NOT: OpExtInst [[fp64_double]] {{.*}} {{(Acos|Acosh|Asin|Asinh|Atan|Atanh|Atan2|Sin|Sinh|Cos|Cosh|Tan|Tanh|Exp|Exp2|Log|Log2|Pow)($| )}}
// FP64-NOT: OpExtInst [[fp64_v4double]] {{.*}} {{(Acos|Acosh|Asin|Asinh|Atan|Atanh|Atan2|Sin|Sinh|Cos|Cosh|Tan|Tanh|Exp|Exp2|Log|Log2|Pow)($| )}}
// FP64-NOT: OpExtInst %v{{[23]}}double {{.*}} {{(Acos|Acosh|Asin|Asinh|Atan|Atanh|Atan2|Sin|Sinh|Cos|Cosh|Tan|Tanh|Exp|Exp2|Log|Log2|Pow)($| )}}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we capture [[fp64_v3double:%[^ ]+]] = OpTypeVector [[fp64_double]] 3 in FP64-DAG (just like fp64_v4double) instead of hardcoding %v{{[23]}}double? (test_mixed only uses double3, and capturing the ID avoids relying on spirv-dis friendly names.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6e904434. I've captured fp64_v3double under FP64-V3 for the --use-native-builtins run since --cl-native-math lowers double3 to %v4double.

Comment thread lib/NativeMathPass.cpp

bool is_unsupported_fp64_glsl_op(clspv::glsl::ExtInst Op) {
switch (Op) {
case clspv::glsl::ExtInst::ExtInstAcos:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool is_unsupported_fp64_glsl_op(clspv::glsl::ExtInst Op) {
switch (Op) {

  • case clspv::glsl::ExtInst::ExtInstRadians:
  • case clspv::glsl::ExtInst::ExtInstDegrees:
    case clspv::glsl::ExtInst::ExtInstAcos:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6e904434. I've added ExtInstRadians and ExtInstDegrees as suggested.

@rjodinchr rjodinchr mentioned this pull request Nov 19, 2026
2 of 6 tasks
@cyrne1-7208

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've updated the commit message and PR description to use Ref #321 and addressed both inline comments in 6e904434.

@rjodinchr rjodinchr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last small thing I missed in the previous pass: since we're keeping #321 open for the remaining FP64 builtin fixes, could you revert the docs/OpenCLCOnVulkan.md change here so we drop that note in the PR that closes #321? Everything else looks great and ready to merge once that's updated.

Ref google#321.

Series follow-up PR (4/4) for google#321 following google#1750, google#1751, and google#1752 (remaining pre-existing FP64 edge cases are tracked in google#321).

## Summary

Keep `libclc` software implementations for FP64 math builtins that lack 64-bit `GLSL.std.450` support in Vulkan SPIR-V under `--cl-native-math` / `--use-native-builtins`, add dedicated `double` operator tests.

## Problem

In SPIR-V `GLSL.std.450`, angle-conversion, transcendental, and power/logarithmic instructions (`Radians`, `Degrees`, `Acos`, `Acosh`, `Asin`, `Asinh`, `Atan`, `Atanh`, `Atan2`, `Sin`, `Sinh`, `Cos`, `Cosh`, `Tan`, `Tanh`, `Exp`, `Exp2`, `Log`, `Log2`, `Pow`) only support 16-bit and 32-bit floats. Under `--cl-native-math` or `--use-native-builtins`, `NativeMathPass` deleted the `libclc` bodies of `double` math builtins regardless of type, either emitting invalid 64-bit `GLSL.std.450` instructions (`spirv-val` failure) or lowering via `ReplaceOpenCLBuiltinPass` into unlowered `llvm.sincos` (`sincos`, `tanpi`), which also broke transitive `libclc` callers such as `erf`, `erfc`, `lgamma`, `lgamma_r`, `tgamma`, and `rootn` (and relied on downstream `ReplaceOpenCLBuiltinPass::replaceDegreesOrRadians` for FP64 `degrees` / `radians`). In addition, there was no standalone `double` operator lit test.

## Change

- In `lib/NativeMathPass.cpp`, add `is_unsupported_fp64_glsl_op` and `keep_fp64_libclc_body` to retain `libclc` bodies (`!F.isDeclaration() && F.getReturnType()->getScalarType()->isDoubleTy()`) for:
  - builtins mapping via `Builtins::getExtInstEnum` to the 20 16/32-bit-only `GLSL.std.450` ops above (covering `radians`, `degrees`, `sin..powr`, and `acospi..atan2pi`), and
  - `ReplaceableBuiltins` math lowerings that expand to them (`exp10`, `expm1`, `log10`, `log1p`, `pown`, `cospi`, `sinpi`, `tanpi`, `sincos`).
- Guard both body deletion (`have_native_impl`) and the `--use-native-builtins` unsupported-builtin error with `!keep_libclc_body`, leaving `float` native math and 64-bit native-capable GLSL/IR builtins (`sqrt`, `rsqrt`, `floor`, `ceil`, `trunc`, `rint`, `fma`, `fmin`, `fmax`, `mix`, `step`, `smoothstep`, `sign`, `ldexp`, `frexp`, `fract`, `length`, `distance`, `normalize`, `cross`, etc.) untouched (while FP64 `degrees` / `radians` lower to `FMul` via `libclc` / `ReplaceOpenCLBuiltinPass`).
- Add `test/double_operators.cl` covering scalar and vector `double` arithmetic, negation, comparisons, and ternary selection (default and `-vec3-to-vec4`).
- Add `test/NativeBuiltins/MathBuiltins/double_native_math.cl`, `double_mixed_float.cl` (capturing `fp64_double`, `fp64_v3double` where `OpTypeVector <double> 3` is emitted, and `fp64_v4double`), and `double_native_capable.cl`.

## Testing

- Verified `lit.py --filter="(NativeBuiltins|double_operators|double_degrees_radians)"` passes `195/195` tests on both `-arch=spirv32` and `-arch=spirv64` (`3,788` unrelated tests excluded), including `--cl-native-math`, `--use-native-builtins`, and `-vec3-to-vec4` with `spirv-val --target-env vulkan1.0`.
@cyrne1-7208

Copy link
Copy Markdown
Contributor Author

Sorry, one last small thing I missed in the previous pass: since we're keeping #321 open for the remaining FP64 builtin fixes, could you revert the docs/OpenCLCOnVulkan.md change here so we drop that note in the PR that closes #321? Everything else looks great and ready to merge once that's updated.

Thanks for catching that. I've reverted the docs/OpenCLCOnVulkan.md change.

@rjodinchr
rjodinchr enabled auto-merge (squash) November 20, 2026 08:39
@rjodinchr
rjodinchr merged commit be544a1 into google:main Nov 20, 2026
17 of 18 checks passed
@cyrne1-7208
cyrne1-7208 deleted the fix-native-math-fp64 branch November 20, 2026 10:51
cyrne1-7208 added a commit to cyrne1-7208/clspv that referenced this pull request Nov 21, 2026
Fixes google#321.

Follow-up to google#1753 resolving the items in google#321 (comment).

## Summary

Fix FP64 generic-pointer `sincos`, `replaceCopysign` sign masks, and `replaceFract` clamping constant, and remove the stale google#321 note from `docs/OpenCLCOnVulkan.md`.

## Problem

- `skipBuiltinsWithGenericPointer` returned `false` whenever `--use-native-builtins` contained the builtin, lowering generic-pointer FP64 `sincos` to `llvm.sin.f64` / `llvm.cos.f64` (`undefined reference to 'llvm.sincos.f64'`) instead of rewriting to the `libclc` implementation kept by `NativeMathPass`.
- `ReplaceLLVMIntrinsicsPass::replaceCopysign` shifted a 32-bit `1` by `63` for `double`, producing wrong 64-bit sign masks and corrupting `copysign(double)` and `round(double)`.
- `ReplaceOpenCLBuiltinPass::replaceFract` shifted a 32-bit `1` by `53` for `double`, evaluating `kJustUnderOneScalar` to `~2.33e-10` instead of `0x1.fffffffffffffp-1`.
- `docs/OpenCLCOnVulkan.md` still contained the incomplete-FP64 note referencing issue 321.

## Change

- Keep skipping FP64 `sincos` in `skipBuiltinsWithGenericPointer` (`lib/ReplaceOpenCLBuiltinPass.cpp`) under `--use-native-builtins=sincos`.
- Use `APInt::getSignMask` and its complement in `ReplaceLLVMIntrinsicsPass::replaceCopysign` (`lib/ReplaceLLVMIntrinsicsPass.cpp`).
- Use `1ULL << num_significand_bits` and `-static_cast<int>(num_significand_bits)` in `ReplaceOpenCLBuiltinPass::replaceFract` (`lib/ReplaceOpenCLBuiltinPass.cpp`).
- Add FP64 tests (`sincos_double_native.cl`, `copysign_double{,4}.cl`, `double_round.cl`, `double_fract_private.cl`).
- Remove the issue 321 note from `docs/OpenCLCOnVulkan.md`.

## Testing

- Full `lit.py` suite passed `3988/3988` on both `-arch=spirv32` and `-arch=spirv64`.
- `clang-format --dry-run --Werror lib/ReplaceOpenCLBuiltinPass.cpp lib/ReplaceLLVMIntrinsicsPass.cpp` passed.
cyrne1-7208 added a commit to cyrne1-7208/clspv that referenced this pull request Nov 22, 2026
Fixes google#321.

Follow-up to google#1753 resolving the items in google#321 (comment).

## Summary

Fix FP64 generic-pointer `sincos`, `replaceCopysign` sign masks, and `replaceFract` clamping constant, and remove the stale google#321 note from `docs/OpenCLCOnVulkan.md`.

## Problem

- `skipBuiltinsWithGenericPointer` returned `false` whenever `--use-native-builtins` contained the builtin, lowering generic-pointer FP64 `sincos` to `llvm.sin.f64` / `llvm.cos.f64` (`undefined reference to 'llvm.sincos.f64'`) instead of rewriting to the `libclc` implementation kept by `NativeMathPass`.
- `ReplaceLLVMIntrinsicsPass::replaceCopysign` shifted a 32-bit `1` by `63` for `double`, producing wrong 64-bit sign masks and corrupting `copysign(double)` and `round(double)`.
- `ReplaceOpenCLBuiltinPass::replaceFract` shifted a 32-bit `1` by `53` for `double`, evaluating `kJustUnderOneScalar` to `~2.33e-10` instead of `0x1.fffffffffffffp-1`.
- `docs/OpenCLCOnVulkan.md` still contained the incomplete-FP64 note referencing issue 321.

## Change

- Remove the `UseNativeBuiltins` check and `builtin` parameter from `skipBuiltinsWithGenericPointer` (`lib/ReplaceOpenCLBuiltinPass.cpp`) so `NativeMathPass` governs all generic-pointer builtins uniformly.
- Use `APInt::getSignMask` and its complement in `ReplaceLLVMIntrinsicsPass::replaceCopysign` (`lib/ReplaceLLVMIntrinsicsPass.cpp`).
- Use `1ULL << num_significand_bits` and `-static_cast<int>(num_significand_bits)` in `ReplaceOpenCLBuiltinPass::replaceFract` (`lib/ReplaceOpenCLBuiltinPass.cpp`).
- Add FP64 tests (`sincos_double_native.cl`, `copysign_double{,4}.cl`, `double_round.cl`, `double_fract_private.cl`).
- Remove the issue 321 note from `docs/OpenCLCOnVulkan.md`.

## Testing

- Targeted `lit.py` run covering `BuiltinsWithGenericPointer`, `AddressSpaceCast`, `NativeBuiltins`, `HalfStorage`, `PhysicalStorageBuffers`, and `MathBuiltins/{copysign,fract,round,sincos}` passed `329/329` on both `-arch=spirv32` and `-arch=spirv64` (earlier full `lit.py` run before removing the `UseNativeBuiltins` early return passed `3988/3988` on both).
- `clang-format --dry-run --Werror lib/ReplaceOpenCLBuiltinPass.cpp lib/ReplaceLLVMIntrinsicsPass.cpp` passed.
rjodinchr pushed a commit that referenced this pull request Nov 22, 2026
Fixes #321.

Follow-up to #1753 resolving the items in
#321 (comment).

## Summary

Fix FP64 generic-pointer `sincos`, `replaceCopysign` sign masks, and
`replaceFract` clamping constant, and remove the stale #321 note from
`docs/OpenCLCOnVulkan.md`.

## Problem

- `skipBuiltinsWithGenericPointer` returned `false` whenever
`--use-native-builtins` contained the builtin, lowering generic-pointer
FP64 `sincos` to `llvm.sin.f64` / `llvm.cos.f64` (`undefined reference
to 'llvm.sincos.f64'`) instead of rewriting to the `libclc`
implementation kept by `NativeMathPass`.
- `ReplaceLLVMIntrinsicsPass::replaceCopysign` shifted a 32-bit `1` by
`63` for `double`, producing wrong 64-bit sign masks and corrupting
`copysign(double)` and `round(double)`.
- `ReplaceOpenCLBuiltinPass::replaceFract` shifted a 32-bit `1` by `53`
for `double`, evaluating `kJustUnderOneScalar` to `~2.33e-10` instead of
`0x1.fffffffffffffp-1`.
- `docs/OpenCLCOnVulkan.md` still contained the incomplete-FP64 note
referencing issue 321.

## Change

- Remove the `UseNativeBuiltins` check and `builtin` parameter from
`skipBuiltinsWithGenericPointer` (`lib/ReplaceOpenCLBuiltinPass.cpp`) so
`NativeMathPass` governs all generic-pointer builtins uniformly.
- Use `APInt::getSignMask` and its complement in
`ReplaceLLVMIntrinsicsPass::replaceCopysign`
(`lib/ReplaceLLVMIntrinsicsPass.cpp`).
- Use `1ULL << num_significand_bits` and
`-static_cast<int>(num_significand_bits)` in
`ReplaceOpenCLBuiltinPass::replaceFract`
(`lib/ReplaceOpenCLBuiltinPass.cpp`).
- Add FP64 tests (`sincos_double_native.cl`, `copysign_double{,4}.cl`,
`double_round.cl`, `double_fract_private.cl`).
- Remove the issue 321 note from `docs/OpenCLCOnVulkan.md`.

## Testing

- Targeted `lit.py` run covering `BuiltinsWithGenericPointer`,
`AddressSpaceCast`, `NativeBuiltins`, `HalfStorage`,
`PhysicalStorageBuffers`, and
`MathBuiltins/{copysign,fract,round,sincos}` passed `329/329` on both
`-arch=spirv32` and `-arch=spirv64` (earlier full `lit.py` run before
removing the `UseNativeBuiltins` early return passed `3988/3988` on
both).
- `clang-format --dry-run --Werror lib/ReplaceOpenCLBuiltinPass.cpp
lib/ReplaceLLVMIntrinsicsPass.cpp` passed.
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.

3 participants