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 5B90F6F81F; Wed, 18 Nov 2026 11:48:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5B90F6F81F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1794991720; bh=Lqrxu+srOBOdZSBBCELsSYcT4BjkFvu6zFX2TRav78k=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=YUdvjHIy6GtN9Fc9ezQ0PQy+XstSYU3kB+om8UlZAK3FpKldb9sW8HdhKwe3tUJx3 R/8IhUWBHU3Zv7ybWnN7fqtTZDrMXK2vF2d2QkkdcSw4aAjuisAXmW0EUPzhf+sT9I ajIOq7bAbXKmjQk+77852wLsq6hHNvdCmfe2OF+4= Received: from send151.i.mail.ru (send151.i.mail.ru [89.221.237.246]) (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 B7C006F81F for ; Wed, 18 Nov 2026 11:48:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B7C006F81F Received: by exim-smtp-canary-30-775d6dcd67-k4lnx with esmtpa (envelope-from ) id 1xTbLJ-00000000FIo-0uJA; Wed, 18 Nov 2026 11:48:37 +0300 To: Sergey Bronnikov , Evgeniy Temirgaleev Date: Wed, 18 Nov 2026 11:48:08 +0300 Message-ID: <20261118084808.3591198-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.56.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9749DDC91F62168F92C5CC3D78C1F4129358BF2273B6E1791182A05F5380850404CFAA856959B35B13DE06ABAFEAF6705137FC241753B4EF6E3D8C17568BDDD21276EAACF4BA1B9AB X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE795530B80AF2ADB7BEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB553375660E512E38EA290F4F45D1396B693067D06B1BD51285E0CDCE9FD218869C49F6BB389733CBF5DBD5E9D5E8D9A59859A8B6AE6B62100CB10EE2CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8090A508E0FED6299176DF2183F8FC7C0BA90D7044347270FCD04E86FAF290E2DB606B96278B59C421DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6C94F115956DE4A7A089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5500F9AFD4A165C995002B1117B3ED696E30514FC2E8C6E08B48B7A7F94616420823CB91A9FED034534781492E4B8EEAD0BC323893F80E328C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34C77DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D6591ECAA634E25E81BBA54A193B1D5345FFA27E479A1962F65149CA52233CD05A9AF7474AA0C6FF8A7BB8341EE9D5BE9A0A0FB399A61252FDF78D98E67716A5402F84AF424806356F77C7CEAA0681F5848F4C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWEBOirmuJPoGnNR0l9D9uPM= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B63B2FB1C9C8441ED3DE06ABAFEAF6705137FC241753B4EF6B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 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 +-- 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) -- 2.56.0