[llvm] [MC][x86-64] Decide if each individual mergeable constants is small or large (PR #209881)
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 15:49:12 PDT 2026
https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/209881
>From ea4940106e051138b2cde8ad482442473b7685ba Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Fri, 10 Jul 2026 22:52:05 +0000
Subject: [PATCH 1/3] [MC][x86-64] Decide if each individual mergeable
constants is small or large
Rather than only relying on the code model. Otherwise we can end up with the IR and MC disagreeing about which ones are small/large.
Don't use MergeableConst*Section in TargetLoweringObjectFileELF::getSectionForConstant(), decide which section dynamically. Other binary formats still use those though.
This resolves some relocation overflows from mixed small/medium code model code.
Assisted-by: Gemini
---
.../CodeGen/TargetLoweringObjectFileImpl.h | 2 +
llvm/include/llvm/MC/MCObjectFileInfo.h | 15 ----
llvm/include/llvm/Target/TargetMachine.h | 1 +
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 80 +++++++++++++------
llvm/lib/MC/MCObjectFileInfo.cpp | 15 +---
llvm/lib/Target/TargetMachine.cpp | 16 +++-
.../X86/code-model-elf-constpool-large.ll | 19 ++++-
.../CodeGen/X86/code-model-elf-strings.ll | 58 ++++++++++++++
8 files changed, 152 insertions(+), 54 deletions(-)
create mode 100644 llvm/test/CodeGen/X86/code-model-elf-strings.ll
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 78954a8bb2121..7d4afbf1d6fbd 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -38,6 +38,8 @@ class LLVM_ABI TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
protected:
uint16_t PLTRelativeSpecifier = 0;
+ bool isLargeConstant(const DataLayout &DL, SectionKind Kind,
+ const Constant *C) const;
public:
~TargetLoweringObjectFileELF() override = default;
diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h
index 379c4e9094531..d10411b56517d 100644
--- a/llvm/include/llvm/MC/MCObjectFileInfo.h
+++ b/llvm/include/llvm/MC/MCObjectFileInfo.h
@@ -380,21 +380,6 @@ class LLVM_ABI MCObjectFileInfo {
MCSection *getPCSection(StringRef Name, const MCSection *TextSec) const;
- // ELF specific sections.
- MCSection *getDataRelROSection() const { return DataRelROSection; }
- const MCSection *getMergeableConst4Section() const {
- return MergeableConst4Section;
- }
- const MCSection *getMergeableConst8Section() const {
- return MergeableConst8Section;
- }
- const MCSection *getMergeableConst16Section() const {
- return MergeableConst16Section;
- }
- const MCSection *getMergeableConst32Section() const {
- return MergeableConst32Section;
- }
-
// MachO specific sections.
const MCSection *getTLSTLVSection() const { return TLSTLVSection; }
const MCSection *getTLSThreadInitSection() const {
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index 6fdd603092fc6..b25c0887c7f40 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -276,6 +276,7 @@ class LLVM_ABI TargetMachine {
void setLargeDataThreshold(uint64_t LDT) { LargeDataThreshold = LDT; }
bool isLargeGlobalValue(const GlobalValue *GV) const;
+ bool isLargeDataSize(uint64_t Size) const;
bool isPositionIndependent() const;
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 174e7b088ca4b..4f74418e9e67d 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1051,59 +1051,89 @@ bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
/// Given a mergeable constant with the specified size and relocation
/// information, return a section that it should be placed in.
+bool TargetLoweringObjectFileELF::isLargeConstant(const DataLayout &DL,
+ SectionKind Kind,
+ const Constant *C) const {
+ if (!TM)
+ return false;
+ if (TM->getCodeModel() == CodeModel::Large)
+ return TM->getTargetTriple().getArch() == Triple::x86_64;
+ if (Kind.isMergeableCString() && C) {
+ assert(C->getType()->isSized());
+ return TM->isLargeDataSize(DL.getTypeAllocSize(C->getType()));
+ }
+ return false;
+}
+
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
- if (Kind.isMergeableConst4() && MergeableConst4Section)
- return MergeableConst4Section;
- if (Kind.isMergeableConst8() && MergeableConst8Section)
- return MergeableConst8Section;
- if (Kind.isMergeableConst16() && MergeableConst16Section)
- return MergeableConst16Section;
- if (Kind.isMergeableConst32() && MergeableConst32Section)
- return MergeableConst32Section;
+ auto &Context = getContext();
+ StringRef CstPrefix = ".rodata";
+ unsigned MergeableCstFlags = ELF::SHF_ALLOC;
+ if (Kind.isMergeableConst() || Kind.isMergeableCString())
+ MergeableCstFlags |= ELF::SHF_MERGE;
+ if (isLargeConstant(DL, Kind, C)) {
+ MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
+ CstPrefix = ".lrodata";
+ }
+
+ if (Kind.isMergeableConst4())
+ return Context.getELFSection(CstPrefix + ".cst4", ELF::SHT_PROGBITS,
+ MergeableCstFlags, 4);
+ if (Kind.isMergeableConst8())
+ return Context.getELFSection(CstPrefix + ".cst8", ELF::SHT_PROGBITS,
+ MergeableCstFlags, 8);
+ if (Kind.isMergeableConst16())
+ return Context.getELFSection(CstPrefix + ".cst16", ELF::SHT_PROGBITS,
+ MergeableCstFlags, 16);
+ if (Kind.isMergeableConst32())
+ return Context.getELFSection(CstPrefix + ".cst32", ELF::SHT_PROGBITS,
+ MergeableCstFlags, 32);
if (Kind.isReadOnly())
- return ReadOnlySection;
+ return Context.getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
- return DataRelROSection;
+ return Context.getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
+ ELF::SHF_ALLOC | ELF::SHF_WRITE);
}
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F, StringRef SectionSuffix) const {
- // TODO: Share code between this function and
- // MCObjectInfo::initELFMCObjectFileInfo.
if (SectionSuffix.empty())
return getSectionForConstant(DL, Kind, C, Alignment, F);
auto &Context = getContext();
StringRef CstPrefix = ".rodata";
- unsigned MergeableCstFlags = ELF::SHF_ALLOC | ELF::SHF_MERGE;
- if (TM->getCodeModel() == CodeModel::Large &&
- TM->getTargetTriple().getArch() == Triple::x86_64) {
+ unsigned MergeableCstFlags = ELF::SHF_ALLOC;
+ if (Kind.isMergeableConst() || Kind.isMergeableCString())
+ MergeableCstFlags |= ELF::SHF_MERGE;
+ if (isLargeConstant(DL, Kind, C)) {
MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
CstPrefix = ".lrodata";
}
- if (Kind.isMergeableConst4() && MergeableConst4Section)
- return Context.getELFSection(CstPrefix + ".cst4." + SectionSuffix + ".",
+ std::string SectionSuffixStr = "." + SectionSuffix.str() + ".";
+
+ if (Kind.isMergeableConst4())
+ return Context.getELFSection(CstPrefix + ".cst4" + SectionSuffixStr,
ELF::SHT_PROGBITS, MergeableCstFlags, 4);
- if (Kind.isMergeableConst8() && MergeableConst8Section)
- return Context.getELFSection(CstPrefix + ".cst8." + SectionSuffix + ".",
+ if (Kind.isMergeableConst8())
+ return Context.getELFSection(CstPrefix + ".cst8" + SectionSuffixStr,
ELF::SHT_PROGBITS, MergeableCstFlags, 8);
- if (Kind.isMergeableConst16() && MergeableConst16Section)
- return Context.getELFSection(CstPrefix + ".cst16." + SectionSuffix + ".",
+ if (Kind.isMergeableConst16())
+ return Context.getELFSection(CstPrefix + ".cst16" + SectionSuffixStr,
ELF::SHT_PROGBITS, MergeableCstFlags, 16);
- if (Kind.isMergeableConst32() && MergeableConst32Section)
- return Context.getELFSection(CstPrefix + ".cst32." + SectionSuffix + ".",
+ if (Kind.isMergeableConst32())
+ return Context.getELFSection(CstPrefix + ".cst32" + SectionSuffixStr,
ELF::SHT_PROGBITS, MergeableCstFlags, 32);
if (Kind.isReadOnly())
- return Context.getELFSection(".rodata." + SectionSuffix + ".",
+ return Context.getELFSection(".rodata" + SectionSuffixStr,
ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
- return Context.getELFSection(".data.rel.ro." + SectionSuffix + ".",
+ return Context.getELFSection(".data.rel.ro" + SectionSuffixStr,
ELF::SHT_PROGBITS,
ELF::SHF_ALLOC | ELF::SHF_WRITE);
}
diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp
index 2ac01cf4b84de..ba255ba14fb11 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -433,24 +433,17 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
ELF::SHF_ALLOC | ELF::SHF_WRITE);
- unsigned MergeableCstFlags = ELF::SHF_ALLOC | ELF::SHF_MERGE;
- StringRef CstPrefix = ".rodata";
- if (Large && T.getArch() == Triple::x86_64) {
- MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
- CstPrefix = ".lrodata";
- }
-
MergeableConst4Section = Ctx->getELFSection(
- CstPrefix + ".cst4", ELF::SHT_PROGBITS, MergeableCstFlags, 4);
+ ".rodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
MergeableConst8Section = Ctx->getELFSection(
- CstPrefix + ".cst8", ELF::SHT_PROGBITS, MergeableCstFlags, 8);
+ ".rodata.cst8", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
MergeableConst16Section = Ctx->getELFSection(
- CstPrefix + ".cst16", ELF::SHT_PROGBITS, MergeableCstFlags, 16);
+ ".rodata.cst16", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
MergeableConst32Section = Ctx->getELFSection(
- CstPrefix + ".cst32", ELF::SHT_PROGBITS, MergeableCstFlags, 32);
+ ".rodata.cst32", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
// Exception Handling Sections.
diff --git a/llvm/lib/Target/TargetMachine.cpp b/llvm/lib/Target/TargetMachine.cpp
index 95263f32236b4..d4acc93246292 100644
--- a/llvm/lib/Target/TargetMachine.cpp
+++ b/llvm/lib/Target/TargetMachine.cpp
@@ -54,6 +54,19 @@ TargetMachine::createMCStreamer(raw_pwrite_stream &Out,
return nullptr;
}
+bool TargetMachine::isLargeDataSize(uint64_t Size) const {
+ if (getTargetTriple().getArch() != Triple::x86_64)
+ return false;
+
+ if (!getTargetTriple().isOSBinFormatELF())
+ return getCodeModel() == CodeModel::Large;
+
+ if (getCodeModel() == CodeModel::Medium || getCodeModel() == CodeModel::Large)
+ return Size == 0 || Size > LargeDataThreshold;
+
+ return false;
+}
+
bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
if (getTargetTriple().getArch() != Triple::x86_64)
return false;
@@ -134,8 +147,7 @@ bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
.isReadOnlyWithRel())
return false;
const DataLayout &DL = GV->getDataLayout();
- uint64_t Size = GV->getGlobalSize(DL);
- return Size == 0 || Size > LargeDataThreshold;
+ return isLargeDataSize(GV->getGlobalSize(DL));
}
return false;
diff --git a/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll b/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll
index 0a48a9483d6e2..e2941783b14f9 100644
--- a/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll
+++ b/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll
@@ -1,21 +1,38 @@
; RUN: llc < %s -relocation-model=pic -filetype=obj -code-model=large -o %t
; RUN: llvm-readelf -S %t | FileCheck %s
+; RUN: llc < %s -relocation-model=pic -filetype=obj -code-model=medium -large-data-threshold=0 -o %t2
+; RUN: llvm-readelf -S %t2 | FileCheck %s --check-prefix=MED
+
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=0 | FileCheck %s --check-prefix=ASM-MED
+; RUN: llc < %s -relocation-model=pic -code-model=large | FileCheck %s --check-prefix=ASM-LARGE
; Verify that anonymous constant pool entries get SHF_X86_64_LARGE
-; and are placed in .lrodata.cst* sections under the large code model.
+; and are placed in .lrodata.cst* sections under the large code model,
+; but stay in .rodata.cst* under the medium code model.
; CHECK: .lrodata.cst16 {{.*}} AMl
; CHECK: .lrodata.cst4 {{.*}} AMl
+; MED: .rodata.cst16 {{.*}} AM
+; MED: .rodata.cst4 {{.*}} AM
+
+; ASM-MED: movaps .LCPI0_0(%rip),
+; ASM-LARGE: movabsq $.LCPI0_0 at GOTOFF,
+
; Also verify the suffixed path (via -partition-static-data-sections).
; The .hot suffix requires profile information (see !prof metadata below)
; so that the partitioner can distinguish hot from cold constant pool entries.
; RUN: llc < %s -relocation-model=pic -code-model=large \
; RUN: -partition-static-data-sections -o - | FileCheck %s --check-prefix=SUFFIX
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=0 \
+; RUN: -partition-static-data-sections -o - | FileCheck %s --check-prefix=SUFFIX-MED
; SUFFIX: .section .lrodata.cst16.hot.,"aMl", at progbits,16
; SUFFIX: .section .lrodata.cst4,"aMl", at progbits,4
+; SUFFIX-MED: .section .rodata.cst16.hot.,"aM", at progbits,16
+; SUFFIX-MED: .section .rodata.cst4,"aM", at progbits,4
+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64--linux"
diff --git a/llvm/test/CodeGen/X86/code-model-elf-strings.ll b/llvm/test/CodeGen/X86/code-model-elf-strings.ll
new file mode 100644
index 0000000000000..c82966f4fa822
--- /dev/null
+++ b/llvm/test/CodeGen/X86/code-model-elf-strings.ll
@@ -0,0 +1,58 @@
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=0 | FileCheck %s --check-prefix=LARGE --implicit-check-not=rodata
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=8 | FileCheck %s --check-prefix=MIXED --implicit-check-not=rodata
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=100 | FileCheck %s --check-prefix=SMALL --implicit-check-not=rodata
+
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=0 \
+; RUN: -partition-static-data-sections | FileCheck %s --check-prefix=SUFFIX-LARGE --implicit-check-not=rodata
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=8 \
+; RUN: -partition-static-data-sections | FileCheck %s --check-prefix=SUFFIX-MIXED --implicit-check-not=rodata
+; RUN: llc < %s -relocation-model=pic -code-model=medium -large-data-threshold=100 \
+; RUN: -partition-static-data-sections | FileCheck %s --check-prefix=SUFFIX-SMALL --implicit-check-not=rodata
+
+; LARGE: .section .lrodata.str1.1,"aMSl", at progbits,1
+
+; MIXED: .section .lrodata.str1.1,"aMSl", at progbits,1
+; MIXED: .section .rodata.str1.1,"aMS", at progbits,1
+
+; SMALL: .section .rodata.str1.1,"aMS", at progbits,1
+
+; SUFFIX-LARGE: .section .lrodata.str1.1.hot.,"aMSl", at progbits,1
+
+; SUFFIX-MIXED: .section .lrodata.str1.1.hot.,"aMSl", at progbits,1
+; SUFFIX-MIXED: .section .rodata.str1.1.hot.,"aMS", at progbits,1
+
+; SUFFIX-SMALL: .section .rodata.str1.1.hot.,"aMS", at progbits,1
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64--linux"
+
+ at .str_large = private unnamed_addr constant [17 x i8] c"0123456789abcdef\00", align 1
+ at .str_small = private unnamed_addr constant [4 x i8] c"abc\00", align 1
+
+define ptr @get_str_large() !prof !17 {
+ ret ptr @.str_large
+}
+
+define ptr @get_str_small() !prof !17 {
+ ret ptr @.str_small
+}
+
+!llvm.module.flags = !{!1}
+
+!1 = !{i32 1, !"ProfileSummary", !2}
+!2 = !{!3, !4, !5, !6, !7, !8, !9, !10, !11, !12}
+!3 = !{!"ProfileFormat", !"InstrProf"}
+!4 = !{!"TotalCount", i64 1460617}
+!5 = !{!"MaxCount", i64 849536}
+!6 = !{!"MaxInternalCount", i64 32769}
+!7 = !{!"MaxFunctionCount", i64 849536}
+!8 = !{!"NumCounts", i64 23784}
+!9 = !{!"NumFunctions", i64 3301}
+!10 = !{!"IsPartialProfile", i64 0}
+!11 = !{!"PartialProfileRatio", double 0.000000e+00}
+!12 = !{!"DetailedSummary", !13}
+!13 = !{!14, !15}
+!14 = !{i32 990000, i64 166, i32 73}
+!15 = !{i32 999999, i64 1, i32 1463}
+!16 = !{!"function_entry_count", i64 1}
+!17 = !{!"function_entry_count", i64 100000}
>From d38cb6363123d0bad5a9a534bba91e72c778575e Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Tue, 28 Jul 2026 17:42:49 +0000
Subject: [PATCH 2/3] address comments
---
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index dde0359af4a0f..62768760c925c 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1101,11 +1101,10 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
return Context.getELFSection(CstPrefix + ".cst32", ELF::SHT_PROGBITS,
MergeableCstFlags, 32);
if (Kind.isReadOnly())
- return Context.getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
+ return ReadOnlySection;
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
- return Context.getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC | ELF::SHF_WRITE);
+ return DataRelROSection;
}
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
@@ -1115,16 +1114,16 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
return getSectionForConstant(DL, Kind, C, Alignment, F);
auto &Context = getContext();
- StringRef CstPrefix = ".rodata";
unsigned MergeableCstFlags = ELF::SHF_ALLOC;
if (Kind.isMergeableConst() || Kind.isMergeableCString())
MergeableCstFlags |= ELF::SHF_MERGE;
- if (isLargeConstant(DL, Kind, C)) {
+ bool IsLarge = isLargeConstant(DL, Kind, C);
+ Twine CstPrefix = IsLarge ? ".lrodata":".rodata";
+ if (IsLarge) {
MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
- CstPrefix = ".lrodata";
}
- std::string SectionSuffixStr = "." + SectionSuffix.str() + ".";
+ Twine SectionSuffixStr = Twine(".") + SectionSuffix.str() + ".";
if (Kind.isMergeableConst4())
return Context.getELFSection(CstPrefix + ".cst4" + SectionSuffixStr,
>From 3ba5d9d56af9e645cd35412b7d29b71cbec4b0f3 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Mon, 10 Aug 2026 22:38:43 +0000
Subject: [PATCH 3/3] use lrodata in Kind.isReadOnly()
---
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 47 +++++--------------
llvm/test/CodeGen/X86/morestack-decl.ll | 2 +-
llvm/test/CodeGen/X86/segmented-stacks.ll | 2 +-
3 files changed, 14 insertions(+), 37 deletions(-)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 4a52a6cea9da4..2a64a2daaed8b 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1052,58 +1052,35 @@ bool TargetLoweringObjectFileELF::isLargeConstant(const DataLayout &DL,
assert(C->getType()->isSized());
return TM->isLargeDataSize(DL.getTypeAllocSize(C->getType()));
}
+ // Globals generated by the compiler, e.g. constant pool entries, are always
+ // small under the x86-64 medium code model.
return false;
}
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
- auto &Context = getContext();
- StringRef CstPrefix = ".rodata";
- unsigned MergeableCstFlags = ELF::SHF_ALLOC;
- if (Kind.isMergeableConst() || Kind.isMergeableCString())
- MergeableCstFlags |= ELF::SHF_MERGE;
- if (isLargeConstant(DL, Kind, C)) {
- MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
- CstPrefix = ".lrodata";
- }
-
- if (Kind.isMergeableConst4())
- return Context.getELFSection(CstPrefix + ".cst4", ELF::SHT_PROGBITS,
- MergeableCstFlags, 4);
- if (Kind.isMergeableConst8())
- return Context.getELFSection(CstPrefix + ".cst8", ELF::SHT_PROGBITS,
- MergeableCstFlags, 8);
- if (Kind.isMergeableConst16())
- return Context.getELFSection(CstPrefix + ".cst16", ELF::SHT_PROGBITS,
- MergeableCstFlags, 16);
- if (Kind.isMergeableConst32())
- return Context.getELFSection(CstPrefix + ".cst32", ELF::SHT_PROGBITS,
- MergeableCstFlags, 32);
- if (Kind.isReadOnly())
- return ReadOnlySection;
-
- assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
- return DataRelROSection;
+ return getSectionForConstant(DL, Kind, C, Alignment, F, "");
}
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F, StringRef SectionSuffix) const {
- if (SectionSuffix.empty())
- return getSectionForConstant(DL, Kind, C, Alignment, F);
-
auto &Context = getContext();
unsigned MergeableCstFlags = ELF::SHF_ALLOC;
if (Kind.isMergeableConst() || Kind.isMergeableCString())
MergeableCstFlags |= ELF::SHF_MERGE;
bool IsLarge = isLargeConstant(DL, Kind, C);
- Twine CstPrefix = IsLarge ? ".lrodata":".rodata";
- if (IsLarge) {
+ if (IsLarge)
MergeableCstFlags |= ELF::SHF_X86_64_LARGE;
- }
- Twine SectionSuffixStr = Twine(".") + SectionSuffix.str() + ".";
+ StringRef CstPrefix = IsLarge ? ".lrodata" : ".rodata";
+ SmallString<32> SectionSuffixStr;
+ if (!SectionSuffix.empty()) {
+ SectionSuffixStr.push_back('.');
+ SectionSuffixStr += SectionSuffix;
+ SectionSuffixStr.push_back('.');
+ }
if (Kind.isMergeableConst4())
return Context.getELFSection(CstPrefix + ".cst4" + SectionSuffixStr,
@@ -1118,7 +1095,7 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
return Context.getELFSection(CstPrefix + ".cst32" + SectionSuffixStr,
ELF::SHT_PROGBITS, MergeableCstFlags, 32);
if (Kind.isReadOnly())
- return Context.getELFSection(".rodata" + SectionSuffixStr,
+ return Context.getELFSection(CstPrefix + SectionSuffixStr,
ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
diff --git a/llvm/test/CodeGen/X86/morestack-decl.ll b/llvm/test/CodeGen/X86/morestack-decl.ll
index d3b44137da5ba..8a1d739e5380e 100644
--- a/llvm/test/CodeGen/X86/morestack-decl.ll
+++ b/llvm/test/CodeGen/X86/morestack-decl.ll
@@ -3,7 +3,7 @@
; Check what happens if we have an existing declaration of __morestack_addr
; CHECK: .section ".note.GNU-stack","", at progbits
-; CHECK-NEXT: .section .rodata,"a", at progbits
+; CHECK-NEXT: .section .lrodata,"a", at progbits
; CHECK-NEXT: __morestack_addr:
; CHECK-NEXT: .quad __morestack
diff --git a/llvm/test/CodeGen/X86/segmented-stacks.ll b/llvm/test/CodeGen/X86/segmented-stacks.ll
index f8627ff56a1f9..2561cd7057801 100644
--- a/llvm/test/CodeGen/X86/segmented-stacks.ll
+++ b/llvm/test/CodeGen/X86/segmented-stacks.ll
@@ -2120,7 +2120,7 @@ attributes #0 = { "split-stack" }
; X64-Linux-Large: .section ".note.GNU-split-stack","", at progbits
; X64-Linux-Large-NEXT: .section ".note.GNU-no-split-stack","", at progbits
; X64-Linux-Large-NEXT: .section ".note.GNU-stack","", at progbits
-; X64-Linux-Large-NEXT: .rodata
+; X64-Linux-Large-NEXT: .lrodata
; X64-Linux-Large-NEXT: __morestack_addr:
; X64-Linux-Large-NEXT: .quad __morestack
More information about the llvm-commits
mailing list