[PATCH] D80029: Fix quadratic LexicalScopes::constructScopeNest(...)

Jan-Willem Maessen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 15 13:03:54 PDT 2020


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

NO FUNCTIONAL CHANGE.  Algorithmic complexity fix.

Our team at Facebook is currently constructing a compile backended by
LLVM.  We sometimes have functions with large numbers of sibling basic
blocks (usually with an error path exit from each one).  This was
triggering the qudratic behavior in this function - after visiting
each child llvm would re-scan the parent from the beginning again.  We
modify the work stack to record the next index to be worked on
alongside the pointer.  This avoids the need to linearly search for
the next unfinished child.

Test Plan:
cmake    -G Ninja ...x86...  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;debuginfo-tests;lld;opt" ...
ninja check


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80029

Files:
  llvm/lib/CodeGen/LexicalScopes.cpp


Index: llvm/lib/CodeGen/LexicalScopes.cpp
===================================================================
--- llvm/lib/CodeGen/LexicalScopes.cpp
+++ llvm/lib/CodeGen/LexicalScopes.cpp
@@ -232,21 +232,20 @@
 /// constructScopeNest
 void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
   assert(Scope && "Unable to calculate scope dominance graph!");
-  SmallVector<LexicalScope *, 4> WorkStack;
-  WorkStack.push_back(Scope);
+  SmallVector<std::pair<LexicalScope *, size_t>, 4> WorkStack;
+  WorkStack.push_back(std::make_pair(Scope, 0));
   unsigned Counter = 0;
   while (!WorkStack.empty()) {
-    LexicalScope *WS = WorkStack.back();
+    auto &p = WorkStack.back();
+    LexicalScope *WS = p.first;
+    size_t i = p.second++;
     const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
-    bool visitedChildren = false;
-    for (auto &ChildScope : Children)
-      if (!ChildScope->getDFSOut()) {
-        WorkStack.push_back(ChildScope);
-        visitedChildren = true;
-        ChildScope->setDFSIn(++Counter);
-        break;
-      }
-    if (!visitedChildren) {
+    if (i < Children.size()) {
+      auto &ChildScope = Children[i];
+      // Because of tree structure, !ChildScope->getDFSIn().
+      WorkStack.push_back(std::make_pair(ChildScope, 0));
+      ChildScope->setDFSIn(++Counter);
+    } else {
       WorkStack.pop_back();
       WS->setDFSOut(++Counter);
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80029.264320.patch
Type: text/x-patch
Size: 1419 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200515/90ee8adb/attachment.bin>


More information about the llvm-commits mailing list