[PATCH] D102818: [PGO] Don't reference functions unless value profiling is enabled

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 20 11:09:38 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8f20ac9595c8: [PGO] Don't reference functions unless value profiling is enabled (authored by rnk).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102818/new/

https://reviews.llvm.org/D102818

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenPGO.cpp
  clang/lib/CodeGen/CodeGenPGO.h
  compiler-rt/test/profile/instrprof-value-prof-2.c
  compiler-rt/test/profile/instrprof-value-prof.c
  llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp


Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -748,7 +748,26 @@
   return (Prefix + Name + "." + Twine(FuncHash)).str();
 }
 
+static uint64_t getIntModuleFlagOrZero(Module *M, StringRef Flag) {
+  auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M->getModuleFlag(Flag));
+  if (!MD)
+    return 0;
+
+  // If the flag is a ConstantAsMetadata, it should be an integer representable
+  // in 64-bits.
+  return cast<ConstantInt>(MD->getValue())->getZExtValue();
+}
+
 static inline bool shouldRecordFunctionAddr(Function *F) {
+  // Only record function addresses if IR PGO is enabled or if clang value
+  // profiling is enabled. Recording function addresses greatly increases object
+  // file size, because it prevents the inliner from deleting functions that
+  // have been inlined everywhere.
+  if (!isIRPGOFlagSet(F->getParent()) &&
+      getIntModuleFlagOrZero(F->getParent(), "EnableValueProfiling") == 0) {
+    return false;
+  }
+
   // Check the linkage
   bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage();
   if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
Index: compiler-rt/test/profile/instrprof-value-prof.c
===================================================================
--- compiler-rt/test/profile/instrprof-value-prof.c
+++ compiler-rt/test/profile/instrprof-value-prof.c
@@ -1,4 +1,4 @@
-// RUN: %clang_profgen -mllvm -vp-static-alloc=false  -O2 -o %t %s
+// RUN: %clang_profgen -mllvm -enable-value-profiling -mllvm -vp-static-alloc=false  -O2 -o %t %s
 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
 // RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t DO_NOT_INSTRUMENT
 // RUN: llvm-profdata merge -o %t.profdata %t.profraw
Index: compiler-rt/test/profile/instrprof-value-prof-2.c
===================================================================
--- compiler-rt/test/profile/instrprof-value-prof-2.c
+++ compiler-rt/test/profile/instrprof-value-prof-2.c
@@ -1,4 +1,4 @@
-// RUN: %clang_profgen -O2 -o %t %s
+// RUN: %clang_profgen -mllvm -enable-value-profiling -O2 -o %t %s
 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
 // RUN: llvm-profdata merge -o %t.profdata %t.profraw
 // RUN: llvm-profdata show --all-functions -ic-targets  %t.profdata > %t.out
Index: clang/lib/CodeGen/CodeGenPGO.h
===================================================================
--- clang/lib/CodeGen/CodeGenPGO.h
+++ clang/lib/CodeGen/CodeGenPGO.h
@@ -87,6 +87,10 @@
   // Insert instrumentation or attach profile metadata at value sites
   void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
                     llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
+
+  // Set a module flag indicating if value profiling is enabled.
+  void setValueProfilingFlag(llvm::Module &M);
+
 private:
   void setFuncName(llvm::Function *Fn);
   void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
Index: clang/lib/CodeGen/CodeGenPGO.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenPGO.cpp
+++ clang/lib/CodeGen/CodeGenPGO.cpp
@@ -962,6 +962,12 @@
         makeArrayRef(Args));
 }
 
+void CodeGenPGO::setValueProfilingFlag(llvm::Module &M) {
+  if (CGM.getCodeGenOpts().hasProfileClangInstr())
+    M.addModuleFlag(llvm::Module::Warning, "EnableValueProfiling",
+                    uint32_t(EnableValueProfiling));
+}
+
 // This method either inserts a call to the profile run-time during
 // instrumentation or puts profile data into metadata for PGO use.
 void CodeGenPGO::valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -511,6 +511,7 @@
   EmitGlobalAnnotations();
   EmitStaticExternCAliases();
   EmitDeferredUnusedCoverageMappings();
+  CodeGenPGO(*this).setValueProfilingFlag(getModule());
   if (CoverageMapping)
     CoverageMapping->emit();
   if (CodeGenOpts.SanitizeCfiCrossDso) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102818.346799.patch
Type: text/x-patch
Size: 4231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210520/47eb1623/attachment.bin>


More information about the llvm-commits mailing list