Skip to content

Backport https://github.com/llvm/llvm-project/commit/281d71604f418eb952e967d9dc4b26241b7f96aa to 18.x #30

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 1 commit into from
Jun 30, 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
26 changes: 20 additions & 6 deletions llvm/lib/Target/TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
if (getTargetTriple().getArch() != Triple::x86_64)
return false;

// Remaining logic below is ELF-specific. For other object file formats where
// the large code model is mostly used for JIT compilation, just look at the
// code model.
if (!getTargetTriple().isOSBinFormatELF())
return getCodeModel() == CodeModel::Large;

auto *GO = GVal->getAliaseeObject();

// Be conservative if we can't find an underlying GlobalObject.
Expand All @@ -51,9 +57,20 @@ bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {

auto *GV = dyn_cast<GlobalVariable>(GO);

auto IsPrefix = [](StringRef Name, StringRef Prefix) {
return Name.consume_front(Prefix) && (Name.empty() || Name[0] == '.');
};

// Functions/GlobalIFuncs are only large under the large code model.
if (!GV)
if (!GV) {
// Handle explicit sections as we do for GlobalVariables with an explicit
// section, see comments below.
if (GO->hasSection()) {
StringRef Name = GO->getSection();
return IsPrefix(Name, ".ltext");
}
return getCodeModel() == CodeModel::Large;
}

if (GV->isThreadLocal())
return false;
Expand All @@ -73,11 +90,8 @@ bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
// data sections. The code model attribute overrides this above.
if (GV->hasSection()) {
StringRef Name = GV->getSection();
auto IsPrefix = [&](StringRef Prefix) {
StringRef S = Name;
return S.consume_front(Prefix) && (S.empty() || S[0] == '.');
};
return IsPrefix(".lbss") || IsPrefix(".ldata") || IsPrefix(".lrodata");
return IsPrefix(Name, ".lbss") || IsPrefix(Name, ".ldata") ||
IsPrefix(Name, ".lrodata");
}

// Respect large data threshold for medium and large code models.
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/X86/code-model-elf-text-sections.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,36 @@
; RUN: llvm-readelf -S %t | FileCheck %s --check-prefix=LARGE-DS

; SMALL: .text {{.*}} AX {{.*}}
; SMALL: .ltext {{.*}} AXl {{.*}}
; SMALL: .ltext.2 {{.*}} AXl {{.*}}
; SMALL: .foo {{.*}} AX {{.*}}
; SMALL-DS: .text.func {{.*}} AX {{.*}}
; SMALL-DS: .ltext {{.*}} AXl {{.*}}
; SMALL-DS: .ltext.2 {{.*}} AXl {{.*}}
; SMALL-DS: .foo {{.*}} AX {{.*}}
; LARGE: .ltext {{.*}} AXl {{.*}}
; LARGE: .ltext.2 {{.*}} AXl {{.*}}
; LARGE: .foo {{.*}} AX {{.*}}
; LARGE-DS: .ltext.func {{.*}} AXl {{.*}}
; LARGE-DS: .ltext {{.*}} AXl {{.*}}
; LARGE-DS: .ltext.2 {{.*}} AXl {{.*}}
; LARGE-DS: .foo {{.*}} AX {{.*}}

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64--linux"

define void @func() {
ret void
}

define void @ltext() section ".ltext" {
ret void
}

define void @ltext2() section ".ltext.2" {
ret void
}

define void @foo() section ".foo" {
ret void
}