Commit 1f3ddcbe

1f3ddcbe0c61Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): keep infer_type of [idx] in sync with the actual emit

compile_poly_method_call emits the polymorphic [] dispatch as a runtime tag-check whose result widens to sp_RbVal whenever poly_index_narrow_int declines to narrow (most commonly when the recv is a block param — its recv is a LocalVariableReadNode, not an ivar chain). analyze.rb's annotate_all_node_types runs ahead of codegen and caches an optimistic concrete type (e.g. "int" picked from one observed elem kind) for the same call.

Downstream consumers like compile_arg0_as_int (used for >>, <<, &, ...) consult the cache via infer_type and skip the unbox when it disagrees with the emit, leaving int >> sp_RbVal in the generated C — gcc rejects it.

Surfaces in optcarrot's PPU#poke_2007:

attr_lut_update.each do |a| a[1] = TILE_LUT[data >> a[2] & 3] end

attr_lut_update arrives via a destructured nested-ivar read which widens it to poly; the block param a is poly; a[2] is sp_RbVal; data >> a[2] fails to compile.

Add a small helper poly_aref_call_type that mirrors compile_poly_method_call's narrow_int decision, and call it before honouring the cache so the static view matches the actual emit.

Details

Commit
1f3ddcbe0c61
Parent
97a359a737f4

spinel_codegen.rbmodifiedView file

@@ -2082,6 +2082,19 @@
20822082 if nid < 0
20832083 return "void"
20842084 end
2085+ # `<poly>[idx]` — analyze may cache an optimistic concrete type
2086+ # (e.g. "int" picked from one observed elem kind), but
2087+ # compile_poly_method_call's actual emit widens to sp_RbVal
2088+ # whenever its narrow_int check fails (e.g. block-param recv).
2089+ # When the cache disagrees with the emit, downstream consumers
2090+ # (compile_arg0_as_int for `>>` / `<<`, ...) skip the unbox and
2091+ # produce `int >> sp_RbVal` in C — fails C compile. Compute the
2092+ # answer that mirrors the actual emit, before honouring the
2093+ # cache.
2094+ pa = poly_aref_call_type(nid)
2095+ if pa != ""
2096+ return pa
2097+ end
20852098 # Cache lookup. analyze.rb's annotate_all_node_types fills
20862099 # @nd_inferred_type for every reachable node OUTSIDE block bodies
20872100 # (block-iteration scope is iterator-specific and dispatched at
@@ -16043,6 +16056,27 @@
1604316056 # `[]` (IntArray + Method, the optcarrot `__fetch__` shape), the
1604416057 # outer dispatch is guaranteed to land on a poly carrying an int
1604516058 # tag, and we can read it as `mrb_int` directly. Returns 0
16059+ # Return the type that compile_poly_method_call would actually emit
16060+ # for `<poly>[idx]`, or "" if `nid` isn't that shape. Used by
16061+ # infer_type to keep its static answer in sync with the emit; the
16062+ # cached annotation from analyze can lag the emit's narrow_int
16063+ # decision (block-param recvs short-circuit narrowing), and
16064+ # downstream consumers like compile_arg0_as_int rely on the
16065+ # answer matching the C type of the temp the emit produces.
16066+ def poly_aref_call_type(nid)
16067+ if @nd_type[nid] != "CallNode" || @nd_name[nid] != "[]"
16068+ return ""
16069+ end
16070+ recv_id = @nd_receiver[nid]
16071+ if recv_id < 0 || infer_type(recv_id) != "poly"
16072+ return ""
16073+ end
16074+ if poly_index_narrow_int(nid) == 1
16075+ return "int"
16076+ end
16077+ "poly"
16078+ end
16079+
1604616080 # whenever the chain doesn't fit the pattern, so the default
1604716081 # poly-typed temp is preserved for everything else.
1604816082 def poly_index_narrow_int(nid)