[clang] [Clang][ABI][X86] Classify fields that occupy no storage as NO_CLASS (PR #245886)

Phoebe Wang via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 22:07:34 PST 2026


https://github.com/phoebewang created https://github.com/llvm/llvm-project/pull/245886

The x86-64 psABI is being updated so that a field that occupies no storage -- a zero-width bit-field, a flexible array member or an array of length zero -- is NO_CLASS regardless of its offset within the eightbyte; any padding it introduces through alignment is NO_CLASS too.

Clang already classified zero-length arrays as NO_CLASS in the common case, but always passed and returned a struct or union with a flexible array member in memory, and still subjected zero-length arrays to the alignment and single-wide-vector checks. Skip both kinds of field when classifying a record, so that e.g.

  struct S5 { float x; int a[]; };

is now passed in an SSE register, like GCC does for C.

Fixes #243361.

Assisted-by: Claude Opus 5.5

>From 55e31a94b089d07fdfda13e46da4f34b83441891 Mon Sep 17 00:00:00 2001
From: Phoebe Wang <phoebe.wang at intel.com>
Date: Tue, 24 Nov 2026 19:07:43 -0800
Subject: [PATCH] [Clang][ABI][X86] Classify fields that occupy no storage as
 NO_CLASS

The x86-64 psABI is being updated so that a field that occupies no
storage -- a zero-width bit-field, a flexible array member or an array
of length zero -- is NO_CLASS regardless of its offset within the
eightbyte; any padding it introduces through alignment is NO_CLASS too.

Clang already classified zero-length arrays as NO_CLASS in the common
case, but always passed and returned a struct or union with a flexible
array member in memory, and still subjected zero-length arrays to the
alignment and single-wide-vector checks. Skip both kinds of field when
classifying a record, so that e.g.

  struct S5 { float x; int a[]; };

is now passed in an SSE register, like GCC does for C.

Fixes #243361.

Assisted-by: Claude Opus 5.5
---
 clang/docs/ReleaseNotes.md                    | 10 ++
 clang/include/clang/Basic/ABIVersions.def     |  5 +
 clang/lib/CodeGen/Targets/X86.cpp             | 29 +++++-
 .../X86/x86_64-no-storage-field-abi-compat.c  | 99 +++++++++++++++++++
 4 files changed, 142 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGen/X86/x86_64-no-storage-field-abi-compat.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 060c9a2c917498..e9ca7968aae166 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -142,6 +142,16 @@ features cannot lower the translation-unit ABI level;
   This also fixes a crash when such a struct was passed or returned.
   `-fclang-abi-compat=23` restores the previous behavior. (#GH202205)
 
+- Except on PlayStation, on x86-64 System V a field that occupies no storage --
+  a flexible array member or an array of length zero -- is now classified as
+  NO_CLASS regardless of its offset, like a zero-width bit-field, following the
+  updated psABI. A struct or union with a flexible array member is no longer
+  always passed and returned in memory; e.g. `struct { float x; int a[]; }` now
+  travels in an SSE register. A zero-length array no longer makes a packed
+  aggregate unaligned or forces an aggregate wrapping a single 256-bit or
+  512-bit vector into memory. `-fclang-abi-compat=23` restores the previous
+  behavior. (#GH243361)
+
 - On 32-bit x86, Clang now matches GCC when classifying arguments for the
   `regparm` calling convention. Floating-point types other than `float` and
   `double` no longer consume general-purpose register slots, complex values
diff --git a/clang/include/clang/Basic/ABIVersions.def b/clang/include/clang/Basic/ABIVersions.def
index 399be760fc7a3a..9fa16da8741dd7 100644
--- a/clang/include/clang/Basic/ABIVersions.def
+++ b/clang/include/clang/Basic/ABIVersions.def
@@ -167,6 +167,11 @@ ABI_VER_MAJOR(22)
 ///     faithfully reproduces Clang 23, including its crash on aggregates such
 ///     as a run of `__int128` bit-fields, where skipping the unnamed field
 ///     leaves part of a wider access unit unclassified.)
+///   - On x86-64 System V, pass and return any aggregate with a flexible array
+///     member in memory, and classify a zero-length array like any other field
+///     (so that it can make a packed aggregate unaligned, or keep an aggregate
+///     wrapping a single 256-bit or 512-bit vector out of a vector register),
+///     instead of treating both as NO_CLASS fields that occupy no storage.
 ///   - On 32-bit x86, use the legacy `regparm` classification for floating-point
 ///     types other than `float` and `double`, complex types, and unions.
 ABI_VER_MAJOR(23)
diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index caf1769ea52bad..4db0ff4616a163 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -2019,6 +2019,16 @@ X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
   return SSE;
 }
 
+/// Return true if \p FD is a flexible array member or an array of length zero,
+/// i.e. an array field that occupies no storage.
+static bool isNoStorageArrayField(const FieldDecl &FD) {
+  QualType FT = FD.getType();
+  if (FT->isIncompleteArrayType())
+    return true;
+  const ASTContext &Context = FD.getASTContext();
+  return Context.getAsConstantArrayType(FT) && Context.getTypeSize(FT) == 0;
+}
+
 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
                              Class &Hi, bool isNamedArg, bool IsRegCall) const {
   // FIXME: This code can be simplified by introducing a simple value class for
@@ -2278,8 +2288,17 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
 
     const RecordDecl *RD = RT->getDecl()->getDefinitionOrSelf();
 
+    // A field that occupies no storage, such as a flexible array member or an
+    // array of length zero, is NO_CLASS regardless of its offset. Clang 23 and
+    // PlayStation instead pass any record with a flexible array member in
+    // memory and classify zero-length arrays like any other field.
+    bool ClassifyNoStorageFieldsAsNoClass =
+        getContext().getLangOpts().getClangABICompat() >
+            LangOptions::ClangABI::Ver23 &&
+        !getContext().getTargetInfo().getTriple().isPS();
+
     // Assume variable sized types are passed in memory.
-    if (RD->hasFlexibleArrayMember())
+    if (!ClassifyNoStorageFieldsAsNoClass && RD->hasFlexibleArrayMember())
       return;
 
     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
@@ -2335,6 +2354,14 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
       bool BitField = i->isBitField();
 
+      // A flexible array member or an array of length zero occupies no
+      // storage, so it is NO_CLASS. Skip it before the size and alignment
+      // checks below; it may still affect the size and alignment of the
+      // record, but the resulting padding is NO_CLASS too.
+      if (ClassifyNoStorageFieldsAsNoClass && !BitField &&
+          isNoStorageArrayField(**i))
+        continue;
+
       // Ignore padding bit-fields. Normally only zero-length bit-fields are
       // padding, but under -fclang-abi-compat=23 every unnamed bit-field is,
       // faithfully reproducing Clang 23 -- including its crash on aggregates
diff --git a/clang/test/CodeGen/X86/x86_64-no-storage-field-abi-compat.c b/clang/test/CodeGen/X86/x86_64-no-storage-field-abi-compat.c
new file mode 100644
index 00000000000000..f091bf3eee2c97
--- /dev/null
+++ b/clang/test/CodeGen/X86/x86_64-no-storage-field-abi-compat.c
@@ -0,0 +1,99 @@
+// RUN: %clang_cc1 -w -triple x86_64-unknown-linux-gnu -target-feature +avx -emit-llvm %s -o - | \
+// RUN:   FileCheck %s -check-prefixes=CHECK,NEW
+// RUN: %clang_cc1 -w -triple x86_64-unknown-linux-gnu -target-feature +avx -emit-llvm -fclang-abi-compat=23 %s -o - | \
+// RUN:   FileCheck %s -check-prefixes=CHECK,COMPAT
+// RUN: %clang_cc1 -w -triple x86_64-scei-ps4 -target-feature +avx -emit-llvm %s -o - | \
+// RUN:   FileCheck %s -check-prefixes=CHECK,COMPAT
+// RUN: %clang_cc1 -w -triple x86_64-sie-ps5 -target-feature +avx -emit-llvm %s -o - | \
+// RUN:   FileCheck %s -check-prefixes=CHECK,COMPAT
+
+// A field that occupies no storage -- a flexible array member or an array of
+// length zero -- is NO_CLASS regardless of its offset, like a zero-width
+// bit-field. Under -fclang-abi-compat=23 (and on PlayStation) a record with a
+// flexible array member is passed in memory, and a zero-length array is
+// classified like any other field.
+
+// A zero-length array at a non-zero offset in an eightbyte was already
+// NO_CLASS.
+struct s4 { float x; int a[0]; };
+
+// CHECK-LABEL: define{{.*}} float @get_s4()
+struct s4 get_s4(void) { return (struct s4){0}; }
+// CHECK-LABEL: define{{.*}} void @put_s4(float %a.coerce)
+void put_s4(struct s4 a) {}
+
+// Flexible array member.
+struct s5 { float x; int a[]; };
+
+// NEW-LABEL:    define{{.*}} float @get_s5()
+// COMPAT-LABEL: define{{.*}} void @get_s5(ptr {{.*}}sret(%struct.s5)
+struct s5 get_s5(void) { return (struct s5){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s5(float %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_s5(ptr noundef byval(%struct.s5)
+void put_s5(struct s5 a) {}
+
+// Flexible array member that raises the alignment, and so the size, of the
+// record. The tail padding is NO_CLASS too.
+struct s6 { char c; double a[]; };
+
+// NEW-LABEL:    define{{.*}} i8 @get_s6()
+// COMPAT-LABEL: define{{.*}} void @get_s6(ptr {{.*}}sret(%struct.s6)
+struct s6 get_s6(void) { return (struct s6){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s6(i8 %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_s6(ptr noundef byval(%struct.s6)
+void put_s6(struct s6 a) {}
+
+// Union with a flexible array member (GNU extension).
+union u7 { float f; int a[]; };
+
+// NEW-LABEL:    define{{.*}} float @get_u7()
+// COMPAT-LABEL: define{{.*}} void @get_u7(ptr {{.*}}sret(%union.u7)
+union u7 get_u7(void) { return (union u7){0}; }
+// NEW-LABEL:    define{{.*}} void @put_u7(float %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_u7(ptr noundef byval(%union.u7)
+void put_u7(union u7 a) {}
+
+// Struct whose last member is a struct with a flexible array member (GNU
+// extension).
+struct s8 { double d; struct s5 s; };
+
+// NEW-LABEL:    define{{.*}} { double, float } @get_s8()
+// COMPAT-LABEL: define{{.*}} void @get_s8(ptr {{.*}}sret(%struct.s8)
+struct s8 get_s8(void) { return (struct s8){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s8(double %a.coerce0, float %a.coerce1)
+// COMPAT-LABEL: define{{.*}} void @put_s8(ptr noundef byval(%struct.s8)
+void put_s8(struct s8 a) {}
+
+// A zero-length array at an offset that is not aligned for its element type
+// no longer makes the record unaligned.
+struct __attribute__((packed)) s9 { char c; int a[0]; };
+
+// NEW-LABEL:    define{{.*}} i8 @get_s9()
+// COMPAT-LABEL: define{{.*}} void @get_s9(ptr {{.*}}sret(%struct.s9)
+struct s9 get_s9(void) { return (struct s9){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s9(i8 %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_s9(ptr noundef byval(%struct.s9)
+void put_s9(struct s9 a) {}
+
+// A zero-length array no longer stops a record holding a single 256-bit
+// vector from being passed in a YMM register.
+typedef float v8f __attribute__((vector_size(32)));
+struct s10 { v8f v; float a[0]; };
+
+// NEW-LABEL:    define{{.*}} <8 x float> @get_s10()
+// COMPAT-LABEL: define{{.*}} void @get_s10(ptr {{.*}}sret(%struct.s10)
+struct s10 get_s10(void) { return (struct s10){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s10(<8 x float> %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_s10(ptr noundef byval(%struct.s10)
+void put_s10(struct s10 a) {}
+
+// Same for a flexible array member. (The vector is coerced to a generic 256-bit
+// vector type, which is passed in the same YMM register.)
+struct s11 { v8f v; float a[]; };
+
+// NEW-LABEL:    define{{.*}} <4 x double> @get_s11()
+// COMPAT-LABEL: define{{.*}} void @get_s11(ptr {{.*}}sret(%struct.s11)
+struct s11 get_s11(void) { return (struct s11){0}; }
+// NEW-LABEL:    define{{.*}} void @put_s11(<4 x double> %a.coerce)
+// COMPAT-LABEL: define{{.*}} void @put_s11(ptr noundef byval(%struct.s11)
+void put_s11(struct s11 a) {}



More information about the cfe-commits mailing list