Skip to content

Commit

Permalink
[LoongArch] Fix ABI mismatch with g++ when handling empty unions (#71025
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
SixWeining committed Nov 4, 2023
1 parent 2d460f2 commit 4253fdc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/Targets/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ bool LoongArchABIInfo::detectFARsEligibleStructHelper(
// copy constructor are not eligible for the FP calling convention.
if (getRecordArgABI(Ty, CGT.getCXXABI()))
return false;
if (isEmptyRecord(getContext(), Ty, true, true))
return true;
const RecordDecl *RD = RTy->getDecl();
// Unions aren't eligible unless they're empty (which is caught above).
if (isEmptyRecord(getContext(), Ty, true, true) &&
(!RD->isUnion() || !isa<CXXRecordDecl>(RD)))
return true;
// Unions aren't eligible unless they're empty in C (which is caught above).
if (RD->isUnion())
return false;
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/LoongArch/abi-lp64d-empty-structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: %clang_cc1 -triple loongarch64 -target-feature +f -target-feature +d -target-abi lp64d -emit-llvm %s -o - -x c++ | \
// RUN: FileCheck --check-prefix=CHECK-CXX %s

// Fields containing empty structs or unions are ignored when flattening
// Fields containing empty structs are ignored when flattening
// structs to examine whether the structs can be passed via FARs, even in C++.
// But there is an exception that non-zero-length array of empty structures are
// not ignored in C++. These rules are not documented in psABI <https://www.github.com/loongson/la-abi-specs>
Expand Down
3 changes: 1 addition & 2 deletions clang/test/CodeGen/LoongArch/abi-lp64d-empty-unions.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ struct s1 {
};

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

0 comments on commit 4253fdc

Please sign in to comment.