[clang] Reject half vector types without cl_khr_fp16 (PR #96640)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 25 07:08:23 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Sven van Haastregt (svenvh)

<details>
<summary>Changes</summary>

Reject `half` vector types (`halfn`) if the `cl_khr_fp16` extension is disabled, in line with the already existing rejection of `half` scalar types and `half` array types.

---
Full diff: https://github.com/llvm/llvm-project/pull/96640.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaDecl.cpp (+9-3) 
- (modified) clang/test/SemaOpenCL/half.cl (+4) 


``````````diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 029ccf944c513..639729467e9e4 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7448,9 +7448,15 @@ static bool diagnoseOpenCLTypes(Sema &Se, VarDecl *NewVD) {
 
   if (!Se.getOpenCLOptions().isAvailableOption("cl_khr_fp16",
                                                Se.getLangOpts())) {
-    // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and
-    // half array type (unless the cl_khr_fp16 extension is enabled).
-    if (Se.Context.getBaseElementType(R)->isHalfType()) {
+    // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half/halfn and
+    // half/halfn array type (unless the cl_khr_fp16 extension is enabled).
+    auto HasHalfTy = [](QualType T) {
+      if (const auto *EVTy = T->getAs<ExtVectorType>()) {
+        return EVTy->getElementType()->isHalfType();
+      }
+      return T->isHalfType();
+    };
+    if (HasHalfTy(R) || HasHalfTy(Se.Context.getBaseElementType(R))) {
       Se.Diag(NewVD->getLocation(), diag::err_opencl_half_declaration) << R;
       NewVD->setInvalidDecl();
       return false;
diff --git a/clang/test/SemaOpenCL/half.cl b/clang/test/SemaOpenCL/half.cl
index d0cd529a8f9af..b86fc95ee1b83 100644
--- a/clang/test/SemaOpenCL/half.cl
+++ b/clang/test/SemaOpenCL/half.cl
@@ -23,6 +23,8 @@ half half_disabled(half *p, // expected-error{{declaring function return value o
   half *allowed3 = p + 1;
 
 #ifdef HAVE_BUILTINS
+  half2 h2; // expected-error{{declaring variable of type '__private half2' (vector of 2 'half' values) is not allowed}}
+  half4 h4a[2]; // expected-error{{declaring variable of type '__private half4[2]' is not allowed}}
   (void)ilogb(*p); // expected-error{{loading directly from pointer to type '__private half' requires cl_khr_fp16. Use vector data load builtin functions instead}}
   vstore_half(42.0f, 0, p);
 #endif
@@ -55,6 +57,8 @@ half half_enabled(half *p, half h)
   half *allowed3 = p + 1;
 
 #ifdef HAVE_BUILTINS
+  half2 h2;
+  half4 h4a[2];
   (void)ilogb(*p);
   vstore_half(42.0f, 0, p);
 #endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/96640


More information about the cfe-commits mailing list