https://github.com/QiYueFeiXue updated 
https://github.com/llvm/llvm-project/pull/141485

>From 5e6d0a59babe2ea168625339565bb52e0ec7590b Mon Sep 17 00:00:00 2001
From: QiYue <yang...@mail.ustc.edu.cn>
Date: Mon, 26 May 2025 19:51:24 +0800
Subject: [PATCH 1/3] [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 <yang...@mail.ustc.edu.cn>
Date: Thu, 29 May 2025 11:47:50 +0800
Subject: [PATCH 2/3] 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@-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 <yang...@mail.ustc.edu.cn>
Date: Thu, 29 May 2025 13:09:22 +0800
Subject: [PATCH 3/3] 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);
-}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to