[llvm] [SystemZ][z/OS] Simplify the GOFF section handling (PR #101068)

Kai Nacke via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 07:31:21 PDT 2024


https://github.com/redstar updated https://github.com/llvm/llvm-project/pull/101068

>From 25e3357db7673ff70062b537c98506ff3cbd6f2c Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Mon, 29 Jul 2024 14:57:28 -0400
Subject: [PATCH 1/3] [SystemZ][z/OS] Simplify the section handling

The z/OS part of the backend makes use of subsections. This change simplifies the handling:

- Removes the parent section and subsectionid from the factory method in MCContext
- Changes all calls to the createGOFFSection() function
- The PPA1 is emitted into a subsection of .text
- The only instance of PPA2 is directly emitted into .text
- The tests for PPA1 and PPA2 are updated
---
 llvm/include/llvm/BinaryFormat/GOFF.h         |  1 -
 llvm/include/llvm/MC/MCContext.h              |  3 +-
 llvm/include/llvm/MC/MCObjectFileInfo.h       |  4 --
 llvm/include/llvm/MC/MCSectionGOFF.h          | 17 +++-----
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  |  5 +--
 llvm/lib/MC/MCContext.cpp                     |  8 ++--
 llvm/lib/MC/MCObjectFileInfo.cpp              | 17 +++-----
 llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp |  9 ++--
 llvm/test/CodeGen/SystemZ/zos-ppa1.ll         | 42 +++++++++++++++++++
 llvm/test/CodeGen/SystemZ/zos-ppa2.ll         | 36 ++++++++--------
 llvm/test/MC/GOFF/ppa1.ll                     | 35 ----------------
 11 files changed, 83 insertions(+), 94 deletions(-)
 create mode 100644 llvm/test/CodeGen/SystemZ/zos-ppa1.ll
 delete mode 100644 llvm/test/MC/GOFF/ppa1.ll

diff --git a/llvm/include/llvm/BinaryFormat/GOFF.h b/llvm/include/llvm/BinaryFormat/GOFF.h
index 443bcfc9479a8..f1a30e41b736b 100644
--- a/llvm/include/llvm/BinaryFormat/GOFF.h
+++ b/llvm/include/llvm/BinaryFormat/GOFF.h
@@ -167,7 +167,6 @@ enum ENDEntryPointRequest : uint8_t {
 // \brief Subsections of the primary C_CODE section in the object file.
 enum SubsectionKind : uint8_t {
   SK_PPA1 = 2,
-  SK_PPA2 = 4,
 };
 } // end namespace GOFF
 
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index 57ba40f7ac26f..4f7290d5cd538 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -606,8 +606,7 @@ class MCContext {
                                                    unsigned Flags,
                                                    unsigned EntrySize);
 
-  MCSectionGOFF *getGOFFSection(StringRef Section, SectionKind Kind,
-                                MCSection *Parent, uint32_t Subsection = 0);
+  MCSectionGOFF *getGOFFSection(StringRef Section, SectionKind Kind);
 
   MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
                                 StringRef COMDATSymName, int Selection,
diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h
index dda3e8a020f3a..b331aff2923f7 100644
--- a/llvm/include/llvm/MC/MCObjectFileInfo.h
+++ b/llvm/include/llvm/MC/MCObjectFileInfo.h
@@ -226,8 +226,6 @@ class MCObjectFileInfo {
   MCSection *GLJMPSection = nullptr;
 
   // GOFF specific sections.
-  MCSection *PPA1Section = nullptr;
-  MCSection *PPA2Section = nullptr;
   MCSection *PPA2ListSection = nullptr;
   MCSection *ADASection = nullptr;
   MCSection *IDRLSection = nullptr;
@@ -433,8 +431,6 @@ class MCObjectFileInfo {
   MCSection *getGLJMPSection() const { return GLJMPSection; }
 
   // GOFF specific sections.
-  MCSection *getPPA1Section() const { return PPA1Section; }
-  MCSection *getPPA2Section() const { return PPA2Section; }
   MCSection *getPPA2ListSection() const { return PPA2ListSection; }
   MCSection *getADASection() const { return ADASection; }
   MCSection *getIDRLSection() const { return IDRLSection; }
diff --git a/llvm/include/llvm/MC/MCSectionGOFF.h b/llvm/include/llvm/MC/MCSectionGOFF.h
index 11c0f95364037..2d6acfd94172d 100644
--- a/llvm/include/llvm/MC/MCSectionGOFF.h
+++ b/llvm/include/llvm/MC/MCSectionGOFF.h
@@ -15,7 +15,6 @@
 #ifndef LLVM_MC_MCSECTIONGOFF_H
 #define LLVM_MC_MCSECTIONGOFF_H
 
-#include "llvm/BinaryFormat/GOFF.h"
 #include "llvm/MC/MCSection.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -25,26 +24,22 @@ class MCExpr;
 
 class MCSectionGOFF final : public MCSection {
 private:
-  MCSection *Parent;
-  uint32_t Subsection;
-
   friend class MCContext;
-  MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, uint32_t Sub)
-      : MCSection(SV_GOFF, Name, K.isText(), /*IsVirtual=*/false, nullptr),
-        Parent(P), Subsection(Sub) {}
+  MCSectionGOFF(StringRef Name, SectionKind K)
+      : MCSection(SV_GOFF, Name, K.isText(), /*IsVirtual=*/false, nullptr) {}
 
 public:
   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                             raw_ostream &OS,
-                            uint32_t /*Subsection*/) const override {
+                            uint32_t Subsection) const override {
     OS << "\t.section\t\"" << getName() << "\"\n";
+    if (Subsection) {
+      OS << "\t.subsection\t" << Subsection << '\n';
+    }
   }
 
   bool useCodeAlign() const override { return false; }
 
-  MCSection *getParent() const { return Parent; }
-  uint32_t getSubsection() const { return Subsection; }
-
   static bool classof(const MCSection *S) { return S->getVariant() == SV_GOFF; }
 };
 } // end namespace llvm
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 0d3e4ba5662e0..7c529fbdab366 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2726,15 +2726,14 @@ MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
 MCSection *TargetLoweringObjectFileGOFF::getSectionForLSDA(
     const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
   std::string Name = ".gcc_exception_table." + F.getName().str();
-  return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr, 0);
+  return getContext().getGOFFSection(Name, SectionKind::getData());
 }
 
 MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   auto *Symbol = TM.getSymbol(GO);
   if (Kind.isBSS())
-    return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS(),
-                                       nullptr, 0);
+    return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS());
 
   return getContext().getObjectFileInfo()->getTextSection();
 }
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 228c4fb03a276..b2cebfaa1725f 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -672,9 +672,7 @@ MCContext::getELFUniqueIDForEntsize(StringRef SectionName, unsigned Flags,
                                       : std::nullopt;
 }
 
-MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
-                                         MCSection *Parent,
-                                         uint32_t Subsection) {
+MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind) {
   // Do the lookup. If we don't have a hit, return a new section.
   auto IterBool =
       GOFFUniquingMap.insert(std::make_pair(Section.str(), nullptr));
@@ -683,8 +681,8 @@ MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
     return Iter->second;
 
   StringRef CachedName = Iter->first;
-  MCSectionGOFF *GOFFSection = new (GOFFAllocator.Allocate())
-      MCSectionGOFF(CachedName, Kind, Parent, Subsection);
+  MCSectionGOFF *GOFFSection =
+      new (GOFFAllocator.Allocate()) MCSectionGOFF(CachedName, Kind);
   Iter->second = GOFFSection;
   allocInitialFragment(*GOFFSection);
   return GOFFSection;
diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp
index 6dadd9752646f..d879e51a18ae7 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -546,18 +546,11 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
 }
 
 void MCObjectFileInfo::initGOFFMCObjectFileInfo(const Triple &T) {
-  TextSection = Ctx->getGOFFSection(".text", SectionKind::getText(), nullptr);
-  BSSSection = Ctx->getGOFFSection(".bss", SectionKind::getBSS(), nullptr);
-  PPA1Section = Ctx->getGOFFSection(".ppa1", SectionKind::getMetadata(),
-                                    TextSection, GOFF::SK_PPA1);
-  PPA2Section = Ctx->getGOFFSection(".ppa2", SectionKind::getMetadata(),
-                                    TextSection, GOFF::SK_PPA2);
-
-  PPA2ListSection =
-      Ctx->getGOFFSection(".ppa2list", SectionKind::getData(), nullptr);
-
-  ADASection = Ctx->getGOFFSection(".ada", SectionKind::getData(), nullptr);
-  IDRLSection = Ctx->getGOFFSection("B_IDRL", SectionKind::getData(), nullptr);
+  TextSection = Ctx->getGOFFSection(".text", SectionKind::getText());
+  BSSSection = Ctx->getGOFFSection(".bss", SectionKind::getBSS());
+  PPA2ListSection = Ctx->getGOFFSection(".ppa2list", SectionKind::getData());
+  ADASection = Ctx->getGOFFSection(".ada", SectionKind::getData());
+  IDRLSection = Ctx->getGOFFSection("B_IDRL", SectionKind::getData());
 }
 
 void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 3d025a99b3d83..b5fa3c79512ec 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -19,6 +19,7 @@
 #include "TargetInfo/SystemZTargetInfo.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/ELF.h"
+#include "llvm/BinaryFormat/GOFF.h"
 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
 #include "llvm/IR/Mangler.h"
@@ -1124,7 +1125,8 @@ void SystemZAsmPrinter::emitFunctionBodyEnd() {
     OutStreamer->emitLabel(FnEndSym);
 
     OutStreamer->pushSection();
-    OutStreamer->switchSection(getObjFileLowering().getPPA1Section());
+    OutStreamer->switchSection(getObjFileLowering().getTextSection(),
+                               GOFF::SK_PPA1);
     emitPPA1(FnEndSym);
     OutStreamer->popSection();
 
@@ -1428,8 +1430,6 @@ void SystemZAsmPrinter::emitStartOfAsmFile(Module &M) {
 }
 
 void SystemZAsmPrinter::emitPPA2(Module &M) {
-  OutStreamer->pushSection();
-  OutStreamer->switchSection(getObjFileLowering().getPPA2Section());
   MCContext &OutContext = OutStreamer->getContext();
   // Make CELQSTRT symbol.
   const char *StartSymbolName = "CELQSTRT";
@@ -1532,8 +1532,9 @@ void SystemZAsmPrinter::emitPPA2(Module &M) {
 
   OutStreamer->emitInt16(0x0000); // Service level string length.
 
-  // The binder requires that the offset to the PPA2 be emitted in a different,
+  // The runtime requires that the offset to the PPA2 be emitted in a different,
   // specially-named section.
+  OutStreamer->pushSection();
   OutStreamer->switchSection(getObjFileLowering().getPPA2ListSection());
   // Emit 8 byte alignment.
   // Emit pointer to PPA2 label.
diff --git a/llvm/test/CodeGen/SystemZ/zos-ppa1.ll b/llvm/test/CodeGen/SystemZ/zos-ppa1.ll
new file mode 100644
index 0000000000000..28e9b7b7343f8
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/zos-ppa1.ll
@@ -0,0 +1,42 @@
+; RUN: llc -mtriple s390x-ibm-zos < %s | FileCheck %s
+
+; CHECK:      L#EPM_void_test_0:   * @void_test
+; CHECK-NEXT:                      * XPLINK Routine Layout Entry
+; CHECK-NEXT: .long   12779717     * Eyecatcher 0x00C300C500C500
+; CHECK-NEXT: .short  197
+; CHECK-NEXT: .byte   0
+; CHECK-NEXT: .byte   241          * Mark Type C'1'
+; CHECK-NEXT: .long	L#PPA1_void_test_0-L#EPM_void_test_0 * Offset to PPA1
+; CHECK-NEXT: .long   8            * DSA Size 0x0
+; CHECK-NEXT: * Entry Flags
+; CHECK-NEXT: *   Bit 1: 1 = Leaf function
+; CHECK-NEXT: *   Bit 2: 0 = Does not use alloca
+;
+; CHECK: L#func_end0:
+; CHECK-NEXT: .section        ".text"
+; CHECK-NEXT: .subsection	    2
+; CHECK-NEXT: L#PPA1_void_test_0:                     * PPA1
+; CHECK-NEXT:        .byte   2                               * Version
+; CHECK-NEXT:        .byte   206                             * LE Signature X'CE'
+; CHECK-NEXT:        .short  0                               * Saved GPR Mask
+; CHECK-NEXT:        .long	 L#PPA2-L#PPA1_void_test_0       * Offset to PPA2
+; CHECK-NEXT:        .byte   128                             * PPA1 Flags 1
+; CHECK-NEXT:                                        *   Bit 0: 1 = 64-bit DSA
+; CHECK-NEXT:        .byte   128                             * PPA1 Flags 2
+; CHECK-NEXT:                                        *   Bit 0: 1 = External procedure
+; CHECK-NEXT:                                        *   Bit 3: 0 = STACKPROTECT is not enabled
+; CHECK-NEXT:        .byte   0                               * PPA1 Flags 3
+; CHECK-NEXT:        .byte   129                             * PPA1 Flags 4
+; CHECK-NEXT:                                        *   Bit 7: 1 = Name Length and Name
+; CHECK-NEXT:        .short  0                               * Length/4 of Parms
+; CHECK-NEXT:        .long   L#func_end0-L#EPM_void_test_0   * Length of Code
+; CHECK-NEXT:        .short	 9                               * Length of Name
+; CHECK-NEXT:        .ascii	 "\245\226\211\204m\243\205\242\243" * Name of Function
+; CHECK-NEXT:        .space	 1
+; CHECK-NEXT:        .long   L#EPM_void_test_0-L#PPA1_void_test_0
+; CHECK-NEXT:        .section        ".text"
+; CHECK-NEXT:                                        * -- End function
+define void @void_test() {
+entry:
+  ret void
+}
diff --git a/llvm/test/CodeGen/SystemZ/zos-ppa2.ll b/llvm/test/CodeGen/SystemZ/zos-ppa2.ll
index 07025091fb240..9c8f7fff54956 100644
--- a/llvm/test/CodeGen/SystemZ/zos-ppa2.ll
+++ b/llvm/test/CodeGen/SystemZ/zos-ppa2.ll
@@ -1,22 +1,24 @@
 ; RUN: llc -mtriple s390x-ibm-zos -mcpu=z15 -asm-verbose=true < %s | FileCheck %s
 
-; CHECK:    .section    ".ppa2"
-; CHECK: L#PPA2:
-; CHECK:    .byte   3
-; CHECK:    .byte   231
-; CHECK:    .byte   34
-; CHECK:    .byte   4
-; CHECK:    .long   CELQSTRT-L#PPA2
-; CHECK:    .long   0
-; CHECK:    .long   L#DVS-L#PPA2
-; CHECK:    .long   0
-; CHECK:    .byte   129
-; CHECK:    .byte   0
-; CHECK:    .short  0
-; CHECK: L#DVS:
-; CHECK:    .ascii  "\361\371\367\360\360\361\360\361\360\360\360\360\360\360"
-; CHECK:    .short  0
-; CHECK:    .quad   L#PPA2-CELQSTRT                 * A(PPA2-CELQSTRT)
+; CHECK:         .section    ".text"
+; CHECK-NEXT: L#PPA2:
+; CHECK-NEXT:    .byte   3
+; CHECK-NEXT:    .byte   231
+; CHECK-NEXT:    .byte   34
+; CHECK-NEXT:    .byte   4
+; CHECK-NEXT:    .long   CELQSTRT-L#PPA2
+; CHECK-NEXT:    .long   0
+; CHECK-NEXT:    .long   L#DVS-L#PPA2
+; CHECK-NEXT:    .long   0
+; CHECK-NEXT:    .byte   129
+; CHECK-NEXT:    .byte   0
+; CHECK-NEXT:    .short  0
+; CHECK-NEXT: L#DVS:
+; CHECK-NEXT:    .ascii  "\361\371\367\360\360\361\360\361\360\360\360\360\360\360"
+; CHECK-NEXT:    .ascii	 "\362\360\360\360\360\360"
+; CHECK-NEXT:    .short  0
+; CHECK-NEXT:	   .section	".ppa2list"
+; CHECK-NEXT:    .quad   L#PPA2-CELQSTRT                 * A(PPA2-CELQSTRT)
 ; CHECK: L#PPA1_void_test_0:
 ; CHECK:    .long   L#PPA2-L#PPA1_void_test_0       * Offset to PPA2
 ; CHECK:    .section    "B_IDRL"
diff --git a/llvm/test/MC/GOFF/ppa1.ll b/llvm/test/MC/GOFF/ppa1.ll
deleted file mode 100644
index 13971c7ec8e72..0000000000000
--- a/llvm/test/MC/GOFF/ppa1.ll
+++ /dev/null
@@ -1,35 +0,0 @@
-; RUN: llc -mtriple s390x-ibm-zos < %s | FileCheck %s
-; REQUIRES: systemz-registered-target
-
-; CHECK: L#EPM_void_test_0: * @void_test
-; CHECK: * XPLINK Routine Layout Entry
-; CHECK: .long   12779717 * Eyecatcher 0x00C300C500C500
-; CHECK: .short  197
-; CHECK: .byte   0
-; CHECK: .byte   241 * Mark Type C'1'
-; CHECK: .long   8 * DSA Size 0x0
-; CHECK: * Entry Flags
-; CHECK: *   Bit 1: 1 = Leaf function
-; CHECK: *   Bit 2: 0 = Does not use alloca
-; CHECK: L#func_end0:
-; CHECK: .section        ".ppa1"
-; CHECK: L#PPA1_void_test_0:                     * PPA1
-; CHECK:        .byte   2                               * Version
-; CHECK:        .byte   206                             * LE Signature X'CE'
-; CHECK:        .short  0                               * Saved GPR Mask
-; CHECK:        .byte   128                             * PPA1 Flags 1
-; CHECK:                                        *   Bit 0: 1 = 64-bit DSA
-; CHECK:        .byte   128                             * PPA1 Flags 2
-; CHECK:                                        *   Bit 0: 1 = External procedure
-; CHECK:                                        *   Bit 3: 0 = STACKPROTECT is not enabled
-; CHECK:         .byte   0                               * PPA1 Flags 3
-; CHECK:        .byte   129                             * PPA1 Flags 4
-; CHECK:        .short  0                               * Length/4 of Parms
-; CHECK:        .long   L#func_end0-L#EPM_void_test_0   * Length of Code
-; CHECK:        .long   L#EPM_void_test_0-L#PPA1_void_test_0
-; CHECK:        .section        ".text"
-; CHECK:                                        * -- End function
-define void @void_test() {
-entry:
-  ret void
-}

>From 1922f756d35e1fa1c68308d38c606aa238a9eca9 Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Mon, 29 Jul 2024 16:01:27 -0400
Subject: [PATCH 2/3] Fix clang-format message.

---
 llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 7c529fbdab366..8e1fab944c877 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2733,7 +2733,8 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   auto *Symbol = TM.getSymbol(GO);
   if (Kind.isBSS())
-    return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS());
+    return getContext().getGOFFSection(Symbol->getName(),
+                                       SectionKind::getBSS());
 
   return getContext().getObjectFileInfo()->getTextSection();
 }

>From 38ca1e964491c0177e8b1b19ca38ed1fe4f8f44e Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Tue, 30 Jul 2024 10:31:04 -0400
Subject: [PATCH 3/3] Address review comment

---
 llvm/include/llvm/MC/MCSectionGOFF.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/MC/MCSectionGOFF.h b/llvm/include/llvm/MC/MCSectionGOFF.h
index 2d6acfd94172d..943d597ec1514 100644
--- a/llvm/include/llvm/MC/MCSectionGOFF.h
+++ b/llvm/include/llvm/MC/MCSectionGOFF.h
@@ -33,9 +33,8 @@ class MCSectionGOFF final : public MCSection {
                             raw_ostream &OS,
                             uint32_t Subsection) const override {
     OS << "\t.section\t\"" << getName() << "\"\n";
-    if (Subsection) {
+    if (Subsection)
       OS << "\t.subsection\t" << Subsection << '\n';
-    }
   }
 
   bool useCodeAlign() const override { return false; }



More information about the llvm-commits mailing list