Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV] Let -data-sections also work on sbss/sdata sections #87040

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

KaiYG
Copy link

@KaiYG KaiYG commented Mar 29, 2024

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.

Kai Kai-Yi Weng added 2 commits March 29, 2024 15:05
…sections

Copied from sdata-limit-8.ll and update the CHECKs, otherwise it matches
any .sbss/.sdata regardless having a suffix or not.
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.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 29, 2024

@llvm/pr-subscribers-backend-risc-v

Author: KaiWeng (KaiYG)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/87040.diff

2 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp (+30-4)
  • (added) llvm/test/CodeGen/RISCV/sdata-sections.ll (+17)
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
new file mode 100644
index 00000000000000..175e6d744843a8
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/sdata-sections.ll
@@ -0,0 +1,17 @@
+; 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.
+
+@v = dso_local global i32 0, align 4
+@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.v,"aw"
+; RV32:    .section        .sdata.r,"aw"
+; RV64:    .section        .sbss.v,"aw"
+; RV64:    .section        .sdata.r,"aw"

@KaiYG
Copy link
Author

KaiYG commented Jun 12, 2024

Ping.
I think this patch will save code size on both bfd/lld linker, and a similar patch has been done on the Hexagon backend.

Copy link
Member

@kito-cheng kito-cheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants