Skip to content

Commit

Permalink
[OpenCL] Add clang extension for bit-fields.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Anastasia Stulova committed May 24, 2021
1 parent 543406a commit 237c692
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 4 deletions.
28 changes: 28 additions & 0 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
--------------------------------
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/OpenCLExtensions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/Targets/AMDGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
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());

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
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;
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Misc/amdgcn.languageOptsOpenCL.cl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Misc/nvptx.languageOptsOpenCL.cl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Misc/r600.languageOptsOpenCL.cl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions clang/test/SemaOpenCL/unsupported.cl
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 237c692

Please sign in to comment.