[clang] [clang][Sparc] Pass packed structs and unions in integer registers on SPARC64 (PR #246226)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 18:59:24 PST 2026
https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/246226
GCC never passes or returns a union in floating-point registers on
SPARC64, and does the same for a struct with a packed field, which is
every struct declared with __attribute__((packed)). Being packed carries
over to the structs and base classes nested in such a struct, but not to
the struct a packed struct is nested in. Clang passed and returned the
floating-point fields of both in floating-point registers.
Follow GCC's traverse_record_type() and keep those fields out of
floating-point registers, treating base classes like fields as GCC does.
As in GCC, the packed attribute has no effect on a byte-aligned
non-bit-field, and a packed class does not pack a field of a non-POD
class type. #pragma pack does not make fields packed.
-fclang-abi-compat=23 restores the previous behavior.
Fixes #39500
Fixes #214840
>From fe088f0a1b458d0c9d562b127c4136d3d454dbaf Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Wed, 25 Nov 2026 21:53:06 -0500
Subject: [PATCH] [clang][Sparc] Pass packed structs and unions in integer
registers on SPARC64
GCC never passes or returns a union in floating-point registers on
SPARC64, and does the same for a struct with a packed field, which is
every struct declared with __attribute__((packed)). Being packed carries
over to the structs and base classes nested in such a struct, but not to
the struct a packed struct is nested in. Clang passed and returned the
floating-point fields of both in floating-point registers.
Follow GCC's traverse_record_type() and keep those fields out of
floating-point registers, treating base classes like fields as GCC does.
As in GCC, the packed attribute has no effect on a byte-aligned
non-bit-field, and a packed class does not pack a field of a non-POD
class type. #pragma pack does not make fields packed.
-fclang-abi-compat=23 restores the previous behavior.
Fixes #39500
Fixes #214840
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
clang/docs/ReleaseNotes.md | 7 +
clang/lib/CodeGen/Targets/Sparc.cpp | 87 +++++++++
.../CodeGen/Sparc/sparcv9-packed-union-abi.c | 184 ++++++++++++++++++
.../CodeGenCXX/sparcv9-packed-union-abi.cpp | 95 +++++++++
4 files changed, 373 insertions(+)
create mode 100644 clang/test/CodeGen/Sparc/sparcv9-packed-union-abi.c
create mode 100644 clang/test/CodeGenCXX/sparcv9-packed-union-abi.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 31faeace476760..b553f6ac4d49b6 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -117,6 +117,13 @@ features cannot lower the translation-unit ABI level;
narrower than a slot, rather than left-justified the way a small struct is.
`-fclang-abi-compat=23` restores the previous behavior. (#GH212340)
+- On SPARC64, unions, and structs with a field that is packed, either directly
+ or through `__attribute__((packed))` on the struct, are now passed and
+ returned in integer registers, as are the structs and base classes nested in
+ such a struct, matching GCC. Clang previously passed and returned their
+ floating-point fields in floating-point registers.
+ `-fclang-abi-compat=23` restores the previous behavior. (#GH39500) (#GH214840)
+
- On MIPS, a `_Complex` value with an integer element type is now returned packed
into a single integer register when it fits in one, matching GCC. A `_Complex char` or
`_Complex short`, and on N32/N64 also a `_Complex int`, is no longer returned
diff --git a/clang/lib/CodeGen/Targets/Sparc.cpp b/clang/lib/CodeGen/Targets/Sparc.cpp
index fcd2086964c488..6921ef12d7bc50 100644
--- a/clang/lib/CodeGen/Targets/Sparc.cpp
+++ b/clang/lib/CodeGen/Targets/Sparc.cpp
@@ -199,12 +199,18 @@ class SparcV9ABIInfo : public ABIInfo {
SparcV9ABIInfo(CodeGenTypes &CGT)
: ABIInfo(CGT),
IsComplexGnuABI(!CGT.getContext().getLangOpts().isCompatibleWith(
+ LangOptions::ClangABI::Ver23)),
+ IsPackedUnionGnuABI(!CGT.getContext().getLangOpts().isCompatibleWith(
LangOptions::ClangABI::Ver23)) {}
private:
/// Whether how `_Complex` values are passed and returned is GCC-compatible.
bool IsComplexGnuABI;
+ /// Whether the floating-point fields of packed structs and of unions are
+ /// kept out of floating-point registers, as GCC does.
+ bool IsPackedUnionGnuABI;
+
ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit,
unsigned &RegOffset, bool IsVarArg) const;
void computeInfo(CGFunctionInfo &FI) const override;
@@ -229,6 +235,9 @@ class SparcV9ABIInfo : public ABIInfo {
uint64_t Size;
bool InReg;
bool IsVarArg;
+ // Bit ranges [Begin, End) whose floating-point elements must be passed in
+ // integer registers.
+ SmallVector<std::pair<uint64_t, uint64_t>, 4> IntRanges;
CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl,
bool IsVarArg)
@@ -268,6 +277,11 @@ class SparcV9ABIInfo : public ABIInfo {
// Unaligned floats are treated as integers.
if (Offset % Bits)
return;
+ // Floats in packed structs and in unions are treated as integers.
+ if (llvm::any_of(IntRanges, [Offset](const auto &R) {
+ return R.first <= Offset && Offset < R.second;
+ }))
+ return;
// The InReg flag is only required if there are any floats < 64 bits.
if (Bits < 64)
InReg = true;
@@ -324,6 +338,75 @@ class SparcV9ABIInfo : public ABIInfo {
};
} // end anonymous namespace
+/// Return true if GCC treats \p FD as a packed field (DECL_PACKED). That is the
+/// case when the field or its record has the packed attribute.
+static bool isPackedField(const ASTContext &Ctx, const FieldDecl *FD) {
+ // The packed attribute has no effect on a byte-aligned non-bit-field.
+ if (!FD->isBitField() &&
+ Ctx.getTypeAlign(FD->getType()) <= Ctx.getCharWidth())
+ return false;
+ if (FD->hasAttr<PackedAttr>())
+ return true;
+ if (!FD->getParent()->hasAttr<PackedAttr>())
+ return false;
+ // A packed class does not pack a field of a non-POD class type, unless that
+ // class is packed itself.
+ const auto *FieldClass =
+ Ctx.getBaseElementType(FD->getType())->getAsCXXRecordDecl();
+ return !FieldClass || FieldClass->isPOD() ||
+ FieldClass->hasAttr<PackedAttr>();
+}
+
+/// Collect the bit ranges of the fields of \p RD, placed at bit \p Offset, that
+/// GCC never passes or returns in floating-point registers.
+///
+/// This follows GCC's traverse_record_type(): a struct with a packed field is
+/// passed in integer registers, and so is everything nested in it, including
+/// its base classes. A union is always passed in integer registers.
+static void
+collectIntRanges(const ASTContext &Ctx, const RecordDecl *RD, uint64_t Offset,
+ bool Packed,
+ SmallVectorImpl<std::pair<uint64_t, uint64_t>> &Ranges) {
+ const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
+
+ if (RD->isUnion()) {
+ Ranges.emplace_back(Offset, Offset + Ctx.toBits(Layout.getSize()));
+ return;
+ }
+
+ if (!Packed)
+ Packed = llvm::any_of(RD->fields(), [&Ctx](const FieldDecl *FD) {
+ return isPackedField(Ctx, FD);
+ });
+
+ // GCC represents base classes as fields, so they are handled the same way.
+ // Virtual bases are not considered: such a class is never passed in
+ // registers.
+ if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
+ for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
+ if (Base.isVirtual())
+ continue;
+ const auto *BaseRD = Base.getType()->castAsCXXRecordDecl();
+ collectIntRanges(Ctx, BaseRD,
+ Offset + Ctx.toBits(Layout.getBaseClassOffset(BaseRD)),
+ Packed, Ranges);
+ }
+ }
+
+ for (const FieldDecl *FD : RD->fields()) {
+ uint64_t FieldOffset = Offset + Layout.getFieldOffset(FD->getFieldIndex());
+ if (const auto *FieldRD = FD->getType()->getAsRecordDecl()) {
+ collectIntRanges(Ctx, FieldRD->getDefinitionOrSelf(), FieldOffset, Packed,
+ Ranges);
+ continue;
+ }
+ // Bit-fields are never floating point.
+ if (Packed && !FD->isBitField())
+ Ranges.emplace_back(FieldOffset,
+ FieldOffset + Ctx.getTypeSize(FD->getType()));
+ }
+}
+
ABIArgInfo SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit,
unsigned &RegOffset,
bool IsVarArg) const {
@@ -419,6 +502,10 @@ ABIArgInfo SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit,
}
CoerceBuilder CB(VMContext, getDataLayout(), IsVarArg);
+ if (IsPackedUnionGnuABI)
+ if (const auto *RD = Ty->getAsRecordDecl())
+ collectIntRanges(Context, RD->getDefinitionOrSelf(), /*Offset=*/0,
+ /*Packed=*/false, CB.IntRanges);
CB.addStruct(0, StrTy);
// All structs, even empty ones, should take up a register argument slot,
// so pin the minimum struct size to one bit.
diff --git a/clang/test/CodeGen/Sparc/sparcv9-packed-union-abi.c b/clang/test/CodeGen/Sparc/sparcv9-packed-union-abi.c
new file mode 100644
index 00000000000000..5d417ff5a75060
--- /dev/null
+++ b/clang/test/CodeGen/Sparc/sparcv9-packed-union-abi.c
@@ -0,0 +1,184 @@
+// RUN: %clang_cc1 -triple sparcv9-unknown-unknown -emit-llvm %s -o - \
+// RUN: | FileCheck %s
+// RUN: %clang_cc1 -triple sparcv9-unknown-unknown -emit-llvm %s -o - \
+// RUN: -fclang-abi-compat=23 | FileCheck %s --check-prefix=COMPAT
+
+// GCC never passes or returns a union, or a struct with a packed field, in
+// floating-point registers on SPARC64. A struct declared packed has all of its
+// fields packed. Being packed carries over to the structs nested in a packed
+// struct, but not to the structs a packed struct is nested in.
+// See https://github.com/llvm/llvm-project/issues/39500 and
+// https://github.com/llvm/llvm-project/issues/214840.
+
+union u {
+ float f;
+ int i;
+};
+
+// CHECK-LABEL: define{{.*}} float @union_float(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} float @union_float(float inreg %x.coerce0, i32 inreg %x.coerce1)
+float union_float(union u x) { return x.f; }
+
+// CHECK-LABEL: define{{.*}} signext i32 @union_int(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} signext i32 @union_int(float inreg %x.coerce0, i32 inreg %x.coerce1)
+int union_int(union u x) { return x.i; }
+
+// CHECK-LABEL: define{{.*}} i64 @union_ret(float noundef %f)
+// COMPAT-LABEL: define{{.*}} inreg { float, i32 } @union_ret(float noundef %f)
+union u union_ret(float f) {
+ union u x;
+ x.f = f;
+ return x;
+}
+
+union ud {
+ double d;
+ long l;
+};
+
+// CHECK-LABEL: define{{.*}} i64 @union_double(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} %union.ud @union_double(double %x.coerce)
+union ud union_double(union ud x) { return x; }
+
+// A union of structs is passed in integer registers.
+union us {
+ struct {
+ float a, b;
+ } s;
+ int i;
+};
+
+// CHECK-LABEL: define{{.*}} i64 @union_struct(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @union_struct(float inreg %x.coerce0, float inreg %x.coerce1)
+union us union_struct(union us x) { return x; }
+
+// A union field is passed in integer registers, the other fields are not
+// affected.
+struct union_first {
+ union u u;
+ float g;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { i32, float } @union_first(i32 inreg %x.coerce0, float inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @union_first(float inreg %x.coerce0, float inreg %x.coerce1)
+struct union_first union_first(struct union_first x) { return x; }
+
+struct union_second {
+ float f;
+ union u u;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { float, i32 } @union_second(float inreg %x.coerce0, i32 inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @union_second(float inreg %x.coerce0, float inreg %x.coerce1)
+struct union_second union_second(struct union_second x) { return x; }
+
+struct packed_float_int {
+ float f;
+ int i;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} i64 @packed_float_int(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg %struct.packed_float_int @packed_float_int(float inreg %x.coerce0, i32 inreg %x.coerce1)
+struct packed_float_int packed_float_int(struct packed_float_int x) {
+ return x;
+}
+
+struct packed_double {
+ double d;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} i64 @packed_double(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} %struct.packed_double @packed_double(double %x.coerce)
+struct packed_double packed_double(struct packed_double x) { return x; }
+
+struct packed_complex {
+ _Complex float c;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} i64 @packed_complex(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @packed_complex(float inreg %x.coerce0, float inreg %x.coerce1)
+struct packed_complex packed_complex(struct packed_complex x) { return x; }
+
+// A struct nested in a packed struct is packed too.
+struct floats {
+ float f, g;
+};
+
+struct packed_outer {
+ struct floats s;
+ double d;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} { i64, i64 } @packed_outer(i64 %x.coerce0, i64 %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float, double } @packed_outer(float inreg %x.coerce0, float inreg %x.coerce1, double inreg %x.coerce2)
+struct packed_outer packed_outer(struct packed_outer x) { return x; }
+
+// A packed struct does not affect the struct it is nested in.
+struct packed_float {
+ float f;
+} __attribute__((packed));
+
+struct plain_outer {
+ struct packed_float s;
+ float g;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { i32, float } @plain_outer(i32 inreg %x.coerce0, float inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @plain_outer(float inreg %x.coerce0, float inreg %x.coerce1)
+struct plain_outer plain_outer(struct plain_outer x) { return x; }
+
+// A single packed field makes the whole struct packed.
+struct packed_field {
+ float f;
+ float g __attribute__((packed));
+};
+
+// CHECK-LABEL: define{{.*}} i64 @packed_field(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg %struct.packed_field @packed_field(float inreg %x.coerce0, float inreg %x.coerce1)
+struct packed_field packed_field(struct packed_field x) { return x; }
+
+struct packed_field_nested {
+ struct floats s;
+ int i __attribute__((packed));
+};
+
+// CHECK-LABEL: define{{.*}} { i64, i64 } @packed_field_nested(i64 %x.coerce0, i64 %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float, i64 } @packed_field_nested(float inreg %x.coerce0, float inreg %x.coerce1, i64 inreg %x.coerce2)
+struct packed_field_nested packed_field_nested(struct packed_field_nested x) {
+ return x;
+}
+
+// The packed attribute has no effect on a byte-aligned field, so the struct is
+// not packed.
+struct packed_char {
+ char c __attribute__((packed));
+ float f;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { i32, float } @packed_char(i32 inreg %x.coerce0, float inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { i32, float } @packed_char(i32 inreg %x.coerce0, float inreg %x.coerce1)
+struct packed_char packed_char(struct packed_char x) { return x; }
+
+// But it does on a bit-field.
+struct packed_char_bitfield {
+ char c : 4 __attribute__((packed));
+ float f;
+};
+
+// CHECK-LABEL: define{{.*}} i64 @packed_char_bitfield(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg { i32, float } @packed_char_bitfield(i32 inreg %x.coerce0, float inreg %x.coerce1)
+struct packed_char_bitfield packed_char_bitfield(struct packed_char_bitfield x) {
+ return x;
+}
+
+// #pragma pack only reduces the alignment, it does not make the fields packed.
+#pragma pack(1)
+struct pragma_packed {
+ float f;
+ int i;
+};
+#pragma pack()
+
+// CHECK-LABEL: define{{.*}} inreg %struct.pragma_packed @pragma_packed(float inreg %x.coerce0, i32 inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg %struct.pragma_packed @pragma_packed(float inreg %x.coerce0, i32 inreg %x.coerce1)
+struct pragma_packed pragma_packed(struct pragma_packed x) { return x; }
diff --git a/clang/test/CodeGenCXX/sparcv9-packed-union-abi.cpp b/clang/test/CodeGenCXX/sparcv9-packed-union-abi.cpp
new file mode 100644
index 00000000000000..b2ea07b8e23ef5
--- /dev/null
+++ b/clang/test/CodeGenCXX/sparcv9-packed-union-abi.cpp
@@ -0,0 +1,95 @@
+// RUN: %clang_cc1 -triple sparcv9-unknown-unknown -emit-llvm %s -o - \
+// RUN: | FileCheck %s
+// RUN: %clang_cc1 -triple sparcv9-unknown-unknown -emit-llvm %s -o - \
+// RUN: -fclang-abi-compat=23 | FileCheck %s --check-prefix=COMPAT
+
+// GCC treats base classes like fields when deciding whether a struct with a
+// packed field, or a union, is passed in integer registers on SPARC64.
+
+struct floats {
+ float f, g;
+};
+
+struct packed_base {
+ float f;
+} __attribute__((packed));
+
+// Only the packed base is passed in integer registers.
+struct derived_from_packed : packed_base {
+ float g;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { i32, float } @_Z19derived_from_packed19derived_from_packed(i32 inreg %x.coerce0, float inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @_Z19derived_from_packed19derived_from_packed(float inreg %x.coerce0, float inreg %x.coerce1)
+derived_from_packed derived_from_packed(derived_from_packed x) { return x; }
+
+struct base_float {
+ float f;
+};
+
+// A packed class makes its base classes packed.
+struct packed_derived : base_float {
+ float g;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} i64 @_Z14packed_derived14packed_derived(i64 %x.coerce)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @_Z14packed_derived14packed_derived(float inreg %x.coerce0, float inreg %x.coerce1)
+packed_derived packed_derived(packed_derived x) { return x; }
+
+struct base_with_packed_field {
+ float f;
+ float g __attribute__((packed));
+};
+
+struct derived_from_packed_field : base_with_packed_field {
+ double d;
+};
+
+// CHECK-LABEL: define{{.*}} { i64, double } @_Z25derived_from_packed_field25derived_from_packed_field(i64 %x.coerce0, double %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float, double } @_Z25derived_from_packed_field25derived_from_packed_field(float inreg %x.coerce0, float inreg %x.coerce1, double inreg %x.coerce2)
+derived_from_packed_field derived_from_packed_field(derived_from_packed_field x) {
+ return x;
+}
+
+union u {
+ float f;
+ int i;
+};
+
+struct base_with_union {
+ u x;
+};
+
+struct derived_from_union_holder : base_with_union {
+ float g;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { i32, float } @_Z25derived_from_union_holder25derived_from_union_holder(i32 inreg %x.coerce0, float inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, float } @_Z25derived_from_union_holder25derived_from_union_holder(float inreg %x.coerce0, float inreg %x.coerce1)
+derived_from_union_holder derived_from_union_holder(derived_from_union_holder x) {
+ return x;
+}
+
+// A packed class does not pack a field of a non-POD class type, so this class
+// is not packed.
+struct non_pod {
+ non_pod() {}
+ float f;
+};
+
+struct packed_with_non_pod {
+ non_pod n;
+} __attribute__((packed));
+
+// CHECK-LABEL: define{{.*}} inreg { float, i32 } @_Z19packed_with_non_pod19packed_with_non_pod(float inreg %x.coerce0, i32 inreg %x.coerce1)
+// COMPAT-LABEL: define{{.*}} inreg { float, i32 } @_Z19packed_with_non_pod19packed_with_non_pod(float inreg %x.coerce0, i32 inreg %x.coerce1)
+packed_with_non_pod packed_with_non_pod(packed_with_non_pod x) { return x; }
+
+// Plain base classes are not affected.
+struct derived_from_floats : floats {
+ float h;
+};
+
+// CHECK-LABEL: define{{.*}} inreg { float, float, float, i32 } @_Z19derived_from_floats19derived_from_floats(float inreg %x.coerce0, float inreg %x.coerce1, float inreg %x.coerce2, i32 inreg %x.coerce3)
+// COMPAT-LABEL: define{{.*}} inreg { float, float, float, i32 } @_Z19derived_from_floats19derived_from_floats(float inreg %x.coerce0, float inreg %x.coerce1, float inreg %x.coerce2, i32 inreg %x.coerce3)
+derived_from_floats derived_from_floats(derived_from_floats x) { return x; }
More information about the cfe-commits
mailing list