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] Place mergeable small read only data into srodata section #82214

Merged
merged 2 commits into from
Mar 11, 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
25 changes: 23 additions & 2 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
ELF::SHF_WRITE | ELF::SHF_ALLOC);
SmallRODataSection =
getContext().getELFSection(".srodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
SmallROData4Section = getContext().getELFSection(
".srodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
SmallROData8Section = getContext().getELFSection(
".srodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
SmallROData16Section = getContext().getELFSection(
".srodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
SmallROData32Section = getContext().getELFSection(
".srodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
}

const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
Expand Down Expand Up @@ -126,8 +136,19 @@ bool RISCVELFTargetObjectFile::isConstantInSmallSection(
MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
if (isConstantInSmallSection(DL, C))
return SmallDataSection;
if (isConstantInSmallSection(DL, C)) {
if (Kind.isMergeableConst4())
return SmallROData4Section;
if (Kind.isMergeableConst8())
return SmallROData8Section;
if (Kind.isMergeableConst16())
return SmallROData16Section;
if (Kind.isMergeableConst32())
return SmallROData32Section;
// LLVM only generate up to .rodata.cst32, and use .rodata section if more
// than 32 bytes, so just use .srodata here.
return SmallRODataSection;
}

// Otherwise, we work the same as ELF.
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace llvm {
/// This implementation is used for RISC-V ELF targets.
class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
MCSection *SmallDataSection;
MCSection *SmallRODataSection;
MCSection *SmallROData4Section;
MCSection *SmallROData8Section;
MCSection *SmallROData16Section;
MCSection *SmallROData32Section;
MCSection *SmallBSSSection;
unsigned SSThreshold = 8;

Expand Down
47 changes: 47 additions & 0 deletions llvm/test/CodeGen/RISCV/srodata.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
; RUN: sed 's/SMALL_DATA_LIMIT/0/g' %s | \
; RUN: llc -mtriple=riscv32 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-0 %s
; RUN: sed 's/SMALL_DATA_LIMIT/0/g' %s | \
; RUN: llc -mtriple=riscv64 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-0 %s
; RUN: sed 's/SMALL_DATA_LIMIT/4/g' %s | \
; RUN: llc -mtriple=riscv32 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-4 %s
; RUN: sed 's/SMALL_DATA_LIMIT/4/g' %s | \
; RUN: llc -mtriple=riscv64 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-4 %s
; RUN: sed 's/SMALL_DATA_LIMIT/8/g' %s | \
; RUN: llc -mtriple=riscv32 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-8 %s
; RUN: sed 's/SMALL_DATA_LIMIT/8/g' %s | \
; RUN: llc -mtriple=riscv64 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-8 %s
; RUN: sed 's/SMALL_DATA_LIMIT/16/g' %s | \
; RUN: llc -mtriple=riscv32 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-16 %s
; RUN: sed 's/SMALL_DATA_LIMIT/16/g' %s | \
; RUN: llc -mtriple=riscv64 -mattr=+d | \
; RUN: FileCheck -check-prefix=CHECK-SDL-16 %s

define dso_local float @foof() {
entry:
ret float 0x400A08ACA0000000
}

define dso_local double @foo() {
entry:
ret double 0x400A08AC91C3E242
}

!llvm.module.flags = !{!0}

!0 = !{i32 8, !"SmallDataLimit", i32 SMALL_DATA_LIMIT}

; CHECK-SDL-0-NOT: .section .srodata.cst4
; CHECK-SDL-0-NOT: .section .srodata.cst8
; CHECK-SDL-4: .section .srodata.cst4
; CHECK-SDL-4-NOT: .section .srodata.cst8
; CHECK-SDL-8: .section .srodata.cst4
; CHECK-SDL-8: .section .srodata.cst8
; CHECK-SDL-16: .section .srodata.cst4
; CHECK-SDL-16: .section .srodata.cst8
Loading