[llvm] [Coroutines] Better optimization remarks for CoroAnnotationElide pass (PR #109352)

Yuxuan Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 16:28:55 PDT 2024


https://github.com/yuxuanchen1997 created https://github.com/llvm/llvm-project/pull/109352

Show why the elision did not happen for a certain coroutine call. Currently there might be two reasons. 
- the caller is not a presplit coroutine. Generally it's because the caller is in the same SCC. 
- the call instruction is not marked safe_elide. 

>From 614a6f752ee1dedc551cceda2cee68ff77bd3ea3 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Thu, 19 Sep 2024 16:25:27 -0700
Subject: [PATCH] [Coroutines] Better optimization remarks for
 CoroAnnotationElide pass

---
 .../Coroutines/CoroAnnotationElide.cpp        | 74 +++++++++++--------
 1 file changed, 43 insertions(+), 31 deletions(-)

diff --git a/llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp b/llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp
index ed036f254a1cfb..5f19d600a983aa 100644
--- a/llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp
@@ -101,42 +101,54 @@ PreservedAnalyses CoroAnnotationElidePass::run(Function &F,
 
   Function *NewCallee =
       F.getParent()->getFunction((F.getName() + ".noalloc").str());
-  if (!NewCallee) {
-    return PreservedAnalyses::all();
-  }
-
-    auto FramePtrArgPosition = NewCallee->arg_size() - 1;
-    auto FrameSize =
-        NewCallee->getParamDereferenceableBytes(FramePtrArgPosition);
-    auto FrameAlign =
-        NewCallee->getParamAlign(FramePtrArgPosition).valueOrOne();
-
-    SmallVector<CallBase *, 4> Users;
-    for (auto *U : F.users()) {
-      if (auto *CB = dyn_cast<CallBase>(U)) {
-        if (CB->getCalledFunction() == &F)
-          Users.push_back(CB);
-      }
-    }
 
-    auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
+  if (!NewCallee)
+    return PreservedAnalyses::all();
 
-    for (auto *CB : Users) {
-      auto *Caller = CB->getFunction();
-      if (Caller && Caller->isPresplitCoroutine() &&
-          CB->hasFnAttr(llvm::Attribute::CoroElideSafe)) {
+  auto FramePtrArgPosition = NewCallee->arg_size() - 1;
+  auto FrameSize = NewCallee->getParamDereferenceableBytes(FramePtrArgPosition);
+  auto FrameAlign = NewCallee->getParamAlign(FramePtrArgPosition).valueOrOne();
 
-        processCall(CB, Caller, NewCallee, FrameSize, FrameAlign);
+  SmallVector<CallBase *, 4> Users;
+  for (auto *U : F.users()) {
+    if (auto *CB = dyn_cast<CallBase>(U)) {
+      if (CB->getCalledFunction() == &F)
+        Users.push_back(CB);
+    }
+  }
 
-        ORE.emit([&]() {
-          return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
-                 << "'" << ore::NV("callee", F.getName()) << "' elided in '"
-                 << ore::NV("caller", Caller->getName());
-        });
-        FAM.invalidate(*Caller, PreservedAnalyses::none());
-        Changed = true;
-      }
+  auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
+
+  for (auto *CB : Users) {
+    auto *Caller = CB->getFunction();
+    if (!Caller)
+      continue;
+
+    bool IsCallerPresplitCoroutine = Caller->isPresplitCoroutine();
+    bool HasAttr = CB->hasFnAttr(llvm::Attribute::CoroElideSafe);
+    if (IsCallerPresplitCoroutine && HasAttr) {
+      processCall(CB, Caller, NewCallee, FrameSize, FrameAlign);
+
+      ORE.emit([&]() {
+        return OptimizationRemark(DEBUG_TYPE, "CoroAnnotationElide", Caller)
+               << "'" << ore::NV("callee", F.getName()) << "' elided in '"
+               << ore::NV("caller", Caller->getName()) << "'";
+      });
+
+      FAM.invalidate(*Caller, PreservedAnalyses::none());
+      Changed = true;
+    } else {
+      ORE.emit([&]() {
+        return OptimizationRemarkMissed(DEBUG_TYPE, "CoroAnnotationElide",
+                                        Caller)
+               << "'" << ore::NV("callee", F.getName()) << "' not elided in '"
+               << ore::NV("caller", Caller->getName()) << "' (caller_presplit="
+               << ore::NV("caller_presplit", IsCallerPresplitCoroutine)
+               << ", elide_safe_attr=" << ore::NV("elide_safe_attr", HasAttr)
+               << ")";
+      });
     }
+  }
 
   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
 }



More information about the llvm-commits mailing list