[compiler-rt] [llvm] [DO NOT SUBMIT] Contextual iFDO "demo" PR (PR #86036)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 20 16:26:29 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 65f07b804c2c05cf49bd043f2a6e9a0020198165 603eccfd4b76ed4c07c9e22e43b44bef62d4adba -- compiler-rt/lib/profile/InstrProfilingContextual.cpp compiler-rt/lib/profile/InstrProfilingContextual.h compiler-rt/lib/profile/tests/InstrProfilingContextualTest.cpp compiler-rt/lib/profile/tests/driver.cpp llvm/include/llvm/ProfileData/CtxInstrProfileReader.h llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp llvm/tools/llvm-ctx-ifdo/llvm-ctx-ifdo.cpp llvm/include/llvm/IR/IntrinsicInst.h llvm/lib/IR/IntrinsicInst.cpp llvm/lib/Passes/PassBuilder.cpp llvm/lib/Passes/PassBuilderPipelines.cpp llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/compiler-rt/lib/profile/InstrProfilingContextual.cpp b/compiler-rt/lib/profile/InstrProfilingContextual.cpp
index e13d0688cc..fe0fe48c0b 100644
--- a/compiler-rt/lib/profile/InstrProfilingContextual.cpp
+++ b/compiler-rt/lib/profile/InstrProfilingContextual.cpp
@@ -49,12 +49,11 @@ __sanitizer::SpinMutex AllContextsMutex;
 SANITIZER_GUARDED_BY(AllContextsMutex)
 __sanitizer::Vector<ContextRoot *> AllContextRoots;
 
-ContextNode * markAsScratch(const ContextNode* Ctx) {
-  return reinterpret_cast<ContextNode*>(reinterpret_cast<uint64_t>(Ctx) | 1);
+ContextNode *markAsScratch(const ContextNode *Ctx) {
+  return reinterpret_cast<ContextNode *>(reinterpret_cast<uint64_t>(Ctx) | 1);
 }
 
-template<typename T>
-T consume(T& V) {
+template <typename T> T consume(T &V) {
   auto R = V;
   V = {0};
   return R;
@@ -102,7 +101,8 @@ extern "C" {
 __thread char __Buffer[kBuffSize] = {0};
 
 #define TheNullContext markAsScratch(reinterpret_cast<ContextNode *>(__Buffer))
-__thread void *volatile __llvm_instrprof_expected_callee[2] = {nullptr, nullptr};
+__thread void *volatile __llvm_instrprof_expected_callee[2] = {nullptr,
+                                                               nullptr};
 __thread ContextNode **volatile __llvm_instrprof_callsite[2] = {0, 0};
 
 COMPILER_RT_VISIBILITY __thread ContextRoot
@@ -113,7 +113,7 @@ __llvm_instprof_slow_get_callsite(uint64_t Guid, ContextNode **InsertionPoint,
                                   uint32_t NrCounters, uint32_t NrCallsites) {
   auto AllocSize = ContextNode::getAllocSize(NrCounters, NrCallsites);
   auto *Mem = __llvm_instrprof_current_context_root->CurrentMem;
-  char* AllocPlace = Mem->tryAllocate(AllocSize);
+  char *AllocPlace = Mem->tryAllocate(AllocSize);
   if (!AllocPlace) {
     __llvm_instrprof_current_context_root->CurrentMem = Mem =
         Mem->allocate(getArenaAllocSize(AllocSize), Mem);
@@ -126,7 +126,7 @@ __llvm_instprof_slow_get_callsite(uint64_t Guid, ContextNode **InsertionPoint,
 
 COMPILER_RT_VISIBILITY ContextNode *
 __llvm_instrprof_get_context(void *Callee, GUID Guid, uint32_t NrCounters,
-                            uint32_t NrCallsites) {
+                             uint32_t NrCallsites) {
   if (!__llvm_instrprof_current_context_root) {
     return TheNullContext;
   }
@@ -144,7 +144,8 @@ __llvm_instrprof_get_context(void *Callee, GUID Guid, uint32_t NrCounters,
   auto *Ret = Callsite ? Callsite
                        : __llvm_instprof_slow_get_callsite(
                              Guid, CallsiteContext, NrCounters, NrCallsites);
-  if (Ret->callsites_size() != NrCallsites || Ret->counters_size() != NrCounters)
+  if (Ret->callsites_size() != NrCallsites ||
+      Ret->counters_size() != NrCounters)
     __sanitizer::Printf("[ctxprof] Returned ctx differs from what's asked: "
                         "Context: %p, Asked: %zu %u %u, Got: %zu %u %u \n",
                         Ret, Guid, NrCallsites, NrCounters, Ret->guid(),
@@ -209,7 +210,7 @@ COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
 }
 
 COMPILER_RT_VISIBILITY
-int __llvm_ctx_profile_dump(const char* Filename) {
+int __llvm_ctx_profile_dump(const char *Filename) {
   __sanitizer::Printf("[ctxprof] Start Dump\n");
   __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
       &AllContextsMutex);
@@ -223,7 +224,7 @@ int __llvm_ctx_profile_dump(const char* Filename) {
     for (auto *Mem = Root->FirstMemBlock; Mem; Mem = Mem->next()) {
       ++NrMemUnits;
       Allocated += reinterpret_cast<uint64_t>(Mem->pos()) -
-                    reinterpret_cast<uint64_t>(Mem);
+                   reinterpret_cast<uint64_t>(Mem);
     }
     auto Valid = validate(Root);
   }
diff --git a/compiler-rt/lib/profile/InstrProfilingContextual.h b/compiler-rt/lib/profile/InstrProfilingContextual.h
index 7525a32cc3..2b9e67565d 100644
--- a/compiler-rt/lib/profile/InstrProfilingContextual.h
+++ b/compiler-rt/lib/profile/InstrProfilingContextual.h
@@ -28,7 +28,7 @@ public:
     return start() + (Pos - S);
   }
   Arena *next() const { return Next; }
-  const char *start() const { return const_cast<Arena*>(this)->start(); }
+  const char *start() const { return const_cast<Arena *>(this)->start(); }
   const char *pos() const { return start() + Pos; }
 
 private:
@@ -87,16 +87,11 @@ public:
 
   void reset();
 
-  void onEntry() {
-    ++counters()[0];
-  }
+  void onEntry() { ++counters()[0]; }
 
-  uint64_t entrycount() const {
-    return counters()[0];
-  }
+  uint64_t entrycount() const { return counters()[0]; }
 };
 
-
 // Exposed for test. Constructed and zero-initialized by LLVM. Implicitly,
 // LLVM must know the shape of this.
 struct ContextRoot {
@@ -107,7 +102,7 @@ struct ContextRoot {
   ::__sanitizer::StaticSpinMutex Taken;
 };
 
-inline bool isScratch(const ContextNode* Ctx) {
+inline bool isScratch(const ContextNode *Ctx) {
   return (reinterpret_cast<uint64_t>(Ctx) & 1);
 }
 
@@ -124,13 +119,13 @@ extern __thread __profile::ContextRoot
 
 COMPILER_RT_VISIBILITY __profile::ContextNode *
 __llvm_instrprof_start_context(__profile::ContextRoot *Root,
-                              __profile::GUID Guid, uint32_t Counters,
-                              uint32_t Callsites);
+                               __profile::GUID Guid, uint32_t Counters,
+                               uint32_t Callsites);
 COMPILER_RT_VISIBILITY void
 __llvm_instrprof_release_context(__profile::ContextRoot *Root);
 
 COMPILER_RT_VISIBILITY __profile::ContextNode *
 __llvm_instrprof_get_context(void *Callee, __profile::GUID Guid,
-                            uint32_t NrCounters, uint32_t NrCallsites);
+                             uint32_t NrCounters, uint32_t NrCallsites);
 }
 #endif
\ No newline at end of file
diff --git a/compiler-rt/lib/profile/tests/InstrProfilingContextualTest.cpp b/compiler-rt/lib/profile/tests/InstrProfilingContextualTest.cpp
index b53fbb5bda..ef350a4eec 100644
--- a/compiler-rt/lib/profile/tests/InstrProfilingContextualTest.cpp
+++ b/compiler-rt/lib/profile/tests/InstrProfilingContextualTest.cpp
@@ -6,11 +6,11 @@
 using namespace __profile;
 
 TEST(ArenaTest, Basic) {
-  Arena * A = Arena::allocate(1024);
+  Arena *A = Arena::allocate(1024);
   EXPECT_EQ(A->size(), 1024U);
   EXPECT_EQ(A->next(), nullptr);
 
-  auto *M1 = A->tryAllocate(1020); 
+  auto *M1 = A->tryAllocate(1020);
   EXPECT_NE(M1, nullptr);
   auto *M2 = A->tryAllocate(4);
   EXPECT_NE(M2, nullptr);
@@ -54,7 +54,7 @@ TEST(ContextTest, Callsite) {
   EXPECT_EQ(Subctx->callsites_size(), 1U);
   EXPECT_EQ(__llvm_instrprof_expected_callee[0], nullptr);
   EXPECT_EQ(__llvm_instrprof_callsite[0], nullptr);
-  
+
   EXPECT_EQ(Subctx->size(), sizeof(ContextNode) + 3 * sizeof(uint64_t) +
                                 1 * sizeof(ContextNode *));
   __llvm_instrprof_release_context(&Root);
@@ -81,14 +81,14 @@ TEST(ContextTest, ScratchDuringCollection) {
   EXPECT_TRUE(isScratch(Subctx));
   EXPECT_EQ(__llvm_instrprof_expected_callee[0], nullptr);
   EXPECT_EQ(__llvm_instrprof_callsite[0], nullptr);
-  
+
   int ThirdOpaqueValue = 0;
   __llvm_instrprof_expected_callee[1] = &ThirdOpaqueValue;
   __llvm_instrprof_callsite[1] = &Subctx->subContexts()[0];
 
   auto *Subctx2 = __llvm_instrprof_get_context(&ThirdOpaqueValue, 3, 0, 0);
   EXPECT_TRUE(isScratch(Subctx2));
-  
+
   __llvm_instrprof_release_context(&Root);
 }
 
diff --git a/llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h b/llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h
index 975775f18a..fc9afc9ca2 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h
@@ -27,7 +27,7 @@ private:
   Type *ContextNodeTy = nullptr;
   Type *ContextRootTy = nullptr;
 
-  DenseMap<const Function*, Constant*> ContextRootMap;
+  DenseMap<const Function *, Constant *> ContextRootMap;
   Function *StartCtx = nullptr;
   Function *GetCtx = nullptr;
   Function *ReleaseCtx = nullptr;
diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index 9bf8dae894..9ee6dae30c 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -291,9 +291,7 @@ Value *InstrProfIncrementInst::getStep() const {
   return ConstantInt::get(Type::getInt64Ty(Context), 1);
 }
 
-Value *InstrProfCallsite::getCallee() const {
-  return getArgOperand(4);
-}
+Value *InstrProfCallsite::getCallee() const { return getArgOperand(4); }
 
 std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
   unsigned NumOperands = arg_size();
diff --git a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
index 1212f7acf5..7685f2a00f 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
@@ -93,7 +93,7 @@ PreservedAnalyses PGOCtxProfLoweringPass::run(Module &M,
                          nullptr, "__llvm_instrprof_expected_callee");
   ExpectedCalleeTLS->setThreadLocal(true);
   ExpectedCalleeTLS->setVisibility(llvm::GlobalValue::HiddenVisibility);
-  
+
   for (auto &F : M)
     lowerFunction(F);
   return PreservedAnalyses::none();
@@ -111,7 +111,8 @@ void PGOCtxProfLoweringPass::lowerFunction(Function &F) {
       for (const auto &I : BB) {
         if (const auto *Incr = dyn_cast<InstrProfIncrementInst>(&I)) {
           if (!NrCounters)
-            NrCounters = static_cast<uint32_t>(Incr->getNumCounters()->getZExtValue());
+            NrCounters =
+                static_cast<uint32_t>(Incr->getNumCounters()->getZExtValue());
         } else if (const auto *CSIntr = dyn_cast<InstrProfCallsite>(&I)) {
           if (!NrCallsites)
             NrCallsites =
@@ -126,7 +127,7 @@ void PGOCtxProfLoweringPass::lowerFunction(Function &F) {
   Value *RealContext = nullptr;
 
   StructType *ThisContextType = nullptr;
-  Value* TheRootContext = nullptr;
+  Value *TheRootContext = nullptr;
   Value *ExpectedCalleeTLSAddr = nullptr;
   Value *CallsiteInfoTLSAddr = nullptr;
 
@@ -145,10 +146,9 @@ void PGOCtxProfLoweringPass::lowerFunction(Function &F) {
       auto Iter = ContextRootMap.find(&F);
       if (Iter != ContextRootMap.end()) {
         TheRootContext = Iter->second;
-        Context = Builder.CreateCall(
-            StartCtx,
-            {TheRootContext, Guid, Builder.getInt32(NrCounters),
-             Builder.getInt32(NrCallsites)});
+        Context = Builder.CreateCall(StartCtx, {TheRootContext, Guid,
+                                                Builder.getInt32(NrCounters),
+                                                Builder.getInt32(NrCallsites)});
       } else {
         Context =
             Builder.CreateCall(GetCtx, {&F, Guid, Builder.getInt32(NrCounters),
@@ -171,7 +171,7 @@ void PGOCtxProfLoweringPass::lowerFunction(Function &F) {
       break;
     }
   }
-  if(!Context) {
+  if (!Context) {
     dbgs() << "[instprof] Function doesn't have instrumentation, skipping "
            << F.getName() << "\n";
     return;
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 5d393c1fda..7a9d595dee 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -901,7 +901,7 @@ static void instrumentOneFunc(
   Visit([&TotalNrCallsites](auto *) { ++TotalNrCallsites; });
   uint32_t CallsiteIndex = 0;
 
-  Visit([&](auto *CB){
+  Visit([&](auto *CB) {
     IRBuilder<> Builder(CB);
     Builder.CreateCall(CSIntrinsic,
                        {Name, CFGHash, Builder.getInt32(TotalNrCallsites),

``````````

</details>


https://github.com/llvm/llvm-project/pull/86036


More information about the llvm-commits mailing list