Skip to content

Commit f68b06f

Browse files
[nfc][StaticDataLayout]Add helper functions to tell the eligibility status of a global variable for section prefix annotation
1 parent a05f76f commit f68b06f

File tree

4 files changed

+65
-35
lines changed

4 files changed

+65
-35
lines changed

llvm/include/llvm/Analysis/StaticDataProfileInfo.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010

1111
namespace llvm {
1212

13+
namespace memprof {
14+
// Represents the eligibility status of a global variable for section prefix
15+
// annotation. Other than AnnotationOk, each enum value indicates a specific
16+
// reason for ineligibility.
17+
enum class AnnotationKind : uint8_t {
18+
AnnotationOK,
19+
DeclForLinker,
20+
ExplicitSection,
21+
ReservedName,
22+
};
23+
/// Returns the annotation kind of the global variable \p GV.
24+
AnnotationKind getAnnotationKind(const GlobalVariable &GV);
25+
26+
/// Returns true if the annotation kind of the global variable \p GV is
27+
/// AnnotationOK.
28+
bool IsAnnotationOK(const GlobalVariable &GV);
29+
} // namespace memprof
30+
1331
/// A class that holds the constants that represent static data and their
1432
/// profile information and provides methods to operate on them.
1533
class StaticDataProfileInfo {

llvm/lib/Analysis/StaticDataProfileInfo.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@
66
#include "llvm/ProfileData/InstrProf.h"
77

88
using namespace llvm;
9+
10+
namespace llvm {
11+
namespace memprof {
12+
// Returns true iff the global variable has custom section either by
13+
// __attribute__((section("name")))
14+
// (https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate)
15+
// or #pragma clang section directives
16+
// (https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section).
17+
static bool hasExplicitSectionName(const GlobalVariable &GVar) {
18+
if (GVar.hasSection())
19+
return true;
20+
21+
auto Attrs = GVar.getAttributes();
22+
if (Attrs.hasAttribute("bss-section") || Attrs.hasAttribute("data-section") ||
23+
Attrs.hasAttribute("relro-section") ||
24+
Attrs.hasAttribute("rodata-section"))
25+
return true;
26+
return false;
27+
}
28+
29+
AnnotationKind getAnnotationKind(const GlobalVariable &GV) {
30+
if (GV.isDeclarationForLinker())
31+
return AnnotationKind::DeclForLinker;
32+
StringRef Name = GV.getName();
33+
if (Name.starts_with("llvm."))
34+
return AnnotationKind::ReservedName;
35+
if (hasExplicitSectionName(GV))
36+
return AnnotationKind::ExplicitSection;
37+
return AnnotationKind::AnnotationOK;
38+
}
39+
40+
bool IsAnnotationOK(const GlobalVariable &GV) {
41+
return getAnnotationKind(GV) == AnnotationKind::AnnotationOK;
42+
}
43+
} // namespace memprof
44+
} // namespace llvm
45+
946
void StaticDataProfileInfo::addConstantProfileCount(
1047
const Constant *C, std::optional<uint64_t> Count) {
1148
if (!Count) {

llvm/lib/CodeGen/StaticDataAnnotator.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,11 @@ bool StaticDataAnnotator::runOnModule(Module &M) {
7575

7676
bool Changed = false;
7777
for (auto &GV : M.globals()) {
78-
if (GV.isDeclarationForLinker())
78+
if (!llvm::memprof::IsAnnotationOK(GV))
7979
continue;
8080

81-
// The implementation below assumes prior passes don't set section prefixes,
82-
// and specifically do 'assign' rather than 'update'. So report error if a
83-
// section prefix is already set.
84-
if (auto maybeSectionPrefix = GV.getSectionPrefix();
85-
maybeSectionPrefix && !maybeSectionPrefix->empty())
86-
llvm::report_fatal_error("Global variable " + GV.getName() +
87-
" already has a section prefix " +
88-
*maybeSectionPrefix);
89-
9081
StringRef SectionPrefix = SDPI->getConstantSectionPrefix(&GV, PSI);
91-
if (SectionPrefix.empty())
92-
continue;
93-
82+
// setSectionPrefix returns true if the section prefix is changed.
9483
Changed |= GV.setSectionPrefix(SectionPrefix);
9584
}
9685

llvm/lib/Transforms/Instrumentation/MemProfUse.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Analysis/MemoryProfileInfo.h"
1919
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
20+
#include "llvm/Analysis/StaticDataProfileInfo.h"
2021
#include "llvm/Analysis/TargetLibraryInfo.h"
2122
#include "llvm/IR/DiagnosticInfo.h"
2223
#include "llvm/IR/Function.h"
@@ -775,23 +776,6 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
775776
return PreservedAnalyses::none();
776777
}
777778

778-
// Returns true iff the global variable has custom section either by
779-
// __attribute__((section("name")))
780-
// (https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate)
781-
// or #pragma clang section directives
782-
// (https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section).
783-
static bool hasExplicitSectionName(const GlobalVariable &GVar) {
784-
if (GVar.hasSection())
785-
return true;
786-
787-
auto Attrs = GVar.getAttributes();
788-
if (Attrs.hasAttribute("bss-section") || Attrs.hasAttribute("data-section") ||
789-
Attrs.hasAttribute("relro-section") ||
790-
Attrs.hasAttribute("rodata-section"))
791-
return true;
792-
return false;
793-
}
794-
795779
bool MemProfUsePass::annotateGlobalVariables(
796780
Module &M, const memprof::DataAccessProfData *DataAccessProf) {
797781
if (!AnnotateStaticDataSectionPrefix || M.globals().empty())
@@ -817,13 +801,16 @@ bool MemProfUsePass::annotateGlobalVariables(
817801
for (GlobalVariable &GVar : M.globals()) {
818802
assert(!GVar.getSectionPrefix().has_value() &&
819803
"GVar shouldn't have section prefix yet");
820-
if (GVar.isDeclarationForLinker())
821-
continue;
822-
823-
if (hasExplicitSectionName(GVar)) {
804+
auto Kind = llvm::memprof::getAnnotationKind(GVar);
805+
switch (Kind) {
806+
case llvm::memprof::AnnotationKind::AnnotationOK:
807+
break;
808+
case llvm::memprof::AnnotationKind::ExplicitSection:
824809
++NumOfMemProfExplicitSectionGlobalVars;
825810
LLVM_DEBUG(dbgs() << "Global variable " << GVar.getName()
826811
<< " has explicit section name. Skip annotating.\n");
812+
[[fallthrough]];
813+
default:
827814
continue;
828815
}
829816

@@ -833,7 +820,6 @@ bool MemProfUsePass::annotateGlobalVariables(
833820
// TODO: Track string content hash in the profiles and compute it inside the
834821
// compiler to categeorize the hotness string literals.
835822
if (Name.starts_with(".str")) {
836-
837823
LLVM_DEBUG(dbgs() << "Skip annotating string literal " << Name << "\n");
838824
continue;
839825
}

0 commit comments

Comments
 (0)