Commit f53c8e73

f53c8e73330aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

feat(codegen): transitive escape analysis for int_array params

Replaces the hand-curated callee whitelist in stack_ctx_safe_arg_method with per-method escape signatures derived from each method body.

For every user method, the analyzer iteratively walks the body treating each int_array parameter as a local and reusing the same escape checks that gate stack-alloc on direct locals. The signature is a comma-separated per-parameter flag string ("0" = non-escaping, "1" = escapes, "_" = non-int_array slot). Signatures are merged across classes by worst case so a call site that resolves to any same-named method whose corresponding param escapes pessimistically marks the slot. The merge runs to a fixed point (capped at 12 iterations, typically converges in 2-3).

Call-site context for argument escape checks now carries the param position ("arg::") so the lookup can hit the right slot in the callee's signature.

Tests: 400 pass, bench 52 pass, optcarrot checksum 59662 unchanged. The earlier hand-coded push_child_ids exception is no longer needed; its signature is derived automatically.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
f53c8e73330a
Parent
7e73dd934969

spinel_codegen.rbmodifiedView file

@@ -352,6 +352,12 @@
352352 # handler. nil means "no stack-alloc analysis ran for this body" —
353353 # treat all locals as heap.
354354 @stack_local_set = "".split(",")
355+ # Per-method int_array param escape signatures (built by
356+ # compute_int_array_param_escape_sigs at start of generate_code).
357+ # Hash str→str: method name → comma-separated per-param flags.
358+ # Empty until the pre-pass runs; downstream lookups treat an empty
359+ # hash as "no signature available" (conservative — escape).
360+ @int_array_param_escape_sigs = {}
355361 # Set during the arity-0 instance_eval trampoline inlining so
356362 # receiverless calls in the spliced block body dispatch against
357363 # the rebound self (the .instance_eval receiver) instead of the
@@ -6203,6 +6209,11 @@
62036209 if @needs_class_parents == 1 || @needs_class_ancestors == 1 || @needs_class_for_poly == 1
62046210 @needs_class_table = 1
62056211 end
6212+ # Compute the per-method int_array param escape signatures so
6213+ # declare_method_locals' stack-alloc detection can replace the
6214+ # hand-curated callee whitelist with a derived one. Runs once,
6215+ # before any method body emit.
6216+ compute_int_array_param_escape_sigs
62066217 emit_sym_runtime
62076218 emit_ffi_externs
62086219 # Emit program-specific regexp patterns
@@ -9365,7 +9376,9 @@
93659376 aargs = get_args(args_id)
93669377 ai = 0
93679378 while ai < aargs.length
9368- arg_ctx = "arg:" + mname
9379+ # Encode the arg position so stack_ctx_is_safe can look up
9380+ # the callee's per-param escape signature.
9381+ arg_ctx = "arg:" + mname + ":" + ai.to_s
93699382 if local_int_array_escapes(aargs[ai], lname, arg_ctx) == 1
93709383 return 1
93719384 end
@@ -9459,10 +9472,19 @@
94599472 end
94609473 return 0
94619474 end
9462- # Argument to whitelisted user method: safe.
9475+ # Argument to a user method: safe iff the callee's escape
9476+ # signature reports this position as non-escaping. Context
9477+ # format: "arg:<mname>:<pos>".
94639478 if ctx.length > 4 && ctx[0, 4] == "arg:"
9464- mname = ctx[4, ctx.length - 4]
9465- if stack_ctx_safe_arg_method(mname) == 1
9479+ rest = ctx[4, ctx.length - 4]
9480+ sep = rest.rindex(":")
9481+ if sep == nil
9482+ return 0
9483+ end
9484+ mname = rest[0, sep]
9485+ pos_s = rest[sep + 1, rest.length - sep - 1]
9486+ pos = pos_s.to_i
9487+ if stack_ctx_safe_arg_method(mname, pos) == 1
94669488 return 1
94679489 end
94689490 return 0
@@ -9541,16 +9563,230 @@
95419563 0
95429564 end
95439565
9544- # Whitelist of user methods known not to let an IntArray parameter
9545- # escape (i.e. the callee only mutates the param via push / set
9546- # etc. and doesn't store the pointer anywhere reachable after
9547- # return). Hand-curated for now; a future pass could derive this
9548- # from a transitive escape signature on each method body.
9549- def stack_ctx_safe_arg_method(mname)
9550- if mname == "push_child_ids"
9551- return 1
9566+ # Returns 1 if `mname`'s int_array parameter at `pos` is known not
9567+ # to escape via this callee (i.e. the callee only mutates the
9568+ # param via push / set / read ops and doesn't store the pointer
9569+ # anywhere reachable after return). Consults the per-method
9570+ # escape signatures computed by compute_int_array_param_escape_sigs.
9571+ # When the same method name appears across multiple classes, the
9572+ # signature is the worst case (any class escaping the param marks
9573+ # the slot as escaping).
9574+ def stack_ctx_safe_arg_method(mname, pos)
9575+ if !@int_array_param_escape_sigs.key?(mname)
9576+ return 0
9577+ end
9578+ sig = @int_array_param_escape_sigs[mname]
9579+ flags = sig.split(",")
9580+ if pos < 0 || pos >= flags.length
9581+ return 0
9582+ end
9583+ flags[pos] == "0" ? 1 : 0
9584+ end
9585+
9586+ # ---- Transitive escape analysis for int_array params ----
9587+ #
9588+ # Builds @int_array_param_escape_sigs, a string keyed by method
9589+ # name. The value is a comma-separated per-param flag:
9590+ # "0" — int_array param doesn't escape via this method body
9591+ # "1" — int_array param escapes (return, ivar/gvar/cvar write,
9592+ # passed to a callee whose corresponding param escapes,
9593+ # captured by closure)
9594+ # "_" — non-int_array param (irrelevant for this analysis)
9595+ #
9596+ # Algorithm: start optimistic (all int_array params marked "0"),
9597+ # iteratively walk each method body re-evaluating param escape
9598+ # using current sigs for callee lookups. When new escapes are
9599+ # discovered, flip them to "1" and re-iterate. Terminates at
9600+ # fixed point (typically within a small number of iterations).
9601+ #
9602+ # Same-named methods across classes merge by worst case: if any
9603+ # class's M#foo escapes its int_array param at position k, then
9604+ # the sig for "foo" reports "1" at position k. This is
9605+ # conservative — call sites that resolve to a different M#foo
9606+ # variant pessimistically assume the worst signature.
9607+ def compute_int_array_param_escape_sigs
9608+ init_int_array_param_sigs_for_top_level
9609+ init_int_array_param_sigs_for_classes
9610+ iter = 0
9611+ while iter < 12
9612+ changed = update_int_array_param_sigs_pass
9613+ if changed == 0
9614+ return
9615+ end
9616+ iter = iter + 1
9617+ end
9618+ end
9619+
9620+ def init_int_array_param_sigs_for_top_level
9621+ mi = 0
9622+ while mi < @meth_names.length
9623+ flags = compute_init_flags_str(@meth_param_types[mi])
9624+ merge_param_sig(@meth_names[mi], flags)
9625+ mi = mi + 1
9626+ end
9627+ end
9628+
9629+ def init_int_array_param_sigs_for_classes
9630+ ci = 0
9631+ while ci < @cls_names.length
9632+ all_p_types = @cls_meth_ptypes[ci].split("|")
9633+ mnames = @cls_meth_names[ci].split(";")
9634+ mj = 0
9635+ while mj < mnames.length
9636+ ptypes_s = ""
9637+ if mj < all_p_types.length
9638+ ptypes_s = all_p_types[mj]
9639+ end
9640+ flags = compute_init_flags_str(ptypes_s)
9641+ merge_param_sig(mnames[mj], flags)
9642+ mj = mj + 1
9643+ end
9644+ ci = ci + 1
9645+ end
9646+ end
9647+
9648+ def compute_init_flags_str(ptypes_str)
9649+ ptypes = ptypes_str.split(",")
9650+ parts = "".split(",")
9651+ pi = 0
9652+ while pi < ptypes.length
9653+ if ptypes[pi] == "int_array"
9654+ parts.push("0")
9655+ else
9656+ parts.push("_")
9657+ end
9658+ pi = pi + 1
9659+ end
9660+ parts.join(",")
9661+ end
9662+
9663+ # Merge `new_flags` into the existing sig for `mname`, taking the
9664+ # worst case per position ("1" wins over "0", and a missing slot
9665+ # becomes "_" only if both sides agree).
9666+ def merge_param_sig(mname, new_flags)
9667+ if !@int_array_param_escape_sigs.key?(mname)
9668+ @int_array_param_escape_sigs[mname] = new_flags
9669+ return
9670+ end
9671+ existing = @int_array_param_escape_sigs[mname]
9672+ a = existing.split(",")
9673+ b = new_flags.split(",")
9674+ n = a.length
9675+ if b.length > n
9676+ n = b.length
9677+ end
9678+ out = "".split(",")
9679+ k = 0
9680+ while k < n
9681+ av = "_"
9682+ bv = "_"
9683+ if k < a.length
9684+ av = a[k]
9685+ end
9686+ if k < b.length
9687+ bv = b[k]
9688+ end
9689+ # Worst case merge: "1" beats "0", "0" beats "_".
9690+ if av == "1" || bv == "1"
9691+ out.push("1")
9692+ elsif av == "0" || bv == "0"
9693+ out.push("0")
9694+ else
9695+ out.push("_")
9696+ end
9697+ k = k + 1
9698+ end
9699+ @int_array_param_escape_sigs[mname] = out.join(",")
9700+ end
9701+
9702+ # One iteration of the fixed-point update. Returns 1 if any sig
9703+ # changed, 0 otherwise.
9704+ def update_int_array_param_sigs_pass
9705+ changed = 0
9706+ mi = 0
9707+ while mi < @meth_names.length
9708+ bid = @meth_body_ids[mi]
9709+ if bid >= 0
9710+ if update_method_sig_for(@meth_names[mi], @meth_param_names[mi], @meth_param_types[mi], bid) == 1
9711+ changed = 1
9712+ end
9713+ end
9714+ mi = mi + 1
95529715 end
9553- 0
9716+ ci = 0
9717+ while ci < @cls_names.length
9718+ all_p_names = @cls_meth_params[ci].split("|")
9719+ all_p_types = @cls_meth_ptypes[ci].split("|")
9720+ bodies = @cls_meth_bodies[ci].split(";")
9721+ mnames = @cls_meth_names[ci].split(";")
9722+ mj = 0
9723+ while mj < mnames.length
9724+ pn = ""
9725+ pt = ""
9726+ bid = -1
9727+ if mj < all_p_names.length
9728+ pn = all_p_names[mj]
9729+ end
9730+ if mj < all_p_types.length
9731+ pt = all_p_types[mj]
9732+ end
9733+ if mj < bodies.length
9734+ bid = bodies[mj].to_i
9735+ end
9736+ if bid >= 0
9737+ if update_method_sig_for(mnames[mj], pn, pt, bid) == 1
9738+ changed = 1
9739+ end
9740+ end
9741+ mj = mj + 1
9742+ end
9743+ ci = ci + 1
9744+ end
9745+ changed
9746+ end
9747+
9748+ def update_method_sig_for(mname, pnames_str, ptypes_str, bid)
9749+ pnames = pnames_str.split(",")
9750+ ptypes = ptypes_str.split(",")
9751+ new_flags = "".split(",")
9752+ pi = 0
9753+ while pi < pnames.length
9754+ if pi < ptypes.length && ptypes[pi] == "int_array"
9755+ # Reuse local_int_array_escapes treating the param as a
9756+ # local. Also check the implicit-return position (the param
9757+ # being the body's last expression).
9758+ if local_int_array_escapes(bid, pnames[pi], "stmt") == 1
9759+ new_flags.push("1")
9760+ elsif last_expr_references_local(bid, pnames[pi]) == 1
9761+ new_flags.push("1")
9762+ else
9763+ new_flags.push("0")
9764+ end
9765+ else
9766+ new_flags.push("_")
9767+ end
9768+ pi = pi + 1
9769+ end
9770+ nf = new_flags.join(",")
9771+ had = @int_array_param_escape_sigs.key?(mname)
9772+ existing = ""
9773+ if had
9774+ existing = @int_array_param_escape_sigs[mname]
9775+ if existing == nf
9776+ return 0
9777+ end
9778+ end
9779+ # Need to merge with existing (worst-case per position); the
9780+ # existing value may include flags merged from same-named methods
9781+ # in other classes. merge_param_sig handles that.
9782+ merge_param_sig(mname, nf)
9783+ if had
9784+ after = @int_array_param_escape_sigs[mname]
9785+ if existing == after
9786+ return 0
9787+ end
9788+ end
9789+ 1
95549790 end
95559791
95569792 # Returns 1 if `nid` is an explicit literal value (not a placeholder or