Skip to content

RISC-V: compare floats inside compound values by value - #89

Merged
284km merged 2 commits into
mainfrom
rv-float-struct-eq
Nov 23, 2026
Merged

284km merged 2 commits into
mainfrom
rv-float-struct-eq

Conversation

@284km

@284km 284km commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

#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

  1. __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.
  2. __cmp_<tag> already called __flt for a float element, but nothing marked __flt reachable. 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_in now has an arm for a Cmp whose operand type reaches a float (cmp_reaches_float). That arm adds __feq for ==/!= and __flt for ordering.
  • cmp_reaches_float walks 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_in moved 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.mere compares tuples, records and variants of floats with ==, != and the four orderings, including NaN and both zeros. After review it also compares a float tree, which is a float as the type argument of a recursive generic type, and orders lists of float tuples.

  • It has no bare float comparison, so nothing else keeps __feq or __flt alive.
  • Its constructors are not None/Some, because the RV prelude has its own option type and refuses a program that redefines those.
  • scripts/parity.sh gives MATCH for C, LLVM and Wasm.

scripts/rv_exec_check.sh adds the case to its must-execute fixtures, which run at both widths against test/parity/float_compound_eq_order.expected.

  • This is needed because the corpus sweep counts a compile refusal instead of failing on it. Without the fixture, the u___flt refusal would have read as coverage.
  • The oracle was written from the interpreter's output, then checked value by value against the source.
Mere RV32I RV64I
before (0c083af) undefined label u___flt undefined label u___flt
after matches matches

On the earlier ctest-style program without ordering, the old RV output was fftffffttf against the interpreter's tfftftftft.

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.
  • The extended parity program: scripts/parity.sh gives MATCH for C, LLVM and Wasm. Its output matches the oracle on RV32I and RV64I, and rv_exec_check.sh --contracts passes 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

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 284km left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_float walks 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 shows float_compound_eq_order:32 and :64 ok.

Findings

Major

  1. CI is red because of the new parity file. first_run_check fails with README 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

  1. The fixture does not cover the paths cmp_reaches_float adds beyond a direct field: a float reached only through the type argument of a recursive type (for example float tree with type '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.
  2. 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_compare uses OCaml's total compare (NaN sorts first). That difference is older than this PR and is documented in eval.ml.

Questions I checked

  • Reach coverage. Every request_eq / request_cmp call comes from compile_cmp or from inside another helper, so every helper chain starts at a source Cmp. Monomorphization runs before reachability, so a == inside a generic function (list_member, a derive (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 by check_map_key, so neither is a hidden comparison site. Type aliases are expanded before codegen, and cmp_reaches_float and compile_cmp both read the left operand's type, so they agree.
  • !=. It is xori of __eq_<tag>, and __eq_float is IEEE, so NaN != NaN is true (fixture line 2 and my probes).
  • Tail call. __eq_float keeps ra and passes a0 / a1 through. That is the same convention __cmp_float already 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 in a0 / a1 is the same kind of value __feq gets from a bare comparison.
  • Moved block. I diffed the removed and added lines: the block from tops to subst_ty is identical apart from one blank line, and vars_in only gains the new arm.
  • rv_exec_check.sh. In the fixture loop a build failure prints FAIL and sets rc=1. The loop is skipped when no runner exists, but CI's --contracts mode 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 .expected file.
  • 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 == v and u < v on float tuples; 'a Box of a float and of a float tuple
    • list_member, list_max, list_min on float tuples
    • derive (Eq, Ord) on a variant with float payloads
    • nested records, and 'a W of 'a W of a list of records with a float
    • t == t where t holds a NaN is false
  • On the merge base (0c083af) the ordering probes fail with undefined label u___flt, and an ==-only probe prints ftftf instead of tffft, so the probes do exercise the change.
  • --bare with a compound float == still assembles.

Not checked

  • I did not run dune runtest, scripts/ctest.sh or the full rv_exec_check.sh --all sweep. 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,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.
@284km

284km commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, both addressed in ce793e5.

  • [Major] parity count: README now says 255. scripts/first_run_check.sh passes locally: "parity count 255, test count 3305".
  • [Suggestion] coverage: the parity program now also compares a float tree, a float as the type argument of a recursive generic type, with ==/!=, including a NaN node. It also orders lists of float tuples, including a longer list against its prefix and -0.0 against 0.0.
    • These are the two paths cmp_reaches_float takes through a type argument and a recursive type's own fields.
    • The extended program matches on interp, C, LLVM and Wasm (parity.sh) and on RV32I/RV64I against the updated oracle.
    • rv_exec_check.sh --contracts passes 13/13.

I could not rerun the old compiler myself: the 90d0589 build I used before is gone. Your check that main refuses both with undefined label u___flt covers the before side. The PR body is updated with these runs.

@284km 284km left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of ce793e5.

  • [Major] parity count: resolved. README now says 255, and test/parity at this head holds 255 .mere files. 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.0 against 0.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.

@284km
284km merged commit 594e412 into main Nov 23, 2026
16 checks passed
@284km
284km deleted the rv-float-struct-eq branch November 23, 2026 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant