Skip to content

Fix T2C crash on Apple Silicon#710

Merged
jserv merged 1 commit intomasterfrom
fix-t2c
Jan 26, 2026
Merged

Fix T2C crash on Apple Silicon#710
jserv merged 1 commit intomasterfrom
fix-t2c

Conversation

@jserv
Copy link
Contributor

@jserv jserv commented Jan 26, 2026

The LLVM MCJIT backend was crashing with SIGBUS when running DOOM on ARM64 macOS. Root causes and fixes:

  1. LLVM struct layout mismatch: The LLVM IR struct definition for riscv_internal had fields in wrong order (io at position 2), not matching the actual C struct layout. Fixed by reordering to match: halt, X[32], PC, timer, data, io.
  2. GEP-based io function offset: t2c_gen_call_io_func used GEP with incorrect struct indices. Replaced with manual pointer arithmetic (PtrToInt -> Add -> IntToPtr) using byte offsets from offsetof(). This is robust regardless of SYSTEM mode configuration.
  3. Hard-coded ecall/ebreak indices: Call sites used magic numbers that were incorrect. Changed to use offsetof(riscv_t, io) + offsetof(riscv_io_t, on_ecall/on_ebreak) for correctness across all build configurations.
  4. Code model on Apple Silicon: LLVMCodeModelLarge generates movz/movk sequences that are problematic on ARM64. Use LLVMCodeModelSmall for Apple Silicon, Large for other platforms.
  5. MCJIT initialization: Changed from LLVMCreateExecutionEngineForModule to LLVMCreateMCJITCompilerForModule with explicit options to ensure code model setting is respected.

Summary by cubic

Fixes a SIGBUS crash on Apple Silicon by aligning LLVM IR with the C struct, correcting io handler address calculation, and using the Small code model on ARM64 macOS. Also switches MCJIT initialization to respect these settings.

  • Bug Fixes
    • Match riscv_internal struct order in LLVM IR: halt, X[32], PC, timer, data, io.
    • Load io function pointers via byte offsets (offsetof) with PtrToInt/Add/IntToPtr; remove incorrect GEP usage.
    • Replace magic indices for ecall/ebreak with offsetof(riscv_t, io) + offsetof(riscv_io_t, on_ecall/on_ebreak).
    • Use LLVMCodeModelSmall on Apple Silicon; Large elsewhere, with PIC relocation.
    • Create the engine with LLVMCreateMCJITCompilerForModule and explicit options so the code model is applied.

Written for commit 7109507. Summary will update on new commits.

The LLVM MCJIT backend was crashing with SIGBUS when running DOOM
on ARM64 macOS. Root causes and fixes:
1. LLVM struct layout mismatch: The LLVM IR struct definition for
   riscv_internal had fields in wrong order (io at position 2), not
   matching the actual C struct layout. Fixed by reordering to match:
   halt, X[32], PC, timer, data, io.
2. GEP-based io function offset: t2c_gen_call_io_func used GEP with
   incorrect struct indices. Replaced with manual pointer arithmetic
   (PtrToInt -> Add -> IntToPtr) using byte offsets from offsetof().
   This is robust regardless of SYSTEM mode configuration.
3. Hard-coded ecall/ebreak indices: Call sites used magic numbers that
   were incorrect. Changed to use offsetof(riscv_t, io) +
   offsetof(riscv_io_t, on_ecall/on_ebreak) for correctness across all
   build configurations.
4. Code model on Apple Silicon: LLVMCodeModelLarge generates movz/movk
   sequences that are problematic on ARM64. Use LLVMCodeModelSmall for
   Apple Silicon, Large for other platforms.
5. MCJIT initialization: Changed from LLVMCreateExecutionEngineForModule
   to LLVMCreateMCJITCompilerForModule with explicit options to ensure
   code model setting is respected.
@jserv jserv added this to the release-2026.1 milestone Jan 26, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@jserv jserv merged commit 7187278 into master Jan 26, 2026
33 checks passed
@jserv jserv deleted the fix-t2c branch January 26, 2026 03:45
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