Skip to content

Commit b7f97d3

Browse files
authored
[RISCV] Place mergeable small read only data into srodata section (#82214)
Small mergeable read only data was place on the sdata before, but it also means it lose the mergeable property, which means lose some code size optimization opportunity during link time.
1 parent d9e6aa7 commit b7f97d3

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
3232
".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
3333
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
3434
ELF::SHF_WRITE | ELF::SHF_ALLOC);
35+
SmallRODataSection =
36+
getContext().getELFSection(".srodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
37+
SmallROData4Section = getContext().getELFSection(
38+
".srodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
39+
SmallROData8Section = getContext().getELFSection(
40+
".srodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
41+
SmallROData16Section = getContext().getELFSection(
42+
".srodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
43+
SmallROData32Section = getContext().getELFSection(
44+
".srodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
3545
}
3646

3747
const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
@@ -126,8 +136,19 @@ bool RISCVELFTargetObjectFile::isConstantInSmallSection(
126136
MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
127137
const DataLayout &DL, SectionKind Kind, const Constant *C,
128138
Align &Alignment) const {
129-
if (isConstantInSmallSection(DL, C))
130-
return SmallDataSection;
139+
if (isConstantInSmallSection(DL, C)) {
140+
if (Kind.isMergeableConst4())
141+
return SmallROData4Section;
142+
if (Kind.isMergeableConst8())
143+
return SmallROData8Section;
144+
if (Kind.isMergeableConst16())
145+
return SmallROData16Section;
146+
if (Kind.isMergeableConst32())
147+
return SmallROData32Section;
148+
// LLVM only generate up to .rodata.cst32, and use .rodata section if more
149+
// than 32 bytes, so just use .srodata here.
150+
return SmallRODataSection;
151+
}
131152

132153
// Otherwise, we work the same as ELF.
133154
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,

llvm/lib/Target/RISCV/RISCVTargetObjectFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace llvm {
1616
/// This implementation is used for RISC-V ELF targets.
1717
class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
1818
MCSection *SmallDataSection;
19+
MCSection *SmallRODataSection;
20+
MCSection *SmallROData4Section;
21+
MCSection *SmallROData8Section;
22+
MCSection *SmallROData16Section;
23+
MCSection *SmallROData32Section;
1924
MCSection *SmallBSSSection;
2025
unsigned SSThreshold = 8;
2126

llvm/test/CodeGen/RISCV/srodata.ll

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: sed 's/SMALL_DATA_LIMIT/0/g' %s | \
2+
; RUN: llc -mtriple=riscv32 -mattr=+d | \
3+
; RUN: FileCheck -check-prefix=CHECK-SDL-0 %s
4+
; RUN: sed 's/SMALL_DATA_LIMIT/0/g' %s | \
5+
; RUN: llc -mtriple=riscv64 -mattr=+d | \
6+
; RUN: FileCheck -check-prefix=CHECK-SDL-0 %s
7+
; RUN: sed 's/SMALL_DATA_LIMIT/4/g' %s | \
8+
; RUN: llc -mtriple=riscv32 -mattr=+d | \
9+
; RUN: FileCheck -check-prefix=CHECK-SDL-4 %s
10+
; RUN: sed 's/SMALL_DATA_LIMIT/4/g' %s | \
11+
; RUN: llc -mtriple=riscv64 -mattr=+d | \
12+
; RUN: FileCheck -check-prefix=CHECK-SDL-4 %s
13+
; RUN: sed 's/SMALL_DATA_LIMIT/8/g' %s | \
14+
; RUN: llc -mtriple=riscv32 -mattr=+d | \
15+
; RUN: FileCheck -check-prefix=CHECK-SDL-8 %s
16+
; RUN: sed 's/SMALL_DATA_LIMIT/8/g' %s | \
17+
; RUN: llc -mtriple=riscv64 -mattr=+d | \
18+
; RUN: FileCheck -check-prefix=CHECK-SDL-8 %s
19+
; RUN: sed 's/SMALL_DATA_LIMIT/16/g' %s | \
20+
; RUN: llc -mtriple=riscv32 -mattr=+d | \
21+
; RUN: FileCheck -check-prefix=CHECK-SDL-16 %s
22+
; RUN: sed 's/SMALL_DATA_LIMIT/16/g' %s | \
23+
; RUN: llc -mtriple=riscv64 -mattr=+d | \
24+
; RUN: FileCheck -check-prefix=CHECK-SDL-16 %s
25+
26+
define dso_local float @foof() {
27+
entry:
28+
ret float 0x400A08ACA0000000
29+
}
30+
31+
define dso_local double @foo() {
32+
entry:
33+
ret double 0x400A08AC91C3E242
34+
}
35+
36+
!llvm.module.flags = !{!0}
37+
38+
!0 = !{i32 8, !"SmallDataLimit", i32 SMALL_DATA_LIMIT}
39+
40+
; CHECK-SDL-0-NOT: .section .srodata.cst4
41+
; CHECK-SDL-0-NOT: .section .srodata.cst8
42+
; CHECK-SDL-4: .section .srodata.cst4
43+
; CHECK-SDL-4-NOT: .section .srodata.cst8
44+
; CHECK-SDL-8: .section .srodata.cst4
45+
; CHECK-SDL-8: .section .srodata.cst8
46+
; CHECK-SDL-16: .section .srodata.cst4
47+
; CHECK-SDL-16: .section .srodata.cst8

0 commit comments

Comments
 (0)