Skip to content

Implement optional FFI stack switching and @[ffi_stack] mode (§14.19) #583

Done

Description

@ehartford

Summary

Track the roadmap item for running selected FFI-heavy code on an OS-thread-sized stack. This is separate from the multi-worker fiber scheduler work in #455: stack switching is about avoiding small fiber-stack overflow at FFI boundaries and honoring the reserved @[ffi_stack] mode.

Current Evidence

  • docs/with-specification.md says FFI stack switching is roadmap / implementation-defined.
  • §14.19 describes switching from the fiber stack to a pre-allocated OS-thread stack at FFI boundaries.
  • The spec reserves @[ffi_stack] for forcing an entire function to run on an OS-thread stack.
  • The current runtime supports configurable fiber stack sizes via [runtime] fiber_stack_size, but no FFI boundary stack-switch mode is implemented.
  • docs/requirements.md marks the roadmap text as inventoried, not as evidence that runtime implementation exists.

Why This Exists

The fixed-size fiber stack baseline is acceptable for normal C calls when users can raise fiber_stack_size. The roadmap feature is still useful for deep C call trees and FFI-heavy code where making every fiber large would waste memory.

Design Constraints

  • @[ffi_stack] must be explicit. Hidden stack switching should not make suspension or stack behavior unpredictable.
  • The compiler must keep may_suspend and FFI callback restrictions intact; stack switching is not permission to yield through C frames.
  • If a platform lacks the required low-level support, the compiler/runtime should reject the mode loudly rather than silently ignoring it.
  • Keep the baseline fixed-size fiber path working and simple.

Implementation Sketch

  1. Parse/validate @[ffi_stack] as a real function attribute rather than only a reserved spelling.
  2. Add sema/codegen metadata so marked functions enter through an OS-stack trampoline.
  3. Add runtime support for an OS-thread-sized stack pool per scheduler/worker thread.
  4. At marked function entry, save the fiber stack context, switch to the OS stack, run the body, then restore the fiber stack.
  5. Audit panic/unwind/error-return paths so stack restoration is guaranteed.
  6. Add platform-specific implementations for Darwin and Windows, or emit a hard diagnostic for unsupported targets.

Acceptance Criteria

  • @[ffi_stack] has executable tests proving a marked function uses the OS-stack path.
  • Deep FFI-call smoke tests can run without requiring globally huge fiber stacks.
  • The mode is explicit and rejected on unsupported platforms.
  • may_suspend restrictions remain enforced for callbacks and C-frame boundaries.
  • Existing async/fiber tests continue to pass in baseline mode.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still open. @[ffi_stack] is reserved and nothing implements it:

    $ grep -rn ffi_stack docs/spec src
    docs/spec/029_lexical-and-binding-rules.md:407:| `@[ffi_stack]` | §14.19 | Reserved: OS-stack execution (roadmap) |
    

    post-release: a runtime feature the spec marks roadmap.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. added a commit that references this issue on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions