Skip to content

[InstrProf][X86] Set code model for counters global variables #77986

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 19 additions & 4 deletions llvm/include/llvm/Transforms/Instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,25 @@ GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
// Returns nullptr on failure.
Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);

// Place global in a large section for x86-64 ELF binaries to mitigate
// relocation overflow pressure. This can be be used for metadata globals that
// aren't directly accessed by code, which has no performance impact.
void setGlobalVariableLargeSection(const Triple &TargetTriple,
// Place global in a large section for x86-64 ELF binaries in code models with
// split small/large data sections to mitigate relocation overflow pressure.
// This can be be used for metadata globals that aren't directly accessed by
// code, which has no performance impact.
void setGlobalVariableLargeCodeModel(const Triple &TargetTriple,
GlobalVariable &GV);

// Place global in a small or large section for x86-64 ELF binaries in code
// models with split small/large data sections. This can be be used for metadata
// globals that vary in size that are put into an explicit section, which can
// cause inconsistent small/large section flags on the explicit section. This
// should only be used for globals that are frequently accessed, otherwise
// setGlobalVariableLargeCodeModel() should be used to mitigate relocation
// pressure.
//
// Currently this places the variables in a small section for the medium code
// model, where performance is still a concern, but in a large section for the
// large code model, where we try to avoid all relocation pressure.
void setHotGlobalVariableCodeModel(const Triple &TargetTriple,
GlobalVariable &GV);

// Insert GCOV profiling instrumentation
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
Metadata->setSection(getGlobalMetadataSection());
// Place metadata in a large section for x86-64 ELF binaries to mitigate
// relocation pressure.
setGlobalVariableLargeSection(TargetTriple, *Metadata);
setGlobalVariableLargeCodeModel(TargetTriple, *Metadata);
return Metadata;
}

Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
// Put the counters and bitmaps in their own sections so linkers can
// remove unneeded sections.
Ptr->setSection(getInstrProfSectionName(IPSK, TT.getObjectFormat()));
setHotGlobalVariableCodeModel(TT, *Ptr);
Ptr->setLinkage(Linkage);
maybeSetComdat(Ptr, Fn, VarName);
return Ptr;
Expand Down Expand Up @@ -1450,7 +1451,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
M, ValuesTy, false, Linkage, Constant::getNullValue(ValuesTy),
getVarName(Inc, getInstrProfValuesVarPrefix(), Renamed));
ValuesVar->setVisibility(Visibility);
setGlobalVariableLargeSection(TT, *ValuesVar);
setGlobalVariableLargeCodeModel(TT, *ValuesVar);
ValuesVar->setSection(
getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
ValuesVar->setAlignment(Align(8));
Expand Down Expand Up @@ -1588,7 +1589,7 @@ void InstrLowerer::emitVNodes() {
auto *VNodesVar = new GlobalVariable(
M, VNodesTy, false, GlobalValue::PrivateLinkage,
Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
setGlobalVariableLargeSection(TT, *VNodesVar);
setGlobalVariableLargeCodeModel(TT, *VNodesVar);
VNodesVar->setSection(
getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat()));
VNodesVar->setAlignment(M.getDataLayout().getABITypeAlign(VNodesTy));
Expand Down Expand Up @@ -1616,7 +1617,7 @@ void InstrLowerer::emitNameData() {
GlobalValue::PrivateLinkage, NamesVal,
getInstrProfNamesVarName());
NamesSize = CompressedNameStr.size();
setGlobalVariableLargeSection(TT, *NamesVar);
setGlobalVariableLargeCodeModel(TT, *NamesVar);
NamesVar->setSection(
ProfileCorrelate == InstrProfCorrelator::BINARY
? getInstrProfSectionName(IPSK_covname, TT.getObjectFormat())
Expand Down
20 changes: 18 additions & 2 deletions llvm/lib/Transforms/Instrumentation/Instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
return C;
}

void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
GlobalVariable &GV) {
void llvm::setGlobalVariableLargeCodeModel(const Triple &TargetTriple,
GlobalVariable &GV) {
// Limit to x86-64 ELF.
if (TargetTriple.getArch() != Triple::x86_64 ||
TargetTriple.getObjectFormat() != Triple::ELF)
Expand All @@ -97,3 +97,19 @@ void llvm::setGlobalVariableLargeSection(const Triple &TargetTriple,
return;
GV.setCodeModel(CodeModel::Large);
}

void llvm::setHotGlobalVariableCodeModel(const Triple &TargetTriple,
GlobalVariable &GV) {
// Limit to x86-64 ELF.
if (TargetTriple.getArch() != Triple::x86_64 ||
TargetTriple.getObjectFormat() != Triple::ELF)
return;
// Limit to medium/large code models.
std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
if (!CM)
return;
if (*CM == CodeModel::Medium)
GV.setCodeModel(CodeModel::Small);
else if (*CM == CodeModel::Large)
GV.setCodeModel(CodeModel::Large);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ attributes #0 = { nounwind }
!0 = !{i32 1, !"Code Model", i32 4}

; CHECK: @__profc_foo =
; CHECK-NOT: code_model "large"
; CHECK-SAME: code_model "large"
; CHECK: @__profvp_foo =
; CHECK-SAME: code_model "large"
; CHECK: @__profd_foo =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ attributes #0 = { nounwind }
!0 = !{i32 1, !"Code Model", i32 3}

; CHECK: @__profc_foo =
; CHECK-NOT: code_model "large"
; X8664-SAME: code_model "small"
; PPC-NOT: code_model
; CHECK: @__profvp_foo =
; X8664-SAME: code_model "large"
; PPC-NOT: code_model "large"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ attributes #0 = { nounwind }
!0 = !{i32 1, !"Code Model", i32 1}

; CHECK: @__profc_foo =
; CHECK-NOT: code_model "large"
; CHECK-NOT: code_model
; CHECK: @__profvp_foo =
; CHECK-NOT: code_model "large"
; CHECK: @__profd_foo =
Expand Down