diff --git a/MEMO b/MEMO index 2ce9d4efcf67b4c8c6fae29e800ed27b10aff535..75c9f6a5394753e754c233e8222574120c7644c2 100644 --- a/MEMO +++ b/MEMO @@ -51,7 +51,7 @@ To-do - ExpandingIdentityMapper extract_subst -> needs WalkMapper padding - replace make_unique_var_name + replace make_unique_var_name [DONE] join_inames [DONE] duplicate_inames [DONE] split_iname [DONE] diff --git a/loopy/__init__.py b/loopy/__init__.py index 9171cd6d918ceddb942572384a15c30d736e9a2d..ac78736d2d809d2f6088f90353d6cd6623e28f7a 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -695,10 +695,10 @@ def add_prefetch(kernel, var_name, sweep_inames=[], dim_arg_names=None, if tag is not None: c_name = c_name + "_" + tag - if rule_name is None: - rule_name = kernel.make_unique_var_name("%s_fetch" % c_name) + var_name_gen = kernel.get_var_name_generator() - newly_created_vars = set([rule_name]) + if rule_name is None: + rule_name = var_name_gen("%s_fetch" % c_name) arg = kernel.arg_dict[var_name] @@ -710,9 +710,7 @@ def add_prefetch(kernel, var_name, sweep_inames=[], dim_arg_names=None, if dim_arg_names is not None and i < len(dim_arg_names): based_on = dim_arg_names[i] - par_name = kernel.make_unique_var_name(based_on=based_on, - extra_used_vars=newly_created_vars) - newly_created_vars.add(par_name) + par_name = var_name_gen(based_on=based_on) parameters.append(par_name) from pymbolic import var diff --git a/loopy/creation.py b/loopy/creation.py index 5c16d24cda2a9d427371bdc07c9fc471b9796e8a..325a56f8fd6dcc96e8d9ac89d9c66da2b7e65d68 100644 --- a/loopy/creation.py +++ b/loopy/creation.py @@ -148,11 +148,10 @@ class CSEToAssignmentMapper(IdentityMapper): def expand_cses(knl): def add_assignment(base_name, expr, dtype): - kwargs = dict(extra_used_vars=newly_created_vars) - if base_name is not None: - kwargs["based_on"] = base_name - new_var_name = knl.make_unique_var_name(**kwargs) - newly_created_vars.add(new_var_name) + if base_name is None: + base_name = "var" + + new_var_name = var_name_gen(base_name) if dtype is None: from loopy import infer_type @@ -181,7 +180,8 @@ def expand_cses(knl): new_insns = [] - newly_created_vars = set() + var_name_gen = knl.get_var_name_generator() + newly_created_insn_ids = set() new_temp_vars = knl.temporary_variables.copy() diff --git a/loopy/cse.py b/loopy/cse.py index 65d89d8b70b889858d21c2566b3497517d76fd3e..01737eb5de584366585bd56ea4574fa694490f23 100644 --- a/loopy/cse.py +++ b/loopy/cse.py @@ -707,7 +707,7 @@ def precompute(kernel, subst_use, sweep_inames=[], within=None, # }}} - newly_created_var_names = set() + var_name_gen = kernel.get_var_name_generator() # {{{ use given / find new storage_axes @@ -753,19 +753,16 @@ def precompute(kernel, subst_use, sweep_inames=[], within=None, if new_storage_axis_names is not None and i < len(new_storage_axis_names): name = new_storage_axis_names[i] tag_lookup_saxis = name - if name in (kernel.all_variable_names() | newly_created_var_names): - raise RuntimeError("new storage axis name '%s' already exists" % name) + if var_name_gen.is_name_conflicting(name): + raise RuntimeError("new storage axis name '%s' " + "conflicts with existing name" % name) - if name in (kernel.all_variable_names() - | newly_created_var_names): - name = kernel.make_unique_var_name( - based_on=name, extra_used_vars=newly_created_var_names) + name = var_name_gen(name) storage_axis_names.append(name) new_iname_to_tag[name] = storage_axis_to_tag.get( tag_lookup_saxis, default_tag) - newly_created_var_names.add(name) expr_subst_dict[old_name] = var(name) del storage_axis_to_tag @@ -815,8 +812,7 @@ def precompute(kernel, subst_use, sweep_inames=[], within=None, # {{{ set up compute insn - target_var_name = kernel.make_unique_var_name(based_on=c_subst_name, - extra_used_vars=newly_created_var_names) + target_var_name = var_name_gen(based_on=c_subst_name) assignee = var(target_var_name) diff --git a/loopy/kernel.py b/loopy/kernel.py index d5ba8bde3006f329f04e95d09137a47b28d5308d..0dd1373fe02501d24bd836d25d0288b7b130adee 100644 --- a/loopy/kernel.py +++ b/loopy/kernel.py @@ -899,8 +899,8 @@ class LoopKernel(Record): for i, arg in enumerate(lhs.parameters): if not isinstance(arg, Variable): raise RuntimeError("Invalid substitution rule " - "left-hand side: %s--arg number %d " - "is not a variable"% (lhs, i)) + "left-hand side: %s--arg number %d " + "is not a variable"% (lhs, i)) arg_names.append(arg.name) else: raise RuntimeError("Invalid substitution rule left-hand side") @@ -1087,15 +1087,6 @@ class LoopKernel(Record): | set(arg.name for arg in self.args) | set(self.all_inames())) - def make_unique_var_name(self, based_on="var", extra_used_vars=set()): - from warnings import warn - warn("make_unique_var_name is deprecated, use get_var_name_generator " - "instead", DeprecationWarning, stacklevel=2) - - gen = self.get_var_name_generator() - gen.add_names(extra_used_vars) - return gen(based_on) - def get_var_name_generator(self): return _UniqueNameGenerator(self.all_variable_names()) diff --git a/loopy/padding.py b/loopy/padding.py index 7c8e50fcca5b563de3af1ef6ae881a7e20e9e917..ccfcf4b1b6c49217805b7283cd8ff359b9e59245 100644 --- a/loopy/padding.py +++ b/loopy/padding.py @@ -137,6 +137,8 @@ def split_arg_axis(kernel, args_and_axes, count): split_vars = {} + var_name_gen = kernel.get_var_name_generator() + def split_access_axis(expr): axis_nr, order = arg_to_rest[expr.aggregate.name] @@ -159,10 +161,8 @@ def split_arg_axis(kernel, args_and_axes, count): try: outer_iname, inner_iname = split_vars[split_iname] except KeyError: - outer_iname = kernel.make_unique_var_name( - split_iname+"_outer") - inner_iname = kernel.make_unique_var_name( - split_iname+"_inner") + outer_iname = var_name_gen(split_iname+"_outer") + inner_iname = var_name_gen(split_iname+"_inner") split_vars[split_iname] = outer_iname, inner_iname idx[axis_nr] = Variable(inner_iname) diff --git a/loopy/preprocess.py b/loopy/preprocess.py index ce5cf8c58753099cd5a068b881ef54fc213225fb..61aefce1ca120f62d4bbd60a0aaccc82ffaee0dc 100644 --- a/loopy/preprocess.py +++ b/loopy/preprocess.py @@ -228,8 +228,9 @@ def realize_reduction(kernel, insn_id_filter=None): new_insns = [] + + var_name_gen = kernel.get_var_name_generator() new_temporary_variables = kernel.temporary_variables.copy() - orig_temp_var_names = set(kernel.temporary_variables) from loopy.codegen.expression import TypeInferenceMapper type_inf_mapper = TypeInferenceMapper(kernel) @@ -240,8 +241,7 @@ def realize_reduction(kernel, insn_id_filter=None): from pymbolic import var - target_var_name = kernel.make_unique_var_name("acc_"+"_".join(expr.inames), - extra_used_vars=set(new_temporary_variables) - orig_temp_var_names) + target_var_name = var_name_gen("acc_"+"_".join(expr.inames)) target_var = var(target_var_name) arg_dtype = type_inf_mapper(expr.expr) diff --git a/loopy/subst.py b/loopy/subst.py index 4110670fed1227c62ddc2c76d7729a5e6fb855fa..31c48dddb371a2d7be318b8c5efb29b46a8f27e1 100644 --- a/loopy/subst.py +++ b/loopy/subst.py @@ -27,20 +27,18 @@ def extract_subst(kernel, subst_name, template, parameters): unifications. """ - newly_created_var_names = set() - if isinstance(template, str): from pymbolic import parse template = parse(template) + var_name_gen = kernel.get_var_name_generator() + # {{{ replace any wildcards in template with new variables def get_unique_var_name(): based_on = subst_name+"_wc" - result = kernel.make_unique_var_name( - based_on=based_on, extra_used_vars=newly_created_var_names) - newly_created_var_names.add(result) + result = var_name_gen(based_on) return result from loopy.symbolic import WildcardToUniqueVariableMapper @@ -63,10 +61,8 @@ def extract_subst(kernel, subst_name, template, parameters): - kernel.non_iname_variable_names()): if iname in kernel.all_inames(): # need to rename to be unique - new_iname = kernel.make_unique_var_name( - based_on=iname, extra_used_vars=newly_created_var_names) + new_iname = var_name_gen(iname) old_to_new[iname] = var(new_iname) - newly_created_var_names.add(new_iname) matching_vars.append(new_iname) else: matching_vars.append(iname)