def test_prof_inline(self): py.test.skip("broken by 5b0e029514d4, but we don't use it any more") if sys.platform == 'win32': py.test.skip("instrumentation support is unix only for now") def add(a, b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from rpython.translator.interactive import Translation t = Translation(entry_point, backend='c') # no counters t.backendopt(inline_threshold=100, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2 t = Translation(entry_point, backend='c') # counters t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST * 0.5, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2
def test_profopt(self): if sys.platform == 'win32': py.test.skip("no profopt on win32") def add(a, b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from rpython.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', profopt="100") # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2 t = Translation(entry_point, backend='c', profopt="100", noprofopt=True) # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2
def test_prof_inline(self): py.test.skip("broken by 5b0e029514d4, but we don't use it any more") if sys.platform == 'win32': py.test.skip("instrumentation support is unix only for now") def add(a,b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from rpython.translator.interactive import Translation t = Translation(entry_point, backend='c') # no counters t.backendopt(inline_threshold=100, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2 t = Translation(entry_point, backend='c') # counters t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST*0.5, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2
def test_profopt(self): if sys.platform == 'win32': py.test.skip("no profopt on win32") def add(a,b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from rpython.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', profopt="100") # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2 t = Translation(entry_point, backend='c', profopt="100", noprofopt=True) # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2
def test_simple_backendopt(): def f(x, y): return x,y t = Translation(f, [int, int], backend='c') t.backendopt() assert 'backendopt_lltype' in t.driver.done
def test_check_that_driver_uses_replace_we_are_jitted(): from rpython.rlib import jit def f(): if jit.we_are_jitted(): return 1 return 2 + jit.we_are_jitted() t = Translation(f, []) t.backendopt() graph = t.driver.translator.graphs[0] assert graph.startblock.exits[0].args[0].value == 2
def translate_to_graph(function, arguments): """ Translate a function to basic blocks and visualize these blocks (see requirements.txt for necessary pip packages); use this mechanism in conjunction with interpret_from_graph :param function: the RPython function to interpret :param arguments: a list of the arguments passed to 'function' :return: the translator RTyper and the basic block graph for the function passed """ assert callable(function) assert isinstance(arguments, list) translation = Translation(function, arguments) translation.annotate() translation.rtype() translation.backendopt() translation.view() return translation.driver.translator.rtyper, translation.driver.translator.graphs[0]
def test_parser(): def f(x): if x: s = "a(X, Y, Z)." else: s = "f(a, X, _, _, X, f(X, 2.455))." term = parsing.parse_file(s) assert isinstance(term, parsing.Nonterminal) return term.symbol assert f(True) == "file" assert f(True) == "file" t = Translation(f) t.annotate([bool]) t.rtype() t.backendopt() func = t.compile_c() assert func(True) == "file" assert func(False) == "file"
def test_profopt_mac_osx_bug(self): if sys.platform == 'win32': py.test.skip("no profopt on win32") def entry_point(argv): import os pid = os.fork() if pid: os.waitpid(pid, 0) else: os._exit(0) return 0 from rpython.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', profopt="") # no counters t.backendopt() exe = t.compile() #py.process.cmdexec(exe) t = Translation(entry_point, backend='c', profopt="", noprofopt=True) # no counters t.backendopt() exe = t.compile()
def compile(self, entry_point, backendopt=True, withsmallfuncsets=None, shared=False, thread=False): t = Translation(entry_point, None, gc="boehm") self.t = t t.set_backend_extra_options(c_debug_defines=True) t.config.translation.reverse_debugger = True t.config.translation.lldebug0 = True t.config.translation.shared = shared t.config.translation.thread = thread if withsmallfuncsets is not None: t.config.translation.withsmallfuncsets = withsmallfuncsets if not backendopt: t.disable(["backendopt_lltype"]) t.annotate() t.rtype() if t.backendopt: t.backendopt() self.exename = t.compile_c() self.rdbname = os.path.join(os.path.dirname(str(self.exename)), 'log.rdb')
def compile(fn, argtypes, view=False, gcpolicy="none", backendopt=True, annotatorpolicy=None, thread=False, **kwds): argtypes_unroll = unrolling_iterable(enumerate(argtypes)) for argtype in argtypes: if argtype not in [ int, float, str, bool, r_ulonglong, r_longlong, r_uint ]: raise Exception("Unsupported argtype, %r" % (argtype, )) def entry_point(argv): args = () for i, argtype in argtypes_unroll: a = argv[i + 1] if argtype is int: args += (int(a), ) elif argtype is r_uint: args += (r_uint(int(a)), ) elif argtype is r_longlong: args += (parse_longlong(a), ) elif argtype is r_ulonglong: args += (parse_ulonglong(a), ) elif argtype is bool: if a == 'True': args += (True, ) else: assert a == 'False' args += (False, ) elif argtype is float: if a == 'inf': args += (INFINITY, ) elif a == '-inf': args += (-INFINITY, ) elif a == 'nan': args += (NAN, ) else: args += (float(a), ) else: if a.startswith('/'): # escaped string if len(a) == 1: a = '' else: l = a[1:].split(',') a = ''.join([chr(int(x)) for x in l]) args += (a, ) res = fn(*args) print "THE RESULT IS:", llrepr_out(res), ";" return 0 t = Translation(entry_point, None, gc=gcpolicy, backend="c", policy=annotatorpolicy, thread=thread, **kwds) if not backendopt: t.disable(["backendopt_lltype"]) t.driver.config.translation.countmallocs = True t.annotate() try: if py.test.config.option.view: t.view() except AttributeError: pass t.rtype() if backendopt: t.backendopt() try: if py.test.config.option.view: t.view() except AttributeError: pass t.compile_c() ll_res = graphof(t.context, fn).getreturnvar().concretetype def output(stdout): for line in stdout.splitlines(False): if len(repr(line)) == len(line) + 2: # no escaped char print line else: print 'REPR:', repr(line) def f(*args, **kwds): expected_extra_mallocs = kwds.pop('expected_extra_mallocs', 0) expected_exception_name = kwds.pop('expected_exception_name', None) assert not kwds assert len(args) == len(argtypes) for arg, argtype in zip(args, argtypes): assert isinstance(arg, argtype) stdout = t.driver.cbuilder.cmdexec( " ".join([llrepr_in(arg) for arg in args]), expect_crash=(expected_exception_name is not None)) # if expected_exception_name is not None: stdout, stderr = stdout print '--- stdout ---' output(stdout) print '--- stderr ---' output(stderr) print '--------------' stderr, prevline, lastline, empty = stderr.rsplit('\n', 3) assert empty == '' expected = 'Fatal RPython error: ' + expected_exception_name assert lastline == expected or prevline == expected return None output(stdout) stdout, lastline, empty = stdout.rsplit('\n', 2) assert empty == '' assert lastline.startswith('MALLOC COUNTERS: ') mallocs, frees = map(int, lastline.split()[2:]) assert stdout.endswith(' ;') pos = stdout.rindex('THE RESULT IS: ') res = stdout[pos + len('THE RESULT IS: '):-2] # if isinstance(expected_extra_mallocs, int): assert mallocs - frees == expected_extra_mallocs else: assert mallocs - frees in expected_extra_mallocs # if ll_res in [ lltype.Signed, lltype.Unsigned, lltype.SignedLongLong, lltype.UnsignedLongLong ]: return int(res) elif ll_res == lltype.Bool: return bool(int(res)) elif ll_res == lltype.Char: assert len(res) == 1 return res elif ll_res == lltype.Float: return float(res) elif ll_res == lltype.Ptr(STR): return res elif ll_res == lltype.Void: return None raise NotImplementedError("parsing %s" % (ll_res, )) class CompilationResult(object): def __repr__(self): return 'CompilationResult(%s)' % (fn.__name__, ) def __call__(self, *args, **kwds): return f(*args, **kwds) cr = CompilationResult() cr.t = t cr.builder = t.driver.cbuilder return cr
def compile(fn, argtypes, view=False, gcpolicy="none", backendopt=True, annotatorpolicy=None, thread=False, **kwds): argtypes_unroll = unrolling_iterable(enumerate(argtypes)) for argtype in argtypes: if argtype not in [int, float, str, bool, r_ulonglong, r_longlong, r_uint]: raise Exception("Unsupported argtype, %r" % (argtype,)) def entry_point(argv): args = () for i, argtype in argtypes_unroll: a = argv[i + 1] if argtype is int: args += (int(a),) elif argtype is r_uint: args += (r_uint(int(a)),) elif argtype is r_longlong: args += (parse_longlong(a),) elif argtype is r_ulonglong: args += (parse_ulonglong(a),) elif argtype is bool: if a == 'True': args += (True,) else: assert a == 'False' args += (False,) elif argtype is float: if a == 'inf': args += (INFINITY,) elif a == '-inf': args += (-INFINITY,) elif a == 'nan': args += (NAN,) else: args += (float(a),) else: if a.startswith('/'): # escaped string if len(a) == 1: a = '' else: l = a[1:].split(',') a = ''.join([chr(int(x)) for x in l]) args += (a,) res = fn(*args) print "THE RESULT IS:", llrepr_out(res), ";" return 0 t = Translation(entry_point, None, gc=gcpolicy, backend="c", policy=annotatorpolicy, thread=thread, **kwds) if not backendopt: t.disable(["backendopt_lltype"]) t.driver.config.translation.countmallocs = True t.annotate() try: if py.test.config.option.view: t.view() except AttributeError: pass t.rtype() if backendopt: t.backendopt() try: if py.test.config.option.view: t.view() except AttributeError: pass t.compile_c() ll_res = graphof(t.context, fn).getreturnvar().concretetype def output(stdout): for line in stdout.splitlines(False): if len(repr(line)) == len(line) + 2: # no escaped char print line else: print 'REPR:', repr(line) def f(*args, **kwds): expected_extra_mallocs = kwds.pop('expected_extra_mallocs', 0) expected_exception_name = kwds.pop('expected_exception_name', None) assert not kwds assert len(args) == len(argtypes) for arg, argtype in zip(args, argtypes): assert isinstance(arg, argtype) stdout = t.driver.cbuilder.cmdexec( " ".join([llrepr_in(arg) for arg in args]), expect_crash=(expected_exception_name is not None)) # if expected_exception_name is not None: stdout, stderr = stdout print '--- stdout ---' output(stdout) print '--- stderr ---' output(stderr) print '--------------' stderr, prevline, lastline, empty = stderr.rsplit('\n', 3) assert empty == '' expected = 'Fatal RPython error: ' + expected_exception_name assert lastline == expected or prevline == expected return None output(stdout) stdout, lastline, empty = stdout.rsplit('\n', 2) assert empty == '' assert lastline.startswith('MALLOC COUNTERS: ') mallocs, frees = map(int, lastline.split()[2:]) assert stdout.endswith(' ;') pos = stdout.rindex('THE RESULT IS: ') res = stdout[pos + len('THE RESULT IS: '):-2] # if isinstance(expected_extra_mallocs, int): assert mallocs - frees == expected_extra_mallocs else: assert mallocs - frees in expected_extra_mallocs # if ll_res in [lltype.Signed, lltype.Unsigned, lltype.SignedLongLong, lltype.UnsignedLongLong]: return int(res) elif ll_res == lltype.Bool: return bool(int(res)) elif ll_res == lltype.Char: assert len(res) == 1 return res elif ll_res == lltype.Float: return float(res) elif ll_res == lltype.Ptr(STR): return res elif ll_res == lltype.Void: return None raise NotImplementedError("parsing %s" % (ll_res,)) class CompilationResult(object): def __repr__(self): return 'CompilationResult(%s)' % (fn.__name__,) def __call__(self, *args, **kwds): return f(*args, **kwds) cr = CompilationResult() cr.t = t cr.builder = t.driver.cbuilder return cr