[clang] [Sema] Fix type mismatch error when arguments to elementwise math builtin have different qualifiers, which should be well-formed (PR #141485)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 29 03:00:38 PDT 2025
https://github.com/QiYueFeiXue updated https://github.com/llvm/llvm-project/pull/141485
>From 5e6d0a59babe2ea168625339565bb52e0ec7590b Mon Sep 17 00:00:00 2001
From: QiYue <yangzhh at mail.ustc.edu.cn>
Date: Mon, 26 May 2025 19:51:24 +0800
Subject: [PATCH 1/4] [Sema] built-in args type checking using
hasSameUnqualifiedType
Fixes #141397
---
clang/lib/Sema/SemaChecking.cpp | 10 +++++-----
clang/test/SemaCXX/bug141397.cpp | 16 ++++++++++++++++
2 files changed, 21 insertions(+), 5 deletions(-)
create mode 100644 clang/test/SemaCXX/bug141397.cpp
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 930e9083365a1..61aeffde338d8 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2907,7 +2907,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
return ExprError();
}
- if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) {
+ if (!Context.hasSameUnqualifiedType(MagnitudeTy, SignTy)) {
return Diag(Sign.get()->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< MagnitudeTy << SignTy;
@@ -5265,7 +5265,7 @@ bool Sema::BuiltinComplex(CallExpr *TheCall) {
Expr *Real = TheCall->getArg(0);
Expr *Imag = TheCall->getArg(1);
- if (!Context.hasSameType(Real->getType(), Imag->getType())) {
+ if (!Context.hasSameUnqualifiedType(Real->getType(), Imag->getType())) {
return Diag(Real->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< Real->getType() << Imag->getType()
@@ -15568,7 +15568,7 @@ Sema::BuiltinVectorMath(CallExpr *TheCall,
if (checkMathBuiltinElementType(*this, LocA, TyA, ArgTyRestr, 1))
return std::nullopt;
- if (TyA.getCanonicalType() != TyB.getCanonicalType()) {
+ if (!Context.hasSameUnqualifiedType(TyA, TyB)) {
Diag(LocA, diag::err_typecheck_call_different_arg_types) << TyA << TyB;
return std::nullopt;
}
@@ -15607,8 +15607,8 @@ bool Sema::BuiltinElementwiseTernaryMath(
}
for (int I = 1; I < 3; ++I) {
- if (Args[0]->getType().getCanonicalType() !=
- Args[I]->getType().getCanonicalType()) {
+ if (!Context.hasSameUnqualifiedType(Args[0]->getType(),
+ Args[I]->getType())) {
return Diag(Args[0]->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< Args[0]->getType() << Args[I]->getType();
diff --git a/clang/test/SemaCXX/bug141397.cpp b/clang/test/SemaCXX/bug141397.cpp
new file mode 100644
index 0000000000000..cb9f13a93f955
--- /dev/null
+++ b/clang/test/SemaCXX/bug141397.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+// expected-no-diagnostics
+
+// This example uncovered a bug in Sema::BuiltinVectorMath, where we should be
+// using ASTContext::hasSameUnqualifiedType().
+
+typedef float vec3 __attribute__((ext_vector_type(3)));
+
+typedef struct {
+ vec3 b;
+} struc;
+
+vec3 foo(vec3 a,const struc* hi) {
+ vec3 b = __builtin_elementwise_max((vec3)(0.0f), a);
+ return __builtin_elementwise_pow(b, hi->b.yyy);
+}
>From 3fd6b57b4a2df4a3563dd123ca7b01e9ee75807f Mon Sep 17 00:00:00 2001
From: QiYue <yangzhh at mail.ustc.edu.cn>
Date: Thu, 29 May 2025 11:47:50 +0800
Subject: [PATCH 2/4] fixup! [Sema] built-in args type checking using
hasSameUnqualifiedType
---
clang/docs/ReleaseNotes.rst | 1 +
clang/test/Sema/builtins-elementwise-math.c | 10 ++++++++++
2 files changed, 11 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 32266fce4d3cb..ef2b3035f896f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -669,6 +669,7 @@ Bug Fixes in This Version
base classes. (GH139452)
- Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130)
- Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701)
+- Fixed type mismatch error caused by const structure pointer dereference when builtins parameter is used. (#GH141397)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/Sema/builtins-elementwise-math.c b/clang/test/Sema/builtins-elementwise-math.c
index b5648a5e5c6e8..c22c18f99247f 100644
--- a/clang/test/Sema/builtins-elementwise-math.c
+++ b/clang/test/Sema/builtins-elementwise-math.c
@@ -3,6 +3,7 @@
typedef double double2 __attribute__((ext_vector_type(2)));
typedef double double4 __attribute__((ext_vector_type(4)));
typedef float float2 __attribute__((ext_vector_type(2)));
+typedef float float3 __attribute__((ext_vector_type(3)));
typedef float float4 __attribute__((ext_vector_type(4)));
typedef int int2 __attribute__((ext_vector_type(2)));
@@ -1202,3 +1203,12 @@ void test_builtin_elementwise_fma(int i32, int2 v2i32, short i16,
c3 = __builtin_elementwise_fma(f32, f32, c3);
// expected-error at -1 {{3rd argument must be a scalar or vector of floating-point types (was '_Complex float')}}
}
+
+typedef struct {
+ float3 b;
+} struct_float3;
+// This example uncovered a bug #141397
+float3 foo(float3 a,const struct_float3* hi) {
+ float3 b = __builtin_elementwise_max((float3)(0.0f), a);
+ return __builtin_elementwise_pow(b, hi->b.yyy);
+}
>From 0feb5eb97e9526ae2dba702895b2e8955a9580c2 Mon Sep 17 00:00:00 2001
From: QiYue <yangzhh at mail.ustc.edu.cn>
Date: Thu, 29 May 2025 13:09:22 +0800
Subject: [PATCH 3/4] Rollback useless changes and update comment
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Sema/SemaChecking.cpp | 8 ++++----
clang/test/Sema/builtins-elementwise-math.c | 3 ++-
clang/test/SemaCXX/bug141397.cpp | 16 ----------------
4 files changed, 7 insertions(+), 22 deletions(-)
delete mode 100644 clang/test/SemaCXX/bug141397.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ef2b3035f896f..3bceeb8f7268d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -669,7 +669,7 @@ Bug Fixes in This Version
base classes. (GH139452)
- Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130)
- Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701)
-- Fixed type mismatch error caused by const structure pointer dereference when builtins parameter is used. (#GH141397)
+- Fixed type missmach error when 'builtin-elementwise-math' arguments have different qualifiers, this should be a well-formed. (#GH141397)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 61aeffde338d8..8d06ee80374bc 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2907,7 +2907,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
return ExprError();
}
- if (!Context.hasSameUnqualifiedType(MagnitudeTy, SignTy)) {
+ if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) {
return Diag(Sign.get()->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< MagnitudeTy << SignTy;
@@ -5265,7 +5265,7 @@ bool Sema::BuiltinComplex(CallExpr *TheCall) {
Expr *Real = TheCall->getArg(0);
Expr *Imag = TheCall->getArg(1);
- if (!Context.hasSameUnqualifiedType(Real->getType(), Imag->getType())) {
+ if (!Context.hasSameType(Real->getType(), Imag->getType())) {
return Diag(Real->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< Real->getType() << Imag->getType()
@@ -15607,8 +15607,8 @@ bool Sema::BuiltinElementwiseTernaryMath(
}
for (int I = 1; I < 3; ++I) {
- if (!Context.hasSameUnqualifiedType(Args[0]->getType(),
- Args[I]->getType())) {
+ if (Args[0]->getType().getCanonicalType() !=
+ Args[I]->getType().getCanonicalType()) {
return Diag(Args[0]->getBeginLoc(),
diag::err_typecheck_call_different_arg_types)
<< Args[0]->getType() << Args[I]->getType();
diff --git a/clang/test/Sema/builtins-elementwise-math.c b/clang/test/Sema/builtins-elementwise-math.c
index c22c18f99247f..7a1db2d403651 100644
--- a/clang/test/Sema/builtins-elementwise-math.c
+++ b/clang/test/Sema/builtins-elementwise-math.c
@@ -1207,7 +1207,8 @@ void test_builtin_elementwise_fma(int i32, int2 v2i32, short i16,
typedef struct {
float3 b;
} struct_float3;
-// This example uncovered a bug #141397
+// This example uncovered a bug #141397 :
+// Type missmach error when 'builtin-elementwise-math' arguments have different qualifiers, this should be a well-formed
float3 foo(float3 a,const struct_float3* hi) {
float3 b = __builtin_elementwise_max((float3)(0.0f), a);
return __builtin_elementwise_pow(b, hi->b.yyy);
diff --git a/clang/test/SemaCXX/bug141397.cpp b/clang/test/SemaCXX/bug141397.cpp
deleted file mode 100644
index cb9f13a93f955..0000000000000
--- a/clang/test/SemaCXX/bug141397.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify
-// expected-no-diagnostics
-
-// This example uncovered a bug in Sema::BuiltinVectorMath, where we should be
-// using ASTContext::hasSameUnqualifiedType().
-
-typedef float vec3 __attribute__((ext_vector_type(3)));
-
-typedef struct {
- vec3 b;
-} struc;
-
-vec3 foo(vec3 a,const struc* hi) {
- vec3 b = __builtin_elementwise_max((vec3)(0.0f), a);
- return __builtin_elementwise_pow(b, hi->b.yyy);
-}
>From e76a942ef825d95ccc3dcc220f0e79231b17c9c4 Mon Sep 17 00:00:00 2001
From: QiYue <yangzhh at mail.ustc.edu.cn>
Date: Thu, 29 May 2025 18:00:19 +0800
Subject: [PATCH 4/4] fixup! Rollback useless changes and update comment
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/test/Sema/builtins-elementwise-math.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3bceeb8f7268d..dc97883de05d0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -669,7 +669,7 @@ Bug Fixes in This Version
base classes. (GH139452)
- Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130)
- Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701)
-- Fixed type missmach error when 'builtin-elementwise-math' arguments have different qualifiers, this should be a well-formed. (#GH141397)
+- Fixed type mismatch error when 'builtin-elementwise-math' arguments have different qualifiers, this should be well-formed. (#GH141397)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/Sema/builtins-elementwise-math.c b/clang/test/Sema/builtins-elementwise-math.c
index 7a1db2d403651..01057b3f8d083 100644
--- a/clang/test/Sema/builtins-elementwise-math.c
+++ b/clang/test/Sema/builtins-elementwise-math.c
@@ -1208,7 +1208,7 @@ typedef struct {
float3 b;
} struct_float3;
// This example uncovered a bug #141397 :
-// Type missmach error when 'builtin-elementwise-math' arguments have different qualifiers, this should be a well-formed
+// Type mismatch error when 'builtin-elementwise-math' arguments have different qualifiers, this should be well-formed
float3 foo(float3 a,const struct_float3* hi) {
float3 b = __builtin_elementwise_max((float3)(0.0f), a);
return __builtin_elementwise_pow(b, hi->b.yyy);
More information about the cfe-commits
mailing list