From 3bb12005fbb25b8bfd5cdd36bbc6c6b580331e0c Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sat, 22 Oct 2011 21:16:28 -0400
Subject: [PATCH] Fix sanity check for matmul test.

(or, more generally, any test where there are instructions that do not
use all inames)
---
 loopy/codegen/__init__.py | 51 +++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/loopy/codegen/__init__.py b/loopy/codegen/__init__.py
index c3ca6c7a9..df65c43a2 100644
--- a/loopy/codegen/__init__.py
+++ b/loopy/codegen/__init__.py
@@ -180,23 +180,60 @@ def make_initial_assignments(kernel):
 # {{{ sanity-check for implemented domains of each instruction
 
 def check_implemented_domains(kernel, implemented_domains):
+    from islpy import dim_type
+
+    parameter_inames = set(
+            kernel.domain.get_dim_name(dim_type.param, i)
+            for i in range(kernel.domain.dim(dim_type.param)))
+
+    from islpy import align_spaces
+    assumptions = align_spaces(kernel.assumptions, kernel.domain)
+
     for insn_id, idomains in implemented_domains.iteritems():
+        insn = kernel.id_to_insn[insn_id]
+
         assert idomains
 
         insn_impl_domain = idomains[0]
         for idomain in idomains[1:]:
             insn_impl_domain = insn_impl_domain | idomain
-        insn_impl_domain = insn_impl_domain.coalesce()
+        insn_impl_domain = (
+                (insn_impl_domain & assumptions)
+                .project_out_except(insn.all_inames(), [dim_type.set]))
 
-        insn = kernel.id_to_insn[insn_id]
-        desired_domain = (kernel.domain
-            .eliminate_except(insn.all_inames(), [isl.dim_type.set]))
+        desired_domain = ((kernel.domain & assumptions)
+            .project_out_except(insn.all_inames(), [dim_type.set]))
 
         if insn_impl_domain != desired_domain:
+            i_minus_d = insn_impl_domain - desired_domain
+            d_minus_i = desired_domain - insn_impl_domain
+
+            lines = []
+            for kind, diff_set in [
+                    ("implemented, but not desired", i_minus_d),
+                    ("desired, but not implemented", d_minus_i)]:
+                diff_set = diff_set.coalesce()
+                pt = diff_set.sample_point()
+                if pt.is_void():
+                    continue
+
+                #pt_set = isl.Set.from_point(pt)
+                #lines.append("point implemented: %s" % (pt_set <= insn_impl_domain))
+                #lines.append("point desired: %s" % (pt_set <= desired_domain))
+
+                point_axes = []
+                for iname in insn.all_inames() | parameter_inames:
+                    tp, dim = kernel.iname_to_dim[iname]
+                    point_axes.append("%s=%d" % (iname, pt.get_coordinate(tp, dim)))
+
+                lines.append(
+                        "sample point %s: %s" % (kind, ", ".join(point_axes)))
+
             raise RuntimeError("sanity check failed--implemented and desired "
-                    "domain for insn '%s' do not match\n  implemented: %s\n"
-                    "  desired:%s"
-                    % (insn_id, insn_impl_domain, desired_domain))
+                    "domain for instruction '%s' do not match\n\n"
+                    "implemented: %s\n\n"
+                    "desired:%s\n\n%s"
+                    % (insn_id, insn_impl_domain, desired_domain, "\n".join(lines)))
 
     # placate the assert at the call site
     return True
-- 
GitLab