From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 914856EC44; Tue, 17 Nov 2026 16:25:03 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 914856EC44 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1794921903; bh=9A9gA7OCJFaoBh7HFRkiZi+706ym0gncMZZcB14x50I=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=b0wyHetnAEdUkkyiS2dghq8kbTTx1tARHGJgJ8rwB7hexXeSr4I3REoiL6UVWS4W5 YkdZSkqsF7moS6PmgA5TYFwwW1O9oxkk80i5ifY+1ZdwzztiNf6JHM2gIujwnfDyT8 swXqOpTItcjSlSKA5TSYYerhfh/yoS3hWDlBzm/k= Received: from send217.i.mail.ru (send217.i.mail.ru [95.163.59.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D07766EC44 for ; Tue, 17 Nov 2026 16:25:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D07766EC44 Received: by exim-smtp-5fd8d99545-zj5kw with esmtpa (envelope-from ) id 1xTJBE-0000000078i-2lyj; Tue, 17 Nov 2026 16:25:01 +0300 Message-ID: Date: Tue, 17 Nov 2026 16:25:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sergey Kaplun References: <20261116120533.2886125-1-skaplun@tarantool.org> Content-Language: en-US In-Reply-To: <20261116120533.2886125-1-skaplun@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9749DDC91F62168F9676E14FC0BE86957E7CAE029AA05B0C7182A05F5380850408B6E847A75CE038D3DE06ABAFEAF6705939B67E1A028581E457BFA98AAB828805C3268B322D90CF0 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BF6702EC5472AA0FEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB553375660E512E38EA290F4F261E1BC582D062ADF51075493584F4222EDBC7ADA149CABB389733CBF5DBD5E9D5E8D9A59859A8B6AEEA5BB16A939343CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8090A508E0FED6299176DF2183F8FC7C0A94733CAF7D34647CD04E86FAF290E2DB606B96278B59C421DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6F6BBD9A0B31367FB089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5F09A8ACA877991205002B1117B3ED69636BDFD536360247D108A05421C070DB8823CB91A9FED034534781492E4B8EEADA3FB0D9844EF8EC5BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34CAB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D6590747139BAF1043203D9F09E36E110EC45BF0DF0FDA7FE0260B3B06C188E139CBBF0D42BBC57DC06EB8341EE9D5BE9A0AFBD44482222040CE4D76A86FA7C34EE8BD99126E5700518AC7CEAA0681F5848F4C41F94D744909CEEF9E35A692215F79327F81E6EEE0AEB67A0F9F91F029B6AF33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWMVh57aHtW2ge+eya20PhEY= X-Mailru-Sender: 015BA6FC0EFA223AFF5DE35D6D355F43D0FD16D4C044F805741D06F8E7BD5C49E0202BFA934E5ADCAF0C20147DBCF3448C34D365DA2E1307B60569B44082A3A3197AF8DA6C69D34BA7BD66BEAC8CCB0C4198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Evgeniy Temirgaleev via Tarantool-patches Reply-To: Evgeniy Temirgaleev Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "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 > > 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< 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< 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