[PATCH] D53282: [SCEV] Limit AddRec "simplifications" to avoid combinatorial explosions

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 15 04:32:42 PDT 2018


mkazantsev created this revision.
mkazantsev added reviewers: sanjoy, kparzysz, rtereshin, reames.
Herald added a subscriber: javed.absar.

SCEV's transform that turns `{A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>` into
a single AddRec of size `2n+`` with complex combinatorial coefficients can easily
trigger exponential growth of the SCEV (in case if nothing gets folded and simplified).
We tried to restrain this transform using the option `scalar-evolution-max-add-rec-size`,
but its default value seems to be insufficiently small: the test attached to this patch
with default value of this option `16` has a SCEV of >3M symbols (when printed out).

This patch reduces the simplification limit. It is not a cure to combinatorial
explosions, but at least it reduces this corner case to something more or less
reasonable.


https://reviews.llvm.org/D53282

Files:
  lib/Analysis/ScalarEvolution.cpp
  test/Analysis/ScalarEvolution/binomial-explision.ll


Index: test/Analysis/ScalarEvolution/binomial-explision.ll
===================================================================
--- test/Analysis/ScalarEvolution/binomial-explision.ll
+++ test/Analysis/ScalarEvolution/binomial-explision.ll
@@ -0,0 +1,47 @@
+; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1"
+
+; Check that we don't have unreasonably huge SCEVs and in particular only a
+; reasonable amount of AddRecs in the notation of %tmp19. If we "simplify" SCEVs
+; too aggressively, we may end up with huge nested expressions.
+define void @test() {
+
+; CHECK: %tmp19 = mul i32 %tmp17, %tmp18
+; CHECK: ((((
+; CHECK-NOT: (((((
+; CHECK: %tmp20 = add i32 %tmp19, undef
+
+bb:
+  br label %bb1
+
+bb1:                                              ; preds = %bb3, %bb
+  %tmp = phi i64 [ undef, %bb ], [ %tmp22, %bb3 ]
+  %tmp2 = phi i32 [ undef, %bb ], [ %tmp4, %bb3 ]
+  br label %bb5
+
+bb3:                                              ; preds = %bb5
+  %tmp4 = add i32 %tmp2, undef
+  br label %bb1
+
+bb5:                                              ; preds = %bb5, %bb1
+  %tmp6 = phi i32 [ %tmp23, %bb5 ], [ %tmp2, %bb1 ]
+  %tmp7 = sub i32 -119, %tmp6
+  %tmp8 = mul i32 %tmp7, undef
+  %tmp9 = sub i32 -120, %tmp6
+  %tmp10 = mul i32 %tmp8, %tmp9
+  %tmp11 = mul i32 undef, %tmp10
+  %tmp12 = sub i32 -121, %tmp6
+  %tmp13 = mul i32 %tmp10, %tmp12
+  %tmp14 = mul i32 %tmp11, %tmp13
+  %tmp15 = sub i32 -122, %tmp6
+  %tmp16 = mul i32 %tmp13, %tmp15
+  %tmp17 = mul i32 %tmp14, %tmp16
+  %tmp18 = mul i32 %tmp16, undef
+  %tmp19 = mul i32 %tmp17, %tmp18
+  %tmp20 = add i32 %tmp19, undef
+  %tmp21 = sext i32 %tmp20 to i64
+  %tmp22 = add i64 undef, %tmp21
+  %tmp23 = add i32 %tmp6, 7
+  br i1 undef, label %bb5, label %bb3
+}
Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -204,7 +204,7 @@
 static cl::opt<unsigned>
     MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
                   cl::desc("Max coefficients in AddRec during evolving"),
-                  cl::init(16));
+                  cl::init(8));
 
 //===----------------------------------------------------------------------===//
 //                           SCEV class definitions


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53282.169678.patch
Type: text/x-patch
Size: 2395 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181015/268288c9/attachment.bin>


More information about the llvm-commits mailing list