Skip to content

selfhost: corpus parity between flowc and the Python host, and the top gaps closed - #1018

Merged
godofecht merged 15 commits into
mainfrom
selfhost/corpus-parity
Sep 29, 2026
Merged

godofecht merged 15 commits into
mainfrom
selfhost/corpus-parity

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Measures how far the flowc host is from replacing the Python host on the C backend, then closes the largest gaps.

Measuring base

Built on origin/main merged with the in-flight compiler branches: fix/stage-a-batch-3, selfhost/lower-proofs, selfhost/lower-effects, port/dynamics-dsl-flowc, port/shader-dsl-flowc and port/pkg-native. The bootstrap C was regenerated for the merge. The merged compiler imports more than 32 modules and emits more than 1 MiB of C, so the bundle caps went up first. This branch should be rebased or re-merged as the train lands.

compiler/scripts/corpus_parity.sh

Every tracked .flow file with a main() is built the way flow run builds it, once with FLOW_HOST=flowc and once with FLOW_HOST=python. Both executables run with a timeout, and the script compares build accept or reject, exit code and stdout. It prints a table by directory and ranks the failure causes by the first flowc diagnostic.

  • With no flags it runs flowc against the Python results recorded in compiler/corpus_parity/python_golden.tsv. It needs no Python, so it can run in CI.
  • --python compares against the live Python host. --python --record rewrites the goldens.
  • --check gates on a per-OS floor in compiler/corpus_parity/floor.txt, set 10 under the run for flaky HTTP and timing programs.
  • The committed report is compiler/corpus_parity/report.txt.

Result

at parity Python builds it, same run
before (merged base) 792 / 998 (79.4%) 543 / 743 (73.1%)
after 960 / 1014 (94.7%), plus 5 where only flowc's C compiles 712 / 759 (93.8%)

The corpus grew by the new regression tests.

Fixes, largest cluster first

Each fix has a test in tests/lang.

  1. flow-driver links the runtime that the Python path links on the flowc path too: runtime/.c, plus lib/runtime/.flow emitted by flowc. It is cached as a static archive. Link failures went from 73 to 9.
  2. A const named like a libc macro (CLOCKS_PER_SEC) gets #undef first. expected ')' went from 29 to 1.
  3. Import forms the Python resolver accepts: bare stdlib names, "stdlib/vec" without a suffix, dotted imports relative to the module with a trailing symbol, and .a.b imports. An import that cannot be resolved now prints a message. Gather failures went from 14 to 1.
  4. Struct literal array fields from named arrays, local array assignment, array literal arguments and len() of sized arrays.
  5. Overload selection uses the declaration in scope and float literals. Overload checks accept span<mut T> and memref_T. Dual operator sugar ([roadmap] Dual + Tensor operators + mutable params #161).
  6. Cross-module &[T] parameters wrap array arguments in a span, via the signature table.
  7. print/println of values flowc cannot type, complex values and multiple arguments.
  8. Struct typedef order, let f = f(x), libc-named functions (heapsort, index, div), str, return void, gpu_thread_id, and an imported module's own main().
  9. @cImport preprocesses with the flags programs build with, so no Annex K prototypes and no SDK preprocessor noise.
  10. Plain function pointers for function-typed lets bound to plain functions or casts, and (p as ptr<T>)[i].
  11. expect statements were dropped entirely. They now emit and exit 1 with the Python message.
  12. test_parser_python_forms and its parity_lowering golden now follow the lowered effect and pattern forms.

Top remaining causes

  • 13 stdout differences. 8 are GA dynamics examples whose output hangs on signed overflow in stdlib/dynamics/ga.flow ga_hash (undefined behaviour, so the result depends on the optimizer). The rest are timing and float formatting.
  • flowc is stricter than Python --lenient on the units and effects fixtures, 9 rejections. These files are meant to be rejected.
  • Exit code differences: Python's runtime overflow and bounds checks (134), Python interop needing a live interpreter, and timeouts.
  • Closures that capture and are passed to function-typed parameters need one closure ABI (test_fn_ptr, test_closures, 11_closure_snapshot).
  • Generic Chan<T> from an imported module (test_generic_channels), trait impl calls, flow.toml [paths], and Tensor operator sugar.

Verification

  • Fixed point: OK.
  • Pass: bootstrap_from_c.sh --verify, self_host_full.sh, roundtrip.sh, parse_coverage.sh --check, parity_dynamics_dsl, parity_effects, parity_field_dsl, parity_flow_blocks, parity_lowering, parity_proofs, and the Python ratchet (tightened).
  • parity_shader_dsl passes its fixtures and host-stub checks. Its Metal end-to-end step needs the Metal toolchain, which the machine it was run on lacks.

corpus_parity.sh is bash because it runs processes, builds and timeouts. The clustering could move into a Flow program later.

@godofecht
godofecht force-pushed the selfhost/corpus-parity branch from 46d5bfb to 6580ea9 Compare September 29, 2026 09:51
The merged compiler imports more than 32 modules and its bundled C is past
1 MiB, so flowc could no longer emit itself. MAX_MODS goes to 128 and the
emit buffer to 4 MiB in main.flow and driver.flow.
compiler/scripts/corpus_parity.sh builds every tracked .flow file with a
main() the way flow run does, once per host, runs both with a timeout and
compares build accept or reject, exit code and stdout. It prints a table by
directory and the failure causes ranked by the first flowc diagnostic.

Without flags it runs flowc against Python results recorded in
compiler/corpus_parity/python_golden.tsv, so it needs no Python and can run
in CI. --python compares against the live Python host and --record rewrites
the goldens. --check gates on a per-OS floor in floor.txt.

Measured on the merged base: 792 of 998 files at parity (79.4%), 543 of the
743 that Python builds run the same.
flowc_link_c linked the generated C alone, so any program that used
channels, fibers, TLS, the audio stub, GPU memory or another runtime entry
point failed at link time on FLOW_HOST=flowc. It now links the same runtime
the Python path does: runtime/*.c (concurrency, fibers, TLS, the dlopen
Python bridge, Metal on Darwin) and the always-linked lib/runtime/*.flow
modules, with those modules emitted by flowc rather than Python. The objects
are built once per source and flowc state into
build/flowc_runtime/<key>/libflowrt.a, so a program pulls in only what it
uses. -lm and the program's own directory as an include path come along.
FLOWC_NO_RUNTIME=1 keeps the old single-file link.

Corpus parity (compiler/scripts/corpus_parity.sh), link failures on the
flowc side: 73 before, 9 after.
The overload compatibility check read span_mut_i32 as a span of mut_i32, so
passing an array to a span<mut T> parameter was "no matching overload".
memref_T, a pointer to T on the Python host, did not accept an array
either.

Corpus parity: "no matching overload" 3 before, 1 after.
Tests: tests/lang/test_span_mut_array_arg.flow, test_memref_param.flow.
Bundle gathering failed with no message on imports the Python host
resolves:

- a bare stdlib module, import "io.flow" or import "lib/stdlib/nn.flow",
  now falls back to lib/stdlib;
- import "stdlib/vec" without the .flow suffix;
- dotted imports against the importing module's directory, and with a
  trailing symbol (import a.b.sym names sym in a/b.flow);
- relative .a.b imports name a/b.flow.

An import no search path finds now says so:
flowc gather: <module>: cannot resolve import <text>. The cross-module
signature table grows to 1 MiB for the span parameter entries cgen adds.

Corpus parity: gather failures 14 before, 1 after (http_json_cache needs
flow.toml [paths]).
Test: tests/lang/test_import_path_forms.flow.
Each fix comes from a cluster in the corpus parity report, with a test in
tests/lang:

- A const named like a libc macro (CLOCKS_PER_SEC) gets #undef first; the
  preamble includes <time.h>. expected ')': 29 files before, 1 after.
  test_const_libc_macro_name.
- A struct literal field set from a named array is copied in with memcpy;
  C decides at compile time whether the field is an array or a pointer.
  float[256] initializers: 9 before, 0 after. test_struct_lit_array_field.
- Overload selection types an identifier argument by the declaration in
  scope. It used to take the first parameter of that name in the whole
  program. test_overload_param_scope.
- i % x with an integer left side and a float right side casts the right
  side, as the Python host does. test_mod_int_by_float.
- An array passed to a &[T] parameter of a function in another module of the
  bundle is wrapped in a span: the signature table now records span
  parameters. test_span_param_cross_module.
- print/println of a value flowc cannot type picks the conversion from the
  C type (complex prints as (re + im j)), and several arguments print
  separated by spaces. test_print_complex_multi.
- memref_T is a pointer to T. test_memref_param.
- @cImport preprocesses with cc -E -std=c11 -D_DEFAULT_SOURCE, the flags
  programs build with, so Annex K prototypes such as memset_s no longer leak
  in and the SDK headers no longer print preprocessor errors.

Corpus parity with all fixes so far: 792 of 998 (79.4%) before, 919 (92.1%)
after.
A file whose Python-generated C does not compile but whose flowc build
succeeds is "flowc better"; the floor gates on parity plus better.
compiler/corpus_parity/nondet.txt lists files whose stdout depends on the
clock although two back-to-back Python runs agreed. Link failures name the
first undefined symbol, and a C error in the generated file wins over
preprocessor noise from system headers. The driver runs as one function so
editing the script during a run is safe.
More corpus parity clusters, each with a test in tests/lang:

- Struct typedefs come after the structs their fields hold by value; flowc
  kept source order, so a struct that used a later one did not compile.
- let f = f(x), where f is also a function, evaluates the initializer into
  a temporary first; C put the new local in scope inside its own initializer.
- Assigning to a local sized array copies (memcpy), from a call or from an
  array literal. test_struct_order_shadow_array_assign.
- gpu_thread_id() with no Flow definition is the Python host's host stub,
  thread 0. link: undefined symbol gpu_thread_id: 5 files before, 0 after.
- An imported module's own main() steps aside for the entry's. flowc renamed
  the entry's main and ran the library demo. test_import_module_with_main.
- A Flow function named like a libc function that the preamble's headers
  declare with another signature (heapsort on macOS, index, random, abs)
  is renamed for the unit with a #define. The Python host mangles every
  function by its parameter types and never clashed. conflicting types:
  2 files before, 0 after.
- str is the Python host's other spelling of string.
- return void is a bare return in the type checker too; cgen already
  emitted it.
- The return checks now say what failed. They counted an error with no
  message, which the parity report could not cluster.

Test: tests/lang/test_libc_names_return_void.flow.
a + b, a - b, a * b and a / b with a Dual operand call add, sub, mul and
div, and -x on a Dual calls neg. Overloads resolve by operand type (Dual or
f32); a single local definition keeps its plain name. div joins the
libc-named functions that get a #define, since <stdlib.h> declares div().

Corpus parity: Dual operand errors 4 files before, 1 after. The corpus run
timeout goes to 20 s; fiber programs timed out under a 12-job load.
Test: tests/lang/test_dual_operator_sugar.flow.
- A let of function type bound to a plain function (a named function, a
  function parameter, a non-capturing lambda variable or a cast to a
  function type) is a plain C function pointer, like function parameters.
  cfn(...) lets always are. flowc emitted a closure struct and could not
  call it. tests/lang/test_dlopen passes.
- (p as ptr<T>)[i] keeps the cast in parentheses; C indexed first.
- A float literal argument selects the f32/f64 overload.
  tests/lang/test_function_as_value passes.
- len() of a sized array is its element count. test_len_sized_array.

Closures that capture and are passed to a function-typed parameter
(test_fn_ptr, test_closures, examples/book/11_closure_snapshot) still need
one closure ABI for parameters and locals.
A statement-level expect parses to a bare AST_UNARY, which emit_stmt did
not handle, so every expect was dropped. It now emits, and a failed expect
prints "expect failed (line N)" and exits 1, as on the Python host. dbg
prints with the conversion of its operand's type.

tests/lang/test_expect_fails and test_dbg_expect now pass on flowc; the
corpus parity run-exit cluster (Python 1, flowc 0) is gone.
An array literal passed to an array<T>, ptr<T> or memref_T parameter is
emitted as (T[]){ ... }. A bare brace list is not a C expression.

Test: tests/lang/test_array_literal_argument.flow.
compiler/corpus_parity/report.txt, from the flowc built from the
regenerated bootstrap C against the Python goldens:

  before (merged base): 792 of 998 at parity (79.4%); 543 of 743 that
                        Python builds run the same (73.1%)
  after:                960 of 1014 at parity (94.7%), plus 5 where only
                        flowc's C compiles; 712 of 759 that Python builds
                        run the same (93.8%)

The corpus grew by the new tests/lang regression tests and their goldens.
The floor is set 10 under the run (955) for the HTTP and timing programs
that are flaky in parallel, the default run timeout is back to the 10 s the
goldens were recorded with, --only skips the floor, and a run names the
untracked files programs leave in the tree.
@godofecht
godofecht force-pushed the selfhost/corpus-parity branch from 6580ea9 to 2a53316 Compare September 29, 2026 10:30
@godofecht
godofecht merged commit 84806b0 into main Sep 29, 2026
24 checks passed
godofecht added a commit that referenced this pull request Sep 29, 2026
…d the rest of #1018's gaps

One closure representation for function types, the Python host's runtime
checks under FLOWC_CHECKS=1, generic templates across bundle modules,
parenthesised unary operands (#1017), impl blocks and flow.toml [paths]
imports with a fallback, Tensor operator sugar, quantity literals,
FLOWC_LENIENT=1, pointer receivers across modules, array<T>(N), @cImport
as a plain include, for ranges without a step, and libc-named Flow
functions reaching their definitions. Squashed from
selfhost/corpus-parity-2 onto main.
godofecht added a commit that referenced this pull request Sep 29, 2026
…d the rest of #1018's gaps (#1025)

One closure representation for function types, the Python host's runtime checks under FLOWC_CHECKS=1, generic templates across bundle modules, impl blocks and flow.toml [paths] imports, Tensor operator sugar, quantity literals, FLOWC_LENIENT=1, pointer receivers across modules, array<T>(N), @cImport as a plain include, for ranges without a step. Files marked flow:flowc-only are skipped by the Python-host test sweep. Corpus parity 1044 >= 1033 (Darwin); every parity gate passes.
@godofecht
godofecht deleted the selfhost/corpus-parity branch October 1, 2026 12:50
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