[llvm] [SystemZ][z/OS] Honor alignment of global data (PR #208095)

Kai Nacke via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 7 13:53:30 PDT 2026


https://github.com/redstar created https://github.com/llvm/llvm-project/pull/208095

The alignment of sections is set after the section are created. Thus, the ED structure set at object construction time is updated with the current section alignment. This only applies to ED type section. As result, the alignment of global data is correctly set.

>From f336f82bd282d70011864ceaea5847acffe14768 Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Tue, 7 Jul 2026 16:14:35 -0400
Subject: [PATCH] [SystemZ][z/OS] Honor alignment of global data

The alignment of sections is set after the section are created.
Thus, the ED structure set at object construction time is updated
with the current section alignment. This only applies to ED type
section. As result, the alignment of global data is correctly set.
---
 llvm/include/llvm/MC/MCSectionGOFF.h       | 12 ++++++++--
 llvm/test/CodeGen/SystemZ/zos-align.ll     | 28 ++++++++++++++++++++++
 llvm/test/CodeGen/SystemZ/zos-section-1.ll |  2 +-
 3 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/CodeGen/SystemZ/zos-align.ll

diff --git a/llvm/include/llvm/MC/MCSectionGOFF.h b/llvm/include/llvm/MC/MCSectionGOFF.h
index 288f9038bb65e..3a2c03dfa4b34 100644
--- a/llvm/include/llvm/MC/MCSectionGOFF.h
+++ b/llvm/include/llvm/MC/MCSectionGOFF.h
@@ -35,7 +35,7 @@ class LLVM_ABI MCSectionGOFF final : public MCSection {
   // The attributes of the GOFF symbols.
   union {
     GOFF::SDAttr SDAttributes;
-    GOFF::EDAttr EDAttributes;
+    mutable GOFF::EDAttr EDAttributes;
     GOFF::PRAttr PRAttributes;
   };
 
@@ -67,7 +67,9 @@ class LLVM_ABI MCSectionGOFF final : public MCSection {
                 GOFF::EDAttr EDAttributes, MCSectionGOFF *Parent)
       : MCSection(Name, K.isText(), IsVirtual, nullptr), Parent(Parent),
         EDAttributes(EDAttributes), SymbolType(GOFF::ESD_ST_ElementDefinition),
-        IsBSS(K.isBSS()), RequiresNonZeroLength(0), Emitted(0) {}
+        IsBSS(K.isBSS()), RequiresNonZeroLength(0), Emitted(0) {
+          setAlignment(Align(1 << EDAttributes.Alignment));
+        }
 
   MCSectionGOFF(StringRef Name, SectionKind K, bool IsVirtual,
                 GOFF::PRAttr PRAttributes, MCSectionGOFF *Parent)
@@ -96,6 +98,12 @@ class LLVM_ABI MCSectionGOFF final : public MCSection {
   }
   GOFF::EDAttr getEDAttributes() const {
     assert(isED() && "Not a ED section");
+    // Update alignment.
+    uint8_t Log = Log2(getAlign());
+    if (Log <= GOFF::ESD_ALIGN_4Kpage)
+      EDAttributes.Alignment = static_cast<GOFF::ESDAlignment>(Log);
+    else
+      llvm_unreachable("Unsupported alignment");
     return EDAttributes;
   }
   GOFF::PRAttr getPRAttributes() const {
diff --git a/llvm/test/CodeGen/SystemZ/zos-align.ll b/llvm/test/CodeGen/SystemZ/zos-align.ll
new file mode 100644
index 0000000000000..59827dec16079
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/zos-align.ll
@@ -0,0 +1,28 @@
+; Test alignment of global constants
+;
+; RUN: llc < %s -mtriple=s390x-ibm-zos -mcpu=z10 | FileCheck %s
+
+ at data = global [8 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7], align 4096
+
+; CHECK:      * @data
+; CHECK-NEXT: data CSECT
+; CHECK-NEXT: C_WSA64 CATTR ALIGN(12),FILL(0),DEFLOAD,NOTEXECUTABLE,RMODE(64),PART(dat
+; CHECK-NEXT:                ta)
+; CHECK-NEXT: data XATTR LINKAGE(XPLINK),REFERENCE(DATA),SCOPE(EXPORT)
+; CHECK-NEXT:  DS 0B
+; CHECK-NEXT: * 0x0
+; CHECK-NEXT:  DC XL4'00000000'
+; CHECK-NEXT: * 0x1
+; CHECK-NEXT:  DC XL4'00000001'
+; CHECK-NEXT: * 0x2
+; CHECK-NEXT:  DC XL4'00000002'
+; CHECK-NEXT: * 0x3
+; CHECK-NEXT:  DC XL4'00000003'
+; CHECK-NEXT: * 0x4
+; CHECK-NEXT:  DC XL4'00000004'
+; CHECK-NEXT: * 0x5
+; CHECK-NEXT:  DC XL4'00000005'
+; CHECK-NEXT: * 0x6
+; CHECK-NEXT:  DC XL4'00000006'
+; CHECK-NEXT: * 0x7
+; CHECK-NEXT:  DC XL4'00000007'
diff --git a/llvm/test/CodeGen/SystemZ/zos-section-1.ll b/llvm/test/CodeGen/SystemZ/zos-section-1.ll
index c5020fcb9f24a..f4b9667d5d915 100644
--- a/llvm/test/CodeGen/SystemZ/zos-section-1.ll
+++ b/llvm/test/CodeGen/SystemZ/zos-section-1.ll
@@ -46,7 +46,7 @@ entry:
 ; CHECK-NEXT: 0000b0 00 00 00 00 00 00 00 00 00 00 00 {{..}} 00 00 00 00
 ; CHECK-NEXT: 0000c0 00 00 00 00 00 00 00 00 01 80 00 00 00 00 00 00
 ; CHECK-NEXT: 0000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 08
-; CHECK-NEXT: 0000e0 00 00 03 00 00 00 00 08 c3 6d c3 d6 c4 c5 f6 f4
+; CHECK-NEXT: 0000e0 00 00 04 00 00 00 00 08 c3 6d c3 d6 c4 c5 f6 f4
 
 ; ESD record, type ED.
 ; The name is C_@@QPPA2.



More information about the llvm-commits mailing list