Skip to content
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

clang does not add !fpmath data to OpenCL sqrt calls #64264

Closed
arsenm opened this issue Jul 31, 2023 · 10 comments
Closed

clang does not add !fpmath data to OpenCL sqrt calls #64264

arsenm opened this issue Jul 31, 2023 · 10 comments

Comments

@arsenm
Copy link
Contributor

arsenm commented Jul 31, 2023

By default (i.e. without -cl-fp32-correctly-rounded-divide-sqrt), calls to float/vector of float sqrt should be annotated with !fpmath metadata. Currently they are not which is interfering with fixing the library from using a global option corresponding to the flag.

I tried working towards this in bac2a07, but this is of limited use for the end use case. The actual user call to the mangled function needs to be annotated (such that a backend pass can swap out the call to the intrinsic with presered metadata) so we can lower it appropriately.

I'm not sure how to really do this. I tried hacking up LANGBUILTINs for all the mangled forms, but they don't seem to be recognized as builtins at that point for interception in CGBuiltin. Another strategy I considered was adding a macro for the correctly rounded option and setting an attribute on the declarations?

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 31, 2023

@llvm/issue-subscribers-clang-codegen

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 31, 2023

@llvm/issue-subscribers-opencl

@arsenm
Copy link
Contributor Author

arsenm commented Jul 31, 2023

Attaching the lit test sqrt-fpmath.cl.txt

@yxsamliu
Copy link
Collaborator

OpenCL sqrt functions are declared in clang/lib/Headers/opencl-c.h .

How about adding a macro (e.g. __CLANG_OPENCL_USE_BULTIN_SQRT) to conditionally define them as inline functions to call clang sqrt builtins. Then AMDGPU target can choose to enable them in clang driver. If all targets are able to handle the sqrt intrinsic then this macro is unnecessary.

@arsenm
Copy link
Contributor Author

arsenm commented Jul 31, 2023

OpenCL sqrt functions are declared in clang/lib/Headers/opencl-c.h .

How about adding a macro (e.g. __CLANG_OPENCL_USE_BULTIN_SQRT) to conditionally define them as inline functions to call clang sqrt builtins. Then AMDGPU target can choose to enable them in clang driver. If all targets are able to handle the sqrt intrinsic then this macro is unnecessary.

That could work, availabile_externally inline definition with the intrinsic call but the implementation would still need to provide a definition in the linked library. I guess we could use the macro in case of alternative implementation choices

@arsenm
Copy link
Contributor Author

arsenm commented Jul 31, 2023

On second thought the intrinsic should always work in theory. We don't need to speculatively work around buggy implementations

@arsenm
Copy link
Contributor Author

arsenm commented Jul 31, 2023

I tried this and it doesn't work with -fdeclare-opencl-builtins: https://reviews.llvm.org/D156743

@svenvh
Copy link
Member

svenvh commented Aug 1, 2023

I tried this and it doesn't work with -fdeclare-opencl-builtins: https://reviews.llvm.org/D156743

-fdeclare-opencl-builtins does not provide any definitions indeed. You could move the definitions into opencl-c-base.h, which is used by both opencl-c.h and -fdeclare-opencl-builtins. That's not ideal though, as we don't want to bloat that common header.

An alternative could be to take the sqrt declarations out of opencl-c.h and OpenCLBuiltins.td and instead handle them similar to how clang handles C11 atomics (see e.g. SemaChecking.cpp).

@arsenm
Copy link
Contributor Author

arsenm commented Aug 1, 2023

I tried this and it doesn't work with -fdeclare-opencl-builtins: https://reviews.llvm.org/D156743

-fdeclare-opencl-builtins does not provide any definitions indeed. You could move the definitions into opencl-c-base.h, which is used by both opencl-c.h and -fdeclare-opencl-builtins. That's not ideal though, as we don't want to bloat that common header.

It should be only 1 function times 6 vector overloads, so I'm not too worried about bloating it. There are some plusses to having a separate inline definition compared to fixing up call sites. It maintains inlining flexibility and avoids needing to introduce a non-standard name for the two different variants. All the fast math flag and attribute handling is also free that way.

However I'm seeing weird behavior when I move just the f32 variant in there, the double and half variants all try to resolve to calling sqrt f32.

An alternative could be to take the sqrt declarations out of opencl-c.h and OpenCLBuiltins.td and instead handle them similar to how clang handles C11 atomics (see e.g. SemaChecking.cpp).

I think this has more in common with printf, which is declared in the base header

@svenvh
Copy link
Member

svenvh commented Aug 1, 2023

However I'm seeing weird behavior when I move just the f32 variant in there, the double and half variants all try to resolve to calling sqrt f32.

The -fdeclare-opencl-builtins mechanism only kicks in when name lookup fails. You'll probably have to provide declarations/definitions for all overloads (so including declarations for half and double) if you're providing the float definitions.

@arsenm arsenm closed this as completed in 15e0fe0 Sep 12, 2023
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this issue Sep 19, 2023
We want the !fpmath metadata to be attached to the sqrt intrinsic to
make it to the backend lowering. Emit an available_externally
definition which uses the builtin, which emits the !fpmath.

Fixes llvm#64264

https://reviews.llvm.org/D156743
raikonenfnu added a commit to shark-infra/ROCm-Device-Libs that referenced this issue Oct 1, 2023
With changes implemented in https://reviews.llvm.org/D156743 and
discussed in llvm/llvm-project#64264. Upstream
llvm added the definition of sqrt(float) and it's variant in clang/lib/Headers/opencl-c-base.h.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this issue Oct 12, 2023
XFAILS new test: clang/test/CodeGenOpenCL/sqrt-fpmath.cl

We want the !fpmath metadata to be attached to the sqrt intrinsic to
make it to the backend lowering. Emit an available_externally
definition which uses the builtin, which emits the !fpmath.

Fixes llvm#64264

https: //reviews.llvm.org/D156743
Change-Id: I590c7595d8df38ccbf0a3b458c255b44aa68255c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants