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
if nid < 0 return "void" end # `<poly>[idx]` — analyze may cache an optimistic concrete type # (e.g. "int" picked from one observed elem kind), but # compile_poly_method_call's actual emit widens to sp_RbVal # whenever its narrow_int check fails (e.g. block-param recv). # When the cache disagrees with the emit, downstream consumers # (compile_arg0_as_int for `>>` / `<<`, ...) skip the unbox and # produce `int >> sp_RbVal` in C — fails C compile. Compute the # answer that mirrors the actual emit, before honouring the # cache. pa = poly_aref_call_type(nid) if pa != "" return pa end # Cache lookup. analyze.rb's annotate_all_node_types fills # @nd_inferred_type for every reachable node OUTSIDE block bodies # (block-iteration scope is iterator-specific and dispatched at # `[]` (IntArray + Method, the optcarrot `__fetch__` shape), the # outer dispatch is guaranteed to land on a poly carrying an int # tag, and we can read it as `mrb_int` directly. Returns 0 # Return the type that compile_poly_method_call would actually emit # for `<poly>[idx]`, or "" if `nid` isn't that shape. Used by # infer_type to keep its static answer in sync with the emit; the # cached annotation from analyze can lag the emit's narrow_int # decision (block-param recvs short-circuit narrowing), and # downstream consumers like compile_arg0_as_int rely on the # answer matching the C type of the temp the emit produces. def poly_aref_call_type(nid) if @nd_type[nid] != "CallNode" || @nd_name[nid] != "[]" return "" end recv_id = @nd_receiver[nid] if recv_id < 0 || infer_type(recv_id) != "poly" return "" end if poly_index_narrow_int(nid) == 1 return "int" end "poly" end # whenever the chain doesn't fit the pattern, so the default # poly-typed temp is preserved for everything else. def poly_index_narrow_int(nid)test/poly_aref_shift_via_block_param.rbaddedView file
Loading diff…
test/poly_aref_shift_via_block_param.rb.expectedaddedView file
Loading diff…