Skip to content

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

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

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,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);
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/CodeGen/RISCV/sdata-sections.ll
Original file line number Diff line number Diff line change
@@ -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.

@v = dso_local global i32 0, align 4
@r = dso_local global i64 7, align 8

; 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
@bb = dso_local global i32 0, section ".sbss_like", align 4
@tt = dso_local global i64 7, section ".sdata_like", align 8
@nn = dso_local global i32 0, section ".custom_a", align 4
@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"
Loading