From 65f6d419c46856ccc1da2c85ab2e545900d718fd Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Tue, 6 Nov 2012 11:39:57 -0500 Subject: [PATCH] Implement rename_iname. --- MEMO | 4 ++-- doc/reference.rst | 4 ++-- loopy/__init__.py | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/MEMO b/MEMO index 9227c654b..13d8e6afe 100644 --- a/MEMO +++ b/MEMO @@ -48,8 +48,6 @@ To-do - Kernel fusion -- rename iname - - when are link_inames, duplicate_inames safe? - ExpandingIdentityMapper @@ -145,6 +143,8 @@ Future ideas Dealt with ^^^^^^^^^^ +- rename iname + - delete unused inames - Expose iname-duplicate-and-rename as a primitive. diff --git a/doc/reference.rst b/doc/reference.rst index e066ba1df..e3a59b97d 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -165,9 +165,9 @@ Wrangling inames .. autofunction:: link_inames -.. autofunction:: rename_inames +.. autofunction:: rename_iname -.. autofunction:: delete_unused_inames +.. autofunction:: remove_unused_inames Dealing with Substitution Rules ------------------------------- diff --git a/loopy/__init__.py b/loopy/__init__.py index 6257e36b0..9766aa370 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -550,6 +550,23 @@ def duplicate_inames(knl, inames, within, new_inames=None, suffix=None, # }}} +def rename_iname(knl, old_iname, new_iname, within): + """ + :arg within: a stack match as understood by + :func:`loopy.context_matching.parse_stack_match`. + """ + + var_name_gen = knl.get_var_name_generator() + + if var_name_gen.is_name_conflicting(new_iname): + raise ValueError("iname '%s' conflicts with an existing identifier" + "--cannot rename" % new_iname) + + knl = duplicate_inames([old_iname], within, [new_iname]) + knl = remove_unused_inames(knl, [old_iname]) + + return knl + # {{{ link inames def link_inames(knl, inames, new_iname, within=None, tag=None): @@ -646,7 +663,7 @@ def link_inames(knl, inames, new_iname, within=None, tag=None): # }}} - knl = delete_unused_inames(knl, inames) + knl = remove_unused_inames(knl, inames) if tag is not None: knl = tag_inames(knl, {new_iname: tag}) @@ -655,9 +672,9 @@ def link_inames(knl, inames, new_iname, within=None, tag=None): # }}} -# {{{ delete unused inames +# {{{ remove unused inames -def delete_unused_inames(knl, inames=None): +def remove_unused_inames(knl, inames=None): """Delete those among *inames* that are unused, i.e. project them out of the domain. If these inames pose implicit restrictions on other inames, these restrictions will persist as existentially -- GitLab