Skip to content

Commit 6e8fc96

Browse files
committed
[InstrProf][X86] Mark non-directly accessed globals as large
We'd like to make various instrprof globals large to make them not contribute to relocation pressure since there are no direct accesses to them in the module. Similar to what was done for asan_globals in llvm#74514. This affects the __llvm_prf_vals, __llvm_prf_vnds, and __llvm_prf_names sections.
1 parent 58c2a4e commit 6e8fc96

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

llvm/include/llvm/Transforms/Instrumentation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
4949
// Returns nullptr on failure.
5050
Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
5151

52+
// Place global in a large section for x86-64 ELF binaries to mitigate
53+
// relocation pressure. Should typically be used for metadata globals that
54+
// aren't directly accessed in the module.
55+
void setGlobalVariableLargeSection(Triple &TargetTriple, GlobalVariable &GV);
56+
5257
// Insert GCOV profiling instrumentation
5358
struct GCOVOptions {
5459
static GCOVOptions getDefault();

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,9 +2146,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
21462146
Metadata->setSection(getGlobalMetadataSection());
21472147
// Place metadata in a large section for x86-64 ELF binaries to mitigate
21482148
// relocation pressure.
2149-
if (TargetTriple.getArch() == Triple::x86_64 &&
2150-
TargetTriple.getObjectFormat() == Triple::ELF)
2151-
Metadata->setCodeModel(CodeModel::Large);
2149+
setGlobalVariableLargeSection(TargetTriple, *Metadata);
21522150
return Metadata;
21532151
}
21542152

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "llvm/Support/Error.h"
5050
#include "llvm/Support/ErrorHandling.h"
5151
#include "llvm/TargetParser/Triple.h"
52+
#include "llvm/Transforms/Instrumentation.h"
5253
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
5354
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
5455
#include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -1294,6 +1295,7 @@ void InstrProfiling::createDataVariable(InstrProfCntrInstBase *Inc) {
12941295
*M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
12951296
getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
12961297
ValuesVar->setVisibility(Visibility);
1298+
setGlobalVariableLargeSection(TT, *ValuesVar);
12971299
ValuesVar->setSection(
12981300
getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
12991301
ValuesVar->setAlignment(Align(8));
@@ -1422,6 +1424,7 @@ void InstrProfiling::emitVNodes() {
14221424
auto *VNodesVar = new GlobalVariable(
14231425
*M, VNodesTy, false, GlobalValue::PrivateLinkage,
14241426
Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
1427+
setGlobalVariableLargeSection(TT, *VNodesVar);
14251428
VNodesVar->setSection(
14261429
getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
14271430
VNodesVar->setAlignment(M->getDataLayout().getABITypeAlign(VNodesTy));
@@ -1449,6 +1452,7 @@ void InstrProfiling::emitNameData() {
14491452
GlobalValue::PrivateLinkage, NamesVal,
14501453
getInstrProfNamesVarName());
14511454
NamesSize = CompressedNameStr.size();
1455+
setGlobalVariableLargeSection(TT, *NamesVar);
14521456
NamesVar->setSection(
14531457
getInstrProfSectionName(IPSK_name, TT.getObjectFormat()));
14541458
// On COFF, it's important to reduce the alignment down to 1 to prevent the

llvm/lib/Transforms/Instrumentation/Instrumentation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
8585
return C;
8686
}
8787

88+
void llvm::setGlobalVariableLargeSection(Triple &TargetTriple,
89+
GlobalVariable &GV) {
90+
if (TargetTriple.getArch() == Triple::x86_64 &&
91+
TargetTriple.getObjectFormat() == Triple::ELF) {
92+
GV.setCodeModel(CodeModel::Large);
93+
}
94+
}

llvm/test/Instrumentation/InstrProfiling/icall-comdat.ll

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
;; Check that static counters are allocated for value profiler
2-
32
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -vp-static-alloc=true -S | FileCheck %s --check-prefix=STATIC
43
; RUN: opt < %s -mtriple=powerpc-unknown-linux -passes=instrprof -vp-static-alloc=true -S | FileCheck %s --check-prefix=STATIC
54
; RUN: opt < %s -mtriple=sparc-unknown-linux -passes=instrprof -vp-static-alloc=true -S | FileCheck %s --check-prefix=STATIC
@@ -16,6 +15,10 @@
1615
; RUN: opt %s -mtriple=powerpc64-ibm-aix -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
1716
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=ALIGN
1817

18+
;; Check that globals have the proper code model.
19+
; RUN: opt %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=X8664-CODEMODEL
20+
; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=PPC-CODEMODEL
21+
1922
@__profn_foo = private constant [3 x i8] c"foo"
2023
@__profn_bar = private constant [3 x i8] c"bar"
2124

@@ -46,8 +49,8 @@ declare void @llvm.instrprof.value.profile(ptr, i64, i64, i32, i32) #0
4649

4750
attributes #0 = { nounwind }
4851

49-
; STATIC: @__profvp_foo = private global [1 x i64] zeroinitializer, section "{{[^"]+}}", comdat($__profc_foo)
50-
; STATIC: @__profvp_bar = private global [1 x i64] zeroinitializer, section "{{[^"]+}}", comdat($__profc_bar)
52+
; STATIC: @__profvp_foo = private global [1 x i64] zeroinitializer, section "{{[^"]+}}",{{.*}} comdat($__profc_foo)
53+
; STATIC: @__profvp_bar = private global [1 x i64] zeroinitializer, section "{{[^"]+}}",{{.*}} comdat($__profc_bar)
5154
; STATIC: @__llvm_prf_vnodes
5255

5356
; DYN-NOT: @__profvp_foo
@@ -73,5 +76,15 @@ attributes #0 = { nounwind }
7376
; ALIGN: @__profc_bar = private global {{.*}} section "__llvm_prf_cnts",{{.*}} align 8
7477
; ALIGN: @__profvp_bar = private global {{.*}} section "__llvm_prf_vals",{{.*}} align 8
7578
; ALIGN: @__profd_bar = private global {{.*}} section "__llvm_prf_data",{{.*}} align 8
76-
; ALIGN: @__llvm_prf_vnodes = private global {{.*}} section "__llvm_prf_vnds", align 8
77-
; ALIGN: @__llvm_prf_nm = private constant {{.*}} section "__llvm_prf_names", align 1
79+
; ALIGN: @__llvm_prf_vnodes = private global {{.*}} section "__llvm_prf_vnds",{{.*}} align 8
80+
; ALIGN: @__llvm_prf_nm = private constant {{.*}} section "__llvm_prf_names",{{.*}} align 1
81+
82+
; X8664-CODEMODEL-NOT: @__profc_foo = {{.*}}, code_model "large"
83+
; X8664-CODEMODEL: @__profvp_foo = {{.*}}, code_model "large"
84+
; X8664-CODEMODEL-NOT: @__profd_foo = {{.*}}, code_model "large"
85+
; X8664-CODEMODEL-NOT: @__profc_bar = {{.*}}, code_model "large"
86+
; X8664-CODEMODEL: @__profvp_bar = {{.*}}, code_model "large"
87+
; X8664-CODEMODEL-NOT: @__profd_bar = {{.*}}, code_model "large"
88+
; X8664-CODEMODEL: @__llvm_prf_vnodes = {{.*}}, code_model "large"
89+
; X8664-CODEMODEL: @__llvm_prf_nm = {{.*}}, code_model "large"
90+
; PPC-CODEMODEL-NOT: code_model "large"

0 commit comments

Comments
 (0)