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_sumtakeinitand seed the accumulator with it.spinel_codegen.rb: newcompile_arg0_as_floathelper, symmetric to the existingcompile_arg0_as_int. It routes poly init throughsp_poly_to_fso a poly INT-tagged value is coerced to float instead of being misread bit-for-bit out of the.v.funion slot.- IntArray sum dispatch forwards
compile_arg0_as_int(nid); FloatArray sum dispatch forwardscompile_arg0_as_float(nid). No-arg call sites still default to"0", so existing[1,2,3].sumcallers emit identical C. compile_array_sum_blockpicksmrb_float+compile_arg0_as_floatwhenrecv_type == "float_array", matchinginfer_type'sfloat_array.sum -> "float"answer end-to-end. Other receiver types keepmrb_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:
| Item | Why |
|---|---|
int_array.sum(float_init) returning Float, with or without a block | Pre-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 Integer | This 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_int | Keeps 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#sum | Different method (bit-width arg, not init). Unchanged. |
Run results:
makereaches the bootstrap fixpoint (gen2.c == gen3.c,analyze.rbandcodegen.rbboth 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
static sp_IntArray*sp_IntArray_shuffle(sp_IntArray*a){sp_IntArray*b=sp_IntArray_dup(a);sp_IntArray_shuffle_bang(b);return b;}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;}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;}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;}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;}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;}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;}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;}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;}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;}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;}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;}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;}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;}static inline mrb_float sp_FloatArray_pop(sp_FloatArray*a){return a->data[--a->len];}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;}spinel_codegen.rbmodifiedView file
Loading diff…
test/array_sum_init.rbaddedView file
Loading diff…
test/array_sum_init.rb.expectedaddedView file
Loading diff…