[OpenCL] Add clang extension for bit-fields.

Allow use of bit-fields as a clang extension
in OpenCL. The extension can be enabled using
pragma directives.

This fixes PR45339!

Differential Revision: https://reviews.llvm.org/D101843
This commit is contained in:
Anastasia Stulova 2021-05-24 12:38:02 +01:00
parent 543406a69b
commit 237c6924bd
9 changed files with 60 additions and 4 deletions

View File

@ -1741,6 +1741,34 @@ OpenCL Features
Clang supports internal OpenCL extensions documented below.
``__cl_clang_bitfields``
--------------------------------
With this extension it is possible to enable bitfields in structs
or unions using the OpenCL extension pragma mechanism detailed in
`the OpenCL Extension Specification, section 1.2
<https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html#extensions-overview>`_.
Use of bitfields in OpenCL kernels can result in reduced portability as struct
layout is not guaranteed to be consistent when compiled by different compilers.
If structs with bitfields are used as kernel function parameters, it can result
in incorrect functionality when the layout is different between the host and
device code.
**Example of Use**:
.. code-block:: c++
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
struct with_bitfield {
unsigned int i : 5; // compiled - no diagnostic generated
};
#pragma OPENCL EXTENSION __cl_clang_bitfields : disable
struct without_bitfield {
unsigned int i : 5; // error - bitfields are not supported
};
``__cl_clang_function_pointers``
--------------------------------

View File

@ -88,6 +88,7 @@ OPENCL_EXTENSION(cl_clang_storage_class_specifiers, true, 100)
OPENCL_EXTENSION(__cl_clang_function_pointers, true, 100)
OPENCL_EXTENSION(__cl_clang_variadic_functions, true, 100)
OPENCL_EXTENSION(__cl_clang_non_portable_kernel_param_types, true, 100)
OPENCL_EXTENSION(__cl_clang_bitfields, true, 100)
// AMD OpenCL extensions
OPENCL_EXTENSION(cl_amd_media_ops, true, 100)

View File

@ -288,6 +288,7 @@ public:
Opts["__cl_clang_variadic_functions"] = true;
Opts["__cl_clang_function_pointers"] = true;
Opts["__cl_clang_non_portable_kernel_param_types"] = true;
Opts["__cl_clang_bitfields"] = true;
bool IsAMDGCN = isAMDGCN(getTriple());

View File

@ -136,6 +136,7 @@ public:
Opts["__cl_clang_function_pointers"] = true;
Opts["__cl_clang_variadic_functions"] = true;
Opts["__cl_clang_non_portable_kernel_param_types"] = true;
Opts["__cl_clang_bitfields"] = true;
Opts["cl_khr_fp64"] = true;
Opts["__opencl_c_fp64"] = true;

View File

@ -16815,8 +16815,10 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
Record->setInvalidDecl();
InvalidDecl = true;
}
// OpenCL v1.2 s6.9.c: bitfields are not supported.
if (BitWidth) {
// OpenCL v1.2 s6.9.c: bitfields are not supported, unless Clang extension
// is enabled.
if (BitWidth && !getOpenCLOptions().isAvailableOption(
"__cl_clang_bitfields", LangOpts)) {
Diag(Loc, diag::err_opencl_bitfields);
InvalidDecl = true;
}

View File

@ -29,6 +29,11 @@
#endif
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
#ifndef __cl_clang_bitfields
#error "Missing __cl_clang_bitfields define"
#endif
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
#ifndef cl_khr_fp16
#error "Missing cl_khr_fp16 define"
#endif

View File

@ -33,6 +33,11 @@
#endif
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
#ifndef __cl_clang_bitfields
#error "Missing __cl_clang_bitfields define"
#endif
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
#ifdef cl_khr_fp16
#error "Incorrect cl_khr_fp16 define"
#endif

View File

@ -39,6 +39,11 @@
#endif
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
#ifndef __cl_clang_bitfields
#error "Missing __cl_clang_bitfields define"
#endif
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
#ifdef cl_khr_fp16
#error "Incorrect cl_khr_fp16 define"
#endif

View File

@ -1,7 +1,15 @@
// RUN: %clang_cc1 -verify %s
// RUN: %clang_cc1 -verify %s -DBITFIELDS_EXT
struct {
int a : 1; // expected-error {{bit-fields are not supported in OpenCL}}
#ifdef BITFIELDS_EXT
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
#endif
struct test {
int a : 1;
#ifndef BITFIELDS_EXT
// expected-error@-2 {{bit-fields are not supported in OpenCL}}
#endif
};
void no_vla(int n) {