Skip to content

Roc platform host shim #8214

Merged
Anton-4 merged 92 commits intomainfrom
roc-shim
Sep 9, 2025
Merged

Roc platform host shim #8214
Anton-4 merged 92 commits intomainfrom
roc-shim

Conversation

@lukewilliamboswell
Copy link
Copy Markdown
Collaborator

@lukewilliamboswell lukewilliamboswell commented Aug 22, 2025

Added support for multiple entrypoints in Roc platforms, allowing platforms to expose multiple functions that can be called by the host (e.g., init!, render!, update!).

Key Changes in this PR

  1. New Platform Header provides to decouple host entrypoint symbol names
  • Updated platform header parsing to support provides { init!: "init_entrypoint", update!: "update_entrypoint" } syntax
  • Changed from [] (array) to {} (record) for platform provides
  1. Multi-Entrypoint Extraction -- temporary implementations until we can use BuildEnv
  • Added extractEntrypointsFromPlatform to parse platform provides and extract function names
  • Added extractPlatformSpecFromApp for to extract from app headers
  1. Added exports field in ModuleEnv to track the exported definitions for an app module

  2. Platform Host Shim Generation

  • New platform_host_shim.zig which generates an LLVM bitcode shim that translate between the host's entrypoints and the pre-built interpreter's single entrypoint.
  • New builder.zig module for roc cli to compiling LLVM bitcode to object file
  • Added LLVM target support
  1. Shared Memory Layout
  • New Header Structure:
 struct {
     parent_base_addr: u64,
     entry_count: u32,
     _padding: u32,
     def_indices_offset: u64,
     module_env_offset: u64,
 }
  • Multi-entrypoint dispatch: entry_idx -> def_idx -> expr_idx
  • Persistent state: Global variables to maintain shared memory allocator and ModuleEnv across calls
  1. Build System Updates
  • Removed -Dllvm build flag (LLVM now included by default)
  • Added ZigLLVMInitializeAllTargets() function to zig_llvm wrapper
  1. Updated Test Platforms
  • Also upgraded the int platform to demonstrate multi-entrypoint functionality.
18:36:06 ~/Documents/GitHub/roc roc-shim $ zig build roc -Doptimize=ReleaseSafe && ./zig-out/bin/roc --no-cache test/int/app.roc
Generated numbers: a = 98, b = 61

=== Testing addInts (entry_idx = 0) ===
Expected add result: 159
Roc computed add: 159
SUCCESS: addInts results match!

=== Testing multiplyInts (entry_idx = 1) ===
Expected multiply result: 5978
Roc computed multiply: 5978
SUCCESS: multiplyInts results match!

=== FINAL RESULT ===
ALL TESTS PASSED: Both entrypoints work correctly!

Comment thread build.zig
// llvm configuration
const use_system_llvm = b.option(bool, "system-llvm", "Attempt to automatically detect and use system installed llvm") orelse false;
const enable_llvm = b.option(bool, "llvm", "Build roc with the llvm backend") orelse use_system_llvm;
const enable_llvm = !use_system_llvm; // removed build flag `-Dllvm`, we include LLVM libraries by default now
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.

removed -Dllvm -- this does increase the compilation times if you are doing things that doesn't need it like running snapshots, or unit tests... I feel it is simpler to default to having it. Maybe we have a flag -Dno-llvm if we still want slimmer builds for other things... or we could probably change the build script to not build llvm if we are just building the snapshots etc.

Comment thread src/cli/main.zig
Comment thread src/cli/test_shared_memory_system.zig
@lukewilliamboswell lukewilliamboswell marked this pull request as ready for review August 22, 2025 09:14
@Anton-4
Copy link
Copy Markdown
Collaborator

Anton-4 commented Aug 30, 2025

@lukewilliamboswell Claude fixed "Run Test Platforms (Unix)" but I'm not sure if the fix is in line with what you had in mind:

Segmentation Fault Fix Report

Summary

The segmentation fault was caused by a target ABI mismatch between:

  1. LLVM compilation: Used x86_64-unknown-linux-musl target (native detection)
  2. Linking: Forced GNU ABI (x86_64-unknown-linux-gnu) for str platforms
  3. Embedded shim library: Compiled with musl ABI

This created an ABI incompatibility where the generated platform shim expected GNU calling conventions but the embedded roc interpreter shim used musl conventions, resulting in a jump to address 0x0.

The Fix

I modified the code in /home/username/gitrepos/roc2/roc/src/cli/main.zig to make str platforms use musl static linking consistently instead of forcing GNU dynamic linking:

Changes Made

  1. Platform shim generation: Uses native target (musl)
  2. Str platform linking: Changed from .gnu to .musl target ABI
  3. CRT files: Uses the same musl vendored files as int platform (test/int/platform/targets/x64musl/)

Result

The fix ensures both LLVM compilation and linking use the same ABI (x86_64-unknown-linux-musl with musl static linking), eliminating the mismatch that caused the segfault.

@lukewilliamboswell lukewilliamboswell marked this pull request as ready for review September 1, 2025 05:04
@Anton-4
Copy link
Copy Markdown
Collaborator

Anton-4 commented Sep 1, 2025

I'm going to check out this flaky CI issue:

/home/runner/work/roc/roc/build.zig.zon:56:20: error: unable to discover remote git server capabilities: EndOfStream
            .url = "git+https://github.com/allyourcodebase/zstd.git#1.5.7",
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EDIT: this is ziglang/zig#21316

Anton-4 and others added 5 commits September 1, 2025 18:43
See ziglang/zig#21316 (comment)

Signed-off-by: Anton-4 <17049058+Anton-4@users.noreply.github.com>
Signed-off-by: Anton-4 <17049058+Anton-4@users.noreply.github.com>
Signed-off-by: Anton-4 <17049058+Anton-4@users.noreply.github.com>
Signed-off-by: Anton-4 <17049058+Anton-4@users.noreply.github.com>
@Anton-4 Anton-4 merged commit cb42d26 into main Sep 9, 2025
113 of 117 checks passed
@Anton-4 Anton-4 deleted the roc-shim branch September 9, 2025 11:15
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.

2 participants