Skip to content

Commit 66b919c

Browse files
committed
Reland [InstrProf][X86] Mark non-directly accessed globals as large (#74778)
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 #74514. This affects the __llvm_prf_vals, __llvm_prf_vnds, and __llvm_prf_names sections. The reland fixes platform.ll.
1 parent 96a5135 commit 66b919c

File tree

6 files changed

+47
-9
lines changed

6 files changed

+47
-9
lines changed

llvm/include/llvm/Transforms/Instrumentation.h

+5
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 overflow pressure. This can be be used for metadata globals that
54+
// aren't directly accessed by code, which has no performance impact.
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

+1-3
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

+4
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

+7
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

+29-5
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-prefixes=CODEMODEL,CODEMODEL-X8664
20+
; RUN: opt %s -mtriple=powerpc-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefixes=CODEMODEL,CODEMODEL-PPC
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,26 @@ 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+
; CODEMODEL: @__profc_foo =
83+
; CODEMODEL-NOT: code_model "large"
84+
; CODEMODEL: @__profvp_foo =
85+
; CODEMODEL-X8664-SAME: code_model "large"
86+
; CODEMODEL-PPC-NOT: code_model
87+
; CODEMODEL: @__profd_foo =
88+
; CODEMODEL-NOT: code_model "large"
89+
; CODEMODEL: @__profc_bar =
90+
; CODEMODEL-NOT: code_model "large"
91+
; CODEMODEL: @__profvp_bar =
92+
; CODEMODEL-X8664-SAME: code_model "large"
93+
; CODEMODEL-PPC-NOT: code_model
94+
; CODEMODEL: @__profd_bar =
95+
; CODEMODEL-NOT: code_model "large"
96+
; CODEMODEL: @__llvm_prf_vnodes =
97+
; CODEMODEL-X8664-SAME: code_model "large"
98+
; CODEMODEL-PPC-NOT: code_model
99+
; CODEMODEL: @__llvm_prf_nm =
100+
; CODEMODEL-X8664-SAME: code_model "large"
101+
; CODEMODEL-PPC-NOT: code_model

llvm/test/Instrumentation/InstrProfiling/platform.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
; WINDOWS: @__profd_foo = private global {{.*}}, section ".lprfd$M", align 8
2626
; AIX: @__profd_foo = private {{.*}}, section "__llvm_prf_data", align 8
2727

28-
; ELF: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}__llvm_prf_names", align 1
28+
; ELF: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}__llvm_prf_names"{{.*}}, align 1
2929
; WINDOWS: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}lprfn$M", align 1
3030
; AIX: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}__llvm_prf_names", align 1
3131

0 commit comments

Comments
 (0)