|
| 1 | +From 237c6924bd46ec0e33da71f9616caf9bf9965b23 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Anastasia Stulova < [email protected]> |
| 3 | +Date: Mon, 24 May 2021 12:38:02 +0100 |
| 4 | +Subject: [PATCH] [OpenCL] Add clang extension for bit-fields. |
| 5 | + |
| 6 | +Allow use of bit-fields as a clang extension |
| 7 | +in OpenCL. The extension can be enabled using |
| 8 | +pragma directives. |
| 9 | + |
| 10 | +This fixes PR45339! |
| 11 | + |
| 12 | +Differential Revision: https://reviews.llvm.org/D101843 |
| 13 | + |
| 14 | + |
| 15 | +diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst |
| 16 | +index 0fec251c4bdc..870b8f9744c1 100644 |
| 17 | +--- a/clang/docs/LanguageExtensions.rst |
| 18 | ++++ b/clang/docs/LanguageExtensions.rst |
| 19 | +@@ -1522,6 +1522,34 @@ Query the presence of this new mangling with |
| 20 | + OpenCL Features |
| 21 | + =============== |
| 22 | + |
| 23 | ++``__cl_clang_bitfields`` |
| 24 | ++-------------------------------- |
| 25 | ++ |
| 26 | ++With this extension it is possible to enable bitfields in structs |
| 27 | ++or unions using the OpenCL extension pragma mechanism detailed in |
| 28 | ++`the OpenCL Extension Specification, section 1.2 |
| 29 | ++<https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html#extensions-overview>`_. |
| 30 | ++ |
| 31 | ++Use of bitfields in OpenCL kernels can result in reduced portability as struct |
| 32 | ++layout is not guaranteed to be consistent when compiled by different compilers. |
| 33 | ++If structs with bitfields are used as kernel function parameters, it can result |
| 34 | ++in incorrect functionality when the layout is different between the host and |
| 35 | ++device code. |
| 36 | ++ |
| 37 | ++**Example of Use**: |
| 38 | ++ |
| 39 | ++.. code-block:: c++ |
| 40 | ++ |
| 41 | ++ #pragma OPENCL EXTENSION __cl_clang_bitfields : enable |
| 42 | ++ struct with_bitfield { |
| 43 | ++ unsigned int i : 5; // compiled - no diagnostic generated |
| 44 | ++ }; |
| 45 | ++ |
| 46 | ++ #pragma OPENCL EXTENSION __cl_clang_bitfields : disable |
| 47 | ++ struct without_bitfield { |
| 48 | ++ unsigned int i : 5; // error - bitfields are not supported |
| 49 | ++ }; |
| 50 | ++ |
| 51 | + C++ for OpenCL |
| 52 | + -------------- |
| 53 | + |
| 54 | +diff --git a/clang/include/clang/Basic/OpenCLExtensions.def b/clang/include/clang/Basic/OpenCLExtensions.def |
| 55 | +index 5536a6e8e4df..6438e5060576 100644 |
| 56 | +--- a/clang/include/clang/Basic/OpenCLExtensions.def |
| 57 | ++++ b/clang/include/clang/Basic/OpenCLExtensions.def |
| 58 | +@@ -76,6 +76,7 @@ OPENCLEXT_INTERNAL(cl_khr_terminate_context, 200, ~0U) |
| 59 | + |
| 60 | + // Clang Extensions. |
| 61 | + OPENCLEXT_INTERNAL(cl_clang_storage_class_specifiers, 100, ~0U) |
| 62 | ++OPENCLEXT_INTERNAL(__cl_clang_bitfields, 100, ~0U) |
| 63 | + |
| 64 | + // AMD OpenCL extensions |
| 65 | + OPENCLEXT_INTERNAL(cl_amd_media_ops, 100, ~0U) |
| 66 | +diff --git a/clang/lib/Basic/Targets/AMDGPU.h b/clang/lib/Basic/Targets/AMDGPU.h |
| 67 | +index 456cb2ebb8b5..1befc0e742cd 100644 |
| 68 | +--- a/clang/lib/Basic/Targets/AMDGPU.h |
| 69 | ++++ b/clang/lib/Basic/Targets/AMDGPU.h |
| 70 | +@@ -247,6 +247,8 @@ public: |
| 71 | + |
| 72 | + bool IsAMDGCN = isAMDGCN(getTriple()); |
| 73 | + |
| 74 | ++ Opts.support("__cl_clang_bitfields"); |
| 75 | ++ |
| 76 | + if (hasFP64()) |
| 77 | + Opts.support("cl_khr_fp64"); |
| 78 | + |
| 79 | +diff --git a/clang/lib/Basic/Targets/NVPTX.h b/clang/lib/Basic/Targets/NVPTX.h |
| 80 | +index 2cdd37ca1b07..5fac6cb70fe0 100644 |
| 81 | +--- a/clang/lib/Basic/Targets/NVPTX.h |
| 82 | ++++ b/clang/lib/Basic/Targets/NVPTX.h |
| 83 | +@@ -132,6 +132,8 @@ public: |
| 84 | + Opts.support("cl_khr_global_int32_extended_atomics"); |
| 85 | + Opts.support("cl_khr_local_int32_base_atomics"); |
| 86 | + Opts.support("cl_khr_local_int32_extended_atomics"); |
| 87 | ++ |
| 88 | ++ Opts.support("__cl_clang_bitfields"); |
| 89 | + } |
| 90 | + |
| 91 | + /// \returns If a target requires an address within a target specific address |
| 92 | +diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp |
| 93 | +index 8f19edbc4f36..8e8d138cb84f 100644 |
| 94 | +--- a/clang/lib/Sema/SemaDecl.cpp |
| 95 | ++++ b/clang/lib/Sema/SemaDecl.cpp |
| 96 | +@@ -15836,8 +15836,10 @@ FieldDecl *Sema::CheckFieldDecl(DeclarationName Name, QualType T, |
| 97 | + Record->setInvalidDecl(); |
| 98 | + InvalidDecl = true; |
| 99 | + } |
| 100 | +- // OpenCL v1.2 s6.9.c: bitfields are not supported. |
| 101 | +- if (BitWidth) { |
| 102 | ++ // OpenCL v1.2 s6.9.c: bitfields are not supported, unless Clang extension |
| 103 | ++ // is enabled. |
| 104 | ++ if (BitWidth && !getOpenCLOptions().isEnabled( |
| 105 | ++ "__cl_clang_bitfields")) { |
| 106 | + Diag(Loc, diag::err_opencl_bitfields); |
| 107 | + InvalidDecl = true; |
| 108 | + } |
| 109 | +diff --git a/clang/test/Misc/amdgcn.languageOptsOpenCL.cl b/clang/test/Misc/amdgcn.languageOptsOpenCL.cl |
| 110 | +index 6a07fc98069c..be59a6cc5227 100644 |
| 111 | +--- a/clang/test/Misc/amdgcn.languageOptsOpenCL.cl |
| 112 | ++++ b/clang/test/Misc/amdgcn.languageOptsOpenCL.cl |
| 113 | +@@ -14,6 +14,11 @@ |
| 114 | + #endif |
| 115 | + #pragma OPENCL EXTENSION cl_clang_storage_class_specifiers: enable |
| 116 | + |
| 117 | ++#ifndef __cl_clang_bitfields |
| 118 | ++#error "Missing __cl_clang_bitfields define" |
| 119 | ++#endif |
| 120 | ++#pragma OPENCL EXTENSION __cl_clang_bitfields : enable |
| 121 | ++ |
| 122 | + #ifndef cl_khr_fp16 |
| 123 | + #error "Missing cl_khr_fp16 define" |
| 124 | + #endif |
| 125 | +diff --git a/clang/test/Misc/r600.languageOptsOpenCL.cl b/clang/test/Misc/r600.languageOptsOpenCL.cl |
| 126 | +index 58444cf7688a..9d2f2ef88939 100644 |
| 127 | +--- a/clang/test/Misc/r600.languageOptsOpenCL.cl |
| 128 | ++++ b/clang/test/Misc/r600.languageOptsOpenCL.cl |
| 129 | +@@ -30,6 +30,11 @@ |
| 130 | + #endif |
| 131 | + #pragma OPENCL EXTENSION cl_clang_storage_class_specifiers: enable |
| 132 | + |
| 133 | ++#ifndef __cl_clang_bitfields |
| 134 | ++#error "Missing __cl_clang_bitfields define" |
| 135 | ++#endif |
| 136 | ++#pragma OPENCL EXTENSION __cl_clang_bitfields : enable |
| 137 | ++ |
| 138 | + #ifdef cl_khr_fp16 |
| 139 | + #error "Incorrect cl_khr_fp16 define" |
| 140 | + #endif |
| 141 | +diff --git a/clang/test/SemaOpenCL/unsupported.cl b/clang/test/SemaOpenCL/unsupported.cl |
| 142 | +index a5fc570e757e..36f60259f8a7 100644 |
| 143 | +--- a/clang/test/SemaOpenCL/unsupported.cl |
| 144 | ++++ b/clang/test/SemaOpenCL/unsupported.cl |
| 145 | +@@ -1,7 +1,15 @@ |
| 146 | + // RUN: %clang_cc1 -verify %s |
| 147 | ++// RUN: %clang_cc1 -verify %s -DBITFIELDS_EXT |
| 148 | + |
| 149 | +-struct { |
| 150 | +- int a : 1; // expected-error {{bit-fields are not supported in OpenCL}} |
| 151 | ++#ifdef BITFIELDS_EXT |
| 152 | ++#pragma OPENCL EXTENSION __cl_clang_bitfields : enable |
| 153 | ++#endif |
| 154 | ++ |
| 155 | ++struct test { |
| 156 | ++ int a : 1; |
| 157 | ++#ifndef BITFIELDS_EXT |
| 158 | ++// expected-error@-2 {{bit-fields are not supported in OpenCL}} |
| 159 | ++#endif |
| 160 | + }; |
| 161 | + |
| 162 | + void no_vla(int n) { |
0 commit comments