[RFC] OpenCL C 3.0 support in clang

Hi! I’m happy to announce OpenCL C 3.0 support in clang.

Language specification is available at https://github.com/KhronosGroup/OpenCL-Docs. OpenCL C 3.0 language standard is now backward compatible with 1.2 and makes 2.0 features optional. New OpenCL language specification is important because it allows platform vendors to optionally support OpenCL C 2.0 features which are no longer required for compliance with the core specification. This allows vendors to port OpenCL implementations on high variety of devices by choosing necessary functionality to implement. Such vendors as Intel and ARM are highly interested in OpenCL C 3.0. So I encourage everyone else who is also looking forward for OpenCL C 3.0 support in clang to take part in the following discussions and reviews.

In OpenCL 3.0 support of various features is indicated with a feature test macros. This is pretty much similar to C++ feature test macros but the difference is that the presence and the value of the latter directly relates to C++ language standard. The presence of feature test macros in OpenCL C 3.0 in the contrary is target specific: macros are being optionally defined by the frontend compiler depending on whether the target architecture supports feature or not.

------- Implementation Details -------

First it is worth mentioning that there are no big conceptual changes in clang expected since OpenCL C 3.0 makes 2.0 features optional. I’ve been working on OpenCL C 3.0 support in Intel and full patch is already published in Intel opencl-clang repo:

https://github.com/intel/opencl-clang/tree/ocl-open-110/patches/clang

There already exists such concept as extensions in OpenCL C language specification which has a similar meaning with OpenCL features. The difference between two of them is that OpenCL extensions are not the part of core specification and require special pragma directive to enable them. Despite the fact that features and extensions are different I tried to reuse the current extension mechanism in clang as much as possible to avoid code duplication. Also, I’m going to introduce new LangOpts flags for OpenCL C specific keywords such as ‘generic’ and ‘pipe’; and ‘OPENCLBUILTIN’ concept (instead of ‘LANGBUILTIN’) to forward OpenCL C feature name as a parameter.

The main idea behind proposed implementation is that feature tests macros are being defined by compiler in all OpenCL versions and can’t be disabled in versions prior to 3.0. This unified approach will help to avoid additional checks when identifying various types and built-ins support for all OpenCL versions in both: in clang and opencl-c.h header. Also, it logically expresses compiler behavior for other OpenCL versions, for example: OpenCL C 2.0 supports generic address space by default thus ‘__opencl_c_generic_address_space’ macro is defined when ‘-cl-std=CL2.0’ option is used.

There are some cases when for OpenCL C 3.0 feature already exists equivalent OpenCL C extension (‘__opencl_c_3d_image_writes’ and ‘cl_khr_3d_image_writes’). The strategy which I decided to follow here is to provide simultaneous test macro presence for both: for feature and corresponding extension because they describe same functionality. Additionally, the reason for this is backward compatibility: there may be older applications where extension macro is checked instead of feature macro. With this approach older applications will continue to compile with OpenCL C 3.0

Please let me know if you have any concerns about this proposal, I would be glad to discuss it.

Hi Anton,

Thank you for this exciting announcement. It is great to see clang continuing
to provide support for evolving OpenCL C standards. However, I would like to
highlight the following - considering that the specification has been released
without the upstream implementation, it is possible that some functionality
wouldn’t be implemented without the need of modifying the specification or
deviating from the specification. As a matter of fact, this is not specific to
OpenCL 3.0 and there were several such issues while implementing the
earlier standards. I would like to be more diligent this time to document what
is not conformant to the spec, this page should be a good place
https://clang.llvm.org/docs/LanguageExtensions.html.

There is one aspect that would be good to clarify before we start implementing
the features - what would be the interface for the targets to set the feature
presence? For example for extensions, different approaches were used i.e.

  • Adding to clang source code i.e. target setting. Those could also be amended
    using -cl-ext flag.
  • Outside of clang the extension macros could be defined in headers or using
    pre-processor flags -D.

I understand that if we follow the first approach only we will provide a more
unified interface but however, we end up adding something in clang that
won’t be used in the parser itself. However, for the second approach, we end
up with more heterogeneity but provide higher flexibility to add any custom
extensions as well as simplifying the clang code base. For the features it is
less critical as they are all known, it is also possible that future standards will
define new features or remove existing ones.

FYI I have provided more details regarding this topic on a separate RFC:
http://lists.llvm.org/pipermail/cfe-dev/2020-September/066911.html

Kind regards,
Anastasia

Hi, Anastasia! Thanks for your reply.

Regarding disagreements between specification and implementation: what now comes to mind is that it is worth mentioning that clang supports compilation only for FULL OpenCL profile, not EMBEDDED profile (https://github.com/KhronosGroup/OpenCL-Docs/blob/master/api/embedded_profile.asciidoc). Though we can discuss if we plan to support compilation for EMBEDDED OpenCL profile in clang.

I also checked out the proposal about simplification in extension mechanism: there is something common with OpenCL features, especially there are features which affect only internal opencl-c.h header, such as:

__opencl_c_atomic_scope_device

__opencl_c_atomic_scope_all_devices

__opencl_c_work_group_collective_functions.

I could do some research to find a proper way to define those macros somehow else (except ‘-D’ flag) and this can be reused for extensions which are mentioned in II section of your letter (http://lists.llvm.org/pipermail/cfe-dev/2020-September/066911.html). However, I don’t see any strong concerns about current implementation in defining such macros since all the extensions are already defined only in OpenCLExtensions.def, so this approach is already unified for extensions of all types. Perhaps we could extend current functionality of ‘-cl-ext’ to allow registering new custom features/extensions which affect header only. Also, we can differentiate if extension or feature is used only in header by adding a flag into definition in OpenCLExtensions.def file.

Regards,

Anton

hi Anton,

Regarding disagreements between specification and implementation: what now comes to mind is that it is worth mentioning that clang supports compilation only for FULL OpenCL profile, not EMBEDDED profile (https://github.com/KhronosGroup/OpenCL-Docs/blob/master/api/embedded_profile.asciidoc). Though we can discuss if we plan to support compilation for EMBEDDED OpenCL profile in clang.

Good point. Thanks! I will try to prepare an update to the docs for review in the next days.

I could do some research to find a proper way to define those macros somehow else (except ‘-D’ flag) and this can be reused for extensions which are mentioned in II section of your letter (http://lists.llvm.org/pipermail/cfe-dev/2020-September/066911.html).

Sounds good!

However, I don’t see any strong concerns about current implementation in defining such macros since all the extensions are already defined only in OpenCLExtensions.def, so this approach is already unified for extensions of all types.

It is not the end of the world but currently if an extension is added into OpenCLExtensions.def aside from macro it will result in a pragma and extra items in the target options which aren’t really used. At the same time if we only need to define a macro clang and C language provides so many mechanisms to do this. I feel like we have reinvented the wheel.

Perhaps we could extend current functionality of ‘-cl-ext’ to allow registering new custom features/extensions which affect header only. Also, we can differentiate if extension or feature is used only in header by adding a flag into definition in OpenCLExtensions.def file.

Yes these could be good options. Perhaps we could also think of providing some target hooks for OpenCLExtensions.def.

Overall I just feel if every extension is added into clang source code this won’t scale, especially that vendor extensions are only valid for a subset of targets and it is annoying to add them into the common code. So if we provide hooks to register extensions instead, it will simplify upstream code and also simplify out-of-tree implementations that won’t be bound by upstream if they need to add new extensions.

Cheers,
Anastasia