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

[clang][dataflow] HTML logger: Mark iterations that have converged. #68204

Merged
merged 1 commit into from
Oct 4, 2023

Conversation

martinboehme
Copy link
Contributor

@martinboehme martinboehme commented Oct 4, 2023

I've eliminated the logText("Block converged") call entirely because

a) These logs are associated with an individual CFGElement, while convergence
should be associated with a block, and

b) The log message was being associated with the wrong block: recordState()
dumps all of the pending log messages, but blockConverged() is called after
the last recordState() call for a given block, so that the "Block
converged" log message was being associated with the first element of the
next block to be processed.

Example:

image

I've eliminated the `logText("Block converged")` call entirely because

a) These logs are associated with an individual `CFGElement`, while convergence
   should be associated with a block, and

b) The log message was being associated with the wrong block: `recordState()`
   dumps all of the pending log messages, but `blockConverged()` is called after
   the last `recordState()` call for a given block, so that the "Block
   converged" log message was being associated with the first element of the
   _next_ block to be processed.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:analysis labels Oct 4, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 4, 2023

@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang

Changes

I've eliminated the logText("Block converged") call entirely because

a) These logs are associated with an individual CFGElement, while convergence
should be associated with a block, and

b) The log message was being associated with the wrong block: recordState()
dumps all of the pending log messages, but blockConverged() is called after
the last recordState() call for a given block, so that the "Block
converged" log message was being associated with the first element of the
next block to be processed.

Example:

image


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

2 Files Affected:

  • (modified) clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp (+12-8)
  • (modified) clang/lib/Analysis/FlowSensitive/HTMLLogger.html (+2)
diff --git a/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp b/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
index 47915958750d1f4..8aef1d6f46089d2 100644
--- a/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
+++ b/clang/lib/Analysis/FlowSensitive/HTMLLogger.cpp
@@ -150,6 +150,7 @@ class HTMLLogger : public Logger {
     const CFGBlock *Block;
     unsigned Iter;
     bool PostVisit;
+    bool Converged;
   };
 
   StreamFactory Streams;
@@ -159,8 +160,8 @@ class HTMLLogger : public Logger {
   const ControlFlowContext *CFG;
   // Timeline of iterations of CFG block visitation.
   std::vector<Iteration> Iters;
-  // Number of times each CFG block has been seen.
-  llvm::DenseMap<const CFGBlock *, llvm::SmallVector<Iteration>> BlockIters;
+  // Indexes  in `Iters` of the iterations for each block.
+  llvm::DenseMap<const CFGBlock *, llvm::SmallVector<size_t>> BlockIters;
   // The messages logged in the current context but not yet written.
   std::string ContextLogs;
   // The number of elements we have visited within the current CFG block.
@@ -207,6 +208,7 @@ class HTMLLogger : public Logger {
           JOS->attribute("block", blockID(E.Block->getBlockID()));
           JOS->attribute("iter", E.Iter);
           JOS->attribute("post_visit", E.PostVisit);
+          JOS->attribute("converged", E.Converged);
         });
       }
     });
@@ -222,10 +224,10 @@ class HTMLLogger : public Logger {
   }
 
   void enterBlock(const CFGBlock &B, bool PostVisit) override {
-    llvm::SmallVector<Iteration> &BIter = BlockIters[&B];
+    llvm::SmallVector<size_t> &BIter = BlockIters[&B];
     unsigned IterNum = BIter.size() + 1;
-    BIter.push_back({&B, IterNum, PostVisit});
-    Iters.push_back({&B, IterNum, PostVisit});
+    BIter.push_back(Iters.size());
+    Iters.push_back({&B, IterNum, PostVisit, /*Converged=*/false});
     ElementIndex = 0;
   }
   void enterElement(const CFGElement &E) override {
@@ -290,7 +292,7 @@ class HTMLLogger : public Logger {
       }
     });
   }
-  void blockConverged() override { logText("Block converged"); }
+  void blockConverged() override { Iters.back().Converged = true; }
 
   void logText(llvm::StringRef S) override {
     ContextLogs.append(S.begin(), S.end());
@@ -301,13 +303,15 @@ class HTMLLogger : public Logger {
   // Write the CFG block details.
   // Currently this is just the list of elements in execution order.
   // FIXME: an AST dump would be a useful view, too.
-  void writeBlock(const CFGBlock &B, llvm::ArrayRef<Iteration> ItersForB) {
+  void writeBlock(const CFGBlock &B, llvm::ArrayRef<size_t> ItersForB) {
     JOS->attributeObject(blockID(B.getBlockID()), [&] {
       JOS->attributeArray("iters", [&] {
-        for (const auto &Iter : ItersForB) {
+        for (size_t IterIdx : ItersForB) {
+          const Iteration &Iter = Iters[IterIdx];
           JOS->object([&] {
             JOS->attribute("iter", Iter.Iter);
             JOS->attribute("post_visit", Iter.PostVisit);
+            JOS->attribute("converged", Iter.Converged);
           });
         }
       });
diff --git a/clang/lib/Analysis/FlowSensitive/HTMLLogger.html b/clang/lib/Analysis/FlowSensitive/HTMLLogger.html
index 6d866d57e144866..ec9d74c71d35d98 100644
--- a/clang/lib/Analysis/FlowSensitive/HTMLLogger.html
+++ b/clang/lib/Analysis/FlowSensitive/HTMLLogger.html
@@ -45,6 +45,7 @@
     {{entry.block}}
     <template data-if="entry.post_visit">(post-visit)</template>
     <template data-if="!entry.post_visit">({{entry.iter}})</template>
+    <template data-if="entry.converged"> &#x2192;&#x7c;<!--Rightwards arrow, vertical line--></template>
   </div>
 </template>
 </section>
@@ -62,6 +63,7 @@
     <a class="chooser {{selection.bb}}:{{iter.iter}}" data-iter="{{selection.bb}}:{{iter.iter}}">
       <template data-if="iter.post_visit">Post-visit</template>
       <template data-if="!iter.post_visit">Iteration {{iter.iter}}</template>
+      <template data-if="iter.converged"> &#x2192;&#x7c;<!--Rightwards arrow, vertical line--></template>
     </a>
   </template>
 </div>

Copy link
Collaborator

@sam-mccall sam-mccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants