Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
@ 2026-11-16 12:05 Sergey Kaplun via Tarantool-patches
  2026-11-17 13:25 ` Evgeniy Temirgaleev via Tarantool-patches
  0 siblings, 1 reply; 5+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-16 12:05 UTC (permalink / raw)
  To: Sergey Bronnikov, Evgeniy Temirgaleev; +Cc: tarantool-patches

From: Mike Pall <mike>

Thanks to Peter Cawley.

(cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)

Logical instructions [1] (`orr`, etc.) have no sign extension form
(unlike `ADD Xd, Xn, Wm, SXTW #k`). This makes the fusing sign extension
into them meaningless and leads to the incorrect result in the
JIT-compiled code.

This patch prevents the fusing of sign extension (`sxtw`) into logical
instructions.

[1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#13442
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1076-not-fuse-sign-ext-logical
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1076
* https://github.com/tarantool/tarantool/issues/13442

 src/lj_asm_arm64.h                            | 11 ++-
 ...lj-1076-not-fuse-sign-ext-logical.test.lua | 70 +++++++++++++++++++
 2 files changed, 74 insertions(+), 7 deletions(-)
 create mode 100644 test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua

diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h
index f731ab05..f080dd6e 100644
--- a/src/lj_asm_arm64.h
+++ b/src/lj_asm_arm64.h
@@ -208,16 +208,13 @@ static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,
 static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
 {
   IRIns *ir = IR(ref);
+  int logical = (ai & 0x1f000000) == 0x0a000000;
   if (ra_hasreg(ir->r)) {
     ra_noweak(as, ir->r);
     return A64F_M(ir->r);
   } else if (irref_isk(ref)) {
-    uint32_t m;
     int64_t k = get_k64val(as, ref);
-    if ((ai & 0x1f000000) == 0x0a000000)
-      m = emit_isk13(k, irt_is64(ir->t));
-    else
-      m = emit_isk12(k);
+    uint32_t m = logical ? emit_isk13(k, irt_is64(ir->t)) : emit_isk12(k);
     if (m)
       return m;
   } else if (mayfuse(as, ref)) {
@@ -229,7 +226,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
 		    (IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31));
       IRIns *irl = IR(ir->op1);
       if (sh == A64SH_LSL &&
-	  irl->o == IR_CONV &&
+	  irl->o == IR_CONV && !logical &&
 	  irl->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT) &&
 	  shift <= 4 &&
 	  canfuse(as, irl)) {
@@ -239,7 +236,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
 	Reg m = ra_alloc1(as, ir->op1, allow);
 	return A64F_M(m) | A64F_SH(sh, shift);
       }
-    } else if (ir->o == IR_CONV &&
+    } else if (ir->o == IR_CONV && !logical &&
 	       ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)) {
       Reg m = ra_alloc1(as, ir->op1, allow);
       return A64F_M(m) | A64F_EX(A64EX_SXTW);
diff --git a/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
new file mode 100644
index 00000000..b199648d
--- /dev/null
+++ b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
@@ -0,0 +1,70 @@
+local tap = require('tap')
+
+-- The test file to demonstrate incorrect LuaJIT's fusing of the
+-- sign extension instruction into logical instructions.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1076.
+local test = tap.test('lj-1076-not-fuse-sign-ext-logical'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(5)
+
+jit.opt.start('hotloop=1')
+
+local ffi = require('ffi')
+local cast = ffi.cast
+
+local bnot = bit.bnot
+
+local N = -5
+
+-- Test avoiding erroneous fusing of:
+-- |  sxtw  x1, w4
+-- |  mvn   x0, x1
+--
+-- to the:
+-- | mov   x0, x4, lsl #48
+
+local r = {}
+for i = 1, 4 do
+  r[i] = bnot(cast('int64_t', N))
+end
+
+test:is(r[1], 4LL, 'correct result for bnot')
+test:samevalues(r, 'avoid fusion for explicit CONV with sign extension')
+
+-- Test avoiding erroneous fusing of:
+-- | sxtw  x21, w23
+-- | orr   x20, x20, x21
+--
+-- to the:
+-- | orn   x20, x20, x23, lsl #48
+
+local function logical_shift_unfused(subtest, expected)
+  subtest:plan(2)
+  jit.flush()
+  jit.opt.start('hotloop=1')
+  -- Use code generation to avoid using non-constant variables
+  -- defining functions preventing folding and fusion.
+  local script = string.format([[
+    local bor = bit.bor
+    local arshift, lshift, rshift = bit.arshift, bit.lshift, bit.rshift
+    local N = -5
+    local zero = 0LL
+    local result = {}
+    for i = 1, 4 do
+      result[i] = bor(%s(N, 1), zero)
+    end
+    return result
+  ]], subtest.name)
+  local f = assert(loadstring(script, 'unfused ' .. subtest.name))
+  local result = f()
+  subtest:is(result[1], expected, 'correct result for ' .. subtest.name)
+  subtest:samevalues(result, 'avoid fusion sxtw and orr for ' .. subtest.name)
+end
+
+test:test('arshift', logical_shift_unfused, -3LL)
+test:test('lshift',  logical_shift_unfused, -10LL)
+test:test('rshift',  logical_shift_unfused, 0x7FFFFFFDLL)
+
+test:done(true)
-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
  2026-11-16 12:05 [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands Sergey Kaplun via Tarantool-patches
@ 2026-11-17 13:25 ` Evgeniy Temirgaleev via Tarantool-patches
  2026-11-18  8:31   ` Sergey Kaplun via Tarantool-patches
  0 siblings, 1 reply; 5+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-17 13:25 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

Hi, Sergey! Thanks for the patch! LGTM with a note.

On 11/16/26 3:05 PM, Sergey Kaplun wrote:
> From: Mike Pall <mike>
> 
> Thanks to Peter Cawley.
> 
> (cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)
> 
> Logical instructions [1] (`orr`, etc.) have no sign extension form
> (unlike `ADD Xd, Xn, Wm, SXTW #k`). This makes the fusing sign extension
> into them meaningless and leads to the incorrect result in the
> JIT-compiled code.
> 
> This patch prevents the fusing of sign extension (`sxtw`) into logical
> instructions.
> 
> [1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions
The article contains only basic form of arithmetic and logical (and 
other) instructions. It omits their complex forms with optional second 
operand shift and/or extension and is not illustrative for this patch.

> 
> Sergey Kaplun:
> * added the description and the test for the problem
> 
> Part of tarantool/tarantool#13442
> ---
> 
> Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1076-not-fuse-sign-ext-logical
> Related issues:
> * https://github.com/LuaJIT/LuaJIT/issues/1076
> * https://github.com/tarantool/tarantool/issues/13442
> 
>   src/lj_asm_arm64.h                            | 11 ++-
>   ...lj-1076-not-fuse-sign-ext-logical.test.lua | 70 +++++++++++++++++++
>   2 files changed, 74 insertions(+), 7 deletions(-)
>   create mode 100644 test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
> 
> diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h
> index f731ab05..f080dd6e 100644
> --- a/src/lj_asm_arm64.h
> +++ b/src/lj_asm_arm64.h
> @@ -208,16 +208,13 @@ static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,
>   static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
>   {
>     IRIns *ir = IR(ref);
> +  int logical = (ai & 0x1f000000) == 0x0a000000;
>     if (ra_hasreg(ir->r)) {
>       ra_noweak(as, ir->r);
>       return A64F_M(ir->r);
>     } else if (irref_isk(ref)) {
> -    uint32_t m;
>       int64_t k = get_k64val(as, ref);
> -    if ((ai & 0x1f000000) == 0x0a000000)
> -      m = emit_isk13(k, irt_is64(ir->t));
> -    else
> -      m = emit_isk12(k);
> +    uint32_t m = logical ? emit_isk13(k, irt_is64(ir->t)) : emit_isk12(k);
>       if (m)
>         return m;
>     } else if (mayfuse(as, ref)) {
> @@ -229,7 +226,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
>   		    (IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31));
>         IRIns *irl = IR(ir->op1);
>         if (sh == A64SH_LSL &&
> -	  irl->o == IR_CONV &&
> +	  irl->o == IR_CONV && !logical &&
>   	  irl->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT) &&
>   	  shift <= 4 &&
>   	  canfuse(as, irl)) {
> @@ -239,7 +236,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
>   	Reg m = ra_alloc1(as, ir->op1, allow);
>   	return A64F_M(m) | A64F_SH(sh, shift);
>         }
> -    } else if (ir->o == IR_CONV &&
> +    } else if (ir->o == IR_CONV && !logical &&
>   	       ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)) {
>         Reg m = ra_alloc1(as, ir->op1, allow);
>         return A64F_M(m) | A64F_EX(A64EX_SXTW);
> diff --git a/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
> new file mode 100644
> index 00000000..b199648d
> --- /dev/null
> +++ b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
> @@ -0,0 +1,70 @@
> +local tap = require('tap')
> +
> +-- The test file to demonstrate incorrect LuaJIT's fusing of the
> +-- sign extension instruction into logical instructions.
> +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1076.
> +local test = tap.test('lj-1076-not-fuse-sign-ext-logical'):skipcond({
> +  ['Test requires JIT enabled'] = not jit.status(),
> +})
> +
> +test:plan(5)
> +
> +jit.opt.start('hotloop=1')
> +
> +local ffi = require('ffi')
> +local cast = ffi.cast
> +
> +local bnot = bit.bnot
> +
> +local N = -5
> +
> +-- Test avoiding erroneous fusing of:
> +-- |  sxtw  x1, w4
> +-- |  mvn   x0, x1
> +--
> +-- to the:
> +-- | mov   x0, x4, lsl #48
> +
> +local r = {}
> +for i = 1, 4 do
> +  r[i] = bnot(cast('int64_t', N))
> +end
> +
> +test:is(r[1], 4LL, 'correct result for bnot')
> +test:samevalues(r, 'avoid fusion for explicit CONV with sign extension')
> +
> +-- Test avoiding erroneous fusing of:
> +-- | sxtw  x21, w23
> +-- | orr   x20, x20, x21
> +--
> +-- to the:
> +-- | orn   x20, x20, x23, lsl #48
> +
> +local function logical_shift_unfused(subtest, expected)
> +  subtest:plan(2)
> +  jit.flush()
> +  jit.opt.start('hotloop=1')
> +  -- Use code generation to avoid using non-constant variables
> +  -- defining functions preventing folding and fusion.
> +  local script = string.format([[
> +    local bor = bit.bor
> +    local arshift, lshift, rshift = bit.arshift, bit.lshift, bit.rshift
> +    local N = -5
> +    local zero = 0LL
> +    local result = {}
> +    for i = 1, 4 do
> +      result[i] = bor(%s(N, 1), zero)
> +    end
> +    return result
> +  ]], subtest.name)
> +  local f = assert(loadstring(script, 'unfused ' .. subtest.name))
> +  local result = f()
> +  subtest:is(result[1], expected, 'correct result for ' .. subtest.name)
> +  subtest:samevalues(result, 'avoid fusion sxtw and orr for ' .. subtest.name)
> +end
> +
> +test:test('arshift', logical_shift_unfused, -3LL)
> +test:test('lshift',  logical_shift_unfused, -10LL)
> +test:test('rshift',  logical_shift_unfused, 0x7FFFFFFDLL)
> +
> +test:done(true)

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
  2026-11-17 13:25 ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-11-18  8:31   ` Sergey Kaplun via Tarantool-patches
  2026-11-18  9:42     ` Evgeniy Temirgaleev via Tarantool-patches
  0 siblings, 1 reply; 5+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-18  8:31 UTC (permalink / raw)
  To: Evgeniy Temirgaleev; +Cc: tarantool-patches

Hi, Evgeniy!
Thanks for the review!

On 17.11.26, Evgeniy Temirgaleev wrote:
> Hi, Sergey! Thanks for the patch! LGTM with a note.
> 
> On 11/16/26 3:05 PM, Sergey Kaplun wrote:
> > From: Mike Pall <mike>
> > 
> > Thanks to Peter Cawley.
> > 
> > (cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)
> > 
> > Logical instructions [1] (`orr`, etc.) have no sign extension form
> > (unlike `ADD Xd, Xn, Wm, SXTW #k`). This makes the fusing sign extension
> > into them meaningless and leads to the incorrect result in the
> > JIT-compiled code.
> > 
> > This patch prevents the fusing of sign extension (`sxtw`) into logical
> > instructions.
> > 
> > [1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions
> The article contains only basic form of arithmetic and logical (and 
> other) instructions. It omits their complex forms with optional second 
> operand shift and/or extension and is not illustrative for this patch.

Yes, I've failed to find the definition of the logical instructions at
the non-blocked resources :(. At least it has the definition of logical
instruction...
Do you think it is better to remove the link to avoid the confusion?

> 
> > 
> > Sergey Kaplun:
> > * added the description and the test for the problem
> > 
> > Part of tarantool/tarantool#13442
> > ---

<snipped>

> --
> Best regards,
> Evgeniy Temirgaleev
> 

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
  2026-11-18  8:31   ` Sergey Kaplun via Tarantool-patches
@ 2026-11-18  9:42     ` Evgeniy Temirgaleev via Tarantool-patches
  2026-11-18 11:39       ` Sergey Kaplun via Tarantool-patches
  0 siblings, 1 reply; 5+ messages in thread
From: Evgeniy Temirgaleev via Tarantool-patches @ 2026-11-18  9:42 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

Hi, Sergey!

On 11/18/26 11:31 AM, Sergey Kaplun wrote:
> Hi, Evgeniy!
> Thanks for the review!
> 
> On 17.11.26, Evgeniy Temirgaleev wrote:
>> Hi, Sergey! Thanks for the patch! LGTM with a note.
>>
>> On 11/16/26 3:05 PM, Sergey Kaplun wrote:
>>> From: Mike Pall <mike>
>>>
>>> Thanks to Peter Cawley.
>>>
>>> (cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)
>>>
>>> Logical instructions [1] (`orr`, etc.) have no sign extension form
>>> (unlike `ADD Xd, Xn, Wm, SXTW #k`). This makes the fusing sign extension
>>> into them meaningless and leads to the incorrect result in the
>>> JIT-compiled code.
>>>
>>> This patch prevents the fusing of sign extension (`sxtw`) into logical
>>> instructions.
>>>
>>> [1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions
>> The article contains only basic form of arithmetic and logical (and
>> other) instructions. It omits their complex forms with optional second
>> operand shift and/or extension and is not illustrative for this patch.
> 
> Yes, I've failed to find the definition of the logical instructions at
> the non-blocked resources :(. At least it has the definition of logical
> instruction...
> Do you think it is better to remove the link to avoid the confusion?
> 
I found a working link [1]. You can use it with the document reference 
data: "Arm® Instruction Set. Version 1.0. Reference Guide" (version 25 
October 2018), "Chapter D2. A64 General Instructions".

[1] 
https://www.heenes.de/ro/material/arm/arm_instruction_set_reference_guide.pdf

>>
>>>
>>> Sergey Kaplun:
>>> * added the description and the test for the problem
>>>
>>> Part of tarantool/tarantool#13442
>>> ---
> 
> <snipped>
> 
>> --
>> Best regards,
>> Evgeniy Temirgaleev
>>
> 

-- 
--
Best regards,
Evgeniy Temirgaleev


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
  2026-11-18  9:42     ` Evgeniy Temirgaleev via Tarantool-patches
@ 2026-11-18 11:39       ` Sergey Kaplun via Tarantool-patches
  0 siblings, 0 replies; 5+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-18 11:39 UTC (permalink / raw)
  To: Evgeniy Temirgaleev; +Cc: tarantool-patches

Evgeniy,

On 18.11.26, Evgeniy Temirgaleev wrote:
> Hi, Sergey!
> 

<snipped>

> >>> [1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions
> >> The article contains only basic form of arithmetic and logical (and
> >> other) instructions. It omits their complex forms with optional second
> >> operand shift and/or extension and is not illustrative for this patch.
> > 
> > Yes, I've failed to find the definition of the logical instructions at
> > the non-blocked resources :(. At least it has the definition of logical
> > instruction...
> > Do you think it is better to remove the link to avoid the confusion?
> > 
> I found a working link [1]. You can use it with the document reference 
> data: "Arm® Instruction Set. Version 1.0. Reference Guide" (version 25 
> October 2018), "Chapter D2. A64 General Instructions".
> 
> [1] 
> https://www.heenes.de/ro/material/arm/arm_instruction_set_reference_guide.pdf

Thanks!
Added to the commit message:

| ARM64: Don't fuse sign extensions into logical operands.
|
| Thanks to Peter Cawley.
|
| (cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)
|
| Logical instructions (`orr`, etc.) have no sign extension form (unlike
| `ADD Xd, Xn, Wm, SXTW #k`), see Arm Instruction Set [1], Chapter D2: A64
| General Instructions. This makes the fusing sign extension into them
| meaningless and leads to the incorrect result in the JIT-compiled code.
|
| This patch prevents the fusing of sign extension (`sxtw`) into logical
| instructions.
|
| [1]: https://www.heenes.de/ro/material/arm/arm_instruction_set_reference_guide.pdf
|
| Sergey Kaplun:
| * added the description and the test for the problem
|
| Part of tarantool/tarantool#13442

Branch is force-pushed.

> 
> >>
> >>>
> >>> Sergey Kaplun:
> >>> * added the description and the test for the problem
> >>>
> >>> Part of tarantool/tarantool#13442
> >>> ---
> > 
> > <snipped>
> > 
> >> --
> >> Best regards,
> >> Evgeniy Temirgaleev
> >>
> > 
> 
> -- 
> --
> Best regards,
> Evgeniy Temirgaleev
> 

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-18 11:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 12:05 [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands Sergey Kaplun via Tarantool-patches
2026-11-17 13:25 ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-18  8:31   ` Sergey Kaplun via Tarantool-patches
2026-11-18  9:42     ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-18 11:39       ` Sergey Kaplun via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox