[PATCH] D40891: Fix accidentally quadratic time in BlockFrequencyInfoImpl::propagateMassToSuccessors

Andrew Scheidecker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 05:03:56 PST 2017


AndrewScheidecker created this revision.
Herald added subscribers: sunfish, aheejin, dschuff, jfb.

Pass the successor-iterator to getEdgeProbability so that it doesn't need to do a linear search of the successor list to calculate the index. This reduces an operation that was O(n^2) time to O(n) time, where n is the number of successors to the basic block.

In my WebAssembly VM that uses LLVM to generate machine code, I was running into this on a test case with a large switch from the WebAssembly reference interpreter test suite: https://github.com/WebAssembly/spec/blob/master/test/core/br_table.wast#L110

To find the bottleneck, I increased the number of cases in the switch statement further, which caused it to spend ~13 seconds in LLVM code generation. This optimization reduced the code generation time to tens of milliseconds.


Repository:
  rL LLVM

https://reviews.llvm.org/D40891

Files:
  include/llvm/Analysis/BlockFrequencyInfoImpl.h


Index: include/llvm/Analysis/BlockFrequencyInfoImpl.h
===================================================================
--- include/llvm/Analysis/BlockFrequencyInfoImpl.h
+++ include/llvm/Analysis/BlockFrequencyInfoImpl.h
@@ -1314,11 +1314,14 @@
       return false;
   } else {
     const BlockT *BB = getBlock(Node);
-    for (const auto Succ : children<const BlockT *>(BB))
-      if (!addToDist(Dist, OuterLoop, Node, getNode(Succ),
-                     getWeightFromBranchProb(BPI->getEdgeProbability(BB, Succ))))
-        // Irreducible backedge.
-        return false;
+    for (auto SuccI = GraphTraits<const BlockT *>::child_begin(BB),
+              SuccE = GraphTraits<const BlockT *>::child_end(BB);
+         SuccI != SuccE;
+         ++SuccI)
+      if (!addToDist(Dist, OuterLoop, Node, getNode(*SuccI),
+                     getWeightFromBranchProb(BPI->getEdgeProbability(BB, SuccI))))
+          // Irreducible backedge.
+        return false;
   }
 
   // Distribute mass to successors, saving exit and backedge data in the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40891.125707.patch
Type: text/x-patch
Size: 1046 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171206/83dd7997/attachment.bin>


More information about the llvm-commits mailing list