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

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 23 06:06:16 PDT 2024


Author: KaiWeng
Date: 2024-08-23T14:06:12+01:00
New Revision: 4d348f72d3ac4289821f944a99cdb4b6af21aa7b

URL: https://github.com/llvm/llvm-project/commit/4d348f72d3ac4289821f944a99cdb4b6af21aa7b
DIFF: https://github.com/llvm/llvm-project/commit/4d348f72d3ac4289821f944a99cdb4b6af21aa7b.diff

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

Add an unique suffix to .sbss/.sdata if -fdata-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.

Added: 
    llvm/test/CodeGen/RISCV/sdata-sections.ll

Modified: 
    llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index 065541ba9f5933..b7d112ecfc72a7 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -105,10 +105,34 @@ 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 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) {
+        SmallString<128> Name(".sbss.");
+        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) {
+        SmallString<128> Name(".sdata.");
+        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
new file mode 100644
index 00000000000000..0357422aaf5249
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -0,0 +1,38 @@
+; RUN: llc -mtriple=riscv32 -data-sections < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 -data-sections < %s | FileCheck -check-prefix=RV64 %s
+
+; Append an unique name to each sdata/sbss section when -data-section.
+
+ at v = dso_local global i32 0, align 4
+ at r = dso_local global i64 7, align 8
+
+; If a symbol has an explicit 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
+ 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.
+!llvm.module.flags = !{!0}
+!0 = !{i32 8, !"SmallDataLimit", i32 8}
+
+; RV32:    .section        .sbss.v,"aw"
+; 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