Skip to content

Implement Node FFI over Tint native calls - #3

Draft
Qard wants to merge 80 commits into
mainfrom
feat/node-ffi
Draft

Qard wants to merge 80 commits into
mainfrom
feat/node-ffi

Conversation

@Qard

@Qard Qard commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Implementation

  • Implements Node 26.13.1 FFI over Tint's native ABI. It covers coded errors, stable byte lending, shared-block mappings and runtime-signature callback registration.
  • Typed call sites use guarded fixed-arity entries. They read the wrapper's typed plan from a stable view field with plain loads, then call C directly or use the classified frame. With the IR inliner now inlining bound record views, a direct typed call makes no calls before the C function.
  • General (untyped) callers reach a wrapper through:
    • its call step, which skips the generic context thunk;
    • a positional register entry for same-context calls with up to six arguments;
    • the typed Buffer leaf and callback trampoline entries;
    • compiled number entries, including the prototypes compiled for the program's call arities, which runtime definitions with the same signature key now use;
    • an exact-int32 fast path in argument validation.
  • Per-call checks avoid object identity comparisons and closure calls. Realm error views make the former costly, and closures with an environment can't be inlined.
  • Compiler:
    • The IR inliner inlines bound record views, which are pure and non-allocating.
    • Realm-typeof modules compile typeof x === "number" | "boolean" | "string" to a tag test.
    • Typed-only programs compile identically: the leaf-call image is byte-identical to main's, and the frozen v7 compiler's code sections are identical.

Status and acceptance

Draft. This PR lands after PR #12 (the general runtime re-architecture). It will then merge main and rerun the paired benchmark on a quiet host.

  • Typed paths: at or below Node in every case, by total call time and by FFI cost over a same-shape JavaScript twin.
  • Untyped paths: judged the same way against Node, by total call time and by FFI cost over the same-shape JavaScript twin. The benchmark also reports FFI cost over a trivial built-in twin (Number(), Math.max, ArrayBuffer.isView, Reflect.apply), which isolates the binding from the engine's built-in call protocol.

Paired performance

Fifteen rotated rounds at ca6306f2d under the shared timing and launch locks, one-minute load median 4.9. All 60 native launches reached LLDB entry and exited 42, and 1,830 checksums verified. Report · Per-round data

  • Typed: 9 of 9 cases at or below Node for both literal and runtime definitions, by total time and FFI cost. A zero-argument typed call takes 9.08 ns.
  • General, over the built-in twin: 9 of 9 for both definition forms; zero arguments costs 4.06 ns (literal) and 5.23 ns (runtime). Over the JavaScript twin and in total time, untyped calls still trail Node at most arities. That cost is the general call protocol PR Propose general runtime re-architecture from Octane diagnostics #12 replaces.
  • Host load: Node is far more sensitive to load than Tint; its zero-argument call took 33.6 ns in this run and 10.08 ns at load 2. The Tint figures above are also below those quiet-host Node figures (10.08 ns total; 7.12 ns over its built-in twin). Quiet-host run at a45f40e41

Validation

  • Pinned suite: 26 fixtures (25 in test/ffi plus one control file) and 122 case records at ca6306f2d: 94 passed, 25 diagnosed limits (Node CLI, permissions, VFS, libuv and V8 fast-API internals) and 3 upstream skips; none failed. Every fixture reached LLDB entry and exited 42, and each case matches the previous inventory. Report
  • Ledger: Linux arm64 with source replay and function fallback, against main's latest ledger general-calls-deep-prototype-dev-v1: 200 forms, no regressions. The only change from that baseline is the untyped explicit-binding-contract form, which current main shows as well. Ledger · Main recheck
  • Budgets: The leaf-call image is byte-identical to main 3d10cb5c0's. The frozen v7 compiler's code, unwind and data sections are identical. Under LLDB, both compilers' emitted frozen v7 images are byte-identical (SHA-256 68c11052…). Budgets
  • Native fixtures: all 18 cases in tools/node-ffi.test.ts pass under LLDB with Node 26.13.1 oracles, including the general-caller fixture added here.
  • Gates: CI runs mac-gate.sh (including the Test262 smoke in both linking modes) and linux-gate.sh on macOS arm64, Linux x86-64 and Linux arm64; all three must be green at the final head.

Known gaps found

Typed code that passes a realm typed array (for example Buffer.from(...)) to a Uint8Array parameter fails, in two ways:

  • through a local, it stops at a lowering invariant;
  • passed directly, the checked projection rejects it at run time.

Both belong to typed lowering, not node:ffi, and are recorded with reproductions in the unified-pipeline gap table.

🤖 Generated with Claude Code

Qard and others added 9 commits November 21, 2026 03:28
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard
Qard changed the base branch from feat/slice-3-and-2b to main November 20, 2026 20:37
Qard and others added 17 commits November 21, 2026 04:57
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The macOS CI child spent 65 seconds waiting for a gate memory slot before its build started. Count active preparation and execution against the existing ten-minute deadline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Preserve the identity and receiver of Node FFI exports in standalone typed programs. Project primitive typed callbacks and select shared owner storage for local byte arrays and their aliases when they cross a realm call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard
Qard marked this pull request as ready for review November 21, 2026 02:49
@Qard
Qard marked this pull request as draft November 21, 2026 02:50
Classify both literal and runtime signatures once. Keep reusable argument cells and result cells over each call frame, and format validation errors only on failure. Interleave benchmark engines with matching warmups and record the exact parity outcome.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Qard and others added 30 commits November 21, 2026 18:53
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	packages/ir/src/optimization/licm.ts
#	packages/ts-native/src/scalar.ts
#	tools/gate-lock.test.ts
#	tools/gate/watchdog.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	tools/check-test-files.json
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep the complete runs the previous delegate left uncommitted as
immutable observations. Later paired reports supersede each of them.

- Benchmarks v4 and v5 at 8cf9a93: nine rotated rounds, all
  checksums verified, every native launch exited 42 under LLDB. They
  predate the typed paths and measured only untyped definitions.
- Conformance v4 at a9fce40: complete 26-fixture inventory, superseded
  by the v5 and split-frame reruns.
- Ledgers at 7e964bb, a9fce40 and 8cf9a93, and the integrated
  ledger at 5e989d2, all on Linux arm64 without source replay. The
  8cf9a93 and 5e989d2 ledgers match main's non-replay ledger
  (general-calls-numeric-loop-dev-v2) in path and outcome for all 200
  forms. They are not comparable with main's latest replay-mode ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each benchmark case now has a JavaScript twin with the same call shape:
the same arity and integer arguments, a Buffer argument, or a callback
passed through a JavaScript caller. Subtracting the twin in the same
round and engine isolates the FFI boundary from the engine's general
call cost. Reports carry the twin time, the paired FFI cost versus Node
and an own-cost parity flag next to the existing control overhead.

The Buffer twin returns a constant, so the C read stays in the FFI cost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`typeof x === "number"`, "boolean" and "string" called the realm typeof
classifier in modules that use it, producing a string per test. The
classifier differs from the value tag only for object-tagged values
(callable and [[IsHTMLDDA]] objects, symbols and BigInts), so these
comparisons now emit the same `general-is` tag test as other modules.
"undefined" still uses the classifier because [[IsHTMLDDA]] objects
report it.

Typed-only programs have no realm typeof modules and are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Wrappers created in a Script or module get a positional register
  entry, so same-context calls with up to six arguments skip the
  argument list, realm thunk and call result record. One argument list
  per wrapper is reused outside reentrant calls and cleared afterwards.
- The owner invariant tests that the identity slot is live instead of
  comparing objects, which realm error views made costly on every call.
  The slot only ever names the owner or is cleared after collection.
- The call context's same-realm test moves into `runRealmBoundCall` in
  the general runtime, avoiding the same identity exposure.
- Integer bounds come from constant-foldable functions instead of
  module tables, and pointer arguments test for a BigInt view instead of
  calling the realm typeof classifier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
General callers of a unary pointer binding with a Number result now use
the typed leaf entry, which lends stable Buffer backing directly. Calls
shaped `(pointer, i32) => i32` with callbacks registered use the typed
callback trampoline entry when the integer operand is already valid and
the pointer projects; other operands take the ordinary adapter, so
errors still follow argument order. Document the general call entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Static number entries and the generic marshaler test an exact int32
first and fall back to the full check otherwise. Register words keep the
bits the general path writes: zero-extended unless the ABI sign-extends.
Stack operands still write four bytes.

Add a general-caller fixture, checked against Node 26.13.1, for argument
counts and validation on positional entries, reentrant calls through a
callback, error order on the callback entry and the Buffer leaf entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Node and the untyped Tint images also call a built-in twin of each shape:
`Number()`, `Math.max` over the same arguments, `ArrayBuffer.isView` on
the Buffer and `Reflect.apply` of the callback. An FFI wrapper is a
built-in function, so the FFI cost over this twin separates the binding
from the engine's call protocol for built-ins. Reports carry the twin
time and the paired comparison with Node when both engines ran it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Paired benchmark: 15 rotated rounds at one-minute load near 2. All 60
  native launches exited 42 under LLDB, and 1,830 checksums verified.
  Typed paths beat Node in 8 of 9 cases; zero arguments is +1.35 ns.
  General callers beat Node in 8 of 9 (literal definitions) and 6 of 9
  (runtime definitions) cases, measured over a built-in twin that shares
  their call protocol.
- Pinned Node FFI suite: 26 fixtures (25 in test/ffi plus one control
  file) and 122 case records: 94 passed, 25 diagnosed limits, 3 upstream
  skips, none failed. Each case matches the previous inventory.
- Ledger on Linux arm64 with source replay and function fallback: no
  regressions against general-calls-deep-prototype-dev-v1. One untyped
  form moved from build-failed to run-failed, and a focused rerun shows
  current main 3d10cb5 does the same.
- Budgets: the leaf-call image is byte-identical to main's, and the frozen
  v7 compiler's code sections are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The inliner now accepts a callee's general-record-is test and its
reference payload read when the callee tests a record, as bound record
views do. They neither allocate nor throw. Typed fixed-arity FFI entries
then resolve the wrapper and its typed record without calling
viewTypeofObject or viewTypedFfiCall. A typed zero-argument call probe
went from 12.4 to 10.7 ns per call (five paired runs).

Numeric fixed-arity entries move the mode and frame path out of line, so
the entry holds only the direct checks.

Functions without a record test inline exactly as before. The leaf-call
image is byte-identical to main's, as are the frozen v7 compiler's
code, unwind and data sections.

A per-call-site cache was not possible: generated realm helpers cannot
hold module instance state, and an optional `unknown` field on the
internal methods record has no native representation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A wrapper's call step runs the binding in the context the ordinary
  entry would select, so callObject skips the context thunk, the realm
  check frame and the action closure. The execution context stays for
  GetFunctionRealm. The same-realm test is `activeInRealm` in the general
  runtime, where it costs no error-view exposure.
- General calls read the maintained callback activity flags instead of
  calling `callbacks.active()` and scanning the native registry.
- Typed-only numeric plans (the i32 prototypes compiled for the
  program's call arities) also get the general wrapper. A binding whose
  runtime signature has the same key, including runtime definitions,
  calls the compiled entry instead of the generic frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Internal methods may carry `typedView`, an optional record holding the
general view that `typedRecord` returns, for records that never change.
FFI wrappers set it, and the typed fixed-arity entries read it with
plain loads. With record views inlined, a direct typed call makes no
calls before the C function. A typed zero-argument call probe went from
10.4 to 9.0 ns per call (five paired runs), and from 12.4 ns in all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The owner liveness test captures nothing, so it moves to module level
where calls can inline, and the general call path tests the library
handle directly instead of calling requireOpen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Paired benchmark: 15 rotated rounds, all 60 native launches exited 42
  under LLDB, 1,830 checksums verified. Typed paths are at or below Node
  in all 18 comparisons; a zero-argument typed call takes 9.08 ns.
  General paths are at or below Node in all 18 comparisons over the
  built-in twin. Intermediate runs at 8364a39 and e9e58c1 are kept.
- Pinned Node FFI suite: 26 fixtures and 122 case records; 94 passed,
  25 diagnosed limits, 3 upstream skips, none failed, matching the
  previous inventory case for case.
- Budgets: the leaf-call image is byte-identical to main 3d10cb5's, and
  the frozen v7 compiler's code sections and emitted image are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since e9e58c1 the generated fixed-arity entries read `typedView`
instead of calling `typedRecord`; the static module test still expected
the closure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Linux arm64 with source replay and function fallback, against
general-calls-deep-prototype-dev-v1: 200 forms, no regressions, and
every path and outcome identical to the a45f40e ledger. The untyped
explicit-binding-contract form remains run-failed, as on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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