[PATCH] D84715: [FIX] Add check for empty body function

Wei Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 27 16:52:40 PDT 2020


weiwang created this revision.
Herald added subscribers: llvm-commits, dexonsmith, hiraditya.
Herald added a project: LLVM.

Missing check for empty body function causes several analyses to fail, including DomTree, LoopInfo, BPI, and BFI.
When function has empty body -- zero instruction and no entry block,  the call Function::getEntryBlock returns the sentinel node in BasicBlocks list.
The issue was first encountered as an assertion failure when a dead function with empty body was passed into ORE for remarks emit, and triggered various analyses stated above. Although empty body function is considered invalid when passing in as an IR input, it could exist internally when function is stripped away as a dead function during LTO.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84715

Files:
  llvm/include/llvm/Analysis/LoopInfoImpl.h
  llvm/include/llvm/Support/GenericDomTreeConstruction.h
  llvm/lib/Analysis/BlockFrequencyInfo.cpp
  llvm/lib/Analysis/BranchProbabilityInfo.cpp


Index: llvm/lib/Analysis/BranchProbabilityInfo.cpp
===================================================================
--- llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -1068,6 +1068,9 @@
                                       PostDominatorTree *PDT) {
   LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()
                     << " ----\n\n");
+  if (F.empty())
+    return;
+
   LastF = &F; // Store the last function we ran on for printing.
   assert(PostDominatedByUnreachable.empty());
   assert(PostDominatedByColdCall.empty());
Index: llvm/lib/Analysis/BlockFrequencyInfo.cpp
===================================================================
--- llvm/lib/Analysis/BlockFrequencyInfo.cpp
+++ llvm/lib/Analysis/BlockFrequencyInfo.cpp
@@ -184,6 +184,9 @@
 void BlockFrequencyInfo::calculate(const Function &F,
                                    const BranchProbabilityInfo &BPI,
                                    const LoopInfo &LI) {
+  if (F.empty())
+    return;
+
   if (!BFI)
     BFI.reset(new ImplType);
   BFI->calculate(F, BPI, LI);
Index: llvm/include/llvm/Support/GenericDomTreeConstruction.h
===================================================================
--- llvm/include/llvm/Support/GenericDomTreeConstruction.h
+++ llvm/include/llvm/Support/GenericDomTreeConstruction.h
@@ -42,6 +42,8 @@
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/IR/Function.h"
+#include "llvm/Support/Casting.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GenericDomTree.h"
 #include <queue>
@@ -528,6 +530,16 @@
     auto *Parent = DT.Parent;
     DT.reset();
     DT.Parent = Parent;
+
+    if constexpr (std::is_same<
+                      Function *,
+                      typename std::remove_const<
+                          typename DomTreeT::ParentPtr>::type>::value) {
+      if (auto *F = dyn_cast<Function>(DT.Parent))
+        if (F->empty())
+          return;
+    }
+
     SemiNCAInfo SNCA(nullptr);  // Since we are rebuilding the whole tree,
                                 // there's no point doing it incrementally.
 
Index: llvm/include/llvm/Analysis/LoopInfoImpl.h
===================================================================
--- llvm/include/llvm/Analysis/LoopInfoImpl.h
+++ llvm/include/llvm/Analysis/LoopInfoImpl.h
@@ -537,6 +537,9 @@
 void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
   // Postorder traversal of the dominator tree.
   const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
+  if (!DomRoot)
+    return;
+
   for (auto DomNode : post_order(DomRoot)) {
 
     BlockT *Header = DomNode->getBlock();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84715.281083.patch
Type: text/x-patch
Size: 2757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200727/11dc4187/attachment.bin>


More information about the llvm-commits mailing list