| 1 |
import sys |
| 2 |
import os |
| 3 |
import platform as _stdlib_platform |
| 4 |
from rpython.config.config import OptionDescription, BoolOption, IntOption, ArbitraryOption, FloatOption |
| 5 |
from rpython.config.config import ChoiceOption, StrOption, Config, ConflictConfigError |
| 6 |
from rpython.config.config import ConfigError |
| 7 |
from rpython.config.support import detect_number_of_processors |
| 8 |
from rpython.translator.platform import platform as compiler |
| 9 |
|
| 10 |
|
| 11 |
DEFL_INLINE_THRESHOLD = 32.4 |
| 12 |
|
| 13 |
|
| 14 |
DEFL_PROF_BASED_INLINE_THRESHOLD = 32.4 |
| 15 |
DEFL_LOW_INLINE_THRESHOLD = DEFL_INLINE_THRESHOLD / 2.0 |
| 16 |
|
| 17 |
DEFL_GC = "incminimark" |
| 18 |
|
| 19 |
DEFL_ROOTFINDER_WITHJIT = "shadowstack" |
| 20 |
|
| 21 |
IS_64_BITS = sys.maxint > 2147483647 |
| 22 |
|
| 23 |
SUPPORT__THREAD = ( |
| 24 |
sys.platform.startswith("linux") or |
| 25 |
|
| 26 |
False) |
| 27 |
|
| 28 |
|
| 29 |
MACHINE = _stdlib_platform.machine() |
| 30 |
if MACHINE == 'aarch64': |
| 31 |
SUPPORT__THREAD = False |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
MAINDIR = os.path.dirname(os.path.dirname(__file__)) |
| 37 |
CACHE_DIR = os.path.realpath(os.path.join(MAINDIR, '_cache', MACHINE)) |
| 38 |
|
| 39 |
PLATFORMS = [ |
| 40 |
'host', |
| 41 |
'arm', |
| 42 |
] |
| 43 |
|
| 44 |
translation_optiondescription = OptionDescription( |
| 45 |
"translation", "Translation Options", [ |
| 46 |
BoolOption("continuation", "enable single-shot continuations", |
| 47 |
default=False, cmdline="--continuation", |
| 48 |
requires=[("translation.type_system", "lltype")]), |
| 49 |
ChoiceOption("type_system", "Type system to use when RTyping", |
| 50 |
["lltype"], cmdline=None, default="lltype"), |
| 51 |
ChoiceOption("backend", "Backend to use for code generation", |
| 52 |
["c"], default="c", |
| 53 |
requires={ |
| 54 |
"c": [("translation.type_system", "lltype")], |
| 55 |
}, |
| 56 |
cmdline="-b --backend"), |
| 57 |
|
| 58 |
BoolOption("shared", "Build as a shared library", |
| 59 |
default=False, cmdline="--shared"), |
| 60 |
|
| 61 |
BoolOption("log", "Include debug prints in the translation (PYPYLOG=...)", |
| 62 |
default=True, cmdline="--log"), |
| 63 |
|
| 64 |
|
| 65 |
ChoiceOption("gc", "Garbage Collection Strategy", |
| 66 |
["boehm", "ref", "semispace", "statistics", |
| 67 |
"generation", "hybrid", "minimark",'incminimark', "none"], |
| 68 |
"ref", requires={ |
| 69 |
"ref": [("translation.rweakref", False), |
| 70 |
("translation.gctransformer", "ref")], |
| 71 |
"none": [("translation.rweakref", False), |
| 72 |
("translation.gctransformer", "none")], |
| 73 |
"semispace": [("translation.gctransformer", "framework")], |
| 74 |
"statistics": [("translation.gctransformer", "framework")], |
| 75 |
"generation": [("translation.gctransformer", "framework")], |
| 76 |
"hybrid": [("translation.gctransformer", "framework")], |
| 77 |
"boehm": [("translation.continuation", False), |
| 78 |
("translation.gctransformer", "boehm")], |
| 79 |
"minimark": [("translation.gctransformer", "framework")], |
| 80 |
"incminimark": [("translation.gctransformer", "framework")], |
| 81 |
}, |
| 82 |
cmdline="--gc"), |
| 83 |
ChoiceOption("gctransformer", "GC transformer that is used - internal", |
| 84 |
["boehm", "ref", "framework", "none"], |
| 85 |
default="ref", cmdline=None, |
| 86 |
requires={ |
| 87 |
"boehm": [("translation.gcrootfinder", "n/a"), |
| 88 |
("translation.gcremovetypeptr", False)], |
| 89 |
"ref": [("translation.gcrootfinder", "n/a"), |
| 90 |
("translation.gcremovetypeptr", False)], |
| 91 |
"none": [("translation.gcrootfinder", "n/a"), |
| 92 |
("translation.gcremovetypeptr", False)], |
| 93 |
}), |
| 94 |
BoolOption("gcremovetypeptr", "Remove the typeptr from every object", |
| 95 |
default=IS_64_BITS, cmdline="--gcremovetypeptr"), |
| 96 |
ChoiceOption("gcrootfinder", |
| 97 |
"Strategy for finding GC Roots (framework GCs only)", |
| 98 |
["n/a", "shadowstack"], |
| 99 |
"shadowstack", |
| 100 |
cmdline="--gcrootfinder", |
| 101 |
requires={ |
| 102 |
"shadowstack": [("translation.gctransformer", "framework")], |
| 103 |
}), |
| 104 |
|
| 105 |
|
| 106 |
BoolOption("thread", "enable use of threading primitives", |
| 107 |
default=False, cmdline="--thread"), |
| 108 |
BoolOption("sandbox", "Produce a fully-sandboxed executable", |
| 109 |
default=False, cmdline="--sandbox", |
| 110 |
suggests=[("translation.gc", "generation"), |
| 111 |
("translation.gcrootfinder", "shadowstack"), |
| 112 |
("translation.thread", False), |
| 113 |
]), |
| 114 |
BoolOption("rweakref", "The backend supports RPython-level weakrefs", |
| 115 |
default=True), |
| 116 |
|
| 117 |
|
| 118 |
BoolOption("jit", "generate a JIT", |
| 119 |
default=False, |
| 120 |
suggests=[("translation.gc", DEFL_GC), |
| 121 |
("translation.gcrootfinder", DEFL_ROOTFINDER_WITHJIT), |
| 122 |
("translation.list_comprehension_operations", True)]), |
| 123 |
ChoiceOption("jit_backend", "choose the backend for the JIT", |
| 124 |
["auto", "x86", "x86-without-sse2", 'arm'], |
| 125 |
default="auto", cmdline="--jit-backend"), |
| 126 |
ChoiceOption("jit_profiler", "integrate profiler support into the JIT", |
| 127 |
["off", "oprofile"], |
| 128 |
default="off"), |
| 129 |
BoolOption("check_str_without_nul", |
| 130 |
"Forbid NUL chars in strings in some external function calls", |
| 131 |
default=False, cmdline=None), |
| 132 |
|
| 133 |
|
| 134 |
BoolOption("verbose", "Print extra information", default=False, |
| 135 |
cmdline="--verbose"), |
| 136 |
StrOption("cc", "Specify compiler to use for compiling generated C", cmdline="--cc"), |
| 137 |
BoolOption("profopt", "Enable profile guided optimization. Defaults to enabling this for PyPy. For other training workloads, please specify them in profoptargs", |
| 138 |
cmdline="--profopt", default=False), |
| 139 |
StrOption("profoptargs", "Absolute path to the profile guided optimization training script + the necessary arguments of the script", cmdline="--profoptargs", default=None), |
| 140 |
BoolOption("instrument", "internal: turn instrumentation on", |
| 141 |
default=False, cmdline=None), |
| 142 |
BoolOption("countmallocs", "Count mallocs and frees", default=False, |
| 143 |
cmdline=None), |
| 144 |
BoolOption("countfieldaccess", "Count field access for C structs", |
| 145 |
default=False), |
| 146 |
ChoiceOption("fork_before", |
| 147 |
"(UNIX) Create restartable checkpoint before step", |
| 148 |
["annotate", "rtype", "backendopt", "database", "source", |
| 149 |
"pyjitpl"], |
| 150 |
default=None, cmdline="--fork-before"), |
| 151 |
BoolOption("dont_write_c_files", |
| 152 |
"Make the C backend write everyting to /dev/null. " + |
| 153 |
"Useful for benchmarking, so you don't actually involve the disk", |
| 154 |
default=False, cmdline="--dont-write-c-files"), |
| 155 |
ArbitraryOption("instrumentctl", "internal", |
| 156 |
default=None), |
| 157 |
StrOption("output", "Output file name", cmdline="--output"), |
| 158 |
StrOption("secondaryentrypoints", |
| 159 |
"Comma separated list of keys choosing secondary entrypoints", |
| 160 |
cmdline="--entrypoints", default="main"), |
| 161 |
|
| 162 |
BoolOption("dump_static_data_info", "Dump static data info", |
| 163 |
cmdline="--dump_static_data_info", |
| 164 |
default=False, requires=[("translation.backend", "c")]), |
| 165 |
|
| 166 |
|
| 167 |
BoolOption("no__thread", |
| 168 |
"don't use __thread for implementing TLS", |
| 169 |
default=not SUPPORT__THREAD, cmdline="--no__thread", |
| 170 |
negation=False), |
| 171 |
IntOption("make_jobs", "Specify -j argument to make for compilation" |
| 172 |
" (C backend only)", |
| 173 |
cmdline="--make-jobs", default=detect_number_of_processors()), |
| 174 |
|
| 175 |
|
| 176 |
BoolOption("list_comprehension_operations", |
| 177 |
"When true, look for and special-case the sequence of " |
| 178 |
"operations that results from a list comprehension and " |
| 179 |
"attempt to pre-allocate the list", |
| 180 |
default=False, |
| 181 |
cmdline='--listcompr'), |
| 182 |
IntOption("withsmallfuncsets", |
| 183 |
"Represent groups of less funtions than this as indices into an array", |
| 184 |
default=0), |
| 185 |
BoolOption("taggedpointers", |
| 186 |
"When true, enable the use of tagged pointers. " |
| 187 |
"If false, use normal boxing", |
| 188 |
default=False), |
| 189 |
BoolOption("keepgoing", |
| 190 |
"Continue annotating when errors are encountered, and report " |
| 191 |
"them all at the end of the annotation phase", |
| 192 |
default=False, cmdline="--keepgoing"), |
| 193 |
BoolOption("lldebug", |
| 194 |
"If true, makes an lldebug build", default=False, |
| 195 |
cmdline="--lldebug"), |
| 196 |
BoolOption("lldebug0", |
| 197 |
"If true, makes an lldebug0 build", default=False, |
| 198 |
cmdline="--lldebug0"), |
| 199 |
BoolOption("lto", "enable link time optimization", |
| 200 |
default=False, cmdline="--lto", |
| 201 |
requires=[("translation.gcrootfinder", "shadowstack")]), |
| 202 |
StrOption("icon", "Path to the (Windows) icon to use for the executable"), |
| 203 |
StrOption("manifest", |
| 204 |
"Path to the (Windows) manifest to embed in the executable"), |
| 205 |
StrOption("libname", |
| 206 |
"Windows: name and possibly location of the lib file to create"), |
| 207 |
|
| 208 |
OptionDescription("backendopt", "Backend Optimization Options", [ |
| 209 |
|
| 210 |
BoolOption("inline", "Do basic inlining and malloc removal", |
| 211 |
default=True), |
| 212 |
FloatOption("inline_threshold", "Threshold when to inline functions", |
| 213 |
default=DEFL_INLINE_THRESHOLD, cmdline="--inline-threshold"), |
| 214 |
StrOption("inline_heuristic", "Dotted name of an heuristic function " |
| 215 |
"for inlining", |
| 216 |
default="rpython.translator.backendopt.inline.inlining_heuristic", |
| 217 |
cmdline="--inline-heuristic"), |
| 218 |
|
| 219 |
BoolOption("print_statistics", "Print statistics while optimizing", |
| 220 |
default=False), |
| 221 |
BoolOption("merge_if_blocks", "Merge if ... elif chains", |
| 222 |
cmdline="--if-block-merge", default=True), |
| 223 |
BoolOption("mallocs", "Remove mallocs", default=True), |
| 224 |
BoolOption("constfold", "Constant propagation", |
| 225 |
default=True), |
| 226 |
|
| 227 |
StrOption("profile_based_inline", |
| 228 |
"Use call count profiling to drive inlining" |
| 229 |
", specify arguments", |
| 230 |
default=None), |
| 231 |
FloatOption("profile_based_inline_threshold", |
| 232 |
"Threshold when to inline functions " |
| 233 |
"for profile based inlining", |
| 234 |
default=DEFL_PROF_BASED_INLINE_THRESHOLD, |
| 235 |
), |
| 236 |
StrOption("profile_based_inline_heuristic", |
| 237 |
"Dotted name of an heuristic function " |
| 238 |
"for profile based inlining", |
| 239 |
default="rpython.translator.backendopt.inline.inlining_heuristic", |
| 240 |
), |
| 241 |
|
| 242 |
BoolOption("remove_asserts", |
| 243 |
"Remove operations that look like 'raise AssertionError', " |
| 244 |
"which lets the C optimizer remove the asserts", |
| 245 |
default=False), |
| 246 |
BoolOption("really_remove_asserts", |
| 247 |
"Really remove operations that look like 'raise AssertionError', " |
| 248 |
"without relying on the C compiler", |
| 249 |
default=False), |
| 250 |
|
| 251 |
BoolOption("storesink", "Perform store sinking", default=True), |
| 252 |
BoolOption("replace_we_are_jitted", |
| 253 |
"Replace we_are_jitted() calls by False", |
| 254 |
default=False, cmdline=None), |
| 255 |
BoolOption("none", |
| 256 |
"Do not run any backend optimizations", |
| 257 |
requires=[('translation.backendopt.inline', False), |
| 258 |
('translation.backendopt.inline_threshold', 0), |
| 259 |
('translation.backendopt.merge_if_blocks', False), |
| 260 |
('translation.backendopt.mallocs', False), |
| 261 |
('translation.backendopt.constfold', False)]) |
| 262 |
]), |
| 263 |
|
| 264 |
ChoiceOption("platform", |
| 265 |
"target platform", ['host'] + PLATFORMS, default='host', |
| 266 |
cmdline='--platform', |
| 267 |
suggests={"arm": [("translation.gcrootfinder", "shadowstack"), |
| 268 |
("translation.jit_backend", "arm")]}), |
| 269 |
|
| 270 |
BoolOption("split_gc_address_space", |
| 271 |
"Ensure full separation of GC and non-GC pointers", default=False), |
| 272 |
BoolOption("reverse_debugger", |
| 273 |
"Give an executable that writes a log file for reverse debugging", |
| 274 |
default=False, cmdline='--revdb', |
| 275 |
requires=[('translation.split_gc_address_space', True), |
| 276 |
('translation.jit', False), |
| 277 |
('translation.gc', 'boehm'), |
| 278 |
('translation.continuation', False)]), |
| 279 |
BoolOption("rpython_translate", |
| 280 |
"Set to true by rpython/bin/rpython and translate.py", |
| 281 |
default=False), |
| 282 |
]) |
| 283 |
|
| 284 |
def get_combined_translation_config(other_optdescr=None, |
| 285 |
existing_config=None, |
| 286 |
overrides=None, |
| 287 |
translating=False): |
| 288 |
if overrides is None: |
| 289 |
overrides = {} |
| 290 |
d = BoolOption("translating", |
| 291 |
"indicates whether we are translating currently", |
| 292 |
default=False, cmdline=None) |
| 293 |
if other_optdescr is None: |
| 294 |
children = [] |
| 295 |
newname = "" |
| 296 |
else: |
| 297 |
children = [other_optdescr] |
| 298 |
newname = other_optdescr._name |
| 299 |
if existing_config is None: |
| 300 |
children += [d, translation_optiondescription] |
| 301 |
else: |
| 302 |
children += [child for child in existing_config._cfgimpl_descr._children |
| 303 |
if child._name != newname] |
| 304 |
descr = OptionDescription("pypy", "all options", children) |
| 305 |
config = Config(descr, **overrides) |
| 306 |
if translating: |
| 307 |
config.translating = True |
| 308 |
if existing_config is not None: |
| 309 |
for child in existing_config._cfgimpl_descr._children: |
| 310 |
if child._name == newname: |
| 311 |
continue |
| 312 |
value = getattr(existing_config, child._name) |
| 313 |
config._cfgimpl_values[child._name] = value |
| 314 |
return config |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
OPT_LEVELS = ['0', '1', 'size', 'mem', '2', '3', 'jit'] |
| 319 |
DEFAULT_OPT_LEVEL = '2' |
| 320 |
|
| 321 |
OPT_TABLE_DOC = { |
| 322 |
'0': 'No optimization. Uses the Boehm GC.', |
| 323 |
'1': 'Enable a default set of optimizations. Uses the Boehm GC.', |
| 324 |
'size': 'Optimize for the size of the executable. Uses the Boehm GC.', |
| 325 |
'mem': 'Optimize for run-time memory usage and use a memory-saving GC.', |
| 326 |
'2': 'Enable most optimizations and use a high-performance GC.', |
| 327 |
'3': 'Enable all optimizations and use a high-performance GC.', |
| 328 |
'jit': 'Enable the JIT.', |
| 329 |
} |
| 330 |
|
| 331 |
OPT_TABLE = { |
| 332 |
|
| 333 |
'0': 'boehm nobackendopt', |
| 334 |
'1': 'boehm lowinline', |
| 335 |
'size': 'boehm lowinline remove_asserts', |
| 336 |
'mem': DEFL_GC + ' lowinline remove_asserts removetypeptr', |
| 337 |
'2': DEFL_GC + ' extraopts', |
| 338 |
'3': DEFL_GC + ' extraopts remove_asserts', |
| 339 |
'jit': DEFL_GC + ' extraopts jit', |
| 340 |
} |
| 341 |
|
| 342 |
def set_opt_level(config, level): |
| 343 |
"""Apply optimization suggestions on the 'config'. |
| 344 |
The optimizations depend on the selected level and possibly on the backend. |
| 345 |
""" |
| 346 |
try: |
| 347 |
opts = OPT_TABLE[level] |
| 348 |
except KeyError: |
| 349 |
raise ConfigError("no such optimization level: %r" % (level,)) |
| 350 |
words = opts.split() |
| 351 |
gc = words.pop(0) |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if config.translation._cfgimpl_value_owners['gc'] != 'suggested': |
| 356 |
config.translation.suggest(gc=gc) |
| 357 |
|
| 358 |
|
| 359 |
for word in words: |
| 360 |
if word == 'nobackendopt': |
| 361 |
config.translation.backendopt.suggest(none=True) |
| 362 |
elif word == 'lowinline': |
| 363 |
config.translation.backendopt.suggest(inline_threshold= |
| 364 |
DEFL_LOW_INLINE_THRESHOLD) |
| 365 |
elif word == 'remove_asserts': |
| 366 |
config.translation.backendopt.suggest(remove_asserts=True) |
| 367 |
elif word == 'extraopts': |
| 368 |
config.translation.suggest(withsmallfuncsets=5) |
| 369 |
elif word == 'jit': |
| 370 |
config.translation.suggest(jit=True) |
| 371 |
elif word == 'removetypeptr': |
| 372 |
config.translation.suggest(gcremovetypeptr=True) |
| 373 |
else: |
| 374 |
raise ValueError(word) |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
config.translation.suggest(list_comprehension_operations=True) |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
config.translation.gc = config.translation.gc |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
def set_platform(config): |
| 388 |
from rpython.translator.platform import set_platform |
| 389 |
set_platform(config.translation.platform, config.translation.cc) |
| 390 |
|
| 391 |
def get_platform(config): |
| 392 |
from rpython.translator.platform import pick_platform |
| 393 |
opt = config.translation.platform |
| 394 |
cc = config.translation.cc |
| 395 |
return pick_platform(opt, cc) |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
_GLOBAL_TRANSLATIONCONFIG = None |
| 402 |
|
| 403 |
|
| 404 |
def get_translation_config(): |
| 405 |
""" Return the translation config when translating. When running |
| 406 |
un-translated returns None """ |
| 407 |
return _GLOBAL_TRANSLATIONCONFIG |
| 408 |
|