Commit 59f48e59

59f48e594a9dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

feat(codegen): narrow param type from single-class method calls in body

Stage 1 of body-side type narrowing — the callee→caller direction that complements scan_new_calls' caller→callee widening.

For each method whose param is still typed at the default int (meaning no concrete call-site arg has pinned it), walk the body for param.<m> calls. If <m> is defined on exactly one user class (and isn't on the primitive-shared deny list), the param's static type narrows to obj_<C>. Conflicting strong signals leave the param alone.

The narrow only fires on still-int slots, so caller-side widening always wins. If a later iteration's caller-side scan finds a real non-class arg flowing in, unify_call_types widens back to poly — net effect no worse than skipping the narrow.

Common-method filter reuses is_primitive_shared_method (already the safety net for #302/#305's compile_int_class_fallback_expr) plus a small operator deny list (+/-/*///%/**/&/|/^ /~/>>/</>/<=/>=/<=>). Without that filter, a user class that overloads e.g. length would draw any param.length call into a false single-class signal even when the real receiver was a String.

Helps cases where a private helper method's param has no concrete-typed call site Spinel sees at scan time (poly-receiver dispatch via method(:m) + obj.method(:m) shapes, etc.) — the body's specific user-class method calls are the only signal.

Existing tests (272 incl. new test/param_narrow_from_body_call.rb): all pass. Bootstrap: gen2.c == gen3.c. Roundhouse archive (article/comment/articles_controller/comments_controller): still BUILD+LINK OK.

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

Details

Commit
59f48e594a9d
Parent
12ba60dca930

spinel_codegen.rbmodifiedView file

@@ -11441,6 +11441,202 @@
1144111441 end
1144211442 end
1144311443
11444+ # Body-side param narrowing (Stage 1 of the callee→caller direction
11445+ # of type inference, complementing scan_new_calls' caller→callee
11446+ # widening). For each method's params still typed at the default
11447+ # `int`, walk the body for `param.<m>` calls. If <m> is defined
11448+ # on exactly one user class (and isn't a common operator / built-
11449+ # in-overlap method), the param's static type narrows to that
11450+ # class. Conflicting strong signals leave the param at int.
11451+ #
11452+ # The narrow direction (int → obj_<C>) only fires when the caller-
11453+ # side widening hasn't already pinned the param to a non-int type,
11454+ # so it never overrides observation from a real call site. If a
11455+ # later iteration's caller-side scan finds an actual int arg flowing
11456+ # into this slot, unify_call_types will widen back to poly — net
11457+ # effect no worse than skipping the narrow.
11458+ #
11459+ # Excluded method names cover operators (which user classes
11460+ # routinely overload but built-in primitives also implement) and
11461+ # the common Object/Enumerable surface (`length`, `each`, `to_s`,
11462+ # ...) — when these match a user class they don't actually
11463+ # discriminate from a built-in.
11464+ def narrow_param_types_from_body_method_calls
11465+ # Top-level methods.
11466+ mi = 0
11467+ while mi < @meth_names.length
11468+ bid = @meth_body_ids[mi]
11469+ if bid >= 0
11470+ pnames = @meth_param_names[mi].split(",")
11471+ ptypes = @meth_param_types[mi].split(",")
11472+ observations = "".split(",")
11473+ collect_param_method_calls(bid, pnames, observations)
11474+ changed = 0
11475+ pk = 0
11476+ while pk < pnames.length
11477+ if pk < ptypes.length && ptypes[pk] == "int"
11478+ target_cls = unify_param_class_from_observations(pnames[pk], observations)
11479+ if target_cls >= 0
11480+ ptypes[pk] = "obj_" + @cls_names[target_cls]
11481+ changed = 1
11482+ end
11483+ end
11484+ pk = pk + 1
11485+ end
11486+ if changed == 1
11487+ @meth_param_types[mi] = ptypes.join(",")
11488+ end
11489+ end
11490+ mi = mi + 1
11491+ end
11492+ # Class instance methods.
11493+ ci = 0
11494+ while ci < @cls_names.length
11495+ all_params = @cls_meth_params[ci].split("|")
11496+ all_ptypes = @cls_meth_ptypes[ci].split("|")
11497+ bodies = @cls_meth_bodies[ci].split(";")
11498+ cls_changed = 0
11499+ mj = 0
11500+ while mj < all_params.length
11501+ bid = -1
11502+ if mj < bodies.length
11503+ bid = bodies[mj].to_i
11504+ end
11505+ if bid >= 0
11506+ cm_pnames = all_params[mj].split(",")
11507+ cm_ptypes = "".split(",")
11508+ if mj < all_ptypes.length
11509+ cm_ptypes = all_ptypes[mj].split(",")
11510+ end
11511+ observations = "".split(",")
11512+ collect_param_method_calls(bid, cm_pnames, observations)
11513+ m_changed = 0
11514+ pk = 0
11515+ while pk < cm_pnames.length
11516+ if pk < cm_ptypes.length && cm_ptypes[pk] == "int"
11517+ target_cls = unify_param_class_from_observations(cm_pnames[pk], observations)
11518+ if target_cls >= 0
11519+ cm_ptypes[pk] = "obj_" + @cls_names[target_cls]
11520+ m_changed = 1
11521+ end
11522+ end
11523+ pk = pk + 1
11524+ end
11525+ if m_changed == 1
11526+ all_ptypes[mj] = cm_ptypes.join(",")
11527+ cls_changed = 1
11528+ end
11529+ end
11530+ mj = mj + 1
11531+ end
11532+ if cls_changed == 1
11533+ @cls_meth_ptypes[ci] = all_ptypes.join("|")
11534+ end
11535+ ci = ci + 1
11536+ end
11537+ end
11538+
11539+ # Walk `nid` for CallNode whose receiver is a LocalVariableReadNode
11540+ # naming one of the params. Each such site contributes one entry
11541+ # `<pname>\t<mname>` to `observations`. Stops at nested DefNode
11542+ # (those introduce their own scope with different params).
11543+ def collect_param_method_calls(nid, pnames, observations)
11544+ if nid < 0
11545+ return
11546+ end
11547+ if @nd_type[nid] == "DefNode"
11548+ return
11549+ end
11550+ if @nd_type[nid] == "CallNode"
11551+ recv = @nd_receiver[nid]
11552+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"
11553+ vname = @nd_name[recv]
11554+ if not_in(vname, pnames) == 0
11555+ mname = @nd_name[nid]
11556+ observations.push(vname + "\t" + mname)
11557+ end
11558+ end
11559+ end
11560+ cs = []
11561+ push_child_ids(nid, cs)
11562+ k = 0
11563+ while k < cs.length
11564+ collect_param_method_calls(cs[k], pnames, observations)
11565+ k = k + 1
11566+ end
11567+ end
11568+
11569+ # For all `<pname>\t<mname>` observations, find the unique user
11570+ # class that defines `<mname>`. If every strong observation
11571+ # (single-class match, not on the deny list) points to the same
11572+ # class, return its index. Conflicting classes or no signal at
11573+ # all return -1.
11574+ def unify_param_class_from_observations(pname, observations)
11575+ unique_class = -1
11576+ has_signal = 0
11577+ k = 0
11578+ while k < observations.length
11579+ tab = observations[k].index("\t")
11580+ if tab >= 0
11581+ obs_p = observations[k][0, tab]
11582+ if obs_p == pname
11583+ mname = observations[k][tab + 1, observations[k].length - tab - 1]
11584+ if is_common_method_name(mname) == 0
11585+ cls_count = 0
11586+ cls_idx = -1
11587+ ci = 0
11588+ while ci < @cls_names.length
11589+ if cls_find_method_direct(ci, mname) >= 0
11590+ cls_count = cls_count + 1
11591+ cls_idx = ci
11592+ end
11593+ ci = ci + 1
11594+ end
11595+ if cls_count == 1
11596+ if unique_class == -1
11597+ unique_class = cls_idx
11598+ has_signal = 1
11599+ elsif unique_class != cls_idx
11600+ return -1
11601+ end
11602+ end
11603+ end
11604+ end
11605+ end
11606+ k = k + 1
11607+ end
11608+ if has_signal == 1
11609+ return unique_class
11610+ end
11611+ -1
11612+ end
11613+
11614+ # Methods whose name doesn't reliably discriminate a user class
11615+ # from a built-in receiver. Reuses `is_primitive_shared_method`
11616+ # (the same list #302/#305 use to gate compile_int_class_fallback
11617+ # _expr) so the body-side narrow stays consistent with the emit-
11618+ # time int-recv fallback's safety net. Plus a small deny list of
11619+ # numeric / boolean operators that the primitive-shared list
11620+ # doesn't currently cover.
11621+ def is_common_method_name(mname)
11622+ if is_primitive_shared_method(mname) == 1
11623+ return 1
11624+ end
11625+ if mname == "+" || mname == "-" || mname == "*" || mname == "/"
11626+ return 1
11627+ end
11628+ if mname == "%" || mname == "**" || mname == "&" || mname == "|" || mname == "^" || mname == "~"
11629+ return 1
11630+ end
11631+ if mname == "<" || mname == ">" || mname == "<=" || mname == ">=" || mname == "<=>"
11632+ return 1
11633+ end
11634+ if mname == ">>"
11635+ return 1
11636+ end
11637+ 0
11638+ end
11639+
1144411640 # Helper: given the set of element types observed in pname.push(...)
1144511641 # patterns, return the typed-array tag to promote to, or "" if the
1144611642 # observations don't agree on a single concrete type.
@@ -14824,6 +15020,7 @@
1482415020 # Then the next iteration's scan_locals back-propagates those
1482515021 # promoted types to caller-side locals.
1482615022 infer_param_array_type_from_body
15023+ narrow_param_types_from_body_method_calls
1482715024 detect_poly_params
1482815025 cur_sig = inference_signature
1482915026 if cur_sig == prev_sig