| 1 |
From aca76d6c29c96ba1f64dd50dadc010d01d19c83c Mon Sep 17 00:00:00 2001 |
| 2 |
From: Wenju He <[email protected]> |
| 3 |
Date: Fri, 13 Feb 2026 14:09:49 +0100 |
| 4 |
Subject: [PATCH] [OpenCL] Diagnose invalid conversion from pointer to vector |
| 5 |
(#180318) |
| 6 |
|
| 7 |
Fix crash in clang PrepareScalarCast when compiling OpenCL source: void |
| 8 |
foo() { |
| 9 |
int a[3] = {1, 2, 3}; |
| 10 |
int3 b = (int3)(a); |
| 11 |
} |
| 12 |
|
| 13 |
PrepareScalarCast requires scalar src, but the provided src is a |
| 14 |
pointer. |
| 15 |
|
| 16 |
Resolves https://github.com/intel/compute-runtime/issues/888 |
| 17 |
--- |
| 18 |
clang/lib/Sema/SemaExpr.cpp | 7 +++++++ |
| 19 |
clang/test/SemaOpenCL/invalid-vector-literals.cl | 4 ++++ |
| 20 |
2 files changed, 11 insertions(+) |
| 21 |
|
| 22 |
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp |
| 23 |
index 687b1be94592..73ffd76ae80c 100644 |
| 24 |
--- a/clang/lib/Sema/SemaExpr.cpp |
| 25 |
+++ b/clang/lib/Sema/SemaExpr.cpp |
| 26 |
@@ -7796,6 +7796,13 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 27 |
// it will be replicated to all components of the vector. |
| 28 |
if (getLangOpts().OpenCL && VTy->getVectorKind() == VectorKind::Generic && |
| 29 |
numExprs == 1) { |
| 30 |
+ QualType SrcTy = exprs[0]->getType(); |
| 31 |
+ if (!SrcTy->isArithmeticType()) { |
| 32 |
+ Diag(exprs[0]->getBeginLoc(), diag::err_typecheck_convert_incompatible) |
| 33 |
+ << Ty << SrcTy << AssignmentAction::AA_Initializing << /*elidable=*/0 |
| 34 |
+ << /*c_style=*/0 << /*cast_kind=*/"" << exprs[0]->getSourceRange(); |
| 35 |
+ return ExprError(); |
| 36 |
+ } |
| 37 |
QualType ElemTy = VTy->getElementType(); |
| 38 |
ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 39 |
if (Literal.isInvalid()) |
| 40 |
diff --git a/clang/test/SemaOpenCL/invalid-vector-literals.cl b/clang/test/SemaOpenCL/invalid-vector-literals.cl |
| 41 |
index 1d82fedf29de..0595256ef82f 100644 |
| 42 |
--- a/clang/test/SemaOpenCL/invalid-vector-literals.cl |
| 43 |
+++ b/clang/test/SemaOpenCL/invalid-vector-literals.cl |
| 44 |
@@ -10,4 +10,8 @@ void vector_literals_invalid() |
| 45 |
int4 b = (int4)(1,2,3,4,5); // expected-error{{excess elements in vector}} |
| 46 |
int8 d = (int8)(a,(float4)(1)); // expected-error{{initializing 'int' with an expression of incompatible type 'float4'}} |
| 47 |
((int4)(0)).x = 8; // expected-error{{expression is not assignable}} |
| 48 |
+ int arr[4]; |
| 49 |
+ int4 e = (int4)(arr); // expected-error{{initializing 'int4' (vector of 4 'int' values) with an expression of incompatible type '__private int[4]'}} |
| 50 |
+ int *p = arr; |
| 51 |
+ int4 f = (int4)(p); // expected-error{{initializing 'int4' (vector of 4 'int' values) with an expression of incompatible type '__private int *__private'}} |
| 52 |
} |
| 53 |
-- |
| 54 |
2.39.1 |
| 55 |
|