From 5ac94e9ebe7abc7664e27c8f6527343f13be1140 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sat, 28 Nov 2015 18:06:41 -0600
Subject: [PATCH] Try two strategies for finding base indices/lengths

---
 loopy/diagnostic.py   |  4 ++++
 loopy/isl_helpers.py  |  2 +-
 loopy/kernel/tools.py | 44 +++++++++++++++++++++++++++++++++++--------
 3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py
index 93cbd125d..2ea1bd40e 100644
--- a/loopy/diagnostic.py
+++ b/loopy/diagnostic.py
@@ -78,6 +78,10 @@ class AutomaticTestFailure(LoopyError):
     pass
 
 
+class StaticValueFindingError(LoopyError):
+    pass
+
+
 class DependencyTypeInferenceFailure(TypeInferenceFailure):
     def __init__(self, message, symbol):
         TypeInferenceFailure.__init__(self, message)
diff --git a/loopy/isl_helpers.py b/loopy/isl_helpers.py
index a7b6c4348..33e91b4c7 100644
--- a/loopy/isl_helpers.py
+++ b/loopy/isl_helpers.py
@@ -196,7 +196,7 @@ def static_extremum_of_pw_aff(pw_aff, constants_only, set_method, what, context)
 
     # }}}
 
-    raise ValueError("a static %s was not found for PwAff '%s'"
+    raise StaticValueFindingError("a static %s was not found for PwAff '%s'"
             % (what, pw_aff))
 
 
diff --git a/loopy/kernel/tools.py b/loopy/kernel/tools.py
index 27c0a2218..dd93ab2f3 100644
--- a/loopy/kernel/tools.py
+++ b/loopy/kernel/tools.py
@@ -292,21 +292,49 @@ class SetOperationCacheManager:
         lower_bound_pw_aff = self.dim_min(set, idx)
         upper_bound_pw_aff = self.dim_max(set, idx)
 
-        from loopy.isl_helpers import static_max_of_pw_aff, static_value_of_pw_aff
+        from loopy.diagnostic import StaticValueFindingError
+        from loopy.isl_helpers import (
+                static_max_of_pw_aff,
+                static_min_of_pw_aff,
+                static_value_of_pw_aff)
         from loopy.symbolic import pw_aff_to_expr
 
+        # {{{ first: try to find static lower bound value
+
+        try:
+            base_index_aff = static_value_of_pw_aff(
+                    lower_bound_pw_aff, constants_only=False,
+                    context=context)
+        except StaticValueFindingError:
+            base_index_aff = None
+
+        if base_index_aff is not None:
+            base_index = pw_aff_to_expr(base_index_aff)
+
+            size = pw_aff_to_expr(static_max_of_pw_aff(
+                    upper_bound_pw_aff - base_index_aff + 1, constants_only=False,
+                    context=context))
+
+            return base_index, size
+
+        # }}}
+
+        # {{{ if that didn't work, try finding a lower bound
+
+        base_index_aff = static_min_of_pw_aff(
+                lower_bound_pw_aff, constants_only=False,
+                context=context)
+
+        base_index = pw_aff_to_expr(base_index_aff)
+
         size = pw_aff_to_expr(static_max_of_pw_aff(
-                upper_bound_pw_aff - lower_bound_pw_aff + 1, constants_only=False,
+                upper_bound_pw_aff - base_index_aff + 1, constants_only=False,
                 context=context))
-        try:
-            base_index = pw_aff_to_expr(
-                    static_value_of_pw_aff(lower_bound_pw_aff, constants_only=False,
-                        context=context))
-        except Exception as e:
-            raise type(e)("while finding lower bound of '%s': %s" % (iname, str(e)))
 
         return base_index, size
 
+        # }}}
+
 # }}}
 
 
-- 
GitLab