Skip to content

Commit 4253fdc

Browse files
authored
[LoongArch] Fix ABI mismatch with g++ when handling empty unions (#71025)
In g++, empty unions are not ignored like empty structs when flattening structs to examine whether the structs can be passed via FARs in C++. This patch aligns clang++ with g++. Fix #70890.
1 parent 2d460f2 commit 4253fdc

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

clang/lib/CodeGen/Targets/LoongArch.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,11 @@ bool LoongArchABIInfo::detectFARsEligibleStructHelper(
170170
// copy constructor are not eligible for the FP calling convention.
171171
if (getRecordArgABI(Ty, CGT.getCXXABI()))
172172
return false;
173-
if (isEmptyRecord(getContext(), Ty, true, true))
174-
return true;
175173
const RecordDecl *RD = RTy->getDecl();
176-
// Unions aren't eligible unless they're empty (which is caught above).
174+
if (isEmptyRecord(getContext(), Ty, true, true) &&
175+
(!RD->isUnion() || !isa<CXXRecordDecl>(RD)))
176+
return true;
177+
// Unions aren't eligible unless they're empty in C (which is caught above).
177178
if (RD->isUnion())
178179
return false;
179180
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);

clang/test/CodeGen/LoongArch/abi-lp64d-empty-structs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %clang_cc1 -triple loongarch64 -target-feature +f -target-feature +d -target-abi lp64d -emit-llvm %s -o - -x c++ | \
44
// RUN: FileCheck --check-prefix=CHECK-CXX %s
55

6-
// Fields containing empty structs or unions are ignored when flattening
6+
// Fields containing empty structs are ignored when flattening
77
// structs to examine whether the structs can be passed via FARs, even in C++.
88
// But there is an exception that non-zero-length array of empty structures are
99
// not ignored in C++. These rules are not documented in psABI <https://www.github.com/loongson/la-abi-specs>

clang/test/CodeGen/LoongArch/abi-lp64d-empty-unions.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ struct s1 {
1919
};
2020

2121
// CHECK-C: define{{.*}} { i32, float } @test2(i32{{[^,]*}}, float{{[^,]*}})
22-
/// FIXME: This doesn't match g++.
23-
// CHECK-CXX: define{{.*}} { i32, float } @_Z5test22s1(i32{{[^,]*}}, float{{[^,]*}})
22+
// CHECK-CXX: define{{.*}} [2 x i64] @_Z5test22s1([2 x i64]{{[^,]*}})
2423
struct s1 test2(struct s1 a) {
2524
return a;
2625
}

0 commit comments

Comments
 (0)