Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Coroutines] Better optimization remarks for CoroAnnotationElide pass #109352

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

yuxuanchen1997
Copy link
Member

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.

@yuxuanchen1997 yuxuanchen1997 force-pushed the users/yuxuanchen1997/coro-transform-remarks branch from 614a6f7 to f25a43f Compare September 19, 2024 23:28
@yuxuanchen1997 yuxuanchen1997 marked this pull request as ready for review September 19, 2024 23:29
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 19, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Yuxuan Chen (yuxuanchen1997)

Changes

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.

Full diff: https://github.com/llvm/llvm-project/pull/109352.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp (+43-31)
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();
 }

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 19, 2024

@llvm/pr-subscribers-coroutines

Author: Yuxuan Chen (yuxuanchen1997)

Changes

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.

Full diff: https://github.com/llvm/llvm-project/pull/109352.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Coroutines/CoroAnnotationElide.cpp (+43-31)
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();
 }

@yuxuanchen1997 yuxuanchen1997 merged commit b5cdb03 into main Sep 20, 2024
12 checks passed
@yuxuanchen1997 yuxuanchen1997 deleted the users/yuxuanchen1997/coro-transform-remarks branch September 20, 2024 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants