| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
from rpython.translator.backendopt import removenoops |
| 4 |
from rpython.translator.backendopt import inline |
| 5 |
from rpython.translator.backendopt.malloc import remove_mallocs |
| 6 |
from rpython.translator.backendopt.constfold import constant_fold_graph |
| 7 |
from rpython.translator.backendopt.constfold import replace_we_are_jitted |
| 8 |
from rpython.translator.backendopt.stat import print_statistics |
| 9 |
from rpython.translator.backendopt.merge_if_blocks import merge_if_blocks |
| 10 |
from rpython.translator import simplify |
| 11 |
from rpython.translator.backendopt.removeassert import remove_asserts |
| 12 |
from rpython.translator.backendopt.support import log |
| 13 |
from rpython.translator.backendopt.storesink import storesink_graph |
| 14 |
from rpython.translator.backendopt import gilanalysis |
| 15 |
from rpython.flowspace.model import checkgraph |
| 16 |
|
| 17 |
INLINE_THRESHOLD_FOR_TEST = 33 |
| 18 |
|
| 19 |
def get_function(dottedname): |
| 20 |
parts = dottedname.split('.') |
| 21 |
module = '.'.join(parts[:-1]) |
| 22 |
name = parts[-1] |
| 23 |
try: |
| 24 |
mod = __import__(module, {}, {}, ['__doc__']) |
| 25 |
except ImportError as e: |
| 26 |
raise Exception("Import error loading %s: %s" % (dottedname, e)) |
| 27 |
|
| 28 |
try: |
| 29 |
func = getattr(mod, name) |
| 30 |
except AttributeError: |
| 31 |
raise Exception("Function %s not found in module" % dottedname) |
| 32 |
|
| 33 |
return func |
| 34 |
|
| 35 |
def backend_optimizations(translator, graphs=None, secondary=False, |
| 36 |
inline_graph_from_anywhere=False, **kwds): |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
config = translator.config.translation.backendopt.copy(as_default=True) |
| 44 |
config.set(**kwds) |
| 45 |
|
| 46 |
if graphs is None: |
| 47 |
graphs = translator.graphs |
| 48 |
for graph in graphs: |
| 49 |
assert not hasattr(graph, '_seen_by_the_backend') |
| 50 |
|
| 51 |
if config.print_statistics: |
| 52 |
print("before optimizations:") |
| 53 |
print_statistics(translator.graphs[0], translator, "per-graph.txt") |
| 54 |
|
| 55 |
if config.replace_we_are_jitted: |
| 56 |
for graph in graphs: |
| 57 |
replace_we_are_jitted(graph) |
| 58 |
|
| 59 |
if config.remove_asserts: |
| 60 |
constfold(config, graphs) |
| 61 |
remove_asserts(translator, graphs) |
| 62 |
|
| 63 |
if config.really_remove_asserts: |
| 64 |
for graph in graphs: |
| 65 |
removenoops.remove_debug_assert(graph) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
def remove_obvious_noops(): |
| 70 |
for graph in graphs: |
| 71 |
removenoops.remove_same_as(graph) |
| 72 |
simplify.eliminate_empty_blocks(graph) |
| 73 |
simplify.transform_dead_op_vars(graph, translator) |
| 74 |
removenoops.remove_duplicate_casts(graph, translator) |
| 75 |
|
| 76 |
if config.print_statistics: |
| 77 |
print("after no-op removal:") |
| 78 |
print_statistics(translator.graphs[0], translator) |
| 79 |
|
| 80 |
remove_obvious_noops() |
| 81 |
|
| 82 |
if config.inline or config.mallocs: |
| 83 |
heuristic = get_function(config.inline_heuristic) |
| 84 |
if config.inline: |
| 85 |
threshold = config.inline_threshold |
| 86 |
else: |
| 87 |
threshold = 0 |
| 88 |
inline_malloc_removal_phase(config, translator, graphs, |
| 89 |
threshold, |
| 90 |
inline_heuristic=heuristic, |
| 91 |
inline_graph_from_anywhere=inline_graph_from_anywhere) |
| 92 |
constfold(config, graphs) |
| 93 |
|
| 94 |
if config.storesink: |
| 95 |
remove_obvious_noops() |
| 96 |
for graph in graphs: |
| 97 |
storesink_graph(graph) |
| 98 |
|
| 99 |
if config.profile_based_inline and not secondary: |
| 100 |
threshold = config.profile_based_inline_threshold |
| 101 |
heuristic = get_function(config.profile_based_inline_heuristic) |
| 102 |
inline.instrument_inline_candidates(graphs, threshold) |
| 103 |
counters = translator.driver_instrument_result( |
| 104 |
config.profile_based_inline) |
| 105 |
n = len(counters) |
| 106 |
def call_count_pred(label): |
| 107 |
if label >= n: |
| 108 |
return False |
| 109 |
return counters[label] > 250 |
| 110 |
inline_malloc_removal_phase(config, translator, graphs, |
| 111 |
threshold, |
| 112 |
inline_heuristic=heuristic, |
| 113 |
call_count_pred=call_count_pred) |
| 114 |
constfold(config, graphs) |
| 115 |
|
| 116 |
if config.merge_if_blocks: |
| 117 |
log.mergeifblocks("starting to merge if blocks") |
| 118 |
for graph in graphs: |
| 119 |
merge_if_blocks(graph, translator.config.translation.verbose) |
| 120 |
|
| 121 |
if config.print_statistics: |
| 122 |
print("after if-to-switch:") |
| 123 |
print_statistics(translator.graphs[0], translator) |
| 124 |
|
| 125 |
remove_obvious_noops() |
| 126 |
|
| 127 |
for graph in graphs: |
| 128 |
checkgraph(graph) |
| 129 |
|
| 130 |
gilanalysis.analyze(graphs, translator) |
| 131 |
|
| 132 |
|
| 133 |
def constfold(config, graphs): |
| 134 |
if config.constfold: |
| 135 |
for graph in graphs: |
| 136 |
constant_fold_graph(graph) |
| 137 |
|
| 138 |
def inline_malloc_removal_phase(config, translator, graphs, inline_threshold, |
| 139 |
inline_heuristic, |
| 140 |
call_count_pred=None, |
| 141 |
inline_graph_from_anywhere=False): |
| 142 |
|
| 143 |
if inline_threshold: |
| 144 |
log.inlining("phase with threshold factor: %s" % inline_threshold) |
| 145 |
log.inlining("heuristic: %s.%s" % (inline_heuristic.__module__, |
| 146 |
inline_heuristic.__name__)) |
| 147 |
|
| 148 |
inline.auto_inline_graphs(translator, graphs, inline_threshold, |
| 149 |
heuristic=inline_heuristic, |
| 150 |
call_count_pred=call_count_pred, |
| 151 |
inline_graph_from_anywhere=inline_graph_from_anywhere) |
| 152 |
|
| 153 |
if config.print_statistics: |
| 154 |
print("after inlining:") |
| 155 |
print_statistics(translator.graphs[0], translator) |
| 156 |
|
| 157 |
|
| 158 |
if config.mallocs: |
| 159 |
log.malloc("starting malloc removal") |
| 160 |
remove_mallocs(translator, graphs) |
| 161 |
|
| 162 |
if config.print_statistics: |
| 163 |
print("after malloc removal:") |
| 164 |
print_statistics(translator.graphs[0], translator) |