Skip to content

RISC-V: lower a builtin applied to more or fewer arguments than its arity; pin RV refusals; build mere-ruby for RV64 in CI - #96

Merged
284km merged 2 commits into
mainfrom
rv-builtin-arity
Nov 30, 2026
Merged

284km merged 2 commits into
mainfrom
rv-builtin-arity

Conversation

@284km

@284km 284km commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

The RISC-V backend refused a builtin applied to a number of arguments other than its own arity:

let hooks = map_new ();
let _ = map_set hooks 0 (fn (u: unit) -> print_int 7);
let _ = (if map_has hooks 0 then (map_get hooks 0) () else ());

The interpreter, C and LLVM print 7, and Wasm compiles it too. -rv / -rv64 said RV64I: `map_get` has no RISC-V lowering yet (host builtin) -- although the two-argument map_get has been lowered all along. The same happened with fewer arguments (let at = vec_get v in at 0, bit_and 12, str_eq "x", char_at s, str_compare "b", float_of_bits, ...), through a top-level alias of the builtin (let g = map_get; then (g m 0) ()), and through () |> map_get m 0 and map_get m 0 <| x.

mere-ruby hit it: since 2026-11-17 it keeps callbacks that a later module registers in a Map or a Vec and calls them as (map_get hook 0) () / (vec_get hook 0) () (three places), and its RV64 build has not compiled since. No gate here or there built it for RISC-V, so nothing said so.

Why

Every builtin case in compile_app (lib/codegen_riscv.ml) matches its builtin's exact arity (| Ast.Var "map_get" when List.length args = 2 -> ...). With any other count, the application fell through all of them to the last case, which refuses every builtin it reaches. Top-level functions have had both other shapes since v0.1.599 (k < arity is an eta closure, k > arity the saturated call plus the rest applied to the result); builtins did not. Builtins used as values already go through compile_eta (v0.1.638).

The change

  • lib/codegen_riscv.ml: in the refusing case, before refusing, compare the argument count k with the arrows in the builtin's type in Typer.initial_env (n). k < n: compile_eta env head n args. k > n: build the saturated application and compile_indirect the rest (tail position preserved). k = n refuses as before. Both re-enter compile_app with exactly n arguments, so a builtin that really has no lowering is still refused by name from inside the saturated form. As this is the last case before the refusal, only programs that were refused before can change.
  • test/parity/builtin_arity_shapes.mere: over-application (map_get, vec_get, two left-over arguments, owned_vec_get, fst/snd, try_or, tail position, a top-level alias, |> and <|) and partial application (map_get, vec_get, bit_and, str_eq, char_at, str_compare, a partial application passed as a value). README's parity count is 258.
  • scripts/rv_exec_check.sh: compile-time refusals of the corpus are pinned by name. Until now a refusal was only counted and listed, and the gate stayed green -- so the parity program above would not have turned RISC-V red if the backend refused it again. EXPECTED_REFUSED32 lists the 20 programs whose literals do not fit RV32's int; EXPECTED_REFUSED64 is empty. An unlisted refusal fails, and so does a listed program that now compiles (a name is only checked when its C reference built, so a host that skips a program earlier does not see a stale entry). The RV64 emulator is now built next to the RV32 one, before the contracts.
  • now_ms on RISC-V: now_ms, the C runtime's monotonic millisecond clock (extern fn now_ms: unit -> int), joins the externs the RV prelude answers (libm_sigs + __libm_now_ms, clock_gettime with CLOCK_MONOTONIC; at 32 bits the count wraps and differences under about 24 days stay right). A call used to abort. test/rv/host_services.mere checks it by properties (does not go backwards, moves within a bounded loop, agrees with time to within a second). That contract now runs at both widths (it ran at 32 bits only) and requires all nine property lines: it used to pass a run that aborted after a few ok lines, since the abort message does not say FAIL.
  • CI, RISC-V group: downstream_rv (mere-ruby still compiles for RV64) runs scripts/downstream_rv_check.sh --poison, which emits each row of the new test/downstream/RV table -- today mere-ruby, prepared by its own tools/nothreads_tree.sh --coro -- with -rv64 --ram 1024, using the dogfood clone that group already has. Emission only: whether a program copes with a target that has no C library is the downstream's own contract. The poison leg first requires a refused program, a failed prepare command and a missing checkout to fail, each for its own reason. ci_gate_check.rb expects 203 checks.
  • test/escape/ROUTES: closure_out and closure_captures_container listed RISC-V as unreadable because both call the stored closure as (vec_get o 0) (). RISC-V reads them now (escape_check fails on a stale unreadable entry, which is how CI found them); both are SAFE as on the other backends and print the interpreter's answer at both widths on memu.
  • README test count 3307: main already passes 3307 tests on CI's toolchain while the README says 3305, so first_run_check was red on main (run of bd5e766). This PR adds no unit tests.
  • v0.1.733, changelog, RUNBOOK and stdlib-reference.

Checked (macOS arm64)

  • Byte-identity: every -rv and -rv64 binary of the 257 existing parity programs and the 20 test/rv programs, compiled by main (bd5e766) and by this branch: 533 identical, 0 different, 21 refused by both (RV32 literal width).
  • dune runtest: 3307 passed, 0 failed (main gives the same 3307, here and on CI).
  • MEMU=<memu 7564b424> sh scripts/rv_exec_check.sh --all: 64 bits 257 passed, 0 failed, 1 known-different, 0 refused; 32 bits 244 passed, 0 failed, 8 known-different, 20 refused (all listed); every contract ok, including host_services@32 and @64 (9 properties). 606 s.
  • Negative control: the same script with main's compiler fails on builtin_arity_shapes (-rv refused it, and it is not in EXPECTED_REFUSED32).
  • sh scripts/parity.sh: 258 passed, 0 failed; failing programs 31 passed, 0 failed. Unchecked lists are the same as on main (schema_reflect on LLVM, coro_pingpong on Wasm; int_width_boundary pinned). builtin_arity_shapes also runs on RV32 and RV64 on memu with the interpreter's output.
  • python3 scripts/rv_exec_partition_check.py, sh scripts/rv_prelude_check.sh (601 names), sh scripts/host_matrix.sh (unchanged: 218 builtins, 0 MISSING), sh scripts/doc_claims_check.sh, sh scripts/doc_coverage_check.sh, ruby scripts/ci_gate_check.rb and --self-test (203 checks across 9 groups), sh scripts/tool_preflight_check.sh --poison (17 caught): all ok.
  • sh scripts/first_run_check.sh: PASS (parity count 258, test count 3307). sh scripts/escape_check.sh: PASS, 27 routes.
  • MERE_DOWNSTREAM=<dir with a fresh mere-ruby clone> sh scripts/downstream_check.sh: mere-ruby ok (33 s).
  • MERE_DOWNSTREAM=... sh scripts/downstream_rv_check.sh --poison with mere-ruby f0f372e7 (its current main): three poisons caught, then ok mere-ruby -rv64 --ram 1024 (main.mere) 46s, peak RSS about 3.1 GB. With main's compiler the same row fails: RV64I: `map_get` has no RISC-V lowering yet (host builtin) at m_numeric.mere:2634.

CI time

Measured on this PR's first CI run: the downstream_rv step took 50 s on ubuntu-24.04 (mere-ruby -rv64 emission 50 s; the three poisons under a second), in the RISC-V group, which finished in 14.1 minutes -- not the longest group (containers took 17.3). So the wall-clock time of CI does not grow. Locally the step is 46 s with a peak RSS of about 3.1 GB. host_services at 64 bits adds a few seconds to rv_exec contracts (19 s in that run). The pinned refusals ran in rv_exec corpus32 / corpus64 and passed. That first run failed only on escape_check (the two stale rows above, fixed in the second commit) and on first_run_check's test count, which was red on main too.

Not covered here

  • mere-ruby's RV64 binary does not run yet even with this compiler: it calls a C-shim extern (process CPU time) unguarded at startup and in every collection, which on RISC-V aborts. That is mere-ruby's to fix (it wraps its other startup externs in try_or); this PR only makes the compile step visible here.
  • The Wasm build of mere-ruby is broken for a reason on its side; no -w row is added.

🤖 Generated with Claude Code

…rity

Every builtin lowering in the RISC-V backend matched its exact arity, so
`(map_get hooks 0) ()` and `(vec_get fs 0) 5` (more arguments: the result
is a function, called on the spot) and `map_get m`, `bit_and 12` or
`str_eq "x"` (fewer: a closure over the arguments given) fell through to
the refusal, which said the builtin had "no RISC-V lowering" although its
exact-arity form was lowered. The typer accepts both shapes and C, LLVM and
Wasm lower both. They now take the path a top-level function's have since
v0.1.599: fewer arguments are an eta closure around the saturated call,
more are the saturated call with the rest applied to its result. This is
the last case before the refusal, so no program that compiled before
changes: every RV32 and RV64 binary of the parity and test/rv programs is
byte-identical. mere-ruby had stopped compiling for RV64 on three such
calls; it compiles again.

test/parity/builtin_arity_shapes.mere covers over-application (two
left-over arguments, tail position, a top-level alias, |> and <|) and
partial application; interp, C, LLVM, Wasm, RV32 and RV64 agree.

rv_exec_check pins the corpus programs RISC-V refuses at compile time
(EXPECTED_REFUSED32: the 20 whose literals do not fit RV32's int;
EXPECTED_REFUSED64: none). A refusal used to be only counted, so a parity
program RISC-V refused kept the gate green.

now_ms, the C runtime's monotonic millisecond clock, is answered on hosted
RISC-V (clock_gettime, CLOCK_MONOTONIC); a call aborted before.
test/rv/host_services.mere checks it, now at both widths, and the check
requires all nine property lines: a run that aborted halfway used to pass.

The RISC-V CI group runs scripts/downstream_rv_check.sh --poison, which
emits mere-ruby (test/downstream/RV, through its tools/nothreads_tree.sh
--coro) with -rv64 --ram 1024. No downstream gate built anything for
RISC-V. 203 checks.

v0.1.733.

@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.

Reviewed exact head b09cd9157a96d517b3a6d04e8a53bee19b4a02a0 against current main bd5e766d6842143cd4f845f194f894cf26386d0c in isolated checkouts. No actionable findings. The fallback preserves saturated builtin lowering, and the typed saturated expression preserves the result information needed by subsequent indirect calls. Pinning compile refusals and requiring every host-service property line close the previous silent-skip/truncated-output gaps.

Independent validation on macOS arm64:

  • Fresh base/head builds with OCaml 5.4.1 and Dune 3.23.1. dune runtest: 3,061 passed, zero failed on each revision. Node is unavailable here, so optional self-host codegen cross-validation was skipped; these are not the full-toolchain CI counts.
  • Compiled the same 257 original parity fixtures and 20 original RV fixtures with both compilers, at both widths: 533 byte-identical outputs, 21 refused by both, no changed acceptance. The original host-services source was used for this compiler comparison; the updated fixture was tested separately.
  • New builtin_arity_shapes runs match the interpreter exactly on RV32/RV64 with memu 7564b424. Both base compiler invocations reproduce the original map_get refusal. Additional focused cases cover one-time argument evaluation/capture, float results, curried function results and nested calls; both widths match the interpreter.
  • Updated host_services produces all nine successful property lines at both widths. This tests ordinary clock behavior, not monotonicity across host wall-clock adjustments in memu.
  • Fresh mere-ruby main f0f372e7, pinned mgz 661a01d8: downstream_rv_check.sh --poison catches all three failure classes, then RV64 emission succeeds (89 seconds). The identical downstream with the base compiler fails on map_get in m_numeric.mere, confirming the repaired regression. Emission success does not establish downstream execution.
  • RV partition audit passes (1,087 fixture compiler invocations); CI assignment and self-tests pass (203 checks / nine groups); documentation claims/coverage, all 601 RV prelude names, shell syntax and diff whitespace checks pass.

I did not independently rerun the complete emulated corpus or every hosted gate. Hosted build/distribution checks passed, but gate groups are still executing, so the queue-only merge exception does not apply. All jobs of latest main CI were inspected: Mere run 46148138711 has the pre-existing README test-count failure (3,305 claimed versus 3,307 observed); mere-ruby run 46362164472 has the recorded Module timeout failure. Neither is claimed repaired here. The recorded hold_all_merges / new_failure_review_required flags remain active. Already-reviewed mere-ruby #410, #414 and #415 were also considered; no merge attempted.

COMMENT because the authenticated account owns this PR.

…; README: 3307 tests

Both escape routes call the stored closure as `(vec_get o 0) ()`, which the
RISC-V backend refused until the previous commit, so the table listed rv as
unreadable for them; escape_check failed on the stale entries. RISC-V now
accepts both (SAFE, as the other backends) and prints the interpreter's
answer at both widths.

The README claimed 3305 tests; the suite passes 3307 on CI's toolchain (it
already did on main, where first_run_check was red).

@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.

Follow-up review of 601c1453c3b7625708a6b9531a874521cb4f288f against current main bd5e766d6842143cd4f845f194f894cf26386d0c: no actionable findings. The delta since the previous reviewed head is exactly four documentation/escape-table files; runtime, compiler and build inputs are unchanged.

Independent focused validation on macOS arm64:

  • Reused the previous independently built compiler only after checking its checkout is clean and all runtime/build inputs are identical to this head.
  • escape_check.sh: PASS, all 27 routes, zero open holes.
  • Both affected original fixtures (closure_out, closure_captures_container) compile and execute on memu at RV32 and RV64, matching the interpreter exactly (8 and 42 respectively).
  • The README/language-reference count of 3,307 agrees with the actual full-toolchain count in the inspected main CI log; main's failure was the stale 3,305 claim. I did not rerun the full-toolchain suite locally; the earlier local count was 3,061 with optional Node checks unavailable.
  • Diff whitespace check passes. The earlier implementation review and its disclosed coverage/limits remain applicable.

Hosted CI is still executing, rather than merely waiting for unassigned runners. No merge attempted: the queue-only exception does not apply, and the recorded global merge holds remain active. Already-reviewed mere-ruby #410/#414/#415 were considered as well; their reviews/checks do not clear those holds.

COMMENT because the authenticated account owns this PR.

@284km
284km merged commit 72bb0d0 into main Nov 30, 2026
17 checks passed
@284km
284km deleted the rv-builtin-arity branch November 30, 2026 12:23
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