Commit d74a5beb

d74a5bebbd06Merged

o8n <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen): Array#sum honours its init argument

Array#sum(init) silently dropped its init argument on the IntArray / FloatArray dispatch paths:

[1, 2, 3].sum(10) # => 6 (CRuby: 16) [].sum(7) # => 0 (CRuby: 7) [1, 2, 3].sum(10) { |x| x*2 } # => 12 (CRuby: 22) [1.5, 2.5].sum(0.5) { |fx| fx } # => 3.0 (CRuby: 4.5)

No warning, no compile error -- the binary just returned the wrong number. sp_IntArray_sum / sp_FloatArray_sum took no init parameter, and compile_array_sum_block hardcoded mrb_int t = 0; for every receiver type.

Fix:

  • lib/sp_runtime.h: sp_IntArray_sum / sp_FloatArray_sum take init and seed the accumulator with it.
  • spinel_codegen.rb: new compile_arg0_as_float helper, symmetric to the existing compile_arg0_as_int. It routes poly init through sp_poly_to_f so a poly INT-tagged value is coerced to float instead of being misread bit-for-bit out of the .v.f union slot.
  • IntArray sum dispatch forwards compile_arg0_as_int(nid); FloatArray sum dispatch forwards compile_arg0_as_float(nid). No-arg call sites still default to "0", so existing [1,2,3].sum callers emit identical C.
  • compile_array_sum_block picks mrb_float + compile_arg0_as_float when recv_type == "float_array", matching infer_type's float_array.sum -> "float" answer end-to-end. Other receiver types keep mrb_int.

Without the poly-unbox step, the very first naive forwarding (sp_IntArray_sum(rc, compile_arg0(nid))) would have turned a silent miscompile into a C build error for any poly init -- sp_RbVal is not assignable to mrb_int. Using the typed unbox helpers keeps the typed runtime path strict while forwarding the value users actually wrote.

Test: test/array_sum_init.rb adds 17 assertions covering IntArray / FloatArray, with / without block, with / without init, empty arrays, negative init, int<->float widening, and poly init (heterogeneous-array element passed as init for both IntArray and FloatArray, including tag-mixed poly INT on FloatArray which exercises the sp_poly_to_f coercion).

Out of scope:

ItemWhy
int_array.sum(float_init) returning Float, with or without a blockPre-existing miscompile. [1, 2].sum(0.5) returns 3 (CRuby: 3.5); the same answer comes back from [1, 2].sum(0.5) { |x| x } because the block-form accumulator is also mrb_int for non-float receivers. int_array.sum's return type is inferred as int and a float init is unboxed via compile_arg0_as_int. Promoting the result type when init is Float (or when a poly init carries a FLT-tagged value) requires changes in infer_type.
[1.0, 2.0].sum { _1.to_i } returning IntegerThis PR widens the accumulator to mrb_float for FloatArray receivers so the common float-returning-block case works; the corner case where the block returns Integer prints the same digits but is tagged Float (CRuby 3 vs spinel 3.0). Choosing the result type from the block's return type needs a parallel infer_type change.
Tag-aware unbox for compile_arg0_as_intKeeps its existing .v.i direct-access convention -- matches every other callsite in the file; switching to sp_poly_to_i is an orthogonal cleanup.
String#sumDifferent method (bit-width arg, not init). Unchanged.

Run results:

  • make reaches the bootstrap fixpoint (gen2.c == gen3.c, analyze.rb and codegen.rb both IR + C OK).
  • make test: 385 pass, 0 fail, 0 error.
  • make bench: 49 pass, 1 fail (bm_io_wordcount), 2 error (bm_csv_process, bm_wordfreq). All three reproduce on master with no diff, unrelated to this PR.

Details

Commit
d74a5bebbd06
Parent
413652fd6ed6

lib/sp_runtime.hmodifiedView file

@@ -477,7 +477,7 @@
477477 static sp_IntArray*sp_IntArray_shuffle(sp_IntArray*a){sp_IntArray*b=sp_IntArray_dup(a);sp_IntArray_shuffle_bang(b);return b;}
478478 static mrb_int sp_IntArray_min(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]<m)m=a->data[a->start+i];return m;}
479479 static mrb_int sp_IntArray_max(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]>m)m=a->data[a->start+i];return m;}
480-static mrb_int sp_IntArray_sum(sp_IntArray*a){mrb_int s=0;for(mrb_int i=0;i<a->len;i++)s+=a->data[a->start+i];return s;}
480+static mrb_int sp_IntArray_sum(sp_IntArray*a,mrb_int init){mrb_int s=init;for(mrb_int i=0;i<a->len;i++)s+=a->data[a->start+i];return s;}
481481 static mrb_bool sp_IntArray_include(sp_IntArray*a,mrb_int v){for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]==v)return TRUE;return FALSE;}
482482 static mrb_int sp_IntArray_index(sp_IntArray*a,mrb_int v){for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]==v)return i;return -1;}
483483 static mrb_int sp_IntArray_rindex(sp_IntArray*a,mrb_int v){for(mrb_int i=a->len-1;i>=0;i--)if(a->data[a->start+i]==v)return i;return -1;}
@@ -502,7 +502,7 @@
502502 static inline void sp_FloatArray_push(sp_FloatArray*a,mrb_float v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);h->size+=sizeof(mrb_float)*a->cap;sp_gc_bytes+=sizeof(mrb_float)*a->cap;}a->data[a->len++]=v;}
503503 static mrb_float sp_FloatArray_min(sp_FloatArray*a){if(a->len==0)return 0;mrb_float m=a->data[0];for(mrb_int i=1;i<a->len;i++)if(a->data[i]<m)m=a->data[i];return m;}
504504 static mrb_float sp_FloatArray_max(sp_FloatArray*a){if(a->len==0)return 0;mrb_float m=a->data[0];for(mrb_int i=1;i<a->len;i++)if(a->data[i]>m)m=a->data[i];return m;}
505-static mrb_float sp_FloatArray_sum(sp_FloatArray*a){mrb_float s=0;for(mrb_int i=0;i<a->len;i++)s+=a->data[i];return s;}
505+static mrb_float sp_FloatArray_sum(sp_FloatArray*a,mrb_float init){mrb_float s=init;for(mrb_int i=0;i<a->len;i++)s+=a->data[i];return s;}
506506 static void sp_FloatArray_replace(sp_FloatArray*dst,sp_FloatArray*src){dst->len=0;if(src->len>dst->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)dst-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*dst->cap;h->size-=sizeof(mrb_float)*dst->cap;void*nd=realloc(dst->data,sizeof(mrb_float)*src->len);if(!nd){perror("realloc");exit(1);}dst->data=(mrb_float*)nd;dst->cap=src->len;h->size+=sizeof(mrb_float)*dst->cap;sp_gc_bytes+=sizeof(mrb_float)*dst->cap;}memcpy(dst->data,src->data,sizeof(mrb_float)*src->len);dst->len=src->len;}
507507 static inline mrb_float sp_FloatArray_pop(sp_FloatArray*a){return a->data[--a->len];}
508508 static inline mrb_float sp_FloatArray_shift(sp_FloatArray*a){if(a->len==0)return 0.0;mrb_float v=a->data[0];for(mrb_int i=0;i+1<a->len;i++)a->data[i]=a->data[i+1];a->len--;return v;}