[llvm] [Coroutines] Improve dump of BB label to avoid str copies (PR #112374)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 15 08:06:13 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-coroutines

Author: Tyler Nowicki (TylerNowicki)

<details>
<summary>Changes</summary>

* This avoids the need to call printAsOperand that requires use of an ostream and thus avoids a str copy.
* ModuleSlotTracker is used to get a BB # for BB's without names when dumping SuspendCrossingInfo and materialization info.
* getBasicBlockLabel() is changed to dumpBasicBlockLabel() that directly prints the label to dbgs()
* The label corresponds with the print-before BB #s.
* This change does not require any additional arguments to be added to dump() methods, at least those that currently do not require any args.

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


3 Files Affected:

- (modified) llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h (+4-1) 
- (modified) llvm/lib/Transforms/Coroutines/MaterializationUtils.cpp (+16-9) 
- (modified) llvm/lib/Transforms/Coroutines/SuspendCrossingInfo.cpp (+23-16) 


``````````diff
diff --git a/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h b/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h
index 49cae6dde47e54..88cbf88acc4cd0 100644
--- a/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h
+++ b/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h
@@ -25,6 +25,8 @@
 
 namespace llvm {
 
+class ModuleSlotTracker;
+
 // Provides two way mapping between the blocks and numbers.
 class BlockToIndexMapping {
   SmallVector<BasicBlock *, 32> V;
@@ -96,7 +98,8 @@ class SuspendCrossingInfo {
   // Print order is in RPO
   void dump() const;
   void dump(StringRef Label, BitVector const &BV,
-            const ReversePostOrderTraversal<Function *> &RPOT) const;
+            const ReversePostOrderTraversal<Function *> &RPOT,
+            ModuleSlotTracker &MST) const;
 #endif
 
   SuspendCrossingInfo(Function &F,
diff --git a/llvm/lib/Transforms/Coroutines/MaterializationUtils.cpp b/llvm/lib/Transforms/Coroutines/MaterializationUtils.cpp
index c3ea0977d42117..6327cea64c0de6 100644
--- a/llvm/lib/Transforms/Coroutines/MaterializationUtils.cpp
+++ b/llvm/lib/Transforms/Coroutines/MaterializationUtils.cpp
@@ -15,6 +15,7 @@
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instruction.h"
+#include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/Transforms/Coroutines/SpillUtils.h"
 #include <deque>
 
@@ -104,19 +105,25 @@ struct RematGraph {
   }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
-  static std::string getBasicBlockLabel(const BasicBlock *BB) {
-    if (BB->hasName())
-      return BB->getName().str();
-
-    std::string S;
-    raw_string_ostream OS(S);
-    BB->printAsOperand(OS, false);
-    return OS.str().substr(1);
+  static void dumpBasicBlockLabel(const BasicBlock *BB,
+                                  ModuleSlotTracker &MST) {
+    if (BB->hasName()) {
+      dbgs() << BB->getName();
+      return;
+    }
+
+    dbgs() << MST.getLocalSlot(BB);
   }
 
   void dump() const {
+    BasicBlock *BB = EntryNode->Node->getParent();
+    Function *F = BB->getParent();
+
+    ModuleSlotTracker MST(F->getParent());
+    MST.incorporateFunction(*F);
+
     dbgs() << "Entry (";
-    dbgs() << getBasicBlockLabel(EntryNode->Node->getParent());
+    dumpBasicBlockLabel(BB, MST);
     dbgs() << ") : " << *EntryNode->Node << "\n";
     for (auto &E : Remats) {
       dbgs() << *(E.first) << "\n";
diff --git a/llvm/lib/Transforms/Coroutines/SuspendCrossingInfo.cpp b/llvm/lib/Transforms/Coroutines/SuspendCrossingInfo.cpp
index f18f23306befb4..c9bb3395a99491 100644
--- a/llvm/lib/Transforms/Coroutines/SuspendCrossingInfo.cpp
+++ b/llvm/lib/Transforms/Coroutines/SuspendCrossingInfo.cpp
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Coroutines/SuspendCrossingInfo.h"
+#include "llvm/IR/ModuleSlotTracker.h"
 
 // The "coro-suspend-crossing" flag is very noisy. There is another debug type,
 // "coro-frame", which results in leaner debug spew.
@@ -20,24 +21,26 @@
 
 namespace llvm {
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
-static std::string getBasicBlockLabel(const BasicBlock *BB) {
-  if (BB->hasName())
-    return BB->getName().str();
-
-  std::string S;
-  raw_string_ostream OS(S);
-  BB->printAsOperand(OS, false);
-  return OS.str().substr(1);
+static void dumpBasicBlockLabel(const BasicBlock *BB, ModuleSlotTracker &MST) {
+  if (BB->hasName()) {
+    dbgs() << BB->getName();
+    return;
+  }
+
+  dbgs() << MST.getLocalSlot(BB);
 }
 
-LLVM_DUMP_METHOD void SuspendCrossingInfo::dump(
-    StringRef Label, BitVector const &BV,
-    const ReversePostOrderTraversal<Function *> &RPOT) const {
+LLVM_DUMP_METHOD void
+SuspendCrossingInfo::dump(StringRef Label, BitVector const &BV,
+                          const ReversePostOrderTraversal<Function *> &RPOT,
+                          ModuleSlotTracker &MST) const {
   dbgs() << Label << ":";
   for (const BasicBlock *BB : RPOT) {
     auto BBNo = Mapping.blockToIndex(BB);
-    if (BV[BBNo])
-      dbgs() << " " << getBasicBlockLabel(BB);
+    if (BV[BBNo]) {
+      dbgs() << " ";
+      dumpBasicBlockLabel(BB, MST);
+    }
   }
   dbgs() << "\n";
 }
@@ -49,12 +52,16 @@ LLVM_DUMP_METHOD void SuspendCrossingInfo::dump() const {
   BasicBlock *const B = Mapping.indexToBlock(0);
   Function *F = B->getParent();
 
+  ModuleSlotTracker MST(F->getParent());
+  MST.incorporateFunction(*F);
+
   ReversePostOrderTraversal<Function *> RPOT(F);
   for (const BasicBlock *BB : RPOT) {
     auto BBNo = Mapping.blockToIndex(BB);
-    dbgs() << getBasicBlockLabel(BB) << ":\n";
-    dump("   Consumes", Block[BBNo].Consumes, RPOT);
-    dump("      Kills", Block[BBNo].Kills, RPOT);
+    dumpBasicBlockLabel(BB, MST);
+    dbgs() << ":\n";
+    dump("   Consumes", Block[BBNo].Consumes, RPOT, MST);
+    dump("      Kills", Block[BBNo].Kills, RPOT, MST);
   }
   dbgs() << "\n";
 }

``````````

</details>


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


More information about the llvm-commits mailing list