Skip to content

Fix FP64 sincos, copysign, fract, and docs - #1757

Merged
rjodinchr merged 1 commit into
google:mainfrom
cyrne1-7208:fix-fp64-builtins
Nov 22, 2026
Merged

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

Conversation

@cyrne1-7208

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

Copy link
Copy Markdown
Contributor

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.

Comment thread lib/ReplaceOpenCLBuiltinPass.cpp Outdated
if (!Builtins::BuiltinWithGenericPointer(name))
return false;
if (clspv::Option::UseNativeBuiltins().count(builtin) > 0)
// NativeMathPass keeps the libclc implementation of FP64 sincos because

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.

Rather than adding an is_fp64_sincos special case that duplicates NativeMathPass's FP64 policy here, could we remove the UseNativeBuiltins check from skipBuiltinsWithGenericPointer altogether (along with its builtin parameter)?

That early return dates back to #1101, before #1103 added a second ReplaceOpenCLBuiltinPass after LowerAddrSpaceCastPass. Today, --cl-native-math already skips generic-pointer builtins in the first pass so LowerAddrSpaceCastPass rewrites them to concrete address spaces, and then the second ReplaceOpenCLBuiltinPass replaces whichever overloads had their bodies deleted by NativeMathPass while leaving preserved libclc bodies (like FP64 sincos) untouched. Doing the same for --use-native-builtins keeps NativeMathPass as the single source of truth and avoids the special case.

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.

Thanks! Fixed in eff46e79.

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
rjodinchr enabled auto-merge (squash) November 22, 2026 16:03
@rjodinchr
rjodinchr merged commit a75645a into google:main Nov 22, 2026
17 of 18 checks 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.

Support double-precision floating point

3 participants