Skip to content

[SYCL] Emit fpbuiltin version of function only for function with FP arguments. #17253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 6, 2025

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Feb 28, 2025

When the arguments of a function are of half types, such as sqrt(half_type v) they are expanded to:
load ptr addrspace(4), ptr addrspace(4) %a.addr.ascast, align 8, !tbaa !5
This will generate a crash in the BE due to type mismatch. The expected type of the function is float.
This patch fixes the issue by restricting the emission of fpbuiltin only to functions that take floating point values arguments.

@zahiraam zahiraam changed the title [SYCL] Emit fpbuiltin version of the function only if its arguments are [SYCL] Emit fpbuiltin version of function only for FP arguments. Feb 28, 2025
@zahiraam zahiraam changed the title [SYCL] Emit fpbuiltin version of function only for FP arguments. [SYCL] Emit fpbuiltin version of function only for function with FP arguments. Feb 28, 2025
@zahiraam zahiraam marked this pull request as ready for review March 3, 2025 15:53
@zahiraam zahiraam requested a review from a team as a code owner March 3, 2025 15:53
Copy link
Contributor

@MrSidims MrSidims left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

[=](id<1> wiID) {
// NFPA: call spir_func float @fdiv(ptr noundef byval({{.*}}) align 1 {{.*}}, ptr noundef byval({{.*}}) align 1 {{.*}})
// NFPA-FAST: call reassoc nnan ninf nsz arcp afn spir_func nofpclass(nan inf) float @fdiv(ptr noundef byval({{.*}}) align 1 {{.*}}, ptr noundef byval({{.*}}) align 1 {{.*}})
a[0] = fdiv(HalfValue1, HalfValue2);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure, if it is a real-world example, do e have fdiv builtin in SYCL/OpenCL/C++?. What about a[0] = Value1 / Value2;, can the issue be observed in this case and does your patch fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MrSidims Thanks for the review. Fixed it.

// as fpbuiltins.
if (!getLangOpts().OffloadFP32PrecSqrt ||
!getLangOpts().OffloadFP32PrecDiv) {
for (auto Arg : IRCallArgs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit (if applicable): to avoid some static analyzers complains about 'auto causes copy'.

Suggested change
for (auto Arg : IRCallArgs) {
for (auto &Arg : IRCallArgs) {

MrSidims

This comment was marked as outdated.


#include "sycl.hpp"

namespace sycl {
class half {};
Copy link
Contributor

@MrSidims MrSidims Mar 4, 2025

Choose a reason for hiding this comment

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

IMHO it's important, that we properly define sycl::half class aka add to it half storage. So / operator mustn't be extern - instead it should have a definition. Otherwise the code here doesn't test the issue - I'm pretty sure, that _ZdvN4sycl4halfES0_ would never become llvm.fpbuiltin intrinsic anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a minimalist class half.

@zahiraam zahiraam requested a review from MrSidims March 4, 2025 17:03
if (hasFPAccuracyFuncMap || hasFPAccuracyVal || isFp32SqrtFunction) {
bool ArgsTypeIsFloat = true;
// In sycl mode, functions' arguments of type half are expanded
// to pointer types. Exclude these functions from bein emitted
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// to pointer types. Exclude these functions from bein emitted
// to pointer types. Exclude these functions from being emitted

Copy link
Contributor

@premanandrao premanandrao left a comment

Choose a reason for hiding this comment

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

I am okay with the changes.

@zahiraam
Copy link
Contributor Author

zahiraam commented Mar 6, 2025

@intel/llvm-gatekeepers Can this be merged in please?

@sarnex sarnex merged commit 48bb501 into intel:sycl Mar 6, 2025
21 checks passed
@zahiraam
Copy link
Contributor Author

zahiraam commented Mar 6, 2025

@sarnex Thanks.

@sarnex
Copy link
Contributor

sarnex commented Mar 6, 2025

sure

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