Skip to content

[SYCL] Improve SYCL <cmath> built-in implementation - #23572

Draft
0x12CC wants to merge 21 commits into
intel:syclfrom
0x12CC:sycl-fast-math
Draft

0x12CC wants to merge 21 commits into
intel:syclfrom
0x12CC:sycl-fast-math

Conversation

@0x12CC

@0x12CC 0x12CC commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Certain users may expect corresponding SYCL and C++ standard-library math functions to produce matching results under floating-point compiler options. This PR makes the following changes to achieve these matching results:

  1. Updates our <cmath> wrapper to define most functions inline so they're affected by the floating-point compiler flags.
  2. Modifies code generation to treat certain __spirv_ocl_* functions as math built-ins. This preserves the existing behavior when accuracy requirements are specified for the functions.
  3. Changes the <cmath> built-ins to match their equivalent SYCL built-ins when compiling device code. This includes enabling optimizations when compiling with the -ffast-math flag.
  4. Removes an old test case that overrides a math function using a redefinition since it's not supported by the updated implementation.
  5. Adds a test case that compares the numerical results of math built-in pairs for bitwise equality with various test input values.

Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC
0x12CC requested review from a team as code owners October 21, 2026 02:55
@0x12CC
0x12CC requested a review from againull October 21, 2026 02:55
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Comment thread llvm-spirv/lib/SPIRV/SPIRVWriter.cpp Outdated
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC
0x12CC marked this pull request as draft October 21, 2026 18:55
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC
0x12CC marked this pull request as ready for review October 23, 2026 02:50
@dm-vodopyanov

Copy link
Copy Markdown
Contributor

@YuriPlyakhin, can you please review as part of dpcpp-spirv-reviewers team? This PR is targeted on the current release, and we would like to create cherry-picks to xmain and xmain-rel and merge them as well today

Comment thread llvm-spirv/lib/SPIRV/SPIRVWriter.cpp Outdated
Comment thread llvm-spirv/lib/SPIRV/SPIRVWriter.cpp Outdated
Comment thread llvm-spirv/lib/SPIRV/SPIRVWriter.cpp Outdated
Comment thread sycl/test-e2e/DeviceLib/built-ins/std-sycl-equivalence.cpp Outdated
Comment thread sycl/test-e2e/DeviceLib/built-ins/std-sycl-equivalence.cpp Outdated
Comment thread sycl/test-e2e/DeviceLib/built-ins/std-sycl-equivalence.cpp Outdated
Comment thread sycl/test-e2e/DeviceLib/built-ins/std-sycl-equivalence.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With -ffast-math, what happens when inputs produce Inf or NaN? Is it UB?
E.g. log(0.0), sqrt(-1.0), pow(1.0e30, 1.0e30), etc...
If it is UB, does it make sense to match results there?
Should cases like that be filtered out for the fast-math build?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit

Please, consider storing the device output and the expected value and printing them in hex to make triaging failures easier.

@bashbaug bashbaug left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Noting for completeness: there are also still some long-standing questions about the required accuracy for OpFMod and OpFRem, especially vs. the fmod and remainder extended instructions.

KhronosGroup/OpenCL-Docs#859

Comment thread llvm-spirv/lib/SPIRV/SPIRVWriter.cpp Outdated
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC 0x12CC changed the title [SYCL] Add math builtin equivalence tests and fix fmod lowering [SYCL] Add math builtin equivalence tests and improve cmath header Oct 26, 2026
@0x12CC 0x12CC changed the title [SYCL] Add math builtin equivalence tests and improve cmath header [SYCL] Add math builtin equivalence tests and improve <cmath> header Oct 26, 2026
@0x12CC 0x12CC changed the title [SYCL] Add math builtin equivalence tests and improve <cmath> header [SYCL] Add math built-in equivalence tests and improve <cmath> header Oct 26, 2026
Signed-off-by: Michael Aziz <michael.aziz@intel.com>

@bashbaug bashbaug left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM!

Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC
0x12CC requested a review from a team as a code owner October 27, 2026 01:59
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Signed-off-by: Michael Aziz <michael.aziz@intel.com>
@0x12CC
0x12CC marked this pull request as draft October 27, 2026 14:17
@0x12CC 0x12CC changed the title [SYCL] Add math built-in equivalence tests and improve <cmath> header [SYCL] Improve SYCL <cmath> built-in implementation Oct 27, 2026

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.

4 participants