Skip to content

Commit 237c692

Browse files
author
Anastasia Stulova
committed
[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
1 parent 543406a commit 237c692

File tree

9 files changed

+60
-4
lines changed

9 files changed

+60
-4
lines changed

clang/docs/LanguageExtensions.rst

+28
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,34 @@ OpenCL Features
17411741
17421742
Clang supports internal OpenCL extensions documented below.
17431743
1744+
``__cl_clang_bitfields``
1745+
--------------------------------
1746+
1747+
With this extension it is possible to enable bitfields in structs
1748+
or unions using the OpenCL extension pragma mechanism detailed in
1749+
`the OpenCL Extension Specification, section 1.2
1750+
<https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html#extensions-overview>`_.
1751+
1752+
Use of bitfields in OpenCL kernels can result in reduced portability as struct
1753+
layout is not guaranteed to be consistent when compiled by different compilers.
1754+
If structs with bitfields are used as kernel function parameters, it can result
1755+
in incorrect functionality when the layout is different between the host and
1756+
device code.
1757+
1758+
**Example of Use**:
1759+
1760+
.. code-block:: c++
1761+
1762+
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
1763+
struct with_bitfield {
1764+
unsigned int i : 5; // compiled - no diagnostic generated
1765+
};
1766+
1767+
#pragma OPENCL EXTENSION __cl_clang_bitfields : disable
1768+
struct without_bitfield {
1769+
unsigned int i : 5; // error - bitfields are not supported
1770+
};
1771+
17441772
``__cl_clang_function_pointers``
17451773
--------------------------------
17461774

clang/include/clang/Basic/OpenCLExtensions.def

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ OPENCL_EXTENSION(cl_clang_storage_class_specifiers, true, 100)
8888
OPENCL_EXTENSION(__cl_clang_function_pointers, true, 100)
8989
OPENCL_EXTENSION(__cl_clang_variadic_functions, true, 100)
9090
OPENCL_EXTENSION(__cl_clang_non_portable_kernel_param_types, true, 100)
91+
OPENCL_EXTENSION(__cl_clang_bitfields, true, 100)
9192

9293
// AMD OpenCL extensions
9394
OPENCL_EXTENSION(cl_amd_media_ops, true, 100)

clang/lib/Basic/Targets/AMDGPU.h

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
288288
Opts["__cl_clang_variadic_functions"] = true;
289289
Opts["__cl_clang_function_pointers"] = true;
290290
Opts["__cl_clang_non_portable_kernel_param_types"] = true;
291+
Opts["__cl_clang_bitfields"] = true;
291292

292293
bool IsAMDGCN = isAMDGCN(getTriple());
293294

clang/lib/Basic/Targets/NVPTX.h

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
136136
Opts["__cl_clang_function_pointers"] = true;
137137
Opts["__cl_clang_variadic_functions"] = true;
138138
Opts["__cl_clang_non_portable_kernel_param_types"] = true;
139+
Opts["__cl_clang_bitfields"] = true;
139140

140141
Opts["cl_khr_fp64"] = true;
141142
Opts["__opencl_c_fp64"] = true;

clang/lib/Sema/SemaDecl.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -16815,8 +16815,10 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T,
1681516815
Record->setInvalidDecl();
1681616816
InvalidDecl = true;
1681716817
}
16818-
// OpenCL v1.2 s6.9.c: bitfields are not supported.
16819-
if (BitWidth) {
16818+
// OpenCL v1.2 s6.9.c: bitfields are not supported, unless Clang extension
16819+
// is enabled.
16820+
if (BitWidth && !getOpenCLOptions().isAvailableOption(
16821+
"__cl_clang_bitfields", LangOpts)) {
1682016822
Diag(Loc, diag::err_opencl_bitfields);
1682116823
InvalidDecl = true;
1682216824
}

clang/test/Misc/amdgcn.languageOptsOpenCL.cl

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
#endif
3030
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
3131

32+
#ifndef __cl_clang_bitfields
33+
#error "Missing __cl_clang_bitfields define"
34+
#endif
35+
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
36+
3237
#ifndef cl_khr_fp16
3338
#error "Missing cl_khr_fp16 define"
3439
#endif

clang/test/Misc/nvptx.languageOptsOpenCL.cl

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
#endif
3434
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
3535

36+
#ifndef __cl_clang_bitfields
37+
#error "Missing __cl_clang_bitfields define"
38+
#endif
39+
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
40+
3641
#ifdef cl_khr_fp16
3742
#error "Incorrect cl_khr_fp16 define"
3843
#endif

clang/test/Misc/r600.languageOptsOpenCL.cl

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
#endif
4040
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
4141

42+
#ifndef __cl_clang_bitfields
43+
#error "Missing __cl_clang_bitfields define"
44+
#endif
45+
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
46+
4247
#ifdef cl_khr_fp16
4348
#error "Incorrect cl_khr_fp16 define"
4449
#endif

clang/test/SemaOpenCL/unsupported.cl

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// RUN: %clang_cc1 -verify %s
2+
// RUN: %clang_cc1 -verify %s -DBITFIELDS_EXT
23

3-
struct {
4-
int a : 1; // expected-error {{bit-fields are not supported in OpenCL}}
4+
#ifdef BITFIELDS_EXT
5+
#pragma OPENCL EXTENSION __cl_clang_bitfields : enable
6+
#endif
7+
8+
struct test {
9+
int a : 1;
10+
#ifndef BITFIELDS_EXT
11+
// expected-error@-2 {{bit-fields are not supported in OpenCL}}
12+
#endif
513
};
614

715
void no_vla(int n) {

0 commit comments

Comments
 (0)