Repository navigation
Fix FP64 sincos, copysign, fract, and docs - #1757
Conversation
| if (!Builtins::BuiltinWithGenericPointer(name)) | ||
| return false; | ||
| if (clspv::Option::UseNativeBuiltins().count(builtin) > 0) | ||
| // NativeMathPass keeps the libclc implementation of FP64 sincos because |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
73a8e67 to
eff46e7
Compare
Fixes #321.
Follow-up to #1753 resolving the items in #321 (comment).
Summary
Fix FP64 generic-pointer
sincos,replaceCopysignsign masks, andreplaceFractclamping constant, and remove the stale #321 note fromdocs/OpenCLCOnVulkan.md.Problem
skipBuiltinsWithGenericPointerreturnedfalsewhenever--use-native-builtinscontained the builtin, lowering generic-pointer FP64sincostollvm.sin.f64/llvm.cos.f64(undefined reference to 'llvm.sincos.f64') instead of rewriting to thelibclcimplementation kept byNativeMathPass.ReplaceLLVMIntrinsicsPass::replaceCopysignshifted a 32-bit1by63fordouble, producing wrong 64-bit sign masks and corruptingcopysign(double)andround(double).ReplaceOpenCLBuiltinPass::replaceFractshifted a 32-bit1by53fordouble, evaluatingkJustUnderOneScalarto~2.33e-10instead of0x1.fffffffffffffp-1.docs/OpenCLCOnVulkan.mdstill contained the incomplete-FP64 note referencing issue 321.Change
UseNativeBuiltinscheck andbuiltinparameter fromskipBuiltinsWithGenericPointer(lib/ReplaceOpenCLBuiltinPass.cpp) soNativeMathPassgoverns all generic-pointer builtins uniformly.APInt::getSignMaskand its complement inReplaceLLVMIntrinsicsPass::replaceCopysign(lib/ReplaceLLVMIntrinsicsPass.cpp).1ULL << num_significand_bitsand-static_cast<int>(num_significand_bits)inReplaceOpenCLBuiltinPass::replaceFract(lib/ReplaceOpenCLBuiltinPass.cpp).sincos_double_native.cl,copysign_double{,4}.cl,double_round.cl,double_fract_private.cl).docs/OpenCLCOnVulkan.md.Testing
lit.pyrun coveringBuiltinsWithGenericPointer,AddressSpaceCast,NativeBuiltins,HalfStorage,PhysicalStorageBuffers, andMathBuiltins/{copysign,fract,round,sincos}passed329/329on both-arch=spirv32and-arch=spirv64(earlier fulllit.pyrun before removing theUseNativeBuiltinsearly return passed3988/3988on both).clang-format --dry-run --Werror lib/ReplaceOpenCLBuiltinPass.cpp lib/ReplaceLLVMIntrinsicsPass.cpppassed.