[PATCH] D51035: [TableGen] CodeGenDAGPatterns::GenerateVariants - basic caching of matching predicates

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 21 07:09:34 PDT 2018


RKSimon created this revision.
RKSimon added reviewers: kparzysz, sdardis, fhahn, rengolin, andreadb.

CodeGenDAGPatterns::GenerateVariants is a costly function in many tblgen commands (33.87% of the total runtime of x86 -gen-dag-isel), and due to the O(N^2) nature of the function, there are a lot of repeated comparisons of the pattern's vector<Predicate> (19.9% of the total runtime of x86 -gen-dag-isel).

This initial patch at least avoids repeating these comparisons for every Variant per pattern. I began investigating caching all the matches before entering the loop but hit issues with how best to store the data and how to update the cache as patterns were added - until thats solved, this is at least an improvement.

Also, its not clear to me if the inner loop over PatternsToMatch needs to compare against the extra patterns generated in the function or not - if not it should be possible to split these loops and reduce its quadratic nature. @kparzysz can you confirm please?

Saves around 15secs in debug builds of x86 -gen-dag-isel.


Repository:
  rL LLVM

https://reviews.llvm.org/D51035

Files:
  utils/TableGen/CodeGenDAGPatterns.cpp


Index: utils/TableGen/CodeGenDAGPatterns.cpp
===================================================================
--- utils/TableGen/CodeGenDAGPatterns.cpp
+++ utils/TableGen/CodeGenDAGPatterns.cpp
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CodeGenDAGPatterns.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallSet.h"
@@ -4477,6 +4478,13 @@
     LLVM_DEBUG(errs() << "FOUND VARIANTS OF: ";
                PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n");
 
+    // Cache matching predicates.
+    // TODO: Is it performant to pull this out of the loop entirely?
+    BitVector MatchedPredicates(PatternsToMatch.size(), false);
+    for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p)
+      MatchedPredicates[p] = (i == p) || (PatternsToMatch[i].getPredicates() ==
+                                          PatternsToMatch[p].getPredicates());
+
     for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
       TreePatternNodePtr Variant = Variants[v];
 
@@ -4487,8 +4495,7 @@
       bool AlreadyExists = false;
       for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
         // Skip if the top level predicates do not match.
-        if ((i != p) && (PatternsToMatch[i].getPredicates() !=
-                         PatternsToMatch[p].getPredicates()))
+        if (!MatchedPredicates[p])
           continue;
         // Check to see if this variant already exists.
         if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
@@ -4507,6 +4514,8 @@
           Variant, PatternsToMatch[i].getDstPatternShared(),
           PatternsToMatch[i].getDstRegs(),
           PatternsToMatch[i].getAddedComplexity(), Record::getNewUID()));
+      MatchedPredicates.resize(PatternsToMatch.size());
+      MatchedPredicates[PatternsToMatch.size() - 1] = true;
     }
 
     LLVM_DEBUG(errs() << "\n");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51035.161710.patch
Type: text/x-patch
Size: 2027 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180821/11d95394/attachment.bin>


More information about the llvm-commits mailing list