def _build_gen(func, annotation, graph=None, backendopt=True, exctrans=False, annotatorpolicy=None, nowrap=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator(policy=annotatorpolicy) inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator(policy=annotatorpolicy) ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() return _build_gen_from_graph(main_graph, t, exctrans, nowrap)
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 _build_gen(func, annotation, graph=None, backendopt=True): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator() inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator() ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir return GenCli(tmpdir, t, TestEntryPoint(main_graph, True))
def _build_gen(func, annotation, graph=None, backendopt=True, exctrans=False, annotatorpolicy=None, nowrap=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator(policy=annotatorpolicy) inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator(policy=annotatorpolicy) ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() return _build_gen_from_graph(main_graph, t, exctrans, nowrap)
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_checkvirtual_simple(): A = Instance("A", ROOT) B = Instance("B", A) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(B, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert B._methods["foo"]._virtual == False
def test_checkvirtual_simple(): A = Instance("A", ROOT) B = Instance("B", A) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(B, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert B._methods["foo"]._virtual == False
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 test_checkvirtual_deep(): A = Instance("A", ROOT) B = Instance("B", A) C = Instance("C", B) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(C, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert "foo" not in B._methods assert C._methods["foo"]._virtual == False
def test_checkvirtual_brother(): A = Instance("A", ROOT) B1 = Instance("B1", A) B2 = Instance("B2", A) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(B1, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert B1._methods["foo"]._virtual == False assert "foo" not in B2._methods
def test_checkvirtual_brother(): A = Instance("A", ROOT) B1 = Instance("B1", A) B2 = Instance("B2", A) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(B1, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert B1._methods["foo"]._virtual == False assert "foo" not in B2._methods
def test_checkvirtual_deep(): A = Instance("A", ROOT) B = Instance("B", A) C = Instance("C", B) addMethods(A, {"foo": meth(Meth([], Void)), "bar": meth(Meth([], Void))}) addMethods(C, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == True assert A._methods["bar"]._virtual == False assert "foo" not in B._methods assert C._methods["foo"]._virtual == False
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 generate_source_for_function(func, annotation, backendopt=False): """ Given a Python function and some hints about its argument types, generates JVM sources that call it and print the result. Returns the JvmGeneratedSource object. """ if hasattr(func, 'im_func'): func = func.im_func t = TranslationContext() ann = t.buildannotator() ann.build_types(func, annotation) t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir jvm = GenJvm(tmpdir, t, EntryPoint(main_graph, True, True)) return jvm.generate_source()
def generate_source_for_function(func, annotation, backendopt=False): """ Given a Python function and some hints about its argument types, generates JVM sources that call it and print the result. Returns the JvmGeneratedSource object. """ if hasattr(func, 'im_func'): func = func.im_func t = TranslationContext() ann = t.buildannotator() ann.build_types(func, annotation) t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir jvm = GenJvm(tmpdir, t, EntryPoint(main_graph, True, True)) return jvm.generate_source()
def backend_optimizations(translator, graphs=None, secondary=False, **kwds): # sensible keywords are # raisingop2direct_call, inline_threshold, mallocs # merge_if_blocks, constfold, heap2stack # clever_malloc_removal, remove_asserts config = translator.config.translation.backendopt.copy(as_default=True) config.set(**kwds) if graphs is None: graphs = translator.graphs for graph in graphs: assert not hasattr(graph, '_seen_by_the_backend') if config.print_statistics: print "before optimizations:" print_statistics(translator.graphs[0], translator, "per-graph.txt") if config.raisingop2direct_call: raisingop2direct_call(translator, graphs) if translator.rtyper.type_system.name == 'ootypesystem': check_virtual_methods() if config.remove_asserts: constfold(config, graphs) remove_asserts(translator, graphs) if config.really_remove_asserts: for graph in graphs: removenoops.remove_debug_assert(graph) # the dead operations will be killed by the remove_obvious_noops below # remove obvious no-ops def remove_obvious_noops(): for graph in graphs: removenoops.remove_same_as(graph) simplify.eliminate_empty_blocks(graph) simplify.transform_dead_op_vars(graph, translator) removenoops.remove_duplicate_casts(graph, translator) if config.print_statistics: print "after no-op removal:" print_statistics(translator.graphs[0], translator) remove_obvious_noops() if config.inline or config.mallocs: heuristic = get_function(config.inline_heuristic) if config.inline: threshold = config.inline_threshold else: threshold = 0 inline_malloc_removal_phase(config, translator, graphs, threshold, inline_heuristic=heuristic) constfold(config, graphs) if config.clever_malloc_removal: threshold = config.clever_malloc_removal_threshold heuristic = get_function(config.clever_malloc_removal_heuristic) log.inlineandremove("phase with threshold factor: %s" % threshold) log.inlineandremove("heuristic: %s.%s" % (heuristic.__module__, heuristic.__name__)) count = mallocprediction.clever_inlining_and_malloc_removal( translator, graphs, threshold=threshold, heuristic=heuristic) log.inlineandremove("removed %d simple mallocs in total" % count) constfold(config, graphs) if config.print_statistics: print "after clever inlining and malloc removal" print_statistics(translator.graphs[0], translator) if config.storesink: for graph in graphs: storesink_graph(graph) if config.profile_based_inline and not secondary: threshold = config.profile_based_inline_threshold heuristic = get_function(config.profile_based_inline_heuristic) inline.instrument_inline_candidates(graphs, threshold) counters = translator.driver_instrument_result( config.profile_based_inline) n = len(counters) def call_count_pred(label): if label >= n: return False return counters[label] > 250 # xxx introduce an option for this inline_malloc_removal_phase(config, translator, graphs, threshold, inline_heuristic=heuristic, call_count_pred=call_count_pred) constfold(config, graphs) if config.merge_if_blocks: log.mergeifblocks("starting to merge if blocks") for graph in graphs: merge_if_blocks(graph, translator.config.translation.verbose) if config.print_statistics: print "after if-to-switch:" print_statistics(translator.graphs[0], translator) remove_obvious_noops() for graph in graphs: checkgraph(graph)
def backend_optimizations(translator, graphs=None, secondary=False, **kwds): # sensible keywords are # raisingop2direct_call, inline_threshold, mallocs # merge_if_blocks, constfold, heap2stack # clever_malloc_removal, remove_asserts config = translator.config.translation.backendopt.copy(as_default=True) config.set(**kwds) if graphs is None: graphs = translator.graphs for graph in graphs: assert not hasattr(graph, '_seen_by_the_backend') if config.print_statistics: print "before optimizations:" print_statistics(translator.graphs[0], translator, "per-graph.txt") if config.raisingop2direct_call: raisingop2direct_call(translator, graphs) if translator.rtyper.type_system.name == 'ootypesystem': check_virtual_methods() if config.remove_asserts: constfold(config, graphs) remove_asserts(translator, graphs) if config.really_remove_asserts: for graph in graphs: removenoops.remove_debug_assert(graph) # the dead operations will be killed by the remove_obvious_noops below # remove obvious no-ops def remove_obvious_noops(): for graph in graphs: removenoops.remove_same_as(graph) simplify.eliminate_empty_blocks(graph) simplify.transform_dead_op_vars(graph, translator) removenoops.remove_duplicate_casts(graph, translator) if config.print_statistics: print "after no-op removal:" print_statistics(translator.graphs[0], translator) remove_obvious_noops() if config.inline or config.mallocs: heuristic = get_function(config.inline_heuristic) if config.inline: threshold = config.inline_threshold else: threshold = 0 inline_malloc_removal_phase(config, translator, graphs, threshold, inline_heuristic=heuristic) constfold(config, graphs) if config.clever_malloc_removal: threshold = config.clever_malloc_removal_threshold heuristic = get_function(config.clever_malloc_removal_heuristic) log.inlineandremove("phase with threshold factor: %s" % threshold) log.inlineandremove("heuristic: %s.%s" % (heuristic.__module__, heuristic.__name__)) count = mallocprediction.clever_inlining_and_malloc_removal( translator, graphs, threshold = threshold, heuristic=heuristic) log.inlineandremove("removed %d simple mallocs in total" % count) constfold(config, graphs) if config.print_statistics: print "after clever inlining and malloc removal" print_statistics(translator.graphs[0], translator) if config.profile_based_inline and not secondary: threshold = config.profile_based_inline_threshold heuristic = get_function(config.profile_based_inline_heuristic) inline.instrument_inline_candidates(graphs, threshold) counters = translator.driver_instrument_result( config.profile_based_inline) n = len(counters) def call_count_pred(label): if label >= n: return False return counters[label] > 250 # xxx introduce an option for this inline_malloc_removal_phase(config, translator, graphs, threshold, inline_heuristic=heuristic, call_count_pred=call_count_pred) constfold(config, graphs) if config.merge_if_blocks: log.mergeifblocks("starting to merge if blocks") for graph in graphs: merge_if_blocks(graph, translator.config.translation.verbose) if config.print_statistics: print "after if-to-switch:" print_statistics(translator.graphs[0], translator) remove_obvious_noops() for graph in graphs: checkgraph(graph)
def test_nonvirtual(): A = Instance("A", ROOT) addMethods(A, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == False
def test_nonvirtual(): A = Instance("A", ROOT) addMethods(A, {"foo": meth(Meth([], Void))}) check_virtual_methods() assert A._methods["foo"]._virtual == False