[llvm] [MC][X86] Set SHF_X86_64_LARGE on mergeable constant sections for large code-model (PR #190903)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 7 20:56:53 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Farid Zakaria (fzakaria)
<details>
<summary>Changes</summary>
Under -mcmodel=large on x86-64, constant pool entries were placed into `.rodata.cst{4,8,16,32}` without the `SHF_X86_64_LARGE` flag. Meanwhile, named global constants correctly received .lrodata with the large flag via getSectionPrefixForGlobal().
Fix initELFMCObjectFileInfo() to use .lrodata.cst* section names with SHF_X86_64_LARGE when the Large parameter is true and the target is x86-64.
I hit this trying to build our codebase with mcmodel=large and ThinLTO. because named globals and anonymous constants end up in different section groups with incompatible flags.
---
Full diff: https://github.com/llvm/llvm-project/pull/190903.diff
2 Files Affected:
- (modified) llvm/lib/MC/MCObjectFileInfo.cpp (+15-8)
- (added) llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll (+42)
``````````diff
diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp
index f2917179e1fc1..abbe06326a5d1 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -422,21 +422,28 @@ 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(".rodata.cst4", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
+ Ctx->getELFSection((CstPrefix + ".cst4").str(), ELF::SHT_PROGBITS,
+ MergeableCstFlags, 4);
MergeableConst8Section =
- Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
+ Ctx->getELFSection((CstPrefix + ".cst8").str(), ELF::SHT_PROGBITS,
+ MergeableCstFlags, 8);
MergeableConst16Section =
- Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
+ Ctx->getELFSection((CstPrefix + ".cst16").str(), ELF::SHT_PROGBITS,
+ MergeableCstFlags, 16);
MergeableConst32Section =
- Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
- ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
+ Ctx->getELFSection((CstPrefix + ".cst32").str(), ELF::SHT_PROGBITS,
+ MergeableCstFlags, 32);
// Exception Handling Sections.
diff --git a/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll b/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll
new file mode 100644
index 0000000000000..ae568fe5464fe
--- /dev/null
+++ b/llvm/test/CodeGen/X86/code-model-elf-constpool-large.ll
@@ -0,0 +1,42 @@
+; RUN: llc < %s -relocation-model=pic -filetype=obj -code-model=large -o %t
+; RUN: llvm-readelf -S %t | FileCheck %s
+
+; Verify that anonymous constant pool entries get SHF_X86_64_LARGE
+; and are placed in .lrodata.cst* sections under the large code model.
+
+; CHECK: .lrodata.cst16 {{.*}} AMl
+; CHECK: .lrodata.cst4 {{.*}} AMl
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64--linux"
+
+define void @vec_add(ptr %out, ptr %a, ptr %b, i32 %n) {
+entry:
+ %cmp = icmp sgt i32 %n, 0
+ br i1 %cmp, label %loop, label %exit
+
+loop:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+ %idx = sext i32 %iv to i64
+ %pa = getelementptr float, ptr %a, i64 %idx
+ %pb = getelementptr float, ptr %b, i64 %idx
+ %po = getelementptr float, ptr %out, i64 %idx
+ %va = load <4 x float>, ptr %pa, align 4
+ %vb = load <4 x float>, ptr %pb, align 4
+ %add = fadd <4 x float> %va, %vb
+ ; Adding a vector splat of 1.0 forces a constant pool entry in .rodata.cst16
+ %ones = fadd <4 x float> %add, <float 1.0, float 1.0, float 1.0, float 1.0>
+ store <4 x float> %ones, ptr %po, align 4
+ %iv.next = add i32 %iv, 4
+ %done = icmp sge i32 %iv.next, %n
+ br i1 %done, label %exit, label %loop
+
+exit:
+ ret void
+}
+
+; A scalar float constant forces a .rodata.cst4 entry.
+define float @scalar_const(float %x) {
+ %r = fadd float %x, 1.0
+ ret float %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/190903
More information about the llvm-commits
mailing list