[llvm] [PowerPC] Alignment of toc-data symbol should not be increased during optimizations (PR #94593)

Kai Luo via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 17 17:49:49 PDT 2024


https://github.com/bzEq updated https://github.com/llvm/llvm-project/pull/94593

>From a499354b0dd9cc987fd9d0a9c6cd270e1a32bb66 Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Thu, 6 Jun 2024 09:07:13 +0000
Subject: [PATCH 1/8] Fix

---
 llvm/lib/IR/Globals.cpp                             | 8 ++++++++
 llvm/test/CodeGen/PowerPC/tocdata-firm-alignment.ll | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 6f071847bb58a..2943a2aa0cf63 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -335,6 +335,14 @@ bool GlobalObject::canIncreaseAlignment() const {
   if (isELF && !isDSOLocal())
     return false;
 
+  bool isXCOFF =
+      (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatXCOFF());
+  if (isXCOFF)
+    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
+      // GV with toc-data attribute is put in the region same as toc entry.
+      // Its alignment should be the same as toc entry.
+      return !GV->hasAttribute("toc-data");
+
   return true;
 }
 
diff --git a/llvm/test/CodeGen/PowerPC/tocdata-firm-alignment.ll b/llvm/test/CodeGen/PowerPC/tocdata-firm-alignment.ll
index c982713d4f8d3..4ecec36bc977a 100644
--- a/llvm/test/CodeGen/PowerPC/tocdata-firm-alignment.ll
+++ b/llvm/test/CodeGen/PowerPC/tocdata-firm-alignment.ll
@@ -5,7 +5,7 @@ target triple = "powerpc-ibm-aix7.2.0.0"
 
 %struct.widget = type { i8, i8, i8 }
 
-; CHECK: @global = {{.*}}constant %struct.widget { i8 4, i8 0, i8 0 }, align 8 #0
+; CHECK: @global = {{.*}}constant %struct.widget { i8 4, i8 0, i8 0 }, align 4 #0
 @global = constant %struct.widget { i8 4, i8 0, i8 0 }, align 4 #0
 
 define void @baz() #1 {

>From 315e42bc7cfea8380a362dbb19b992d9ebd08fcd Mon Sep 17 00:00:00 2001
From: Kai Luo <gluokai at gmail.com>
Date: Thu, 6 Jun 2024 22:18:00 +0800
Subject: [PATCH 2/8] Adjust comment

Co-authored-by: Sean Fertile <sd.fertile at gmail.com>
---
 llvm/lib/IR/Globals.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 2943a2aa0cf63..1d4116a9c4f9b 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -339,7 +339,8 @@ bool GlobalObject::canIncreaseAlignment() const {
       (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatXCOFF());
   if (isXCOFF)
     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
-      // GV with toc-data attribute is put in the region same as toc entry.
+      // GV with toc-data attribute is defined in a TOC entry which 
+      // has a fixed alignment and cannot be arbitrarily increased.
       // Its alignment should be the same as toc entry.
       return !GV->hasAttribute("toc-data");
 

>From f51b8d44b0b67dc4d6afe14ad75245d788bdcfa1 Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Thu, 6 Jun 2024 22:21:22 +0800
Subject: [PATCH 3/8] Adjust comment

---
 llvm/lib/IR/Globals.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 1d4116a9c4f9b..9a91a324df732 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -341,7 +341,6 @@ bool GlobalObject::canIncreaseAlignment() const {
     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
       // GV with toc-data attribute is defined in a TOC entry which 
       // has a fixed alignment and cannot be arbitrarily increased.
-      // Its alignment should be the same as toc entry.
       return !GV->hasAttribute("toc-data");
 
   return true;

>From 30aea5a91d2721a0832ed3f7fc0c28a4ebd4c804 Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Thu, 6 Jun 2024 22:24:55 +0800
Subject: [PATCH 4/8] Format

---
 llvm/lib/IR/Globals.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 9a91a324df732..6a0536827f3ac 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -339,8 +339,8 @@ bool GlobalObject::canIncreaseAlignment() const {
       (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatXCOFF());
   if (isXCOFF)
     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
-      // GV with toc-data attribute is defined in a TOC entry which 
-      // has a fixed alignment and cannot be arbitrarily increased.
+      // GV with toc-data attribute is defined in a TOC entry which has a fixed
+      // alignment and cannot be arbitrarily increased.
       return !GV->hasAttribute("toc-data");
 
   return true;

>From a935dc9a1c1cd2e2ef682f2fa11c49bc585cf00b Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Sat, 8 Jun 2024 09:52:44 +0800
Subject: [PATCH 5/8] Adjust comment

---
 llvm/lib/IR/Globals.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 6a0536827f3ac..4d212e658d745 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -335,12 +335,13 @@ bool GlobalObject::canIncreaseAlignment() const {
   if (isELF && !isDSOLocal())
     return false;
 
+  // GV with toc-data attribute is defined in a TOC entry. To mitigate TOC
+  // overflow, the alignment of such symbol should not be increased. Otherwise,
+  // padding is needed thus more TOC entries are wasted.
   bool isXCOFF =
       (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatXCOFF());
   if (isXCOFF)
     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
-      // GV with toc-data attribute is defined in a TOC entry which has a fixed
-      // alignment and cannot be arbitrarily increased.
       return !GV->hasAttribute("toc-data");
 
   return true;

>From 54621e10ddade2b30cc045f1ecb93c9451cf747c Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Sat, 8 Jun 2024 10:16:56 +0800
Subject: [PATCH 6/8] Add doc here

---
 llvm/docs/LangRef.rst | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 9d7ade8eb523b..dde5ffc356b2a 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -812,6 +812,10 @@ doesn't). These kinds of structs (we may call them homogeneous scalable vector
 structs) are considered sized and can be used in loads, stores, allocas, but
 not GEPs.
 
+Globals with ``toc-data`` attribute set are stored in TOC of XCOFF. Their
+alignments are not larger than that of a TOC entry. Optimizations should not
+increase their alignments to prevent TOC overflow.
+
 Syntax::
 
       @<GlobalVarName> = [Linkage] [PreemptionSpecifier] [Visibility]

>From 6ac55a80ab5a5541d823715b5d80767f51862b9e Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Sat, 8 Jun 2024 10:18:50 +0800
Subject: [PATCH 7/8] Adjust wording

---
 llvm/docs/LangRef.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index dde5ffc356b2a..8d2ce4d16aabf 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -814,7 +814,7 @@ not GEPs.
 
 Globals with ``toc-data`` attribute set are stored in TOC of XCOFF. Their
 alignments are not larger than that of a TOC entry. Optimizations should not
-increase their alignments to prevent TOC overflow.
+increase their alignments to mitigate TOC overflow.
 
 Syntax::
 

>From 3dfc0edb4ed3da06980a602411132c036858710a Mon Sep 17 00:00:00 2001
From: Kai Luo <gluokai at gmail.com>
Date: Tue, 18 Jun 2024 08:49:39 +0800
Subject: [PATCH 8/8] Update llvm/lib/IR/Globals.cpp

Co-authored-by: Eli Friedman <efriedma at quicinc.com>
---
 llvm/lib/IR/Globals.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 4d212e658d745..3cc29ea8a340d 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -342,7 +342,8 @@ bool GlobalObject::canIncreaseAlignment() const {
       (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatXCOFF());
   if (isXCOFF)
     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
-      return !GV->hasAttribute("toc-data");
+      if (GV->hasAttribute("toc-data"))
+        return false;
 
   return true;
 }



More information about the llvm-commits mailing list