From 0a7c42630de2ddf029e0caad347cf7b00311f76c Mon Sep 17 00:00:00 2001
From: Kaushik Kulkarni <kaushikcfd@gmail.com>
Date: Tue, 13 Mar 2018 17:15:06 -0500
Subject: [PATCH] Checked that the functions are scoped.

---
 loopy/preprocess.py | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/loopy/preprocess.py b/loopy/preprocess.py
index 5e36e51a1..30ce5b8ab 100644
--- a/loopy/preprocess.py
+++ b/loopy/preprocess.py
@@ -37,6 +37,8 @@ from loopy.version import DATA_MODEL_VERSION
 from loopy.kernel.data import make_assignment
 # for the benefit of loopy.statistics, for now
 from loopy.type_inference import infer_unknown_types
+from pymbolic.primitives import Variable
+from pymbolic.mapper import Collector
 
 import logging
 logger = logging.getLogger(__name__)
@@ -2097,6 +2099,29 @@ def check_atomic_loads(kernel):
 # }}}
 
 
+# {{{ check for unscoped calls
+
+class UnScopedCallCollector(Collector):
+    def map_call(self, expr):
+        if isinstance(expr.function, Variable):
+            return set([expr.function.name])
+        else:
+            return set()
+
+
+def check_function_are_scoped(kernel):
+    """ Checks if all the calls in the instruction expression have been scoped,
+    otherwise indicate to what all calls we await signature.
+    """
+    for insn in kernel.instructions:
+        unscoped_calls = UnScopedCallCollector()(insn.expression)
+        if unscoped_calls:
+            raise LoopyError("Unknown function obtained %s -- register a function"
+                    " or a kernel corresponding to it." % unscoped_calls[0])
+
+# }}}
+
+
 preprocess_cache = WriteOncePersistentDict(
         "loopy-preprocess-cache-v2-"+DATA_MODEL_VERSION,
         key_builder=LoopyKeyBuilder())
@@ -2146,6 +2171,10 @@ def preprocess_kernel(kernel, device=None):
     from loopy.transform.subst import expand_subst
     kernel = expand_subst(kernel)
 
+    # Checking if all the functions being used in the kernel and scoped to a
+    # finite namespace
+    check_function_are_scoped(kernel)
+
     # Ordering restriction:
     # Type inference and reduction iname uniqueness don't handle substitutions.
     # Get them out of the way.
-- 
GitLab