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 D0F6F6E1EC; Mon, 23 Nov 2026 18:33:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D0F6F6E1EC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1795448029; bh=kJNsRSJGd0WQwVOeO6kotOdIrPsWXQ6SX5SJjSvbwa4=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=GgJygXgrzMjWAxuskeLReRug5waLxoh7o67rO3yh1/b7zI2H5POE5eZ87JFJXKsx1 KrDxZxRRAHgPi4OZkseuK/oo22pSJOzor01eDZwetznMtLCyVH2CoIgTy/SF1c5hCH pSo1TKXa5ROVhg6kuibZz8S/DD3mpklZ2tuJmP6Y= Received: from send279.i.mail.ru (send279.i.mail.ru [95.163.59.118]) (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 98DA36E1EC for ; Mon, 23 Nov 2026 18:33:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 98DA36E1EC Received: by exim-smtp-canary-30-7cf57c478c-f72z2 with esmtpa (envelope-from ) id 1xVW38-000000007gc-2Bml; Mon, 23 Nov 2026 18:33:47 +0300 Message-ID: <79a76da9-a60f-41f6-b3bc-dcae4faa1ed1@tarantool.org> Date: Mon, 23 Nov 2026 18:33:43 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sergey Kaplun , Evgeniy Temirgaleev Cc: tarantool-patches@dev.tarantool.org References: <20261118084808.3591198-1-skaplun@tarantool.org> Content-Language: en-US In-Reply-To: <20261118084808.3591198-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: 4F1203BC0FB41BD9DFC18640EC9820975275C27875E8B4E424D398147D189020182A05F538085040A7D461F2BE9C86DA3DE06ABAFEAF6705959E2F384B66994E7C1969D54C34111A508F272F1A7473F0 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7624C4D757C4F5837EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB5533756603DA5D6BE41EFC0DD169A2F06815F10D68D7BBEADDC906696212EF3F2C680641389733CBF5DBD5E9D5E8D9A59859A8B6D6FBC3EC642A93BBCC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB86D1867E19FE14079C09775C1D3CA48CF3D321E7403792E342EB15956EA79C166A417C69337E82CC275ECD9A6C639B01B78DA827A17800CE70BB89B22BF4660DC731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A5E7389C37B5120F575002B1117B3ED6969D69EA5B96183D35D57BAD45EC4C5DE1823CB91A9FED034534781492E4B8EEAD17AEC49845D0B908 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE1918E10F71CB4DF9F96AB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D6594264CD5502EED90CA98DCB437AA00250585A13353B3BE22F2D8032CA3880A3B524CCC7F3AF2D037FB8341EE9D5BE9A0A82C9A22958E8E921064CF1B055BC710A6B67903C4DE4A1EF6536EB022892E5344C41F94D744909CE2512F26BEC029E55448553D2254B8D95CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWNz7XJ74wpKYnNR0l9D9uPM= X-Mailru-Sender: 689FA8AB762F73937C9FA53A4753B313AB34A05DFF645873D32CB73C9BE34F09F88875361AF264AAEF86D5F70DA33880E41E8EF7A07863ECB274557F927329BE2DDF8182D28ACDB545BD1C3CC395C826B4A721A3011E896F X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] x86/x64: Use scratch register for IR_TOBIT and IR_CONV u64.num. 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: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey, thanks for the patch! Please see my comments below. Sergey On 11/18/26 11:48, Sergey Kaplun wrote: > From: Mike Pall > > Reported by aleksisch. Thanks to Sergey Kaplun. > > (cherry picked from commit 5585364753b52ac7de4695026fc28be2480c82bd) > > If `asm_tobit()` allocates a register for the `op1` in the TOBIT IR, its > value is modified by adding the TOBIT bias (the second operand). Hence, > the restoring value of the source IR is incorrect due to this > modification. > > This patch uses a scratch register instead. The `IR_CONV` case is > similar but is unobservable since the adding value `LJ_K64_M2P64` > (-2^64) is overlapped in the uint64_t representation. > > Sergey Kaplun: > * added the description for the problem and a regression test, right? > > Part of tarantool/tarantool#13442 > --- > > Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1536-tobit-regalloc > Related issues: > * https://github.com/LuaJIT/LuaJIT/issues/1536 > * https://github.com/tarantool/tarantool/issues/13442 > > Side note: macOS (x86_64) CI is red due to runner problems. The metrics > CI is red due to problems with configuration. > > src/lj_asm_x86.h | 7 ++-- > .../lj-1536-tobit-regalloc.test.lua | 36 +++++++++++++++++++ > 2 files changed, 38 insertions(+), 5 deletions(-) > create mode 100644 test/tarantool-tests/lj-1536-tobit-regalloc.test.lua > > diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h > index 0e3a473c..8e2156eb 100644 > --- a/src/lj_asm_x86.h > +++ b/src/lj_asm_x86.h > @@ -802,9 +802,7 @@ static void asm_tointg(ASMState *as, IRIns *ir, Reg left) > static void asm_tobit(ASMState *as, IRIns *ir) > { > Reg dest = ra_dest(as, ir, RSET_GPR); > - Reg tmp = ra_noreg(IR(ir->op1)->r) ? > - ra_alloc1(as, ir->op1, RSET_FPR) : > - ra_scratch(as, RSET_FPR); > + Reg tmp = ra_scratch(as, RSET_FPR); > Reg right; > emit_rr(as, XO_MOVDto, tmp, dest); > right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp)); > @@ -867,8 +865,7 @@ static void asm_conv(ASMState *as, IRIns *ir) > if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) { > /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */ > /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */ > - Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) : > - ra_scratch(as, RSET_FPR); > + Reg tmp = ra_scratch(as, RSET_FPR); > MCLabel l_end = emit_label(as); > if (LJ_32) > emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000); > diff --git a/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua b/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua > new file mode 100644 > index 00000000..120fa1b0 > --- /dev/null > +++ b/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua > @@ -0,0 +1,36 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate incorrect register allocation for > +-- `IR_TOBIT` on x86/x64. > +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1536. > +local test = tap.test('lj-1536-tobit-regalloc'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(1) > + > +local tobit = bit.tobit > + > +jit.opt.start('hotloop=1') > + > +local res > +local VALUE = 0x100 Why exactly this value? > +-- Use a table for the `noreg` of the tobit operand. > +local t = {k = VALUE} > +-- 5 iterations to hit invariant loop part. > +for _ = 1, 5 do > + -- Generates the following IRs in the LOOP part: > + -- 0011 xmm0 > num SLOAD #4 T > + -- ... > + -- 0015 ------------ LOOP ------------ > + -- 0016 rbp + int TOBIT 0011 bias > + -- The resulting machine code adding the bias to the allocated > + -- for the SLOAD (before the patch) register corrupts it and > + -- hence restores an incorrect value to the interpreter. > + res = tobit(t.k) > + t.k = VALUE -- `noreg`. > +end > + > +test:is(res, VALUE, 'correct tobit result') > + > +test:done(true)