[llvm] [RISCV] Let -data-sections also work on sbss/sdata sections (PR #87040)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 00:19:58 PDT 2024


https://github.com/KaiYG updated https://github.com/llvm/llvm-project/pull/87040

>From 26180ed93536588f4ce65c648ff1e9fce962f654 Mon Sep 17 00:00:00 2001
From: Kai Kai-Yi Weng <kaiweng at andestech.com>
Date: Wed, 20 Mar 2024 09:25:32 +0800
Subject: [PATCH 1/5] [RISCV] Pre-commit a test showing apply -data-sections on
 sbss/sdata sections

Copied from sdata-limit-8.ll and update the CHECKs, otherwise it matches
any .sbss/.sdata regardless having a suffix or not.
---
 llvm/test/CodeGen/RISCV/sdata-sections.ll | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 llvm/test/CodeGen/RISCV/sdata-sections.ll

diff --git a/llvm/test/CodeGen/RISCV/sdata-sections.ll b/llvm/test/CodeGen/RISCV/sdata-sections.ll
new file mode 100644
index 00000000000000..ec9db289301940
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -0,0 +1,18 @@
+; RUN: llc -mtriple=riscv32 -data-sections < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 -data-sections < %s | FileCheck -check-prefix=RV64 %s
+
+; FIXME: Should append an unique name to each sdata/sbss section if -data-sections,
+; otherwise gc-section cannot remove them. This also matches the behavior on gcc.
+
+ at v = dso_local global i32 0, align 4
+ at r = dso_local global i64 7, align 8
+
+; SmallDataLimit set to 8, so we expect @v will be put in sbss
+; and @r will be put in sdata.
+!llvm.module.flags = !{!0}
+!0 = !{i32 8, !"SmallDataLimit", i32 8}
+
+; RV32:    .section        .sbss,"aw"
+; RV32:    .section        .sdata,"aw"
+; RV64:    .section        .sbss,"aw"
+; RV64:    .section        .sdata,"aw"

>From eccdabb499f4340647d7432ecccb8a41826fdcda Mon Sep 17 00:00:00 2001
From: Kai Kai-Yi Weng <kaiweng at andestech.com>
Date: Wed, 20 Mar 2024 14:10:33 +0800
Subject: [PATCH 2/5] [RISCV] Let -data-sections also work on sbss/sdata
 sections

Without assigning an unique .sbss/.sdata section to each symbols, a linker
may not be able to remove unused part when gc-section since all used and
unused symbols are all mixed in the same .sbss/.sdata section.
I believe this also matches the behavior of gcc.
---
 .../Target/RISCV/RISCVTargetObjectFile.cpp    | 34 ++++++++++++++++---
 llvm/test/CodeGen/RISCV/sdata-sections.ll     | 11 +++---
 2 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index e7c1a7e5d8bca1..b13df905249ab5 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -104,10 +104,36 @@ bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
 MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   // Handle Small Section classification here.
-  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
-    return SmallBSSSection;
-  if (Kind.isData() && isGlobalInSmallSection(GO, TM))
-    return SmallDataSection;
+  if (isGlobalInSmallSection(GO, TM)) {
+    // Emit a unique section for sdata/sbss when -fdata-section.
+    bool EmitUniquedSection = TM.getDataSections();
+
+    if (Kind.isBSS()) {
+      if (EmitUniquedSection) {
+        StringRef Prefix(".sbss");
+        SmallString<128> Name(Prefix);
+        Name.append(".");
+        Name.append(GO->getName());
+        return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
+                                          ELF::SHF_WRITE | ELF::SHF_ALLOC);
+      }
+
+      return SmallBSSSection;
+    }
+
+    if (Kind.isData()) {
+      if (EmitUniquedSection) {
+        StringRef Prefix(".sdata");
+        SmallString<128> Name(Prefix);
+        Name.append(".");
+        Name.append(GO->getName());
+        return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
+                                          ELF::SHF_WRITE | ELF::SHF_ALLOC);
+      }
+
+      return SmallDataSection;
+    }
+  }
 
   // Otherwise, we work the same as ELF.
   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
diff --git a/llvm/test/CodeGen/RISCV/sdata-sections.ll b/llvm/test/CodeGen/RISCV/sdata-sections.ll
index ec9db289301940..175e6d744843a8 100644
--- a/llvm/test/CodeGen/RISCV/sdata-sections.ll
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -1,8 +1,7 @@
 ; RUN: llc -mtriple=riscv32 -data-sections < %s | FileCheck -check-prefix=RV32 %s
 ; RUN: llc -mtriple=riscv64 -data-sections < %s | FileCheck -check-prefix=RV64 %s
 
-; FIXME: Should append an unique name to each sdata/sbss section if -data-sections,
-; otherwise gc-section cannot remove them. This also matches the behavior on gcc.
+; Append an unique name to each sdata/sbss section when -data-section.
 
 @v = dso_local global i32 0, align 4
 @r = dso_local global i64 7, align 8
@@ -12,7 +11,7 @@
 !llvm.module.flags = !{!0}
 !0 = !{i32 8, !"SmallDataLimit", i32 8}
 
-; RV32:    .section        .sbss,"aw"
-; RV32:    .section        .sdata,"aw"
-; RV64:    .section        .sbss,"aw"
-; RV64:    .section        .sdata,"aw"
+; RV32:    .section        .sbss.v,"aw"
+; RV32:    .section        .sdata.r,"aw"
+; RV64:    .section        .sbss.v,"aw"
+; RV64:    .section        .sdata.r,"aw"

>From 816d447b6bdfbe18ec1aab30caaf31b21bfbe9e4 Mon Sep 17 00:00:00 2001
From: Kai Kai-Yi Weng <kaiweng at andestech.com>
Date: Mon, 19 Aug 2024 16:52:05 +0800
Subject: [PATCH 3/5] Polish the usage of StringRef

---
 llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index b13df905249ab5..a6ba1001a699c2 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -110,9 +110,7 @@ MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
 
     if (Kind.isBSS()) {
       if (EmitUniquedSection) {
-        StringRef Prefix(".sbss");
-        SmallString<128> Name(Prefix);
-        Name.append(".");
+        SmallString<128> Name(".sbss.");
         Name.append(GO->getName());
         return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
                                           ELF::SHF_WRITE | ELF::SHF_ALLOC);
@@ -123,9 +121,7 @@ MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
 
     if (Kind.isData()) {
       if (EmitUniquedSection) {
-        StringRef Prefix(".sdata");
-        SmallString<128> Name(Prefix);
-        Name.append(".");
+        SmallString<128> Name(".sdata.");
         Name.append(GO->getName());
         return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
                                           ELF::SHF_WRITE | ELF::SHF_ALLOC);

>From 763aa11f24548556fd11c8b51267ae54671d636f Mon Sep 17 00:00:00 2001
From: Kai Kai-Yi Weng <kaiweng at andestech.com>
Date: Tue, 20 Aug 2024 15:26:28 +0800
Subject: [PATCH 4/5] Honor explicit sdata/sbss sections

---
 llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp | 6 ++++--
 llvm/test/CodeGen/RISCV/sdata-sections.ll       | 8 ++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index a6ba1001a699c2..14c5d1aa334a62 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -105,8 +105,10 @@ MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   // Handle Small Section classification here.
   if (isGlobalInSmallSection(GO, TM)) {
-    // Emit a unique section for sdata/sbss when -fdata-section.
-    bool EmitUniquedSection = TM.getDataSections();
+    // Emit to an unique sdata/sbss section when -fdata-section is set.
+    // However, if a symbol has an explicit sdata/sbss section, place it in that
+    // section.
+    bool EmitUniquedSection = TM.getDataSections() && !GO->hasSection();
 
     if (Kind.isBSS()) {
       if (EmitUniquedSection) {
diff --git a/llvm/test/CodeGen/RISCV/sdata-sections.ll b/llvm/test/CodeGen/RISCV/sdata-sections.ll
index 175e6d744843a8..3e8c6e31ac6e85 100644
--- a/llvm/test/CodeGen/RISCV/sdata-sections.ll
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -6,6 +6,10 @@
 @v = dso_local global i32 0, align 4
 @r = dso_local global i64 7, align 8
 
+; If a symbol has an explicit sdata/sbss section name, we should honor it.
+ at vv = dso_local global i32 0, section ".sbss", align 4
+ at rr = dso_local global i64 7, section ".sdata", align 8
+
 ; SmallDataLimit set to 8, so we expect @v will be put in sbss
 ; and @r will be put in sdata.
 !llvm.module.flags = !{!0}
@@ -13,5 +17,9 @@
 
 ; RV32:    .section        .sbss.v,"aw"
 ; RV32:    .section        .sdata.r,"aw"
+; RV32:    .section        .sbss,"aw"
+; RV32:    .section        .sdata,"aw"
 ; RV64:    .section        .sbss.v,"aw"
 ; RV64:    .section        .sdata.r,"aw"
+; RV64:    .section        .sbss,"aw"
+; RV64:    .section        .sdata,"aw"

>From 67945a45cb5bfeaaa0a3675c2b36ceee3475c29d Mon Sep 17 00:00:00 2001
From: Kai Kai-Yi Weng <kaiweng at andestech.com>
Date: Thu, 22 Aug 2024 14:59:51 +0800
Subject: [PATCH 5/5] Add tests with non-sdata/sbss custom section names

---
 llvm/test/CodeGen/RISCV/sdata-sections.ll | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/RISCV/sdata-sections.ll b/llvm/test/CodeGen/RISCV/sdata-sections.ll
index 3e8c6e31ac6e85..0357422aaf5249 100644
--- a/llvm/test/CodeGen/RISCV/sdata-sections.ll
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -6,9 +6,13 @@
 @v = dso_local global i32 0, align 4
 @r = dso_local global i64 7, align 8
 
-; If a symbol has an explicit sdata/sbss section name, we should honor it.
+; If a symbol has an explicit section name, we should honor it.
 @vv = dso_local global i32 0, section ".sbss", align 4
 @rr = dso_local global i64 7, section ".sdata", align 8
+ at bb = dso_local global i32 0, section ".sbss_like", align 4
+ at tt = dso_local global i64 7, section ".sdata_like", align 8
+ at nn = dso_local global i32 0, section ".custom_a", align 4
+ at yy = dso_local global i64 7, section ".custom_b", align 8
 
 ; SmallDataLimit set to 8, so we expect @v will be put in sbss
 ; and @r will be put in sdata.
@@ -19,7 +23,16 @@
 ; RV32:    .section        .sdata.r,"aw"
 ; RV32:    .section        .sbss,"aw"
 ; RV32:    .section        .sdata,"aw"
+; RV32:    .section        .sbss_like,"aw"
+; RV32:    .section        .sdata_like,"aw"
+; RV32:    .section        .custom_a,"aw"
+; RV32:    .section        .custom_b,"aw"
+
 ; RV64:    .section        .sbss.v,"aw"
 ; RV64:    .section        .sdata.r,"aw"
 ; RV64:    .section        .sbss,"aw"
 ; RV64:    .section        .sdata,"aw"
+; RV64:    .section        .sbss_like,"aw"
+; RV64:    .section        .sdata_like,"aw"
+; RV64:    .section        .custom_a,"aw"
+; RV64:    .section        .custom_b,"aw"



More information about the llvm-commits mailing list