Background:
Extensions in OpenCL allow adding extra functionality to the core standards. The analogy to them in C/C++ are libraries. They are not to be confused with language extensions in Clang that modify the standard language behavior. Apart from very few exceptions that add extra language features.
OpenCL differs to the standard C/C++ because the application code doesn't need to include library headers or to link to the library functions.
There are a number of extensions that are added to Clang without any need for changes in parsing (see list II in RFC: http://lists.llvm.org/pipermail/cfe-dev/2020-September/066911.html). Such extensions are implemented only in the libraries. Clang now supports internal headers that can be included implicitly and provide declarations for the majority of builtin library like functionality. Without the internal header, the functionality of the stand-alone frontend is incomplete. Therefore it makes more sense to move the extensions that add functionality using regular libraries mechanisms outside of clang. The benefit of such refactoring are:
- Simplify the frontend code
- Allow adding extensions flexibly without the dependency on clang source code
- Make user interface more coherent i.e. the extensions macro are now only available when the declarations of functionality from the extensions are available too - via implicit headers.
Design:
The design is illustrated on cl_khr_depth_images. This extension has only been added to guard the declaration of corresponding functions in the headers by the extension macro. The use of extension pragma has no effect and setting the extension by targets doesn't change anything but the definition of the macro. This, therefore, shows that the only reason why the extension is added to clang is to define the macro. Moreover, the definition of this macro has also been modified in opencl-c.h. Please note that presumably the depth image types could be made available in the earlier OpenCL versions via this extension but however it has never been implemented. Therefore, it is assumed there is no interest in this in the community.
A new hook to define macros for OpenCL extensions has been added to TargetInfo that can be overridden to define the macros as needed by the targets and additionally it is shown that macros can be defined in the header files completely outside of the clang source code. The preference (at least for SPIR target) is to define the macros in the implicit headers.
Examples:
- In the following invocation of clang the macro cl_khr_depth_images is not defined.
clang -cc1 -finclude-default-header -triple=spir -cl-std=CL1.1 test.cl clang -cc1 -finclude-default-header -triple=ppc -cl-std=CL1.1 test.cl clang -cc1 -finclude-default-header -triple=ppc -cl-std=CL1.2 test.cl
- In the following invocation of clang the macro cl_khr_depth_images is defined.
clang -cc1 -finclude-default-header -triple=spir -cl-std=CL1.2 test.cl clang -cc1 -finclude-default-header -triple=spir -cl-std=CL2.0 test.cl clang -cc1 -finclude-default-header -triple=ppc -cl-std=CL2.0 test.cl
- Defining __undef_cl_khr_depth_images can alter the default behavior of the predefined macro. This is equivalent to passing -cl-ext=-cl_khr_depth_images.
- In the following invocation of clang the macro cl_khr_depth_images is not defined.
clang -cc1 -finclude-default-header -triple=spir -cl-std=CL1.2 test.cl -D__undef_cl_khr_depth_images
- cl_khr_depth_images is a core functionality of CL2.0 and thefore defining __undef_cl_khr_depth_images doesn't modify the default behavior.
clang -cc1 -finclude-default-header -triple=spir -cl-std=CL1.2 test.cl -D__undef_cl_khr_depth_images
NOTE
The refactoring of extension pragma and tablegen header (enabled via -fdeclare-opencl-builtins) are going to be covered in a separate RFC patches.