def hannotate(func, argtypes, policy=P_DEFAULT, annotator=False, inline=None, backendoptimize=False): # build the normal ll graphs for ll_function t = TranslationContext() a = t.buildannotator() a.build_types(func, argtypes) rtyper = t.buildrtyper() rtyper.specialize() if inline: auto_inlining(t, threshold=inline) if backendoptimize: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) graph1 = graphof(t, func) # build hint annotator types hannotator = HintAnnotator(base_translator=t, policy=policy) hs = hannotator.build_types(graph1, [SomeLLAbstractConstant(v.concretetype, {OriginFlags(): True}) for v in graph1.getargs()]) hannotator.simplify() t = hannotator.translator if conftest.option.view: t.view() if annotator: return hs, hannotator else: return hs
def check_auto_inlining(self, func, sig, multiplier=None, call_count_check=False, checkvirtual=False, remove_same_as=False): t = self.translate(func, sig) if checkvirtual: check_virtual_methods() if option.view: t.view() # inline! sanity_check(t) # also check before inlining (so we don't blame it) threshold = INLINE_THRESHOLD_FOR_TEST if multiplier is not None: threshold *= multiplier call_count_pred = None if call_count_check: call_count_pred = lambda lbl: True instrument_inline_candidates(t.graphs, threshold) if remove_same_as: for graph in t.graphs: removenoops.remove_same_as(graph) auto_inlining(t, threshold, call_count_pred=call_count_pred) sanity_check(t) if option.view: t.view() interp = LLInterpreter(t.rtyper) def eval_func(args): return interp.eval_graph(graphof(t, func), args) return eval_func, t
def test_replace_exitswitch_by_constant_bug(): class X: pass def constant9(): x = X() x.n = 3 x.n = 9 return x.n def fn(): n = constant9() if n == 1: return 5 elif n == 2: return 6 elif n == 3: return 8 elif n == 4: return -123 elif n == 5: return 12973 else: return n t = TranslationContext() a = t.buildannotator() a.build_types(fn, []) rtyper = t.buildrtyper() rtyper.specialize() graph = t.graphs[0] remove_same_as(graph) merge_if_blocks_once(graph) from pypy.translator.backendopt import malloc, inline inline.auto_inlining(t, 20) malloc.remove_mallocs(t, t.graphs) from pypy.translator import simplify simplify.join_blocks(graph)
def hannotate(self, func, argtypes, policy=P_DEFAULT, annotator=False, inline=None, backendoptimize=False): # build the normal ll graphs for ll_function t = TranslationContext() a = t.buildannotator() a.build_types(func, argtypes) rtyper = t.buildrtyper(type_system = self.type_system) rtyper.specialize() if inline: auto_inlining(t, threshold=inline) if backendoptimize: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) graph1 = graphof(t, func) # build hint annotator types policy = self.fixpolicy(policy) hannotator = HintAnnotator(base_translator=t, policy=policy) hs = hannotator.build_types(graph1, [SomeLLAbstractConstant(v.concretetype, {OriginFlags(): True}) for v in graph1.getargs()]) hannotator.simplify() t = hannotator.translator if conftest.option.view: t.view() if annotator: return hs, hannotator else: return hs
def check_auto_inlining(self, func, sig, multiplier=None, call_count_check=False, checkvirtual=False, remove_same_as=False, heuristic=None, const_fold_first=False): t = self.translate(func, sig) if checkvirtual: check_virtual_methods() if const_fold_first: from pypy.translator.backendopt.constfold import constant_fold_graph from pypy.translator.simplify import eliminate_empty_blocks for graph in t.graphs: constant_fold_graph(graph) eliminate_empty_blocks(graph) if option.view: t.view() # inline! sanity_check(t) # also check before inlining (so we don't blame it) threshold = INLINE_THRESHOLD_FOR_TEST if multiplier is not None: threshold *= multiplier call_count_pred = None if call_count_check: call_count_pred = lambda lbl: True instrument_inline_candidates(t.graphs, threshold) if remove_same_as: for graph in t.graphs: removenoops.remove_same_as(graph) if heuristic is not None: kwargs = {"heuristic": heuristic} else: kwargs = {} auto_inlining(t, threshold, call_count_pred=call_count_pred, **kwargs) sanity_check(t) if option.view: t.view() interp = LLInterpreter(t.rtyper) def eval_func(args): return interp.eval_graph(graphof(t, func), args) return eval_func, t
def inline_and_remove(t, graphs, threshold=BIG_THRESHOLD, heuristic=inline.inlining_heuristic): callgraph, caller_candidates = find_malloc_removal_candidates(t, graphs) log.inlineandremove("found %s malloc removal candidates" % len(caller_candidates)) if callgraph: count = inline.auto_inlining(t, callgraph=callgraph, threshold=threshold, heuristic=heuristic) if not count: return False log.inlineandremove('inlined %d callsites.'% (count,)) count = remove_mallocs(t, caller_candidates.keys()) return count else: return False
def inline_and_remove(t, graphs, threshold=BIG_THRESHOLD, heuristic=inline.inlining_heuristic): callgraph, caller_candidates = find_malloc_removal_candidates(t, graphs) log.inlineandremove("found %s malloc removal candidates" % len(caller_candidates)) if callgraph: count = inline.auto_inlining(t, callgraph=callgraph, threshold=threshold, heuristic=heuristic) if not count: return False log.inlineandremove('inlined %d callsites.' % (count, )) count = remove_mallocs(t, caller_candidates.keys()) return count else: return False