[llvm] 803dd6f - [BFI] Add a debug check for unknown block queries.

Hiroshi Yamauchi via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 4 10:05:54 PST 2020


Author: Hiroshi Yamauchi
Date: 2020-02-04T10:05:28-08:00
New Revision: 803dd6fe6bb493e34a8747dc286a88aa05f353e1

URL: https://github.com/llvm/llvm-project/commit/803dd6fe6bb493e34a8747dc286a88aa05f353e1
DIFF: https://github.com/llvm/llvm-project/commit/803dd6fe6bb493e34a8747dc286a88aa05f353e1.diff

LOG: [BFI] Add a debug check for unknown block queries.

Summary:
Add a debug check for frequency queries for unknown blocks (typically blocks
that are created after BFI is computed but their frequencies are not
communicated to BFI.)

This is useful for detecting and debugging missed BFI updates.

This is debug build only and disabled behind a flag.

Reviewers: davidxl

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D73920

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
    llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h b/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
index 1b8832a8e1c9..4d8059145f4c 100644
--- a/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
+++ b/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
@@ -26,6 +26,7 @@
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/BranchProbability.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/DOTGraphTraits.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -46,6 +47,8 @@
 
 #define DEBUG_TYPE "block-freq"
 
+extern llvm::cl::opt<bool> CheckBFIUnknownBlockQueries;
+
 namespace llvm {
 
 class BranchProbabilityInfo;
@@ -1043,6 +1046,15 @@ void BlockFrequencyInfoImpl<BT>::calculate(const FunctionT &F,
   computeMassInFunction();
   unwrapLoops();
   finalizeMetrics();
+
+  if (CheckBFIUnknownBlockQueries) {
+    // To detect BFI queries for unknown blocks, add entries for unreachable
+    // blocks, if any. This is to distinguish between known/existing unreachable
+    // blocks and unknown blocks.
+    for (const BlockT &BB : F)
+      if (!Nodes.count(&BB))
+        setBlockFreq(&BB, 0);
+  }
 }
 
 template <class BT>

diff  --git a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
index 0db6dd04a7e8..e4fda2472b3a 100644
--- a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
+++ b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
@@ -40,6 +40,12 @@ using namespace llvm::bfi_detail;
 
 #define DEBUG_TYPE "block-freq"
 
+cl::opt<bool> CheckBFIUnknownBlockQueries(
+    "check-bfi-unknown-block-queries",
+    cl::init(false), cl::Hidden,
+    cl::desc("Check if block frequency is queried for an unknown block "
+             "for debugging missed BFI updates"));
+
 ScaledNumber<uint64_t> BlockMass::toScaled() const {
   if (isFull())
     return ScaledNumber<uint64_t>(1, 0);
@@ -550,8 +556,17 @@ void BlockFrequencyInfoImplBase::finalizeMetrics() {
 
 BlockFrequency
 BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const {
-  if (!Node.isValid())
+  if (!Node.isValid()) {
+#ifndef NDEBUG
+    if (CheckBFIUnknownBlockQueries) {
+      SmallString<256> Msg;
+      raw_svector_ostream OS(Msg);
+      OS << "*** Detected BFI query for unknown block " << getBlockName(Node);
+      report_fatal_error(OS.str());
+    }
+#endif
     return 0;
+  }
   return Freqs[Node.Index].Integer;
 }
 


        


More information about the llvm-commits mailing list