Repository navigation
RISC-V: compare floats inside compound values by value - #89
Conversation
The C and Wasm backends learned this in #87; RV32I and RV64I had two holes of their own. - `__eq_<tag>` had no float case. A float element fell to the word compare, and a float here is a pointer to its bits, so it compared the boxes: two equal floats were unequal and a NaN equalled itself. It now tail-calls the prelude's __feq (IEEE: -0.0 equals 0.0, a NaN equals nothing). - `__cmp_<tag>` already called __flt for a float element, but nothing marked __flt reachable: a program that ordered compound floats and compared no two bare floats did not assemble (`undefined label u___flt`). Both helpers are emitted after the reachable functions are chosen, so the comparison has to mark what they will call. vars_in now does, through cmp_reaches_float, which walks a tuple's elements and a record's or a variant's fields (with its type arguments substituted). vars_in moves below the type tables it reads; its body is unchanged apart from the new arm. test/parity/float_compound_eq_order.mere compares tuples, records and variants of floats by ==, != and order, with NaN and both zeros, and no bare float comparison to keep __feq or __flt alive. It matches on interp, C, LLVM and Wasm. rv_exec_check's must-execute fixtures run it at both widths against a hand-checked oracle, because the corpus sweep counts a compile refusal instead of failing on it.
284km
left a comment
There was a problem hiding this comment.
Summary
This PR makes RV32I and RV64I compare a float inside a tuple, record or variant by value, as C and Wasm do since #87. __eq_<tag> now tail-calls __feq. A new arm in vars_in keeps __feq / __flt reachable when a comparison's operand type contains a float.
What is good
- The root cause is stated clearly: the helpers are emitted after reachability is decided, so the comparison site has to mark what they will call.
cmp_reaches_floatwalks type arguments, record fields and variant payloads with substitution. The seen-set makes recursive types terminate. Being conservative only costs an unused prelude function.- The fixture has no bare float comparison, so it really tests the new reachability path. It runs in
--contracts, which CI runs with both emulators; the riscv group log showsfloat_compound_eq_order:32and:64ok.
Findings
Major
- CI is red because of the new parity file.
first_run_checkfails withREADME claims 254 parity programs, test/parity holds 255. The README sentence "cross-backend parity (254 programs)" needs to say 255. The "Gates (cross-backend and differential)" job fails only because this group failed. (Inline comment on the new fixture.)
Critical / Minor: none found.
Suggestion
- The fixture does not cover the paths
cmp_reaches_floatadds beyond a direct field: a float reached only through the type argument of a recursive type (for examplefloat treewithtype 'a tree = Leaf | Node of 'a tree * 'a * 'a tree), and ordering of a list of float tuples. I ran both (see below) and they pass. One line each would keep them covered. - Ordering with a NaN is not in the fixture, and that is fine. RV agrees with C there: a NaN is neither less nor greater, so the next field decides. The interpreter differs because
value_compareuses OCaml's totalcompare(NaN sorts first). That difference is older than this PR and is documented ineval.ml.
Questions I checked
- Reach coverage. Every
request_eq/request_cmpcall comes fromcompile_cmpor from inside another helper, so every helper chain starts at a sourceCmp. Monomorphization runs before reachability, so a==inside a generic function (list_member, aderive (Eq, Ord)default method) is seen with its concrete type. Float literal patterns do not parse, and a Map key with a float inside is refused bycheck_map_key, so neither is a hidden comparison site. Type aliases are expanded before codegen, andcmp_reaches_floatandcompile_cmpboth read the left operand's type, so they agree. !=. It isxoriof__eq_<tag>, and__eq_floatis IEEE, soNaN != NaNis true (fixture line 2 and my probes).- Tail call.
__eq_floatkeepsraand passesa0/a1through. That is the same convention__cmp_floatalready uses for__flt. Callers reload their saved x / y from the stack after each call, so nothing relies on a temporary. A float is a pointer to its box at both widths, so the word ina0/a1is the same kind of value__feqgets from a bare comparison. - Moved block. I diffed the removed and added lines: the block from
topstosubst_tyis identical apart from one blank line, andvars_inonly gains the new arm. rv_exec_check.sh. In the fixture loop a build failure prints FAIL and setsrc=1. The loop is skipped when no runner exists, but CI's--contractsmode requires both runners, so a refusal does fail CI.
Verified by running
Own build of the head commit, memu emulators at both widths, output compared with the interpreter and the C backend:
- The new fixture: interpreter, C, RV32 and RV64 all print the
.expectedfile. - Extra probes, identical on interpreter / RV32 / RV64 (and on C where C builds):
float tree(recursive, generic):==,!=,<,<=, NaN, ±0.0- lists of float tuples:
==,!=,<,<=,>,>= Some (P { x: float, .. }):==,!=,<, NaN- generic
fn u -> fn v -> u == vandu < von float tuples;'a Boxof a float and of a float tuple list_member,list_max,list_minon float tuplesderive (Eq, Ord)on a variant with float payloads- nested records, and
'a Wof'a Wof a list of records with a float t == twheretholds a NaN is false
- On the merge base (0c083af) the ordering probes fail with
undefined label u___flt, and an==-only probe printsftftfinstead oftffft, so the probes do exercise the change. --barewith a compound float==still assembles.
Not checked
- I did not run
dune runtest,scripts/ctest.shor the fullrv_exec_check.sh --allsweep. CI's riscv group passed. - LLVM and Wasm were not run on the probes.
Aside (not this PR)
While probing, the C backend failed to compile two of the programs, on main too: eq_Box_tuple_float_int and eq_W_list_Out call eq_int on a field whose type is a type parameter. So a generic record whose type argument is a tuple or a list does not build on C. It may be worth an issue.
| @@ -0,0 +1,40 @@ | |||
| // Floats inside compound values, compared by `==`, `!=` and by order: a tuple, | |||
There was a problem hiding this comment.
Major: this is parity program number 255, and the README still says 254. first_run_check fails on it (README claims 254 parity programs, test/parity holds 255), which turns the runtime gate group and the aggregate Gates job red. Updating the count in the README's "cross-backend parity (254 programs)" sentence should fix it.
| // Execution mode: 4 backends (interp + C + LLVM + Wasm), diff = 0 | ||
|
|
||
| type P = { x: float, n: int }; | ||
| type 'a mb = Nought | Just of 'a; |
There was a problem hiding this comment.
Suggestion: cmp_reaches_float also follows type arguments through recursive types, and __cmp_<tag> descends into lists. Neither is in the fixture. A recursive generic such as type 'a tree = Leaf | Node of 'a tree * 'a * 'a tree compared at float tree, plus one ordering of lists of float tuples ([(1.5, 1), (2.5, 2)] < [(1.5, 1), (0.5, 2)]), would cover both. Both give the interpreter's answer on RV32 and RV64 with this branch, and both fail on main with undefined label u___flt.
…e generic type's float and lists of float tuples first_run_check re-derives the parity count and failed on the README's 254. The new parity program also compares a `float tree` (a float as the type argument of a recursive generic type) and orders lists of float tuples: the two paths cmp_reaches_float takes through a type argument and a recursive type's own fields, which the reviewer found main refuses with `undefined label u___flt`.
|
Thanks, both addressed in ce793e5.
I could not rerun the old compiler myself: the 90d0589 build I used before is gone. Your check that main refuses both with |
284km
left a comment
There was a problem hiding this comment.
Re-review of ce793e5.
- [Major] parity count: resolved. README now says 255, and
test/parityat this head holds 255.merefiles. The earlier CI failure was exactly this mismatch (README claims 254 parity programs, test/parity holds 255). - [Suggestion] coverage: resolved. The parity program now compares a
float tree(a float as the type argument of a recursive generic type) with==/!=, including a NaN node, and orders lists of float tuples, including a longer list against its prefix and-0.0against0.0.
How I checked: by reading. I checked the new oracle line tftftft against the seven comparisons one by one: equal trees t, different trees f, != t, NaN node f, [(1.5, 2)] < [(2.5, 1)] t, a longer list against its own prefix f, [(-0.0, 1)] <= [(0.0, 1)] t (the zeros are equal under IEEE, so the next element decides). The line is placed where the new print runs. I did not rerun the backends; the author reports parity.sh MATCH on interp/C/LLVM/Wasm and RV32I/RV64I against the updated oracle, and rv_exec_check.sh --contracts 13/13.
No Critical, Major or Minor findings remain. CI for this head is still running.
#87 taught the C and Wasm backends to compare a float inside a tuple, record or variant by value. RV32I and RV64I had two holes of their own, and this PR fixes both.
The two holes
__eq_<tag>had no float case. A float element fell through to the word compare. A float on RISC-V is a pointer to its bits, so that compare looked at the boxes: two equal floats were unequal, and a NaN equalled itself. The helper now tail-calls the prelude's__feq, which is IEEE: -0.0 equals 0.0, and a NaN equals nothing.__cmp_<tag>already called__fltfor a float element, but nothing marked__fltreachable. A program that ordered compound floats and never compared two bare floats did not assemble:codegen_riscv: undefined label u___flt.Why reachability needed a change
Both helpers are emitted after the reachable top-level functions have been chosen. So whatever a helper will call has to be marked when the comparison is seen.
vars_innow has an arm for aCmpwhose operand type reaches a float (cmp_reaches_float). That arm adds__feqfor==/!=and__fltfor ordering.cmp_reaches_floatwalks tuple elements and record or variant fields, substituting type arguments, with a seen-set for recursive types. It is deliberately conservative: a type argument that holds a float counts even where the helper would not descend into it. That only keeps a prelude function alive that might not be called.vars_inmoved below the type tables it now reads (type_records,type_variants,ty_tag,subst_ty,zip_tyenv). Its body is unchanged apart from the new arm.Tests
test/parity/float_compound_eq_order.merecompares tuples, records and variants of floats with==,!=and the four orderings, including NaN and both zeros. After review it also compares afloat tree, which is a float as the type argument of a recursive generic type, and orders lists of float tuples.__feqor__fltalive.None/Some, because the RV prelude has its own option type and refuses a program that redefines those.scripts/parity.shgives MATCH for C, LLVM and Wasm.scripts/rv_exec_check.shadds the case to its must-execute fixtures, which run at both widths againsttest/parity/float_compound_eq_order.expected.u___fltrefusal would have read as coverage.undefined label u___fltundefined label u___fltOn the earlier ctest-style program without ordering, the old RV output was
fftffffttfagainst the interpreter'stfftftftft.Checked locally
These were run again after the review commit (ce793e5).
scripts/first_run_check.sh: PASS. It reports parity count 255 and test count 3305. The README said 254, which is what failed CI.scripts/parity.shgives MATCH for C, LLVM and Wasm. Its output matches the oracle on RV32I and RV64I, andrv_exec_check.sh --contractspasses 13 of 13.The runs below were made before the review commit.
dune runtest: rc 0.scripts/ctest.sh: 21 passed.MEMU=... sh scripts/rv_exec_check.sh --all: rc 0. That is 240 passed at 32 bits and 254 at 64, 0 failed, with the known-different counts as listed in the script, and every contract check passing.🤖 Generated with Claude Code