Skip to content

Commit 6df6884

Browse files
committed
Attempt to fix JIT icache coherency on Arm64
The JIT compiler was experiencing intermittent failures on Arm64/Apple Silicon due to missing instruction cache invalidation after patching branch instructions. When update_branch_imm() modified branch targets in JIT-compiled code, the CPU's icache wasn't being invalidated, causing it to execute stale cached instructions instead of the newly patched ones. This manifested as non-deterministic test failures, particularly in compute-intensive benchmarks like the pi calculation test. The fix adds sys_icache_invalidate() after memcpy() in update_branch_imm to ensure the icache is synchronized with the data cache after code modification. This is critical on Arm64 cores which have separate L1 instruction and data caches.
1 parent d22b787 commit 6df6884

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/jit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ static void update_branch_imm(struct jit_state *state,
611611
pthread_jit_write_protect_np(false);
612612
#endif
613613
memcpy(state->buf + offset, &insn, sizeof(uint32_t));
614+
sys_icache_invalidate(state->buf + offset, sizeof(uint32_t));
614615
#if defined(__APPLE__) && defined(__aarch64__)
615616
pthread_jit_write_protect_np(true);
616617
#endif
@@ -2229,6 +2230,7 @@ static void resolve_jumps(struct jit_state *state)
22292230

22302231
uint8_t *offset_ptr = &state->buf[jump.offset_loc];
22312232
memcpy(offset_ptr, &rel, sizeof(uint32_t));
2233+
sys_icache_invalidate(offset_ptr, sizeof(uint32_t));
22322234
#elif defined(__aarch64__)
22332235
int32_t rel = target_loc - jump.offset_loc;
22342236
update_branch_imm(state, jump.offset_loc, rel);

0 commit comments

Comments
 (0)