Commit 97fed56b
97fed56baba1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1346 from ryanseys/rs-iexec-trampoline
codegen: instance_exec trampoline with fixed-arity forwarding
Details
- Commit
- 97fed56baba1
- Parent
- 2516ddf9d780
- Merge parent
- 0e6c3e6700db
spinel_codegen.rbmodifiedView file
@@ -870,6 +870,46 @@
870870
pnames[0]871871
end872872
873+
# Returns the &block param's name on (ci, midx), or "" if the874+
# method's last param isn't proc-typed. Sibling of875+
# cls_method_sole_proc_param_name, but allows the method to have876+
# other (required) params before the &block. Used by the877+
# instance_exec trampoline detector, where the trampoline forwards878+
# N required params and the block:879+
# def m(a, b, ..., &blk); instance_exec(a, b, ..., &blk); end880+
# cls_method_sole_proc_param_name requires the method to have881+
# exactly one param; this lifts that restriction by checking only882+
# the last position.883+
def cls_method_trailing_proc_param_name(ci, midx)884+
pnames = cls_meth_pnames_get(ci, midx)885+
ptypes = cls_meth_ptypes_get(ci, midx)886+
if pnames.length == 0 || pnames.length != ptypes.length887+
return ""888+
end889+
last = pnames.length - 1890+
if ptypes[last] != "proc"891+
return ""892+
end893+
# Earlier params must be ordinary (no rest, no keyword). The894+
# baseline supports fixed-arity forwarding only; splat / mixed-args895+
# extensions land in follow-ups.896+
k = 0897+
while k < last898+
pt = ptypes[k]899+
# The required-arg ptypes include type names (int, str_array,900+
# obj_<C>, etc.), never "rest" / "kwrest" / "block" markers. If901+
# a non-required-arg slot appears here, abort the detection so902+
# we don't conflate splat trampolines with the fixed-arity903+
# shape. A follow-up relaxes this to accept a "rest"-typed904+
# final-required-param when the splat trampoline path lands.905+
if pt == "" || pt == "proc"906+
return ""907+
end908+
k = k + 1909+
end910+
pnames[last]911+
end912+
873913
# Detects the exact arity-0 instance_eval trampoline shape:874914
# `def m(&b); instance_eval(&b); end`. Returns 1 when the875915
# (ci, midx) method body is a single CallNode of `instance_eval`@@ -917,6 +957,104 @@
917957
1918958
end919959
960+
# Detects the fixed-arity instance_exec trampoline shape:961+
# def m(a1, ..., aN, &b); instance_exec(a1, ..., aN, &b); end962+
# for N >= 0 with strict 1:1 forwarding -- positional method963+
# params become positional instance_exec args, in order, and964+
# the &b proc param is forwarded as the &-block. Returns 1 on965+
# match, 0 otherwise.966+
#967+
# Unlike instance_eval (arity-0 only, no block params), this968+
# detector accepts methods that take required positional params969+
# alongside the &block, because instance_exec's args become the970+
# block's params at the call site. The user-visible call shape is971+
# `recv.m(arg1, ..., argN) { |p1, ..., pN| body }`; codegen972+
# inlines body with self rebound to recv and block params bound973+
# to the call-site args.974+
#975+
# Boundaries (rejected by returning 0 -> falls through to976+
# ordinary dispatch which will error at codegen with the usual977+
# warn-and-emit-0):978+
# - No mixed-args (literals, ivars in trampoline body)979+
# - No splat (def m(*args, &b))980+
# - No keyword args981+
# Follow-ups relax these once the baseline path is solid.982+
def is_instance_exec_trampoline(ci, midx)983+
bid = cls_method_body_id(ci, midx)984+
if bid < 0985+
return 0986+
end987+
stmts = get_stmts(bid)988+
if stmts.length != 1989+
return 0990+
end991+
s = stmts[0]992+
if @nd_type[s] != "CallNode"993+
return 0994+
end995+
if @nd_name[s] != "instance_exec"996+
return 0997+
end998+
if @nd_receiver[s] >= 0999+
return 01000+
end1001+
# The block-arg must be a bare local-var ref matching the method's1002+
# trailing &block param. Same gate as is_instance_eval_trampoline.1003+
inner = find_block_arg(s)1004+
if inner < 01005+
return 01006+
end1007+
if @nd_type[inner] != "LocalVariableReadNode"1008+
return 01009+
end1010+
bp_name = cls_method_trailing_proc_param_name(ci, midx)1011+
if bp_name == ""1012+
return 01013+
end1014+
if @nd_name[inner] != bp_name1015+
return 01016+
end1017+
# Walk the trampoline body's positional args. The baseline requires1018+
# each arg to be a LocalVariableReadNode matching the method's1019+
# required param at the same index, in order. Mixed-args (literals,1020+
# ivars, splat) reject for now; the strict-enumerable extension1021+
# lands in a follow-up.1022+
pnames = cls_meth_pnames_get(ci, midx)1023+
req_n = pnames.length - 1 # last is the &block, already checked1024+
args_id = @nd_arguments[s]1025+
arg_ids = []1026+
if args_id >= 01027+
arg_ids = get_args(args_id)1028+
end1029+
# The block-arg shows up in get_args too (as BlockArgumentNode).1030+
# Filter to positional args only; CallNode args = positional +1031+
# block-arg, in that order.1032+
positional = []1033+
k = 01034+
while k < arg_ids.length1035+
aid = arg_ids[k]1036+
if @nd_type[aid] != "BlockArgumentNode"1037+
positional.push(aid)1038+
end1039+
k = k + 11040+
end1041+
if positional.length != req_n1042+
return 01043+
end1044+
k = 01045+
while k < req_n1046+
aid = positional[k]1047+
if @nd_type[aid] != "LocalVariableReadNode"1048+
return 01049+
end1050+
if @nd_name[aid] != pnames[k]1051+
return 01052+
end1053+
k = k + 11054+
end1055+
11056+
end1057+
9201058
# Flatten a constant reference into an internal name.9211059
# C -> C9221060
# ::C -> C@@ -12512,6 +12650,13 @@
1251212650
if is_instance_eval_trampoline(ci, midx) == 11251312651
is_tramp = 11251412652
end12653+
# instance_exec trampoline: same dead-body rationale -- every12654+
# call site is splice-rewritten by try_yield_or_trampoline_dispatch,12655+
# so the body's `instance_exec(a, ..., &b)` call would otherwise12656+
# warn-and-emit-0 once per trampoline definition.12657+
if is_instance_exec_trampoline(ci, midx) == 112658+
is_tramp = 112659+
end1251512660
end1251612661
# stub uncalled methods so an inferred-int param1251712662
# doesn't fail C-compile against a narrower ivar slot.@@ -50013,6 +50158,10 @@
5001350158
compile_instance_eval_inlined_stmt(nid, recv)5001450159
return 15001550160
end50161+
if is_instance_exec_trampoline(cls_idx, midx) == 150162+
compile_instance_exec_inlined_stmt(nid, recv, cls_idx, midx)50163+
return 150164+
end5001650165
05001750166
end5001850167
@@ -50082,6 +50231,136 @@
5008250231
@self_override = saved_self_override5008350232
end5008450233
50234+
# Inlines a `recv.m(args) { |params| body }` call when `m` is a50235+
# Fixed-arity instance_exec trampoline:50236+
# def m(a1, ..., aN, &b); instance_exec(a1, ..., aN, &b); end50237+
#50238+
# Hybrid of compile_instance_eval_inlined_stmt (self rebind via50239+
# splice_block_with_self_rebound) and compile_yield_method_call_stmt50240+
# (block-param rename machinery via @inline_rename_map_from / _to).50241+
# The block params become fresh suffixed C locals containing the50242+
# call-site args; the block body is then spliced at the call site50243+
# with @self_override / @instance_eval_self_var / _type set so50244+
# bare method calls and @ivar reads dispatch through the receiver.50245+
#50246+
# TODO: value-typed receivers crash on the pointer cast emitted for50247+
# self_var, same shape as the corresponding TODO in50248+
# compile_instance_eval_inlined_stmt. Expression-position support50249+
# (return value of the call usable in `total = recv.compute(...) { ... }`)50250+
# is a follow-up; this path ships statement-form only.50251+
def compile_instance_exec_inlined_stmt(nid, recv, cci, midx)50252+
blk = @nd_block[nid]50253+
if blk < 050254+
return50255+
end50256+
rtype = infer_type(recv)50257+
cname = ""50258+
if is_obj_type(rtype) == 150259+
cname = rtype[4, rtype.length - 4]50260+
end50261+
if cname == ""50262+
return50263+
end50264+
50265+
# Block-param names from the user's `{ |p1, p2, ...| body }` at50266+
# the call site. Same shape as compile_yield_method_call_stmt's50267+
# block-param extraction; the AST traversal is identical.50268+
bp_names = "".split(",")50269+
bp = @nd_parameters[blk]50270+
if bp >= 050271+
inner = @nd_parameters[bp]50272+
if inner >= 050273+
reqs = parse_id_list(@nd_requireds[inner])50274+
k = 050275+
while k < reqs.length50276+
bp_names.push(@nd_name[reqs[k]])50277+
k = k + 150278+
end50279+
end50280+
end50281+
50282+
# Trampoline-method param types: forward through @cls_meth_ptypes50283+
# so each call-site arg gets a typed local. The trampoline's50284+
# ptypes[k] for k < N (the required params) are the types the50285+
# block body should see for its block params.50286+
ptypes_full = cls_meth_ptypes_get(cci, midx)50287+
req_n = ptypes_full.length - 1 # last is &block50288+
50289+
# Arity check: the user's block at the call site must declare the50290+
# same number of required params as the trampoline forwards. A50291+
# mismatch is a hard error rather than a silent miscompile --50292+
# Spinel uses strict arity here (CRuby's auto-splat / auto-pack is50293+
# not modeled), so the counts must match exactly.50294+
if bp_names.length != req_n50295+
$stderr.puts "Spinel: instance_exec trampoline: " + bp_names.length.to_s + " block param(s) do not match " + req_n.to_s + " trampoline arg(s); Spinel uses strict arity (CRuby's auto-splat / auto-pack is not modeled). Match the counts exactly."50296+
exit(1)50297+
end50298+
50299+
# Evaluate call-site args BEFORE rebinding self -- the args are50300+
# in the outer scope, not the splice context. Mirrors the order50301+
# in compile_yield_method_call_stmt.50302+
args_id = @nd_arguments[nid]50303+
arg_ids = []50304+
if args_id >= 050305+
arg_ids = get_args(args_id)50306+
end50307+
50308+
rc = compile_expr_gc_rooted(recv)50309+
self_var = new_temp50310+
emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")50311+
if @in_gc_scope == 150312+
emit(" SP_GC_ROOT(" + self_var + ");")50313+
end50314+
50315+
# Per-call-site suffix for block-param locals so nested splices50316+
# / same-trampoline-different-call-sites don't shadow. Reuses50317+
# @block_counter (also used by compile_yield_method_call_stmt's50318+
# `_y<N>` suffix); the prefix is `_ix<N>` to distinguish.50319+
@block_counter = @block_counter + 150320+
suffix = "_ix" + @block_counter.to_s50321+
50322+
map_from = "".split(",")50323+
map_to = "".split(",")50324+
50325+
k = 050326+
while k < req_n50327+
pt = "int"50328+
if k < ptypes_full.length50329+
pt = ptypes_full[k]50330+
end50331+
tname = bp_names[k] + suffix50332+
val = "0"50333+
if k < arg_ids.length50334+
val = compile_expr(arg_ids[k])50335+
end50336+
emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")50337+
if type_is_pointer(pt) == 1 && @in_gc_scope == 150338+
emit(" SP_GC_ROOT(lv_" + tname + ");")50339+
end50340+
map_from.push(bp_names[k])50341+
map_to.push(tname)50342+
declare_var(tname, pt)50343+
k = k + 150344+
end50345+
50346+
# Route bare @ivar reads/writes inside the spliced block to the50347+
# rebound receiver (same retrofit as50348+
# compile_instance_eval_inlined_stmt). Save/restore for nested50349+
# rebinds.50350+
saved_self_override = @self_override50351+
@self_override = self_var50352+
saved_in_rmf = @inline_rename_map_from50353+
saved_in_rmt = @inline_rename_map_to50354+
@inline_rename_map_from = map_from50355+
@inline_rename_map_to = map_to50356+
50357+
splice_block_with_self_rebound(@nd_body[blk], self_var, cname)50358+
50359+
@inline_rename_map_from = saved_in_rmf50360+
@inline_rename_map_to = saved_in_rmt50361+
@self_override = saved_self_override50362+
end50363+
5008550364
def compile_yield_method_call_stmt(nid, cci, midx, mname)5008650365
# Call a yield-using class method with a block - inline the method body5008750366
blk = @nd_block[nid]test/instance_exec_trampoline.rbaddedView file
Loading diff…
test/instance_exec_trampoline.rb.expectedaddedView file
Loading diff…