[clang] Reject half vector types without cl_khr_fp16 (PR #96640)
Sven van Haastregt via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 25 07:07:52 PDT 2024
https://github.com/svenvh created https://github.com/llvm/llvm-project/pull/96640
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.
>From e26114f1aa6809e7b5f93f4e866e7ff25355edb3 Mon Sep 17 00:00:00 2001
From: Sven van Haastregt <sven.vanhaastregt at arm.com>
Date: Tue, 25 Jun 2024 14:59:14 +0100
Subject: [PATCH] Reject half vector types without cl_khr_fp16
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.
---
clang/lib/Sema/SemaDecl.cpp | 12 +++++++++---
clang/test/SemaOpenCL/half.cl | 4 ++++
2 files changed, 13 insertions(+), 3 deletions(-)
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
More information about the cfe-commits
mailing list