[cfe-dev] [RFC][OpenCL] Implement OpenCL builtin functions in Clang

Andrew Savonichev via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 25 08:02:01 PDT 2018


OpenCL C language has ~200 builtin functions defined in the
specification: math builtins (sin, cos, tan, etc.), conversion builtins
(float -> half, char2 -> int2), image builtins, and many others.

Currently, all these builtins are declared in `opencl-c.h' header for
each supported combination of types (using `overloadable' attribute), so
~200 builtins turn into ~14000 function declarations.

It takes a significant amount of time to parse these ~14000
declarations, and we have to do it for *every* OpenCL C program. This
leads to bad compile time performance for small OpenCL C programs, and
most of the time is eaten by `opencl-c.h' header.

There were several of attempts to improve this design by pre-compilation
(see D51544: Split opencl-c.h header[1]), but pre-compiled headers are
difficult to manage, and they make overall design more complicated.

Alternatively, we can implement all these ~200 builtins in clang. With a
custom type checking we can handle different argument types. Custom
codegen can generate mangled function calls, so IR should be identical
to what we had before.

In the future, I think we can add a switch to replace mangled functions
with LLVM IR intrinsics, as it could be useful for SPIR-V translation
(see SPIR-V discussion[4] for rationale).

I implemented several builtins as a POC:

  - D52457: [OpenCL] Implement OpenCL convert builtin         [2]
  - D52458: [OpenCL] Implement OpenCL math builtins: fp -> fp [3]

Implementation of the remaining ~150 builtins should be straightforward,
but it is time-consuming, so I'd like to make sure that:

  1) this approach fits the needs of OpenCL community

  2) the rest of the Clang community is OK with adding a few hundred
     builtins to Clang.

  [1]: https://reviews.llvm.org/D51544
  [2]: https://reviews.llvm.org/D52457
  [3]: https://reviews.llvm.org/D52458
  [4]: https://lists.llvm.org/pipermail/llvm-dev/2018-September/125977.html


Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

More information about the cfe-dev mailing list