[PATCH] D140926: [ConstraintElim] Add option to limit number of rows tracked in system.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 3 14:31:03 PST 2023


fhahn created this revision.
fhahn added reviewers: nikic, zjaffal, fcloutier, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

Once the constraint system grows too large in terms of number of rows,
queries can become very slow. This patch adds a new option to limit the
number of rows tracked.

The python script below can be used to generate worst-case IR with a
chain of conditional branches with N branches.

With this limit, we get the following runtimes:

- python3 generate.py 100:   0.1s
- python3 generate.py 1000:  2s
- python3 generate.py 10000: 4s

Without the limit, the case with 1000 chained conditions takes 20+
seconds.

generate.py:

  import sys
  
  N = int(sys.argv[1])
  
  args = []
  checks = []
  
  for i in range(0, N):
      args.append('i32 %l{}'.format(i))
      checks.append("""
  bb{0}:
    %c{0} = icmp uge i32 %l{0}, 100
    br i1 %c{0}, label %bb{1}, label %exit
  """.format(i, i+1))
  
  print("""
  define i1 @foo({0}) {{
  {1}
  
  bb{2}:
    %c{2} = icmp uge i32 %l0, 100
    ret i1 %c{2}
  
  exit:
    ret i1 false
  }}
  """.format(' ,'.join(args), '\n'.join(checks), N))


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140926

Files:
  llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
  llvm/test/Transforms/ConstraintElimination/max-row-limit.ll


Index: llvm/test/Transforms/ConstraintElimination/max-row-limit.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/ConstraintElimination/max-row-limit.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck --check-prefixes=COMMON,SIMP %s
+; RUN: opt -passes=constraint-elimination -constraint-elimination-max-rows=4 -S %s | FileCheck --check-prefixes=COMMON,SIMP %s
+; RUN: opt -passes=constraint-elimination -constraint-elimination-max-rows=3 -S %s | FileCheck --check-prefixes=COMMON,NOSIMP %s
+
+
+define i1 @test_max_row_limit(i32 %l0, i32 %l1, i32 %l2, i32 %l3, i32 %l4) {
+; COMMON-LABEL: @test_max_row_limit(
+; COMMON-NEXT:  bb0:
+; COMMON-NEXT:    [[C0:%.*]] = icmp uge i32 [[L0:%.*]], 100
+; COMMON-NEXT:    br i1 [[C0]], label [[BB1:%.*]], label [[EXIT:%.*]]
+; COMMON:       bb1:
+; COMMON-NEXT:    [[C1:%.*]] = icmp uge i32 [[L1:%.*]], 100
+; COMMON-NEXT:    br i1 [[C1]], label [[BB2:%.*]], label [[EXIT]]
+; COMMON:       bb2:
+; COMMON-NEXT:    [[C2:%.*]] = icmp uge i32 [[L2:%.*]], 100
+; COMMON-NEXT:    br i1 [[C2]], label [[BB3:%.*]], label [[EXIT]]
+; COMMON:       bb3:
+; COMMON-NEXT:    [[C3:%.*]] = icmp uge i32 [[L3:%.*]], 100
+; COMMON-NEXT:    br i1 [[C3]], label [[BB4:%.*]], label [[EXIT]]
+; COMMON:       bb4:
+; COMMON-NEXT:    [[C4:%.*]] = icmp uge i32 [[L4:%.*]], 100
+; COMMON-NEXT:    br i1 [[C4]], label [[BB5:%.*]], label [[EXIT]]
+; COMMON:       bb5:
+; COMMON-NEXT:    [[C5:%.*]] = icmp uge i32 [[L4]], 100
+; SIMP-NEXT:      ret i1 true
+; NOSIMP-NEXT:    ret i1 [[C5]]
+; COMMON:       exit:
+; COMMON-NEXT:    ret i1 false
+;
+bb0:
+  %c0 = icmp uge i32 %l0, 100
+  br i1 %c0, label %bb1, label %exit
+
+bb1:
+  %c1 = icmp uge i32 %l1, 100
+  br i1 %c1, label %bb2, label %exit
+
+bb2:
+  %c2 = icmp uge i32 %l2, 100
+  br i1 %c2, label %bb3, label %exit
+
+bb3:
+  %c3 = icmp uge i32 %l3, 100
+  br i1 %c3, label %bb4, label %exit
+
+bb4:
+  %c4 = icmp uge i32 %l4, 100
+  br i1 %c4, label %bb5, label %exit
+
+bb5:
+  %c5 = icmp uge i32 %l4, 100
+  ret i1 %c5
+
+exit:
+  ret i1 false
+}
Index: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -28,6 +28,7 @@
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/DebugCounter.h"
 #include "llvm/Support/MathExtras.h"
@@ -45,6 +46,10 @@
 DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
               "Controls which conditions are eliminated");
 
+static cl::opt<unsigned>
+    MaxRows("constraint-elimination-max-rows", cl::init(500), cl::Hidden,
+            cl::desc("Maximum number of rows to keep in constraint system"));
+
 static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
 static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
 
@@ -1019,6 +1024,13 @@
     Value *Cmp = CB.Inst;
     match(Cmp, m_Intrinsic<Intrinsic::assume>(m_Value(Cmp)));
     if (match(Cmp, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
+      if (Info.getCS(CmpInst::isSigned(Pred)).size() > MaxRows) {
+        LLVM_DEBUG(
+            dbgs()
+            << "Skip adding constraint because system has too many rows.\n");
+        continue;
+      }
+
       // Use the inverse predicate if required.
       if (CB.Not)
         Pred = CmpInst::getInversePredicate(Pred);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140926.486090.patch
Type: text/x-patch
Size: 3684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230103/6b252cf5/attachment.bin>


More information about the llvm-commits mailing list