[PATCH] D42129: [polly] [ScopInfo] Don't use isl_val_get_num_si.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 13:46:30 PST 2018


efriedma created this revision.
efriedma added reviewers: grosser, Meinersbur.
Herald added a reviewer: bollu.

isl_val_get_num_si crashes on overflow, so don't use it on arbitrary integers.  Instead, convert to an APInt and explicitly check for overflow.

Testcase only crashes on platforms where long is 32 bits because of the signature of isl_val_get_num_si; not sure if it's possible to write a testcase which crashes if long is 64 bits.

There are a few other places in polly which use isl_val_get_num_si; they probably need to be fixed as well. I don't think polly uses any of the other "long" isl APIs in an unsafe manner.


Repository:
  rPLO Polly

https://reviews.llvm.org/D42129

Files:
  lib/Analysis/ScopInfo.cpp
  test/ScopInfo/multidim_fold_constant_dim.ll


Index: test/ScopInfo/multidim_fold_constant_dim.ll
===================================================================
--- test/ScopInfo/multidim_fold_constant_dim.ll
+++ test/ScopInfo/multidim_fold_constant_dim.ll
@@ -36,6 +36,7 @@
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 
 %struct.com = type { double, double }
+%struct.com2 = type { [20000000000 x double] }
 
 define void @foo(i64 %n, %struct.com* %A) {
 entry:
@@ -84,10 +85,39 @@
   ret void
 }
 
-define i32 @main() {
+; CHECK:      Arrays {
+; CHECK-NEXT:     double MemRef_O[*][%n]; // Element size 8
+; CHECK-NEXT: }
+
+define void @foo_overflow(i64 %n, %struct.com2* nocapture %O) local_unnamed_addr #0 {
 entry:
-  %A = alloca [100 x [1000 x %struct.com]], align 16
-  %tmp = getelementptr inbounds [100 x [1000 x %struct.com]], [100 x [1000 x %struct.com]]* %A, i64 0, i64 0, i64 0
-  call void @foo(i64 1000, %struct.com* nonnull %tmp)
-  ret i32 0
+  br label %for.body
+
+for.cond.cleanup:                                 ; preds = %for.cond.cleanup3
+  ret void
+
+for.body:                                         ; preds = %for.cond.cleanup3, %entry
+  %i.024 = phi i64 [ 0, %entry ], [ %inc12, %for.cond.cleanup3 ]
+  %0 = mul nsw i64 %i.024, %n
+  %arrayidx = getelementptr inbounds %struct.com2, %struct.com2* %O, i64 %0
+  br label %for.body4
+
+for.cond.cleanup3:                                ; preds = %for.body4
+  %inc12 = add nuw nsw i64 %i.024, 1
+  %exitcond25 = icmp eq i64 %inc12, 100
+  br i1 %exitcond25, label %for.cond.cleanup, label %for.body
+
+for.body4:                                        ; preds = %for.body4, %for.body
+  %j.023 = phi i64 [ 0, %for.body ], [ %inc, %for.body4 ]
+  %arrayidx5 = getelementptr inbounds %struct.com2, %struct.com2* %arrayidx, i64 %j.023
+  %Real = getelementptr inbounds %struct.com2, %struct.com2* %arrayidx5, i64 0, i32 0
+  %arrayidx6 = getelementptr inbounds [20000000000 x double], [20000000000 x double]* %Real, i64 0, i64 1
+  %1 = load double, double* %arrayidx6, align 8
+  %arrayidx10 = getelementptr inbounds [20000000000 x double], [20000000000 x double]* %Real, i64 0, i64 0
+  %2 = load double, double* %arrayidx10, align 8
+  %add = fadd double %1, %2
+  store double %add, double* %arrayidx10, align 8
+  %inc = add nuw nsw i64 %j.023, 1
+  %exitcond = icmp eq i64 %inc, 1000
+  br i1 %exitcond, label %for.cond.cleanup3, label %for.body4
 }
Index: lib/Analysis/ScopInfo.cpp
===================================================================
--- lib/Analysis/ScopInfo.cpp
+++ lib/Analysis/ScopInfo.cpp
@@ -3428,9 +3428,13 @@
 
         int ValInt = 1;
 
-        if (isl_val_is_int(Val))
-          ValInt = isl_val_get_num_si(Val);
-        isl_val_free(Val);
+        if (isl_val_is_int(Val)) {
+          auto ValAPInt = APIntFromVal(Val);
+          if (ValAPInt.isSignedIntN(32))
+            ValInt = ValAPInt.getSExtValue();
+        } else {
+          isl_val_free(Val);
+        }
 
         Int.push_back(ValInt);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42129.130022.patch
Type: text/x-patch
Size: 2994 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180116/0d8d048d/attachment.bin>


More information about the llvm-commits mailing list