From 1033e2b02f198664e22dcd2f078a5c25f6b4fba4 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Tue, 1 Sep 2015 00:57:07 -0500
Subject: [PATCH] Add remove_unused_arguments()

---
 doc/reference.rst |  2 ++
 loopy/__init__.py | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/doc/reference.rst b/doc/reference.rst
index 9aa9727cd..52cd92bb8 100644
--- a/doc/reference.rst
+++ b/doc/reference.rst
@@ -430,6 +430,8 @@ Influencing data access
 
 .. autofunction:: tag_data_axes
 
+.. autofunction:: remove_unused_arguments
+
 Padding
 ^^^^^^^
 
diff --git a/loopy/__init__.py b/loopy/__init__.py
index 77da1e40e..10fac9cfc 100644
--- a/loopy/__init__.py
+++ b/loopy/__init__.py
@@ -2090,4 +2090,22 @@ def to_batched(knl, nbatches, batch_varying_args, batch_iname_prefix="ibatch"):
 
 # }}}
 
+
+# {{{ remove_unused_arguments
+
+def remove_unused_arguments(knl):
+    new_args = []
+
+    refd_vars = set(knl.all_params())
+    for insn in knl.instructions:
+        refd_vars.update(insn.dependency_names())
+
+    for arg in knl.args:
+        if arg.name in refd_vars:
+            new_args.append(arg)
+
+    return knl.copy(args=new_args)
+
+# }}}
+
 # vim: foldmethod=marker
-- 
GitLab