Skip to content

Conversation

@vitalybuka
Copy link
Collaborator

@vitalybuka vitalybuka commented Sep 12, 2025

According GCC documentation transparent union
calling convention is the same as the type of the
first member of the union.

C++ ignores attribute.

Follow up to #158193.

Created using spr 1.3.6
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Sep 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-clang

Author: Vitaly Buka (vitalybuka)

Changes

According GCC documentation transparent union
calling convention is the same as the type of the
first member of the union.

C++ ignores attribute.


Full diff: https://github.com/llvm/llvm-project/pull/158194.diff

5 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+12-5)
  • (modified) clang/test/CodeGen/cfi-icall-generalize.c (+1-1)
  • (modified) clang/test/CodeGen/cfi-icall-normalize2.c (+1-1)
  • (modified) clang/test/CodeGen/kcfi-generalize.c (+1-1)
  • (modified) clang/test/CodeGen/kcfi-normalize.c (+1-1)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 46dbd85665e5d..44441df5ca6d8 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2352,8 +2352,8 @@ static QualType GeneralizeTransparentUnion(QualType Ty) {
   return Ty;
 }
 
-static QualType GeneralizeTransparentUnion(QualType Ty) {
-}
+static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty,
+                                       bool GeneralizePointers);
 
 // Generalize pointer types to a void pointer with the qualifiers of the
 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
@@ -2363,12 +2363,19 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty,
                                bool GeneralizePointers) {
   Ty = GeneralizeTransparentUnion(Ty);
 
-  if (!GeneralizePointers || !Ty->isPointerType())
+  if (!Ty->isPointerType())
+    return Ty;
+
+  QualType PTy = Ty->getPointeeType();
+  if (PTy->getAs<FunctionProtoType>() || PTy->getAs<FunctionNoProtoType>())
+    Ty = Ctx.getPointerType(
+        GeneralizeFunctionType(Ctx, PTy, GeneralizePointers));
+
+  if (!GeneralizePointers)
     return Ty;
 
   return Ctx.getPointerType(
-      QualType(Ctx.VoidTy)
-          .withCVRQualifiers(Ty->getPointeeType().getCVRQualifiers()));
+      QualType(Ctx.VoidTy).withCVRQualifiers(PTy.getCVRQualifiers()));
 }
 
 // Apply type generalization to a FunctionType's return and argument types
diff --git a/clang/test/CodeGen/cfi-icall-generalize.c b/clang/test/CodeGen/cfi-icall-generalize.c
index 5359134805198..6659d76fbbbb2 100644
--- a/clang/test/CodeGen/cfi-icall-generalize.c
+++ b/clang/test/CodeGen/cfi-icall-generalize.c
@@ -30,5 +30,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: [[TYPE]] = !{i64 0, !"_ZTSFPPiPKcPS2_E"}
 // CHECK: [[TYPE_GENERALIZED]] = !{i64 0, !"_ZTSFPvPKvS_E.generalized"}
 
-// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFv5UnionEPcE"}
+// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFvPcES_E"}
 // CHECK: [[TYPE2_GENERALIZED]] = !{i64 0, !"_ZTSFvPvS_E.generalized"}
diff --git a/clang/test/CodeGen/cfi-icall-normalize2.c b/clang/test/CodeGen/cfi-icall-normalize2.c
index b9d9af7c8a47b..49b279c04fd56 100644
--- a/clang/test/CodeGen/cfi-icall-normalize2.c
+++ b/clang/test/CodeGen/cfi-icall-normalize2.c
@@ -39,4 +39,4 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvPFvu3i32ES_E.normalized"}
 // CHECK: ![[TYPE2]] = !{i64 0, !"_ZTSFvPFvu3i32S_ES_S_E.normalized"}
 // CHECK: ![[TYPE3]] = !{i64 0, !"_ZTSFvPFvu3i32S_S_ES_S_S_E.normalized"}
-// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFv5UnionEPu2i8E.normalized"}
+// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFvPu2i8ES0_E.normalized"}
diff --git a/clang/test/CodeGen/kcfi-generalize.c b/clang/test/CodeGen/kcfi-generalize.c
index 24e054549d527..91f2be7b74818 100644
--- a/clang/test/CodeGen/kcfi-generalize.c
+++ b/clang/test/CodeGen/kcfi-generalize.c
@@ -44,5 +44,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // UNGENERALIZED: [[TYPE3]] = !{i32 874141567}
 // GENERALIZED: [[TYPE3]] = !{i32 954385378}
 
-// UNGENERALIZED: [[TYPE2]] = !{i32 -1619636625}
+// UNGENERALIZED: [[TYPE2]] = !{i32 -1954865805}
 // GENERALIZED: [[TYPE2]] = !{i32 -125078496}
\ No newline at end of file
diff --git a/clang/test/CodeGen/kcfi-normalize.c b/clang/test/CodeGen/kcfi-normalize.c
index 08f5249b6d6c3..532959a7d30fd 100644
--- a/clang/test/CodeGen/kcfi-normalize.c
+++ b/clang/test/CodeGen/kcfi-normalize.c
@@ -45,5 +45,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: ![[TYPE1]] = !{i32 -1143117868}
 // CHECK: ![[TYPE2]] = !{i32 -460921415}
 // CHECK: ![[TYPE3]] = !{i32 -333839615}
-// C: ![[TYPE4]] = !{i32 -650530463}
+// C: ![[TYPE4]] = !{i32 1186327125}
 // CPP: ![[TYPE4]] = !{i32 1766237188}
\ No newline at end of file

@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-clang-codegen

Author: Vitaly Buka (vitalybuka)

Changes

According GCC documentation transparent union
calling convention is the same as the type of the
first member of the union.

C++ ignores attribute.


Full diff: https://github.com/llvm/llvm-project/pull/158194.diff

5 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+12-5)
  • (modified) clang/test/CodeGen/cfi-icall-generalize.c (+1-1)
  • (modified) clang/test/CodeGen/cfi-icall-normalize2.c (+1-1)
  • (modified) clang/test/CodeGen/kcfi-generalize.c (+1-1)
  • (modified) clang/test/CodeGen/kcfi-normalize.c (+1-1)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 46dbd85665e5d..44441df5ca6d8 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2352,8 +2352,8 @@ static QualType GeneralizeTransparentUnion(QualType Ty) {
   return Ty;
 }
 
-static QualType GeneralizeTransparentUnion(QualType Ty) {
-}
+static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty,
+                                       bool GeneralizePointers);
 
 // Generalize pointer types to a void pointer with the qualifiers of the
 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
@@ -2363,12 +2363,19 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty,
                                bool GeneralizePointers) {
   Ty = GeneralizeTransparentUnion(Ty);
 
-  if (!GeneralizePointers || !Ty->isPointerType())
+  if (!Ty->isPointerType())
+    return Ty;
+
+  QualType PTy = Ty->getPointeeType();
+  if (PTy->getAs<FunctionProtoType>() || PTy->getAs<FunctionNoProtoType>())
+    Ty = Ctx.getPointerType(
+        GeneralizeFunctionType(Ctx, PTy, GeneralizePointers));
+
+  if (!GeneralizePointers)
     return Ty;
 
   return Ctx.getPointerType(
-      QualType(Ctx.VoidTy)
-          .withCVRQualifiers(Ty->getPointeeType().getCVRQualifiers()));
+      QualType(Ctx.VoidTy).withCVRQualifiers(PTy.getCVRQualifiers()));
 }
 
 // Apply type generalization to a FunctionType's return and argument types
diff --git a/clang/test/CodeGen/cfi-icall-generalize.c b/clang/test/CodeGen/cfi-icall-generalize.c
index 5359134805198..6659d76fbbbb2 100644
--- a/clang/test/CodeGen/cfi-icall-generalize.c
+++ b/clang/test/CodeGen/cfi-icall-generalize.c
@@ -30,5 +30,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: [[TYPE]] = !{i64 0, !"_ZTSFPPiPKcPS2_E"}
 // CHECK: [[TYPE_GENERALIZED]] = !{i64 0, !"_ZTSFPvPKvS_E.generalized"}
 
-// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFv5UnionEPcE"}
+// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFvPcES_E"}
 // CHECK: [[TYPE2_GENERALIZED]] = !{i64 0, !"_ZTSFvPvS_E.generalized"}
diff --git a/clang/test/CodeGen/cfi-icall-normalize2.c b/clang/test/CodeGen/cfi-icall-normalize2.c
index b9d9af7c8a47b..49b279c04fd56 100644
--- a/clang/test/CodeGen/cfi-icall-normalize2.c
+++ b/clang/test/CodeGen/cfi-icall-normalize2.c
@@ -39,4 +39,4 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvPFvu3i32ES_E.normalized"}
 // CHECK: ![[TYPE2]] = !{i64 0, !"_ZTSFvPFvu3i32S_ES_S_E.normalized"}
 // CHECK: ![[TYPE3]] = !{i64 0, !"_ZTSFvPFvu3i32S_S_ES_S_S_E.normalized"}
-// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFv5UnionEPu2i8E.normalized"}
+// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFvPu2i8ES0_E.normalized"}
diff --git a/clang/test/CodeGen/kcfi-generalize.c b/clang/test/CodeGen/kcfi-generalize.c
index 24e054549d527..91f2be7b74818 100644
--- a/clang/test/CodeGen/kcfi-generalize.c
+++ b/clang/test/CodeGen/kcfi-generalize.c
@@ -44,5 +44,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // UNGENERALIZED: [[TYPE3]] = !{i32 874141567}
 // GENERALIZED: [[TYPE3]] = !{i32 954385378}
 
-// UNGENERALIZED: [[TYPE2]] = !{i32 -1619636625}
+// UNGENERALIZED: [[TYPE2]] = !{i32 -1954865805}
 // GENERALIZED: [[TYPE2]] = !{i32 -125078496}
\ No newline at end of file
diff --git a/clang/test/CodeGen/kcfi-normalize.c b/clang/test/CodeGen/kcfi-normalize.c
index 08f5249b6d6c3..532959a7d30fd 100644
--- a/clang/test/CodeGen/kcfi-normalize.c
+++ b/clang/test/CodeGen/kcfi-normalize.c
@@ -45,5 +45,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
 // CHECK: ![[TYPE1]] = !{i32 -1143117868}
 // CHECK: ![[TYPE2]] = !{i32 -460921415}
 // CHECK: ![[TYPE3]] = !{i32 -333839615}
-// C: ![[TYPE4]] = !{i32 -650530463}
+// C: ![[TYPE4]] = !{i32 1186327125}
 // CPP: ![[TYPE4]] = !{i32 1766237188}
\ No newline at end of file

Created using spr 1.3.6
Created using spr 1.3.6
Created using spr 1.3.6
Created using spr 1.3.6
Created using spr 1.3.6
return Ty;

QualType PTy = Ty->getPointeeType();
if (PTy->getAs<FunctionProtoType>() || PTy->getAs<FunctionNoProtoType>())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is not the only way you can end up with a function type. E.g. pointer to pointer to function, reference to pointer to function, etc.

What do you think about doing this in the mangler? E.g. add a call to a hook near the top of void CXXNameMangler::mangleType(QualType T) and supply a hook that does the transparent_union generalization from here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mangler is too late, if we have pointer generalization, it should apply to pointers form transparent union

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook could also do the pointer generalization, no?

@vitalybuka vitalybuka requested a review from pcc September 12, 2025 20:52
@vitalybuka vitalybuka marked this pull request as draft September 16, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants