Commit 136bb797
136bb797f367Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #128 from dak2/fix/reduce-each-with-object-block-param-shadow
Fix block-param shadowing for reduce and each_with_object
Details
- Commit
- 136bb797f367
- Parent
- d34baf443d0a
- Merge parent
- edc01a685617
spinel_codegen.rbmodifiedView file
@@ -23324,22 +23324,20 @@
2332423324
@in_loop = 12332523325
rt = infer_type(@nd_receiver[nid])2332623326
rc = compile_expr_gc_rooted(@nd_receiver[nid])23327+
# Bind the seed to an outer-scope temp so the expression form can23328+
# surface the final accumulator. CallNode seeds get GC-rooted by23329+
# compile_expr_gc_rooted; literal/local seeds rely on the caller23330+
# already holding a reference for the duration of the loop.2332723331
obj_ct = "mrb_int"23332+
obj_t = "int"23333+
obj_arg_nid = -12332823334
args_id = @nd_arguments[nid]2332923335
if args_id >= 02333023336
aargs = get_args(args_id)2333123337
if aargs.length > 023332-
obj_ct = c_type(infer_type(aargs[0]))23333-
end23334-
end23335-
# Root the seed: it lives across every loop iteration, and any23336-
# allocation inside the block can trigger a GC that would otherwise23337-
# sweep the freshly constructed seed object before the loop ends.23338-
obj_arg_nid = -123339-
if args_id >= 023340-
aargs2 = get_args(args_id)23341-
if aargs2.length > 023342-
obj_arg_nid = aargs2[0]23338+
obj_arg_nid = aargs[0]23339+
obj_t = infer_type(obj_arg_nid)23340+
obj_ct = c_type(obj_t)2334323341
end2334423342
end2334523343
obj_arg = compile_expr_gc_rooted(obj_arg_nid)@@ -23366,6 +23364,7 @@
2336623364
@indent = @indent + 12336723365
push_scope2336823366
declare_var(bp1, elem_type_of_array(rt))23367+
declare_var(bp2, obj_t)2336923368
compile_stmts_body(@nd_body[@nd_block[nid]])2337023369
pop_scope2337123370
@indent = @indent - 1@@ -24659,18 +24658,25 @@
2465924658
end2466024659
2466124660
def compile_reduce_expr(nid)24662-
# Emit the reduce loop as side effects, return accumulator2466324661
compile_reduce_block(nid)24664-
bp1 = get_block_param(nid, 0)24665-
if bp1 == ""24666-
bp1 = "_acc"24667-
end24668-
"lv_" + bp12466924662
end2467024663
2467124664
def compile_reduce_block(nid)24665+
old = @in_loop24666+
@in_loop = 124667+
@needs_gc = 12467224668
rc = compile_expr_gc_rooted(@nd_receiver[nid])24673-
init_val = compile_arg0(nid)24669+
# Hold on to the seed AST nid so we can `infer_type` it later for24670+
# the accumulator's inner-scope registration.24671+
seed_nid = -124672+
args_id = @nd_arguments[nid]24673+
if args_id >= 024674+
aargs = get_args(args_id)24675+
if aargs.length > 024676+
seed_nid = aargs[0]24677+
end24678+
end24679+
init_val = compile_expr_gc_rooted(seed_nid)2467424680
bp1 = get_block_param(nid, 0)2467524681
bp2 = get_block_param(nid, 1)2467624682
if bp1 == ""@@ -24679,16 +24685,51 @@
2467924685
if bp2 == ""2468024686
bp2 = "_x"2468124687
end24682-
emit(" lv_" + bp1 + " = " + init_val + ";")2468324688
rt = infer_type(@nd_receiver[nid])2468424689
pfx = array_c_prefix(rt)2468524690
elem_t = elem_type_of_array(rt)24691+
# bp1 takes the seed's type. Seed-less form is currently treated as24692+
# 0-seeded (init_val resolves to "0" via compile_expr(-1)); seed_t24693+
# falls back to elem_t to keep the type system consistent. This is24694+
# not true Ruby first-element seeding — known limitation.24695+
seed_t = elem_t24696+
if seed_nid >= 024697+
seed_t = infer_type(seed_nid)24698+
end24699+
# See compile_each_slice_block for the typed-shadow + SP_GC_ROOT24700+
# technique inside a C block scope. reduce additionally needs24701+
# result_tmp because it's expression-form: the final value must24702+
# survive past the inner scope's `}` for the caller to consume.24703+
outer_t = find_var_type(bp1)24704+
shadowed = 024705+
if outer_t != "" && outer_t != seed_t24706+
shadowed = 124707+
end24708+
acc_expr = "lv_" + bp124709+
if shadowed == 124710+
result_tmp = new_temp24711+
seed_ct = c_type(seed_t)24712+
# Evaluate init_val once and share it between result_tmp and the24713+
# shadowed lv_<bp1>; some seed expressions (ArrayNode/HashNode24714+
# literals) are not lifted to a temp by compile_expr_gc_rooted.24715+
emit(" " + seed_ct + " " + result_tmp + " = " + init_val + ";")24716+
emit(" {")24717+
@indent = @indent + 124718+
emit(" SP_GC_SAVE();")24719+
emit(" " + seed_ct + " lv_" + bp1 + " = " + result_tmp + ";")24720+
if type_is_pointer(seed_t) == 124721+
emit(" SP_GC_ROOT(lv_" + bp1 + ");")24722+
end24723+
acc_expr = result_tmp24724+
else24725+
emit(" lv_" + bp1 + " = " + init_val + ";")24726+
end2468624727
tmp = new_temp2468724728
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")2468824729
emit(" " + c_type(elem_t) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")2468924730
@indent = @indent + 12469024731
push_scope24691-
declare_var(bp1, elem_t)24732+
declare_var(bp1, seed_t)2469224733
declare_var(bp2, elem_t)2469324734
blk = @nd_block[nid]2469424735
if blk >= 0@@ -24705,6 +24746,13 @@
2470524746
pop_scope2470624747
@indent = @indent - 12470724748
emit(" }")24749+
if shadowed == 124750+
emit(" " + acc_expr + " = lv_" + bp1 + ";")24751+
@indent = @indent - 124752+
emit(" }")24753+
end24754+
@in_loop = old24755+
acc_expr2470824756
end2470924757
2471024758
def compile_reject_expr(nid)test/each_with_object_seed_shadow.rbaddedView file
Loading diff…
test/reduce_acc_shadow.rbaddedView file
Loading diff…