Skip to content

Commit ddebce7

Browse files
authored
[X86] Respect code_model when determining if a global is small/large (#74498)
Using the GlobalVariable code_model property added in #72077. code_model = "small" means the global should be treated as small regardless of the TargetMachine code model. code_model = "large" means the global should be treated as large regardless of the TargetMachine code model. Inferring small/large based on a known section name still takes precedence for correctness. The intention is to use this for globals that are accessed very infrequently but also take up a lot of space in the binary to mitigate relocation overflows. Prime examples are globals that go in "__llvm_prf_names" for coverage/PGO instrumented builds and "asan_globals" for ASan builds.
1 parent 5c3496f commit ddebce7

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/lib/Target/TargetMachine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ bool TargetMachine::isLargeGlobalObject(const GlobalObject *GO) const {
6666
return true;
6767
}
6868

69+
// For x86-64, we treat an explicit GlobalVariable small code model to mean
70+
// that the global should be placed in a small section, and ditto for large.
71+
// Well-known section names above take precedence for correctness.
72+
if (auto CM = GV->getCodeModel()) {
73+
if (*CM == CodeModel::Small)
74+
return false;
75+
if (*CM == CodeModel::Large)
76+
return true;
77+
}
78+
6979
if (getCodeModel() == CodeModel::Medium ||
7080
getCodeModel() == CodeModel::Large) {
7181
const DataLayout &DL = GV->getParent()->getDataLayout();

llvm/test/CodeGen/X86/code-model-elf-sections.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
; SMALL: .ldata {{.*}} WAl {{.*}}
2525
; SMALL: .ldata.x {{.*}} WAl {{.*}}
2626
; SMALL: .ldata0 {{.*}} WA {{.*}}
27+
; SMALL: force_small {{.*}} WA {{.*}}
28+
; SMALL: force_large {{.*}} WAl {{.*}}
2729
; SMALL: foo {{.*}} WA {{.*}}
2830
; SMALL: .bss {{.*}} WA {{.*}}
2931
; SMALL: .lbss {{.*}} WAl {{.*}}
@@ -40,6 +42,8 @@
4042
; SMALL-DS: .ldata.x {{.*}} WAl {{.*}}
4143
; SMALL-DS: .ldata0 {{.*}} WA {{.*}}
4244
; SMALL-DS: .data.data {{.*}} WA {{.*}}
45+
; SMALL-DS: force_small {{.*}} WA {{.*}}
46+
; SMALL-DS: force_large {{.*}} WAl {{.*}}
4347
; SMALL-DS: foo {{.*}} WA {{.*}}
4448
; SMALL-DS: .lbss {{.*}} WAl {{.*}}
4549
; SMALL-DS: .bss.bss {{.*}} WA {{.*}}
@@ -55,6 +59,8 @@
5559
; LARGE: .ldata {{.*}} WAl {{.*}}
5660
; LARGE: .ldata.x {{.*}} WAl {{.*}}
5761
; LARGE: .ldata0 {{.*}} WAl {{.*}}
62+
; LARGE: force_small {{.*}} WA {{.*}}
63+
; LARGE: force_large {{.*}} WAl {{.*}}
5864
; LARGE: foo {{.*}} WAl {{.*}}
5965
; LARGE: .bss {{.*}} WA {{.*}}
6066
; LARGE: .lbss {{.*}} WAl {{.*}}
@@ -71,6 +77,8 @@
7177
; LARGE-DS: .ldata.x {{.*}} WAl {{.*}}
7278
; LARGE-DS: .ldata0 {{.*}} WAl {{.*}}
7379
; LARGE-DS: .ldata.data {{.*}} WAl {{.*}}
80+
; LARGE-DS: force_small {{.*}} WA {{.*}}
81+
; LARGE-DS: force_large {{.*}} WAl {{.*}}
7482
; LARGE-DS: foo {{.*}} WAl {{.*}}
7583
; LARGE-DS: .bss {{.*}} WA {{.*}}
7684
; LARGE-DS: .lbss.bss {{.*}} WAl {{.*}}
@@ -90,6 +98,10 @@ target triple = "x86_64--linux"
9098
@ldata_with_explicit_section2 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".ldata.x"
9199
@ldata_with_explicit_section0 = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section ".ldata0"
92100
@data = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0]
101+
@data_force_small = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], code_model "small", section "force_small"
102+
@data_force_large = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], code_model "large", section "force_large"
103+
@data_force_small_ldata = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], code_model "small", section ".ldata"
104+
@data_force_large_data = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], code_model "large", section ".data"
93105
@foo_with_explicit_section = internal global [10 x i64] [i64 1, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0], section "foo"
94106
@bss_with_explicit_section = internal global [10 x i64] zeroinitializer, section ".bss"
95107
@lbss_with_explicit_section = internal global [10 x i64] zeroinitializer, section ".lbss"

0 commit comments

Comments
 (0)