<HTML><BODY><div>Hi, Sergey!</div><div>Thanks for the patch!</div><div>Please consider my comments below.</div><div> </div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div> <blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div id=""><div class="js-helper js-readmsg-msg"><div><div id="style_16740732231615555589_BODY">From: Mike Pall <mike><br><br>(cherry picked from commit 522d2073da4be2af79db4728cbb375db0fbdfc48)<br><br>`asm_intarith()` function may try to drop `test r, r` instruction before</div></div></div></div></blockquote><div>Please note that "r" is an allocated register for the instruction.</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>the Jcc instruction. However, in case when Jcc instruction is "Jump</div></div></div></div></blockquote><div>Typo: s/in case when/in cases where/</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>short if ..." instruction (i.e. has no 0F opcode prefix like "Jump near<br>if ..."), the `test` instruction is dropped when shouldn't be, due to</div></div></div></div></blockquote><div>Typo: s/when/when it/</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>memory miss. As the result, the loop can't be realigned later in</div></div></div></div></blockquote><div>Typo: s/memory/a memory/</div><div>Also, that part about the memory miss is unclear, it would be better if you</div><div>could clarify it a bit.</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>`asm_loop_fixup` due to target to jump isn't aligned and the assertion</div></div></div></div></blockquote><div>Typo: s/isn’t aligned/being misaligned/</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>fails.<br><br>This patch adds the additional check for 0F opcode in `asm_intarith()`.</div></div></div></div></blockquote><div>Typo: s/for 0F/for the 0F/</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div class="js-helper js-readmsg-msg"><br>Sergey Kaplun:<br>* added the description and the test for the problem<br><br>Part of tarantool/tarantool#8069<br>---<br> src/lj_asm_x86.h | 5 +++--<br> .../lj-556-fix-loop-realignment.test.lua | 18 ++++++++++++++++++<br> 2 files changed, 21 insertions(+), 2 deletions(-)<br> create mode 100644 test/tarantool-tests/lj-556-fix-loop-realignment.test.lua<br><br>diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h<br>index 8efda8e5..e6c42c6d 100644<br>--- a/src/lj_asm_x86.h<br>+++ b/src/lj_asm_x86.h<br>@@ -2068,8 +2068,9 @@ static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa)<br>   int32_t k = 0;<br>   if (as->flagmcp == as->mcp) { /* Drop test r,r instruction. */<br>     MCode *p = as->mcp + ((LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2);<br>- if ((p[1] & 15) < 14) {<br>- if ((p[1] & 15) >= 12) p[1] -= 4; /* L <->S, NL <-> NS */<br>+ MCode *q = p[0] == 0x0f ? p+1 : p;<br>+ if ((*q & 15) < 14) {<br>+ if ((*q & 15) >= 12) *q -= 4; /* L <->S, NL <-> NS */<br>       as->flagmcp = NULL;<br>       as->mcp = p;<br>     } /* else: cannot transform LE/NLE to cc without use of OF. */<br>diff --git a/test/tarantool-tests/lj-556-fix-loop-realignment.test.lua b/test/tarantool-tests/lj-556-fix-loop-realignment.test.lua<br>new file mode 100644<br>index 00000000..9a8e6098<br>--- /dev/null<br>+++ b/test/tarantool-tests/lj-556-fix-loop-realignment.test.lua<br>@@ -0,0 +1,18 @@<br>+local tap = require('tap')<br>+<br>+local test = tap.test('lj-505-fold-icorrect-behavior')<br>+test:plan(1)<br>+<br>+-- Test file to demonstrate JIT misbehaviour for loop realignment<br>+-- in LUAJIT_NUMMODE=2. See also<br>+-- <a href="https://github.com/LuaJIT/LuaJIT/issues/556" target="_blank">https://github.com/LuaJIT/LuaJIT/issues/556</a>.<br>+<br>+jit.opt.start('hotloop=1')<br>+<br>+local s = 4<br>+while s > 0 do<br>+ s = s - 1<br>+end<br>+<br>+test:ok(true, 'loop is compiled and ran successfully')<br>+os.exit(test:check() and 0 or 1)<br>--</div></blockquote><div>The test works just fine with HEAD on </div><div><span style="font-family: var(--vkui--octavius_font_family_global,var(--vkui--font_family_base,Helvetica,Arial,sans-serif)); letter-spacing: var(--vkui--font_text--letter_spacing--regular,normal);">f7d61d96</span><span style="font-family: var(--vkui--octavius_font_family_global,var(--vkui--font_family_base,Helvetica,Arial,sans-serif)); letter-spacing: var(--vkui--font_text--letter_spacing--regular,normal);"> ci: introduce workflow for exotic builds.</span></div><div> </div><div>Tested configurations: </div><div>LJ_64: True, LJ_GC64: True, LJ_DUALNUM: True</div><div><div><div>LJ_64: True, LJ_GC64: False, LJ_DUALNUM: True</div></div></div><div><div>--<br>Best regards,</div><div>Maxim Kokryashkin</div></div><div> </div></div></blockquote></BODY></HTML>