Skip to content

Reject half vector types without cl_khr_fp16 #96640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

svenvh
Copy link
Member

@svenvh svenvh commented Jun 25, 2024

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.

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.
@svenvh svenvh added the OpenCL label Jun 25, 2024
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-clang

Author: Sven van Haastregt (svenvh)

Changes

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 --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

@silverclaw
Copy link
Contributor

I'd love to see this finally committed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category OpenCL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants