https://github.com/wenju-he updated 
https://github.com/llvm/llvm-project/pull/180318

>From 794f4040471c481249646384392af574ee4c5684 Mon Sep 17 00:00:00 2001
From: Wenju He <[email protected]>
Date: Sat, 7 Feb 2026 04:44:15 +0100
Subject: [PATCH 1/2] [OpenCL] Diagnose invalid conversion from pointer to
 vector

Fix crash in clang PrepareScalarCast when compiling OpenCL source:
void foo() {
  int a[3] = {1, 2, 3};
  int3 b = (int3)(a);
}

PrepareScalarCast requires scalar src, but the provided src is a pointer.

Resolves https://github.com/intel/compute-runtime/issues/888
---
 clang/lib/Sema/SemaExpr.cpp                      | 6 ++++++
 clang/test/SemaOpenCL/invalid-vector-literals.cl | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 77d3c4ef63f1f..0653a13ee88e0 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8112,6 +8112,12 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation 
LParenLoc,
     // it will be replicated to all components of the vector.
     if (getLangOpts().OpenCL && VTy->getVectorKind() == VectorKind::Generic &&
         numExprs == 1) {
+      if (!exprs[0]->getType()->isArithmeticType()) {
+        Diag(exprs[0]->getBeginLoc(), diag::err_typecheck_convert_incompatible)
+            << Ty << exprs[0]->getType() << 4 << 0 << 0 << ""
+            << exprs[0]->getSourceRange();
+        return ExprError();
+      }
       QualType ElemTy = VTy->getElementType();
       ExprResult Literal = DefaultLvalueConversion(exprs[0]);
       if (Literal.isInvalid())
diff --git a/clang/test/SemaOpenCL/invalid-vector-literals.cl 
b/clang/test/SemaOpenCL/invalid-vector-literals.cl
index 1d82fedf29de1..9ae5142b4a1b3 100644
--- a/clang/test/SemaOpenCL/invalid-vector-literals.cl
+++ b/clang/test/SemaOpenCL/invalid-vector-literals.cl
@@ -10,4 +10,6 @@ void vector_literals_invalid()
   int4 b = (int4)(1,2,3,4,5); // expected-error{{excess elements in vector}}
   int8 d = (int8)(a,(float4)(1)); // expected-error{{initializing 'int' with 
an expression of incompatible type 'float4'}}
   ((int4)(0)).x = 8; // expected-error{{expression is not assignable}}
+  int arr[4];
+  int4 e = (int4)(arr); // expected-error{{initializing 'int4' (vector of 4 
'int' values) with an expression of incompatible type '__private int[4]'}}
 }

>From 134f31bd3a03150c206e65df27ee3a8481242e34 Mon Sep 17 00:00:00 2001
From: Wenju He <[email protected]>
Date: Sat, 7 Feb 2026 05:01:00 +0100
Subject: [PATCH 2/2] address copilot review comments

---
 clang/lib/Sema/SemaExpr.cpp                      | 4 ++--
 clang/test/SemaOpenCL/invalid-vector-literals.cl | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 0653a13ee88e0..faf07b96155a2 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8114,8 +8114,8 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation 
LParenLoc,
         numExprs == 1) {
       if (!exprs[0]->getType()->isArithmeticType()) {
         Diag(exprs[0]->getBeginLoc(), diag::err_typecheck_convert_incompatible)
-            << Ty << exprs[0]->getType() << 4 << 0 << 0 << ""
-            << exprs[0]->getSourceRange();
+            << Ty << exprs[0]->getType() << AssignmentAction::Initializing << 0
+            << 0 << "" << exprs[0]->getSourceRange();
         return ExprError();
       }
       QualType ElemTy = VTy->getElementType();
diff --git a/clang/test/SemaOpenCL/invalid-vector-literals.cl 
b/clang/test/SemaOpenCL/invalid-vector-literals.cl
index 9ae5142b4a1b3..0595256ef82f2 100644
--- a/clang/test/SemaOpenCL/invalid-vector-literals.cl
+++ b/clang/test/SemaOpenCL/invalid-vector-literals.cl
@@ -12,4 +12,6 @@ void vector_literals_invalid()
   ((int4)(0)).x = 8; // expected-error{{expression is not assignable}}
   int arr[4];
   int4 e = (int4)(arr); // expected-error{{initializing 'int4' (vector of 4 
'int' values) with an expression of incompatible type '__private int[4]'}}
+  int *p = arr;
+  int4 f = (int4)(p); // expected-error{{initializing 'int4' (vector of 4 
'int' values) with an expression of incompatible type '__private int 
*__private'}}
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to