Skip to content

Commit

Permalink
refactor(zscript): use faster register allocation in jit
Browse files Browse the repository at this point in the history
asmjit's register allocation was using a bin-packing algorithm. This
turns out to be very slow for the assembly that our jit compiler emits,
since the vast majority of variables exist for only a single basic
block. Instead of bin-packing, the much faster and simpler linear scan
register allocation algorithm is now used.

For a very large 200k-instruction script, compilation went from 19.5s to
2.8s (a 85% decrease).

With this improvement, the 20k-instruction limit for jit has been
removed. One example of the impact of this is that `yuurand.zplay`,
which the jit compiler was previously mostly disabled for since most of
its scripts are huge, now has all its scripts compiled. This increased
the average FPS for `yuurand.zplay` from ~320 to ~2000, while still
reducing the total time spent compiling.

Theoretically this change in register allocation may produce slower
runtime code, but there was no drop in FPS realized in the `maths.zs`
stress test.
  • Loading branch information
connorjclark committed May 3, 2024
1 parent 0ab66ae commit 50b8b0b
Show file tree
Hide file tree
Showing 528 changed files with 838 additions and 710 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,9 @@ elseif(JIT_BACKEND STREQUAL "x64")
FetchContent_Declare(
asmjit
GIT_REPOSITORY
https://github.com/asmjit/asmjit.git
https://github.com/connorjclark/asmjit.git
GIT_TAG
bfa0bf690c2e90cc0844f2f012efa41b916bde7e
6d153ad8e13984841c2c642c0fa11e60f41e54dc
)
FetchContent_MakeAvailable(asmjit)
target_link_libraries(zplayer PRIVATE asmjit)
Expand Down
10 changes: 0 additions & 10 deletions src/zc/jit_x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,6 @@ JittedFunction jit_compile_script(script_data *script)

al_trace("[jit] compiling script type: %s index: %d size: %zu name: %s\n", ScriptTypeToString(script->id.type), script->id.index, size, script->meta.script_name.c_str());

// Check if script is like, really big.
// Anything over 20,000 takes ~5s, which is an unacceptable delay. Until scripts can be compiled w/o pausing execution of the engine,
// or a way to speed up compilation is found, set a hard limit on script size.
size_t limit = 20000;
if (!is_ci() && size >= limit)
{
al_trace("[jit] script type %s index %d too large to compile quickly, skipping\n", ScriptTypeToString(script->id.type), script->id.index);
return nullptr;
}

std::optional<ScriptDebugHandle> debug_handle_ = std::nullopt;
if (DEBUG_JIT_PRINT_ASM)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-1.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-10.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-11.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-12.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-13.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-14.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-15.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-16.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-17.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-18.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-19.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-2.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-20.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-21.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-22.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-23.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-24.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-25.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-26.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-27.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-28.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-29.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-3.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-30.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-31.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-32.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-33.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-34.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-35.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-36.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-37.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-38.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-39.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-4.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-40.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-41.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-42.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-43.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-44.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-45.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-46.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-47.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-48.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-5.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-6.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-7.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-8.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-ffc-9.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-global-0.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-global-1.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-global-2.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-1.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-2.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-3.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-4.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-5.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-6.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-7.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/100_rooms_of_wisdom/zasm-item-8.txt
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/snapshots/jit/combo_rotator/zasm-ffc-1.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/combo_rotator/zasm-ffc-2.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/combo_rotator/zasm-global-0.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/combo_rotator/zasm-item-1.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/combo_rotator/zasm-item-2.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/snapshots/jit/combo_rotator/zasm-item-3.txt
Git LFS file not shown

0 comments on commit 50b8b0b

Please sign in to comment.