Commit 40672965
40672965ab1fMerged
Ori Pekelman <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: coerce a poly Array.new size operand to int (emit_int_expr)
Array.new(size, fill) (and Array.new(size) / Array.new(size){...}) lowered the
size into an mrb_int temp via plain emit_expr. When the size expression is
poly — e.g. nrows * ncols where a factor widened to poly, so it emits
sp_poly_mul(...) returning sp_RbVal — the result was assigned straight into
the mrb_int temp:
mrb_int _t = sp_poly_mul(sp_box_int(lv_nrows), lv_ncols); // sp_RbVal -> mrb_int
// error: incompatible types when initializing 'mrb_int' using 'sp_RbVal'
The element type was inferred correctly (float, from the 0.0 fill); only the size operand was uncoerced. Fix: emit the size via emit_int_expr, which wraps a poly value in sp_poly_to_i — the same coercion already used at other array-bound sites (indices, ranges). Applied to all three Array.new size emissions.
Found reducing toy's Mat substrate (Mat#initialize @flat = Array.new(nrows * ncols, 0.0)), where ncols widened to poly from a Mat.new(_, @d) call site
with an untyped ivar. Fixes the blocker that kept toy's whole compute surface
(eval/serve/infer/train) from compiling on master.
Test added (fails pre-fix with the exact 'mrb_int'/'sp_RbVal' error). Full suite 864 pass / 0 fail.
Co-Authored-By: Claude Fable 5 [email protected]
Details
- Commit
- 40672965ab1f
- Parent
- 34f8cd5461a2
src/codegen_call.cmodifiedView file
if (cn && !strcmp(cn, "Array") && argc == 1 && nt_ref(nt, id, "block") < 0) { /* Array.new(n) -> PolyArray of n nils */ int tn = ++g_tmp, tr = ++g_tmp, ti = ++g_tmp; Buf nb; memset(&nb, 0, sizeof nb); emit_expr(c, argv[0], &nb); Buf nb; memset(&nb, 0, sizeof nb); emit_int_expr(c, argv[0], &nb); /* poly size -> int (spinel-dev#24) */ emit_indent(g_pre, g_indent); buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : "0"); buf_puts(g_pre, ";\n"); emit_indent(g_pre, g_indent); const char *ip = block_param_name(c, blk, 0); const char *irn = ip ? rename_local(ip) : NULL; Buf nb; memset(&nb, 0, sizeof nb); if (argc >= 1) emit_expr(c, argv[0], &nb); if (argc >= 1) emit_int_expr(c, argv[0], &nb); /* poly size -> int (spinel-dev#24) */ emit_indent(g_pre, g_indent); if (argc >= 1) { buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : "0"); buf_puts(g_pre, ";\n"); } else { buf_printf(g_pre, "mrb_int _t%d = 0;\n", tn); } const char *k = (at == TY_POLY_ARRAY) ? "Poly" : array_kind(at); if (k) { int tn = ++g_tmp, tv = ++g_tmp, tr = ++g_tmp, ti = ++g_tmp; Buf nb; memset(&nb, 0, sizeof nb); emit_expr(c, argv[0], &nb); /* The size goes into an `mrb_int` temp; coerce a poly size expression (e.g. `nrows * ncols` where a factor widened to poly -> sp_poly_mul, which returns sp_RbVal) through sp_poly_to_i. spinel-dev#24. */ Buf nb; memset(&nb, 0, sizeof nb); emit_int_expr(c, argv[0], &nb); Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, argv[1], &vb); emit_indent(g_pre, g_indent); buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : ""); buf_puts(g_pre, ";\n");test/array_new_poly_size.rbaddedView file
Loading diff…
test/array_new_poly_size.rb.expectedaddedView file
Loading diff…