[PATCH] D103175: [C++4OpenCL] Fix ICE with invalid use of half

Ole Strohm via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 26 08:48:44 PDT 2021


olestrohm created this revision.
olestrohm added reviewers: Anastasia, svenvh.
olestrohm added a project: clang.
Herald added subscribers: ldrumm, yaxunl.
olestrohm requested review of this revision.
Herald added a subscriber: cfe-commits.

Because half is limited to the `cl_khr_fp16` extension being enabled, `DefaultLvalueConversion` can fail when it's not enabled.
Because of this the original assumption that it will never return an error is wrong.

This is fixed by checking for an error and returning early if there is one, instead of assuming that `DefaultLvalueConversion` is infallible.

Fixes: PR47976


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103175

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/SemaOpenCLCXX/half.clcpp


Index: clang/test/SemaOpenCLCXX/half.clcpp
===================================================================
--- /dev/null
+++ clang/test/SemaOpenCLCXX/half.clcpp
@@ -0,0 +1,15 @@
+//RUN: %clang_cc1 %s -triple spir -verify -fsyntax-only
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : disable
+
+typedef half half2 __attribute__((ext_vector_type(2)));
+
+half f(half2 h2) { // expected-error{{declaring function return value of type 'half' is not allowed ; did you forget * ?}}
+    return h2.s0; // expected-error{{loading directly from pointer to type '__private half' requires cl_khr_fp16. Use vector data load builtin functions instead}}
+}
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+half f(half2 h2) {
+    return h2.s0;
+}
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -4193,7 +4193,9 @@
   case ICK_Lvalue_To_Rvalue: {
     assert(From->getObjectKind() != OK_ObjCProperty);
     ExprResult FromRes = DefaultLvalueConversion(From);
-    assert(!FromRes.isInvalid() && "Can't perform deduced conversion?!");
+    if (FromRes.isInvalid())
+      return ExprError();
+
     From = FromRes.get();
     FromType = From->getType();
     break;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103175.347972.patch
Type: text/x-patch
Size: 1282 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210526/8c752d97/attachment.bin>


More information about the cfe-commits mailing list