-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[StaticDataLayout] Factor out a helper function for section prefix eligibility and use it in both optimizer and codegen #162348
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
Changes from 10 commits
a05f76f
f68b06f
331888b
dcb9f00
3565bc0
905b80f
95292a6
64be09f
4a90c77
f9c6266
34b47c9
3756370
b880ee3
3ca69f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -75,7 +75,7 @@ bool StaticDataAnnotator::runOnModule(Module &M) { | |||||
|
||||||
bool Changed = false; | ||||||
for (auto &GV : M.globals()) { | ||||||
if (GV.isDeclarationForLinker()) | ||||||
if (!llvm::memprof::IsAnnotationOK(GV)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper function includes more checks than just isDeclarationForLinker - so is this change really NFC? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I agree. I think this PR is more of 'NFCI' (non functional change intended) or no user-visible change intended. I removed the 'NFC' tag so (unfamiliar) readers won't get (unintentionally) fooled.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But are the effects of this change and the other one I mentioned earlier not observable by tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, the test coverage is limited to [1] before. Improved the test coverage now, PTAL, thanks! [1] llvm-project/llvm/test/Transforms/PGOProfile/data-access-profile.ll Lines 28 to 29 in 139a6bf
|
||||||
continue; | ||||||
|
||||||
// The implementation below assumes prior passes don't set section prefixes, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Analysis/MemoryProfileInfo.h" | ||
#include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||
#include "llvm/Analysis/StaticDataProfileInfo.h" | ||
#include "llvm/Analysis/TargetLibraryInfo.h" | ||
#include "llvm/IR/DiagnosticInfo.h" | ||
#include "llvm/IR/Function.h" | ||
|
@@ -194,6 +195,30 @@ static bool isAllocationWithHotColdVariant(const Function *Callee, | |
} | ||
} | ||
|
||
static void HandleUnsupportedAnnotationKinds(GlobalVariable &GVar, | ||
AnnotationKind Kind) { | ||
assert(Kind != llvm::memprof::AnnotationKind::AnnotationOK && | ||
"Should not handle AnnotationOK here"); | ||
SmallString<32> Reason; | ||
switch (Kind) { | ||
case llvm::memprof::AnnotationKind::ExplicitSection: | ||
++NumOfMemProfExplicitSectionGlobalVars; | ||
Reason.append("explicit section name"); | ||
break; | ||
case llvm::memprof::AnnotationKind::DeclForLinker: | ||
Reason.append("linker declaration"); | ||
break; | ||
case llvm::memprof::AnnotationKind::ReservedName: | ||
Reason.append("name starts with `llvm.`"); | ||
break; | ||
default: | ||
llvm_unreachable("Unexpected annotation kind"); | ||
} | ||
LLVM_DEBUG(dbgs() << "Skip annotation for " << GVar.getName() << " due to " | ||
<< Reason << ".\n"); | ||
return; | ||
} | ||
|
||
struct AllocMatchInfo { | ||
uint64_t TotalSize = 0; | ||
AllocationType AllocType = AllocationType::None; | ||
|
@@ -775,36 +800,21 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) { | |
return PreservedAnalyses::none(); | ||
} | ||
|
||
// Returns true iff the global variable has custom section either by | ||
// __attribute__((section("name"))) | ||
// (https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate) | ||
// or #pragma clang section directives | ||
// (https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section). | ||
static bool hasExplicitSectionName(const GlobalVariable &GVar) { | ||
if (GVar.hasSection()) | ||
return true; | ||
|
||
auto Attrs = GVar.getAttributes(); | ||
if (Attrs.hasAttribute("bss-section") || Attrs.hasAttribute("data-section") || | ||
Attrs.hasAttribute("relro-section") || | ||
Attrs.hasAttribute("rodata-section")) | ||
return true; | ||
return false; | ||
} | ||
|
||
bool MemProfUsePass::annotateGlobalVariables( | ||
Module &M, const memprof::DataAccessProfData *DataAccessProf) { | ||
if (!AnnotateStaticDataSectionPrefix || M.globals().empty()) | ||
return false; | ||
|
||
if (!DataAccessProf) { | ||
M.addModuleFlag(Module::Warning, "EnableDataAccessProf", 0U); | ||
M.getContext().diagnose(DiagnosticInfoPGOProfile( | ||
MemoryProfileFileName.data(), | ||
StringRef("Data access profiles not found in memprof. Ignore " | ||
"-memprof-annotate-static-data-prefix."), | ||
DS_Warning)); | ||
return false; | ||
} | ||
M.addModuleFlag(Module::Warning, "EnableDataAccessProf", 1U); | ||
|
||
bool Changed = false; | ||
// Iterate all global variables in the module and annotate them based on | ||
|
@@ -815,13 +825,12 @@ bool MemProfUsePass::annotateGlobalVariables( | |
for (GlobalVariable &GVar : M.globals()) { | ||
assert(!GVar.getSectionPrefix().has_value() && | ||
"GVar shouldn't have section prefix yet"); | ||
if (GVar.isDeclarationForLinker()) | ||
continue; | ||
|
||
if (hasExplicitSectionName(GVar)) { | ||
++NumOfMemProfExplicitSectionGlobalVars; | ||
LLVM_DEBUG(dbgs() << "Global variable " << GVar.getName() | ||
<< " has explicit section name. Skip annotating.\n"); | ||
auto Kind = llvm::memprof::getAnnotationKind(GVar); | ||
switch (Kind) { | ||
|
||
case llvm::memprof::AnnotationKind::AnnotationOK: | ||
break; | ||
default: | ||
HandleUnsupportedAnnotationKinds(GVar, Kind); | ||
continue; | ||
} | ||
|
||
|
@@ -831,7 +840,6 @@ bool MemProfUsePass::annotateGlobalVariables( | |
// TODO: Track string content hash in the profiles and compute it inside the | ||
// compiler to categeorize the hotness string literals. | ||
if (Name.starts_with(".str")) { | ||
|
||
LLVM_DEBUG(dbgs() << "Skip annotating string literal " << Name << "\n"); | ||
continue; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" | ||
target triple = "x86_64-unknown-linux-gnu" | ||
|
||
;; A minimal test case. llc will crash if global variables already has a section | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the behavior of this test affected by this PR? If not, commit it separately as a standalone change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in #162938 to pre-commit the test case. |
||
;; prefix. Subsequent PRs will expand on this test case to test the hotness | ||
;; reconcillation implementation. | ||
|
||
; RUN: not llc -mtriple=x86_64-unknown-linux-gnu -relocation-model=pic \ | ||
; RUN: -partition-static-data-sections=true \ | ||
; RUN: -data-sections=true -unique-section-names=false \ | ||
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefix=ERR | ||
|
||
; ERR: Global variable hot_bss already has a section prefix hot | ||
|
||
@hot_bss = internal global i32 0, !section_prefix !17 | ||
|
||
define void @hot_func() !prof !14 { | ||
%9 = load i32, ptr @hot_bss | ||
%11 = call i32 (...) @func_taking_arbitrary_param(i32 %9) | ||
ret void | ||
} | ||
|
||
declare i32 @func_taking_arbitrary_param(...) | ||
|
||
!llvm.module.flags = !{!1} | ||
|
||
!1 = !{i32 1, !"ProfileSummary", !2} | ||
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10} | ||
!3 = !{!"ProfileFormat", !"InstrProf"} | ||
!4 = !{!"TotalCount", i64 1460183} | ||
!5 = !{!"MaxCount", i64 849024} | ||
!6 = !{!"MaxInternalCount", i64 32769} | ||
!7 = !{!"MaxFunctionCount", i64 849024} | ||
!8 = !{!"NumCounts", i64 23627} | ||
!9 = !{!"NumFunctions", i64 3271} | ||
!10 = !{!"DetailedSummary", !11} | ||
!11 = !{!12, !13} | ||
!12 = !{i32 990000, i64 166, i32 73} | ||
!13 = !{i32 999999, i64 3, i32 1443} | ||
!14 = !{!"function_entry_count", i64 100000} | ||
!15 = !{!"function_entry_count", i64 1} | ||
!16 = !{!"branch_weights", i32 1, i32 99999} | ||
!17 = !{!"section_prefix", !"hot"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is new afaict - so is this NFC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry, you are correct.
This is from another similar call site [1] and this PR means to cover that one. Updated the PR.
[1]
llvm-project/llvm/lib/CodeGen/StaticDataSplitter.cpp
Lines 133 to 137 in 30b9ef8