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"
@@ -194,6 +195,30 @@ static bool isAllocationWithHotColdVariant(const Function *Callee,
194195 }
195196}
196197
198+ static void HandleUnsupportedAnnotationKinds (GlobalVariable &GVar,
199+ AnnotationKind Kind) {
200+ assert (Kind != llvm::memprof::AnnotationKind::AnnotationOK &&
201+ " Should not handle AnnotationOK here" );
202+ SmallString<32 > Reason;
203+ switch (Kind) {
204+ case llvm::memprof::AnnotationKind::ExplicitSection:
205+ ++NumOfMemProfExplicitSectionGlobalVars;
206+ Reason.append (" explicit section name" );
207+ break ;
208+ case llvm::memprof::AnnotationKind::DeclForLinker:
209+ Reason.append (" linker declaration" );
210+ break ;
211+ case llvm::memprof::AnnotationKind::ReservedName:
212+ Reason.append (" name starts with `llvm.`" );
213+ break ;
214+ default :
215+ llvm_unreachable (" Unexpected annotation kind" );
216+ }
217+ LLVM_DEBUG (dbgs () << " Skip annotation for " << GVar.getName () << " due to "
218+ << Reason << " .\n " );
219+ return ;
220+ }
221+
197222struct AllocMatchInfo {
198223 uint64_t TotalSize = 0 ;
199224 AllocationType AllocType = AllocationType::None;
@@ -775,23 +800,6 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
775800 return PreservedAnalyses::none ();
776801}
777802
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-
795803bool MemProfUsePass::annotateGlobalVariables (
796804 Module &M, const memprof::DataAccessProfData *DataAccessProf) {
797805 if (!AnnotateStaticDataSectionPrefix || M.globals ().empty ())
@@ -817,13 +825,9 @@ bool MemProfUsePass::annotateGlobalVariables(
817825 for (GlobalVariable &GVar : M.globals ()) {
818826 assert (!GVar.getSectionPrefix ().has_value () &&
819827 " GVar shouldn't have section prefix yet" );
820- if (GVar.isDeclarationForLinker ())
821- continue ;
822-
823- if (hasExplicitSectionName (GVar)) {
824- ++NumOfMemProfExplicitSectionGlobalVars;
825- LLVM_DEBUG (dbgs () << " Global variable " << GVar.getName ()
826- << " has explicit section name. Skip annotating.\n " );
828+ auto Kind = llvm::memprof::getAnnotationKind (GVar);
829+ if (Kind != llvm::memprof::AnnotationKind::AnnotationOK) {
830+ HandleUnsupportedAnnotationKinds (GVar, Kind);
827831 continue ;
828832 }
829833
@@ -833,7 +837,6 @@ bool MemProfUsePass::annotateGlobalVariables(
833837 // TODO: Track string content hash in the profiles and compute it inside the
834838 // compiler to categeorize the hotness string literals.
835839 if (Name.starts_with (" .str" )) {
836-
837840 LLVM_DEBUG (dbgs () << " Skip annotating string literal " << Name << " \n " );
838841 continue ;
839842 }
0 commit comments