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

@@ -2340,7 +2340,7 @@
23402340 if (cn && !strcmp(cn, "Array") && argc == 1 && nt_ref(nt, id, "block") < 0) {
23412341 /* Array.new(n) -> PolyArray of n nils */
23422342 int tn = ++g_tmp, tr = ++g_tmp, ti = ++g_tmp;
2343- Buf nb; memset(&nb, 0, sizeof nb); emit_expr(c, argv[0], &nb);
2343+ Buf nb; memset(&nb, 0, sizeof nb); emit_int_expr(c, argv[0], &nb); /* poly size -> int (spinel-dev#24) */
23442344 emit_indent(g_pre, g_indent);
23452345 buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : "0"); buf_puts(g_pre, ";\n");
23462346 emit_indent(g_pre, g_indent);
@@ -2364,7 +2364,7 @@
23642364 const char *ip = block_param_name(c, blk, 0);
23652365 const char *irn = ip ? rename_local(ip) : NULL;
23662366 Buf nb; memset(&nb, 0, sizeof nb);
2367- if (argc >= 1) emit_expr(c, argv[0], &nb);
2367+ if (argc >= 1) emit_int_expr(c, argv[0], &nb); /* poly size -> int (spinel-dev#24) */
23682368 emit_indent(g_pre, g_indent);
23692369 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"); }
23702370 else { buf_printf(g_pre, "mrb_int _t%d = 0;\n", tn); }
@@ -2416,7 +2416,10 @@
24162416 const char *k = (at == TY_POLY_ARRAY) ? "Poly" : array_kind(at);
24172417 if (k) {
24182418 int tn = ++g_tmp, tv = ++g_tmp, tr = ++g_tmp, ti = ++g_tmp;
2419- Buf nb; memset(&nb, 0, sizeof nb); emit_expr(c, argv[0], &nb);
2419+ /* The size goes into an `mrb_int` temp; coerce a poly size expression
2420+ (e.g. `nrows * ncols` where a factor widened to poly -> sp_poly_mul,
2421+ which returns sp_RbVal) through sp_poly_to_i. spinel-dev#24. */
2422+ Buf nb; memset(&nb, 0, sizeof nb); emit_int_expr(c, argv[0], &nb);
24202423 Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, argv[1], &vb);
24212424 emit_indent(g_pre, g_indent);
24222425 buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : ""); buf_puts(g_pre, ";\n");