Skip to content

Commit 7a213e1

Browse files
committed
armv6m: Reduce heuristic threshold for maybe_flush_literal_pool
Literal pools need to be within 1024 bytes because of limited range of ldr reg, [pc, #imm] instruction. A pool is emitted when mov_immediate is called and the pool reference is farther than a given threshold. Emitting the pool in such a condition requires a branch over the pool, and is therefore less efficient than the usual pool flush that happens when a branch is emitted (tail calls). This threshold was set to 900 but new test module bigint.beam wouldn't compile. This commit changes the threshold to 512 which works (the test actually requires 663). The optimal threshold value depends on the pattern usage of mov_immediate. If regular code hits the bug, we may need to change the heuristic and emit the pool based on other conditions. Signed-off-by: Paul Guyot <[email protected]>
1 parent 58768ab commit 7a213e1

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

libs/jit/src/jit_armv6m.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,9 +2771,10 @@ maybe_flush_literal_pool(
27712771
% Determine the offset of the last item.
27722772
Offset = StreamModule:offset(Stream0),
27732773
{Addr, _, _} = lists:last(LP),
2774-
% Heuristically set the threshold at 900
2774+
% Heuristically set the threshold at 512 (half the range of ldr inst.).
2775+
% bigint.beam currently requires 663 or lower to compile.
27752776
if
2776-
Offset - Addr > 900 ->
2777+
Offset - Addr > 512 ->
27772778
NbLiterals = length(LP),
27782779
Continuation = NbLiterals * 4 + 4 - (Offset rem 4),
27792780
Stream1 = StreamModule:append(Stream0, jit_armv6m_asm:b(Continuation)),

tests/libs/jit/jit_armv6m_tests.erl

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,27 +3055,27 @@ move_to_native_register_test_() ->
30553055
?BACKEND:free_native_registers(AccSt4, [RegA])
30563056
end,
30573057
State0,
3058-
lists:seq(1025, 1140)
3058+
lists:seq(1025, 1090)
30593059
),
30603060
State2 = ?BACKEND:flush(State1),
30613061
Stream = ?BACKEND:stream(State2),
3062-
{_, LoadAndBranch0} = split_binary(Stream, 16#38a),
3062+
{_, LoadAndBranch0} = split_binary(Stream, 16#210),
30633063
{LoadAndBranch, _} = split_binary(LoadAndBranch0, 10),
30643064
LoadAndBranchDump = <<
3065-
" 38a: 4f62 ldr r7, [pc, #392] ; (0x514)\n"
3066-
" 38c: e0c4 b.n 0x518\n"
3067-
" 38e: ffff .dword\n\n"
3068-
" 392: 0401 .dword\n\n"
3069-
" 394: 0000 .dword"
3065+
" 210: 4f38 ldr r7, [pc, #224] ; (0x2f4)\n"
3066+
" 212: e071 b.n 0x2f8\n"
3067+
" 214: 0401 .dword 0x0401\n\n"
3068+
" 216: 0000 .dword 0x0000\n\n"
3069+
" 218: 0402 .dword 0x0402\n\n"
30703070
>>,
30713071
?assertEqual(dump_to_bin(LoadAndBranchDump), LoadAndBranch),
3072-
{_, Continuation0} = split_binary(Stream, 16#518),
3072+
{_, Continuation0} = split_binary(Stream, 16#2f8),
30733073
{Continuation, _} = split_binary(Continuation0, 8),
30743074
ContinuationDump = <<
3075-
" 518: 19ff adds r7, r7, r7\n"
3076-
" 51a: 19ff adds r7, r7, r7\n"
3077-
" 51c: 19ff adds r7, r7, r7\n"
3078-
" 51e: 278e movs r7, #142 ; 0x8e"
3075+
" 2f8: 19ff adds r7, r7, r7\n"
3076+
" 2fa: 19ff adds r7, r7, r7\n"
3077+
" 2fc: 19ff adds r7, r7, r7\n"
3078+
" 2fe: 4f02 ldr r7, [pc, #8] ; (0x308)"
30793079
>>,
30803080
?assertEqual(dump_to_bin(ContinuationDump), Continuation)
30813081
end),
@@ -3091,26 +3091,27 @@ move_to_native_register_test_() ->
30913091
?BACKEND:free_native_registers(AccSt4, [RegA])
30923092
end,
30933093
State1,
3094-
lists:seq(1025, 1140)
3094+
lists:seq(1025, 1090)
30953095
),
30963096
State3 = ?BACKEND:flush(State2),
30973097
Stream = ?BACKEND:stream(State3),
3098-
{_, LoadAndBranch0} = split_binary(Stream, 16#38c),
3099-
{LoadAndBranch, _} = split_binary(LoadAndBranch0, 8),
3098+
{_, LoadAndBranch0} = split_binary(Stream, 16#212),
3099+
{LoadAndBranch, _} = split_binary(LoadAndBranch0, 10),
31003100
LoadAndBranchDump = <<
3101-
" 38c: 4e61 ldr r6, [pc, #388] ; (0x514)\n"
3102-
" 38e: e0c3 b.n 0x518\n"
3103-
" 390: 0401 lsls r1, r0, #16\n"
3104-
" 392: 0000 movs r0, r0"
3101+
" 212: 4e39 ldr r6, [pc, #228] ; (0x2f8)\n"
3102+
" 214: e072 b.n 0x2fc\n"
3103+
" 216: ffff .dword 0xffff\n\n" % padding
3104+
" 218: 0401 .dword 0x401\n\n"
3105+
" 21a: 0000 .dword 0x000"
31053106
>>,
31063107
?assertEqual(dump_to_bin(LoadAndBranchDump), LoadAndBranch),
3107-
{_, Continuation0} = split_binary(Stream, 16#518),
3108+
{_, Continuation0} = split_binary(Stream, 16#2fc),
31083109
{Continuation, _} = split_binary(Continuation0, 8),
31093110
ContinuationDump = <<
3110-
" 518: 19b6 adds r6, r6, r6\n"
3111-
" 51a: 19b6 adds r6, r6, r6\n"
3112-
" 51c: 19b6 adds r6, r6, r6\n"
3113-
" 51e: 268e movs r6, #142 ; 0x8e"
3111+
" 2fc: 19b6 adds r6, r6, r6\n"
3112+
" 2fe: 19b6 adds r6, r6, r6\n"
3113+
" 300: 19b6 adds r6, r6, r6\n"
3114+
" 302: 4e02 ldr r6, [pc, #8] ; (0x30c)"
31143115
>>,
31153116
?assertEqual(dump_to_bin(ContinuationDump), Continuation)
31163117
end)

0 commit comments

Comments
 (0)