def test_rpython_dict_new_1(): def new_1(): a = {0: 42} fn, (db, bdlgen) = fncptr_from_rpy_func(new_1, [], lltype.Void) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) fn()
def test_rpython_dict_lookup(): def test_lookup(): a = {0: 42} return a[0] fn, (db, bdlgen) = fncptr_from_rpy_func(test_lookup, [], lltype.Signed) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fn() == 42
def test_rpython_list_new_5(): def new_5(): a = [1, 2, 3, 4, 5] return len(a) fn, (db, bdlgen) = fncptr_from_rpy_func(new_5, [], rffi.LONGLONG) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fn() == 5
def test_rpython_list_new_empty(): def new_empty(): a = [] return a fn, (db, bdlgen) = fncptr_from_rpy_func(new_empty, [], lltype.Void) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) fn()
def test_rpython_dict_new_100(): def new_100(): a = {} for i in range(0, 100): a[i] = i fn, (db, bdlgen) = fncptr_from_rpy_func(new_100, [], lltype.Void) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) fn()
def test_rpython_list_append(): def list_append(n): a = [] for i in range(0, n): a.append(i) return len(a) fn, (db, bdlgen) = fncptr_from_rpy_func(list_append, [rffi.LONGLONG], rffi.LONGLONG) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fn(5) == 5 assert fn(10) == 10 assert fn(100) == 100
def test_quicksort_handcraft(): from perftarget.quicksort import build_quicksort_bundle, setup, teardown fnc, (mu, ctx, bldr) = fncptr_from_py_script(build_quicksort_bundle, None, 'quicksort', [rffi.CArrayPtr(rffi.LONGLONG), lltype.Signed, lltype.Signed], lltype.Void) mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) N = 100 arr, s, e = setup(N) lst = list(arr) fnc(arr, s, e) lst.sort() for i in range(len(lst)): assert lst[i] == arr[i] teardown(arr, s, e)
def test_quicksort(): from perftarget.quicksort import quicksort, setup, teardown fnc, (db, bdlgen) = fncptr_from_rpy_func(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), lltype.Signed, lltype.Signed], lltype.Void) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) N = 100 arr, s, e = setup(N) lst = list(arr) fnc(arr, s, e) lst.sort() for i in range(len(lst)): assert lst[i] == arr[i] teardown(arr, s, e)
def test_fibonacci_iterative(): def fib_iter(n): if n <= 1: return n k = 2 fib_k_2 = 0 fib_k_1 = 1 fib_k = 0 while k <= n: fib_k = fib_k_2 + fib_k_1 fib_k_2 = fib_k_1 fib_k_1 = fib_k k += 1 return fib_k fnc, (db, bdlgen) = fncptr_from_rpy_func(fib_iter, [rffi.ULONGLONG], rffi.ULONGLONG) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fnc(rffi.cast(rffi.ULONGLONG, 20))
def test_fibonacci(): from perftarget.fibonacci import fib fnc, (db, bdlgen) = fncptr_from_rpy_func(fib, [rffi.ULONGLONG], rffi.ULONGLONG) bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) fnc(rffi.cast(rffi.ULONGLONG, 20)) == 6765
def test_load_ref_from_global(): def build_test_bundle(bldr, rmu): """ Builds the following test bundle. .typedef @i64 = int<64> .typedef @refi64 = ref<@i64> .global @gcl <@refi64> .funcsig @sig__i64 = () -> (@i64) .funcdef @test_fnc VERSION @test_fnc.v1 <@sig__i64> { %blk0(): %r = LOAD <@refi64> @gcl %ir = GETIREF <@i64> %r %res = LOAD <@i64> %ir RET %res } :type bldr: rpython.rlib.rmu.MuIRBuilder :type rmu: rpython.rlib.rmu :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID) """ i64 = bldr.gen_sym("@i64") bldr.new_type_int(i64, 64) refi64 = bldr.gen_sym("@refi64") bldr.new_type_ref(refi64, i64) gcl = bldr.gen_sym("@gcl") bldr.new_global_cell(gcl, refi64) sig__i64 = bldr.gen_sym("@sig__i64") bldr.new_funcsig(sig__i64, [], [i64]) test_fnc = bldr.gen_sym("@test_fnc") bldr.new_func(test_fnc, sig__i64) test_fnc_v1 = bldr.gen_sym("@test_fnc.v1") blk0 = bldr.gen_sym("@test_fnc.v1.blk0") r = bldr.gen_sym("@test_fnc.v1.blk0.r") ir = bldr.gen_sym("@test_fnc.v1.blk0.ir") res = bldr.gen_sym("@test_fnc.v1.blk0.res") op_load1 = bldr.gen_sym() bldr.new_load(op_load1, r, False, rmu.MuMemOrd.NOT_ATOMIC, refi64, gcl) op_getiref = bldr.gen_sym() bldr.new_getiref(op_getiref, ir, i64, r) op_load2 = bldr.gen_sym() bldr.new_load(op_load2, res, False, rmu.MuMemOrd.NOT_ATOMIC, i64, ir) op_ret = bldr.gen_sym() bldr.new_ret(op_ret, [res]) bldr.new_bb(blk0, [], [], rmu.MU_NO_ID, [op_load1, op_getiref, op_load2, op_ret]) bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0]) return { "test_fnc": test_fnc, "test_fnc_sig": sig__i64, "result_type": i64, "@i64": i64, "@refi64": refi64, "@gcl": gcl, } def init_heap(ctx, id_dic, rmu): """ :type ctx: rpython.rlib.rmu.MuCtx :type id_dic: dict :type rmu: rpython.rlib.rmu """ ref_hdl = ctx.new_fixed(id_dic['@i64']) iref_hdl = ctx.get_iref(ref_hdl) hdl_num = ctx.handle_from_sint64(42, 64) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, iref_hdl, hdl_num) gcl_hdl = ctx.handle_from_global(id_dic['@gcl']) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, gcl_hdl, ref_hdl) (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, init_heap, 'test_fnc') mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fnp() == 42
def test_preserve_ref_field(): def build_test_bundle(bldr, rmu): """ Builds the following test bundle. .typedef @i64 = int<64> .typedef @refi64 = ref<@i64> .typedef @node = struct <@i64 @refnode> .typedef @refnode = ref<@node> .const @NULL <@refnode> = NULL .global @gcl <@refnode> .funcsig @sig__i64 = () -> (@i64) .funcdef @test_fnc VERSION @test_fnc.v1 <@sig__i64> { %blk0(): %rhd = LOAD <@refnode> @gcl %irhd = GETIREF <@node> %rhd %irhd_fld = GETFIELDIREF <@node 0> %irhd %num = LOAD <@i64> %irhd_fld %irhd_fld2 = GETFIELDIREF <@node 1> %irhd %rnxt = LOAD <@refnode> %irhd_fld2 %irnxt = GETIREF <@node> %rnxt %irnxt_fld = GETFIELDIREF <@node 0> %irnxt %n_nxt = LOAD <@i64> %irnxt_fld %res = ADD <@i64> %num %n_nxt RET %res } :type bldr: rpython.rlib.rmu.MuIRBuilder :type rmu: rpython.rlib.rmu :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID) """ i64 = bldr.gen_sym("@i64") bldr.new_type_int(i64, 64) refi64 = bldr.gen_sym("@refi64") bldr.new_type_ref(refi64, i64) node = bldr.gen_sym("@node") refnode = bldr.gen_sym("@refnode") bldr.new_type_struct(node, [i64, refnode]) bldr.new_type_ref(refnode, node) NULL = bldr.gen_sym("@NULL") bldr.new_const_null(NULL, refnode) gcl = bldr.gen_sym("@gcl") bldr.new_global_cell(gcl, refnode) sig__i64 = bldr.gen_sym("@sig__i64") bldr.new_funcsig(sig__i64, [], [i64]) test_fnc = bldr.gen_sym("@test_fnc") bldr.new_func(test_fnc, sig__i64) test_fnc_v1 = bldr.gen_sym("@test_fnc.v1") blk0 = bldr.gen_sym("@test_fnc.v1.blk0") rhd = bldr.gen_sym("@test_fnc.v1.blk0.rhd") irhd = bldr.gen_sym("@test_fnc.v1.blk0.irhd") irhd_fld = bldr.gen_sym("@test_fnc.v1.blk0.irhd_fld") num = bldr.gen_sym("@test_fnc.v1.blk0.num") irhd_fld2 = bldr.gen_sym("@test_fnc.v1.blk0.irhd_fld2") rnxt = bldr.gen_sym("@test_fnc.v1.blk0.rnxt") irnxt = bldr.gen_sym("@test_fnc.v1.blk0.irnxt") irnxt_fld = bldr.gen_sym("@test_fnc.v1.blk0.irnxt_fld") n_nxt = bldr.gen_sym("@test_fnc.v1.blk0.n_nxt") res = bldr.gen_sym("@test_fnc.v1.blk0.res") op_load1 = bldr.gen_sym() bldr.new_load(op_load1, rhd, False, rmu.MuMemOrd.NOT_ATOMIC, refnode, gcl) op_getiref1 = bldr.gen_sym() bldr.new_getiref(op_getiref1, irhd, node, rhd) op_getfieldiref1 = bldr.gen_sym() bldr.new_getfieldiref(op_getfieldiref1, irhd_fld, False, node, 0, irhd) op_load2 = bldr.gen_sym() bldr.new_load(op_load2, num, False, rmu.MuMemOrd.NOT_ATOMIC, i64, irhd_fld) op_getfieldiref2 = bldr.gen_sym() bldr.new_getfieldiref(op_getfieldiref2, irhd_fld2, False, node, 1, irhd) op_load3 = bldr.gen_sym() bldr.new_load(op_load3, rnxt, False, rmu.MuMemOrd.NOT_ATOMIC, refnode, irhd_fld2) op_getiref2 = bldr.gen_sym() bldr.new_getiref(op_getiref2, irnxt, node, rnxt) op_getfieldiref3 = bldr.gen_sym() bldr.new_getfieldiref(op_getfieldiref3, irnxt_fld, False, node, 0, irnxt) op_load4 = bldr.gen_sym() bldr.new_load(op_load4, n_nxt, False, rmu.MuMemOrd.NOT_ATOMIC, i64, irnxt_fld) op_add = bldr.gen_sym() bldr.new_binop(op_add, res, rmu.MuBinOptr.ADD, i64, num, n_nxt) op_ret = bldr.gen_sym() bldr.new_ret(op_ret, [res]) bldr.new_bb(blk0, [], [], rmu.MU_NO_ID, [ op_load1, op_getiref1, op_getfieldiref1, op_load2, op_getfieldiref2, op_load3, op_getiref2, op_getfieldiref3, op_load4, op_add, op_ret ]) bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0]) return { "test_fnc": test_fnc, "test_fnc_sig": sig__i64, "result_type": i64, "@i64": i64, "@refi64": refi64, "@node": node, "@refnode": refnode, "@NULL": NULL, "@gcl": gcl, } def init_heap(ctx, id_dic, rmu): """ :type ctx: rpython.rlib.rmu.MuCtx :type id_dic: dict :type rmu: rpython.rlib.rmu """ ref_hd = ctx.new_fixed(id_dic['@node']) ref_nxt = ctx.new_fixed(id_dic['@node']) iref_hd = ctx.get_iref(ref_hd) iref_hd_num = ctx.get_field_iref(iref_hd, 0) hdl_num = ctx.handle_from_sint64(42, 64) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, iref_hd_num, hdl_num) iref_hd_nxt = ctx.get_field_iref(iref_hd, 1) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, iref_hd_nxt, ref_nxt) iref_nxt = ctx.get_iref(ref_nxt) iref_nxt_num = ctx.get_field_iref(iref_nxt, 0) hdl_num = ctx.handle_from_sint64(256, 64) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, iref_nxt_num, hdl_num) iref_nxt_nxt = ctx.get_field_iref(iref_nxt, 1) hdl_NULL = ctx.handle_from_const(id_dic['@NULL']) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, iref_nxt_nxt, hdl_NULL) gcl_hdl = ctx.handle_from_global(id_dic['@gcl']) ctx.store(rmu.MuMemOrd.NOT_ATOMIC, gcl_hdl, ref_hd) (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, init_heap, 'test_fnc') mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr)) assert fnp() == 298