Commit ca4fb56d authored by John Zhang's avatar John Zhang

some refactoring

parent e520ccac
......@@ -247,7 +247,8 @@ class MuDatabase:
# graphs
for g in self.tlc.graphs:
graph_name = '@' + g.name
g.name = man.get_graph_name(g)
graph_name = g.name
self.mu_name_map[g] = graph_name
for i, blk in enumerate(g.iterblocks()):
blk_name = '%(graph_name)s.blk%(i)d' % locals()
......@@ -342,6 +343,7 @@ class MuNameManager:
}
self._assigned_names = set()
self._conflict_ctr = {}
self.graph_name_dic = {}
# verbose struct name?
from rpython.config.translationoption import get_translation_config
......@@ -453,3 +455,19 @@ class MuNameManager:
name = 'extfnc_' + const.value._name
return '@' + name
def get_graph_name(self, g):
name = g.name if '.' in g.name else g.name.split('__')[0]
name = re.sub(r'[^0-9a-zA-Z_-]', '_', name) # replace illegal characters in names
name_dic = self.graph_name_dic
if name not in name_dic:
ctr = 0
name_dic[name] = ([g], ctr)
else:
gs, ctr = name_dic[name]
if g not in gs:
gs.append(g)
ctr += 1
name_dic[name] = (gs, ctr)
return "@%s_%d" % (name, ctr)
......@@ -35,14 +35,27 @@ class MuTyper:
self.ll2mu.set_threadlocal_struct_type(self.TLStt)
def prepare_all(self):
self.graphs = prepare(self.tlc.graphs, self.tlc.entry_point_graph)
if hasattr(self.tlc, 'entry_point_graph'):
# Task 1: prune and remove inlined graphs
n0 = len(self.tlc.graphs)
graphs = graph_closure(self.tlc.entry_point_graph)
log.prune("%d -> %d graphs" % (n0, len(graphs)))
else:
graphs = self.tlc.graphs
for g in graphs:
prepare(g)
self.graphs = graphs
def specialise_all(self):
if not hasattr(self, 'graphs'):
raise AttributeError("don't have graphs. Run prepare_all() first.")
for g in self.ll2mu.llhelper_graphs:
self.graphs = self.graphs.union(prepare([g], g))
_gs = graph_closure(g)
for _g in _gs:
prepare(_g)
self.graphs = self.graphs.union(_gs)
log.specialise_all('specialising graphs...')
mdb.restart()
......@@ -63,9 +76,13 @@ class MuTyper:
arg_llts = map(lambda arg: arg.concretetype, g.startblock.inputargs)
g.sig = mutype.MuFuncSig([self.ll2mu.map_type(arg_t) for arg_t in arg_llts],
[self.ll2mu.map_type(ret_llt)] if ret_llt != lltype.Void else [])
for blk in g.iterblocks():
self.specialise_block(blk)
if (len(g.sig.ARGS) == 1 and g.sig.ARGS[0] == mutype.MU_VOID):
raise AssertionError("input arg can not have type void")
remove_unaryops(g, ['same_as', 'likely'])
def specialise_block(self, blk):
......@@ -215,7 +232,8 @@ class MuTyper:
muops = []
muops.extend(self.extract_load_gcell(llop.args))
muops.extend(self.ll2mu.map_op(llop))
muops.extend(self.ll2mu.map_op(llop
))
return muops
def extract_load_gcell(self, args):
......@@ -318,7 +336,7 @@ def graph_closure(g_entry):
return graph_closure
def prepare(graphs, entry_graph):
def prepare(g):
def _keep_arg(arg, opname=''):
from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS
_OPS_ALLOW_LLTYPE_ARGS = []
......@@ -341,49 +359,23 @@ def prepare(graphs, entry_graph):
# log.keep_arg("Throwing argument %(arg)r from operation %(opname)s" % locals())
return False
name_dic = {}
def rename(g):
""" reassign graph names (shorter names more readable) """
name = g.name if '.' in g.name else g.name.split('__')[0]
name = re.sub(r'[^0-9a-zA-Z_-]', '_', name) # replace illegal characters in names
if name not in name_dic:
ctr = 0
name_dic[name] = ([g], ctr)
else:
gs, ctr = name_dic[name]
if g not in gs:
gs.append(g)
ctr += 1
name_dic[name] = (gs, ctr)
g.name = "%s_%d" % (name, ctr)
# Task 1: prune and remove inlined graphs
n0 = len(graphs)
graphs = graph_closure(entry_graph)
log.prune("%d -> %d graphs" % (n0, len(graphs)))
for g in graphs:
rename(g)
for blk in list(g.iterblocks()) + [g.returnblock]: # force include return block, in case always throwing exceptions
# Task 2: Remove Void args and parameters in inputargs, operations and links
blk.inputargs = [arg for arg in blk.inputargs if arg.concretetype != lltype.Void]
for lnk in blk.exits:
lnk.args = [arg for arg in lnk.args if arg.concretetype != lltype.Void]
for op in blk.operations:
op.args = [arg for arg in op.args if _keep_arg(arg, op.opname)]
if op.opname in ('force_cast', 'cast_primitive'):
# HACK: save original arg and result types to discern signedness.
op._src_llt = op.args[0].concretetype
op._res_llt = op.result.concretetype
# replace constants with dummy variables in inputargs --> they shouldn't appear there
idx_cnsts = filter(lambda _i: isinstance(blk.inputargs[_i], Constant), range(len(blk.inputargs)))
if len(idx_cnsts) > 0:
for i in idx_cnsts:
_v = Variable('dummy')
_v.concretetype = blk.inputargs[i].concretetype
blk.inputargs[i] = _v
return graphs
\ No newline at end of file
for blk in list(g.iterblocks()) + [g.returnblock]: # force include return block, in case always throwing exceptions
# Task 2: Remove Void args and parameters in inputargs, operations and links
blk.inputargs = [arg for arg in blk.inputargs if arg.concretetype != lltype.Void]
for lnk in blk.exits:
lnk.args = [arg for arg in lnk.args if arg.concretetype != lltype.Void]
for op in blk.operations:
op.args = [arg for arg in op.args if _keep_arg(arg, op.opname)]
if op.opname in ('force_cast', 'cast_primitive'):
# HACK: save original arg and result types to discern signedness.
op._src_llt = op.args[0].concretetype
op._res_llt = op.result.concretetype
# replace constants with dummy variables in inputargs --> they shouldn't appear there
idx_cnsts = filter(lambda _i: isinstance(blk.inputargs[_i], Constant), range(len(blk.inputargs)))
if len(idx_cnsts) > 0:
for i in idx_cnsts:
_v = Variable('dummy')
_v.concretetype = blk.inputargs[i].concretetype
blk.inputargs[i] = _v
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment