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 end
872872
873+ # Returns the &block param's name on (ci, midx), or "" if the
874+ # method's last param isn't proc-typed. Sibling of
875+ # cls_method_sole_proc_param_name, but allows the method to have
876+ # other (required) params before the &block. Used by the
877+ # instance_exec trampoline detector, where the trampoline forwards
878+ # N required params and the block:
879+ # def m(a, b, ..., &blk); instance_exec(a, b, ..., &blk); end
880+ # cls_method_sole_proc_param_name requires the method to have
881+ # exactly one param; this lifts that restriction by checking only
882+ # 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.length
887+ return ""
888+ end
889+ last = pnames.length - 1
890+ if ptypes[last] != "proc"
891+ return ""
892+ end
893+ # Earlier params must be ordinary (no rest, no keyword). The
894+ # baseline supports fixed-arity forwarding only; splat / mixed-args
895+ # extensions land in follow-ups.
896+ k = 0
897+ while k < last
898+ pt = ptypes[k]
899+ # The required-arg ptypes include type names (int, str_array,
900+ # obj_<C>, etc.), never "rest" / "kwrest" / "block" markers. If
901+ # a non-required-arg slot appears here, abort the detection so
902+ # we don't conflate splat trampolines with the fixed-arity
903+ # shape. A follow-up relaxes this to accept a "rest"-typed
904+ # final-required-param when the splat trampoline path lands.
905+ if pt == "" || pt == "proc"
906+ return ""
907+ end
908+ k = k + 1
909+ end
910+ pnames[last]
911+ end
912+
873913 # Detects the exact arity-0 instance_eval trampoline shape:
874914 # `def m(&b); instance_eval(&b); end`. Returns 1 when the
875915 # (ci, midx) method body is a single CallNode of `instance_eval`
@@ -917,6 +957,104 @@
917957 1
918958 end
919959
960+ # Detects the fixed-arity instance_exec trampoline shape:
961+ # def m(a1, ..., aN, &b); instance_exec(a1, ..., aN, &b); end
962+ # for N >= 0 with strict 1:1 forwarding -- positional method
963+ # params become positional instance_exec args, in order, and
964+ # the &b proc param is forwarded as the &-block. Returns 1 on
965+ # match, 0 otherwise.
966+ #
967+ # Unlike instance_eval (arity-0 only, no block params), this
968+ # detector accepts methods that take required positional params
969+ # alongside the &block, because instance_exec's args become the
970+ # block's params at the call site. The user-visible call shape is
971+ # `recv.m(arg1, ..., argN) { |p1, ..., pN| body }`; codegen
972+ # inlines body with self rebound to recv and block params bound
973+ # to the call-site args.
974+ #
975+ # Boundaries (rejected by returning 0 -> falls through to
976+ # ordinary dispatch which will error at codegen with the usual
977+ # warn-and-emit-0):
978+ # - No mixed-args (literals, ivars in trampoline body)
979+ # - No splat (def m(*args, &b))
980+ # - No keyword args
981+ # 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 < 0
985+ return 0
986+ end
987+ stmts = get_stmts(bid)
988+ if stmts.length != 1
989+ return 0
990+ end
991+ s = stmts[0]
992+ if @nd_type[s] != "CallNode"
993+ return 0
994+ end
995+ if @nd_name[s] != "instance_exec"
996+ return 0
997+ end
998+ if @nd_receiver[s] >= 0
999+ return 0
1000+ end
1001+ # The block-arg must be a bare local-var ref matching the method's
1002+ # trailing &block param. Same gate as is_instance_eval_trampoline.
1003+ inner = find_block_arg(s)
1004+ if inner < 0
1005+ return 0
1006+ end
1007+ if @nd_type[inner] != "LocalVariableReadNode"
1008+ return 0
1009+ end
1010+ bp_name = cls_method_trailing_proc_param_name(ci, midx)
1011+ if bp_name == ""
1012+ return 0
1013+ end
1014+ if @nd_name[inner] != bp_name
1015+ return 0
1016+ end
1017+ # Walk the trampoline body's positional args. The baseline requires
1018+ # each arg to be a LocalVariableReadNode matching the method's
1019+ # required param at the same index, in order. Mixed-args (literals,
1020+ # ivars, splat) reject for now; the strict-enumerable extension
1021+ # lands in a follow-up.
1022+ pnames = cls_meth_pnames_get(ci, midx)
1023+ req_n = pnames.length - 1 # last is the &block, already checked
1024+ args_id = @nd_arguments[s]
1025+ arg_ids = []
1026+ if args_id >= 0
1027+ arg_ids = get_args(args_id)
1028+ end
1029+ # 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 = 0
1034+ while k < arg_ids.length
1035+ aid = arg_ids[k]
1036+ if @nd_type[aid] != "BlockArgumentNode"
1037+ positional.push(aid)
1038+ end
1039+ k = k + 1
1040+ end
1041+ if positional.length != req_n
1042+ return 0
1043+ end
1044+ k = 0
1045+ while k < req_n
1046+ aid = positional[k]
1047+ if @nd_type[aid] != "LocalVariableReadNode"
1048+ return 0
1049+ end
1050+ if @nd_name[aid] != pnames[k]
1051+ return 0
1052+ end
1053+ k = k + 1
1054+ end
1055+ 1
1056+ end
1057+
9201058 # Flatten a constant reference into an internal name.
9211059 # C -> C
9221060 # ::C -> C
@@ -12512,6 +12650,13 @@
1251212650 if is_instance_eval_trampoline(ci, midx) == 1
1251312651 is_tramp = 1
1251412652 end
12653+ # instance_exec trampoline: same dead-body rationale -- every
12654+ # call site is splice-rewritten by try_yield_or_trampoline_dispatch,
12655+ # so the body's `instance_exec(a, ..., &b)` call would otherwise
12656+ # warn-and-emit-0 once per trampoline definition.
12657+ if is_instance_exec_trampoline(ci, midx) == 1
12658+ is_tramp = 1
12659+ end
1251512660 end
1251612661 # stub uncalled methods so an inferred-int param
1251712662 # doesn't fail C-compile against a narrower ivar slot.
@@ -50013,6 +50158,10 @@
5001350158 compile_instance_eval_inlined_stmt(nid, recv)
5001450159 return 1
5001550160 end
50161+ if is_instance_exec_trampoline(cls_idx, midx) == 1
50162+ compile_instance_exec_inlined_stmt(nid, recv, cls_idx, midx)
50163+ return 1
50164+ end
5001650165 0
5001750166 end
5001850167
@@ -50082,6 +50231,136 @@
5008250231 @self_override = saved_self_override
5008350232 end
5008450233
50234+ # Inlines a `recv.m(args) { |params| body }` call when `m` is a
50235+ # Fixed-arity instance_exec trampoline:
50236+ # def m(a1, ..., aN, &b); instance_exec(a1, ..., aN, &b); end
50237+ #
50238+ # Hybrid of compile_instance_eval_inlined_stmt (self rebind via
50239+ # splice_block_with_self_rebound) and compile_yield_method_call_stmt
50240+ # (block-param rename machinery via @inline_rename_map_from / _to).
50241+ # The block params become fresh suffixed C locals containing the
50242+ # call-site args; the block body is then spliced at the call site
50243+ # with @self_override / @instance_eval_self_var / _type set so
50244+ # bare method calls and @ivar reads dispatch through the receiver.
50245+ #
50246+ # TODO: value-typed receivers crash on the pointer cast emitted for
50247+ # self_var, same shape as the corresponding TODO in
50248+ # compile_instance_eval_inlined_stmt. Expression-position support
50249+ # (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 < 0
50254+ return
50255+ end
50256+ rtype = infer_type(recv)
50257+ cname = ""
50258+ if is_obj_type(rtype) == 1
50259+ cname = rtype[4, rtype.length - 4]
50260+ end
50261+ if cname == ""
50262+ return
50263+ end
50264+
50265+ # Block-param names from the user's `{ |p1, p2, ...| body }` at
50266+ # the call site. Same shape as compile_yield_method_call_stmt's
50267+ # block-param extraction; the AST traversal is identical.
50268+ bp_names = "".split(",")
50269+ bp = @nd_parameters[blk]
50270+ if bp >= 0
50271+ inner = @nd_parameters[bp]
50272+ if inner >= 0
50273+ reqs = parse_id_list(@nd_requireds[inner])
50274+ k = 0
50275+ while k < reqs.length
50276+ bp_names.push(@nd_name[reqs[k]])
50277+ k = k + 1
50278+ end
50279+ end
50280+ end
50281+
50282+ # Trampoline-method param types: forward through @cls_meth_ptypes
50283+ # so each call-site arg gets a typed local. The trampoline's
50284+ # ptypes[k] for k < N (the required params) are the types the
50285+ # 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 &block
50288+
50289+ # Arity check: the user's block at the call site must declare the
50290+ # same number of required params as the trampoline forwards. A
50291+ # mismatch is a hard error rather than a silent miscompile --
50292+ # Spinel uses strict arity here (CRuby's auto-splat / auto-pack is
50293+ # not modeled), so the counts must match exactly.
50294+ if bp_names.length != req_n
50295+ $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+ end
50298+
50299+ # Evaluate call-site args BEFORE rebinding self -- the args are
50300+ # in the outer scope, not the splice context. Mirrors the order
50301+ # in compile_yield_method_call_stmt.
50302+ args_id = @nd_arguments[nid]
50303+ arg_ids = []
50304+ if args_id >= 0
50305+ arg_ids = get_args(args_id)
50306+ end
50307+
50308+ rc = compile_expr_gc_rooted(recv)
50309+ self_var = new_temp
50310+ emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")
50311+ if @in_gc_scope == 1
50312+ emit(" SP_GC_ROOT(" + self_var + ");")
50313+ end
50314+
50315+ # Per-call-site suffix for block-param locals so nested splices
50316+ # / same-trampoline-different-call-sites don't shadow. Reuses
50317+ # @block_counter (also used by compile_yield_method_call_stmt's
50318+ # `_y<N>` suffix); the prefix is `_ix<N>` to distinguish.
50319+ @block_counter = @block_counter + 1
50320+ suffix = "_ix" + @block_counter.to_s
50321+
50322+ map_from = "".split(",")
50323+ map_to = "".split(",")
50324+
50325+ k = 0
50326+ while k < req_n
50327+ pt = "int"
50328+ if k < ptypes_full.length
50329+ pt = ptypes_full[k]
50330+ end
50331+ tname = bp_names[k] + suffix
50332+ val = "0"
50333+ if k < arg_ids.length
50334+ val = compile_expr(arg_ids[k])
50335+ end
50336+ emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")
50337+ if type_is_pointer(pt) == 1 && @in_gc_scope == 1
50338+ emit(" SP_GC_ROOT(lv_" + tname + ");")
50339+ end
50340+ map_from.push(bp_names[k])
50341+ map_to.push(tname)
50342+ declare_var(tname, pt)
50343+ k = k + 1
50344+ end
50345+
50346+ # Route bare @ivar reads/writes inside the spliced block to the
50347+ # rebound receiver (same retrofit as
50348+ # compile_instance_eval_inlined_stmt). Save/restore for nested
50349+ # rebinds.
50350+ saved_self_override = @self_override
50351+ @self_override = self_var
50352+ saved_in_rmf = @inline_rename_map_from
50353+ saved_in_rmt = @inline_rename_map_to
50354+ @inline_rename_map_from = map_from
50355+ @inline_rename_map_to = map_to
50356+
50357+ splice_block_with_self_rebound(@nd_body[blk], self_var, cname)
50358+
50359+ @inline_rename_map_from = saved_in_rmf
50360+ @inline_rename_map_to = saved_in_rmt
50361+ @self_override = saved_self_override
50362+ end
50363+
5008550364 def compile_yield_method_call_stmt(nid, cci, midx, mname)
5008650365 # Call a yield-using class method with a block - inline the method body
5008750366 blk = @nd_block[nid]