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 4EC636F3C8; Tue, 20 Sep 2022 14:17:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4EC636F3C8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1663672622; bh=9Du6FLYcHUYTnj34BnJlMpePwLn7raB5kefIFH2nlXs=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=QsmpTZdncbYzM63oi9JctqPb4xquFwqUzaETsawVgkppiZLYxzN17usQOf3aMKgJ2 +pLwGJcSPB6j9YPn7MHIWUyrDlL3bT34VNXf5kymxCDyDgGkZ3+yhLplaBaAipbznh ZQMAYu554x05m62L4nJ8wXOsQw9OZLUL7hIjlOIs= Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 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 BC02B6F3C8 for ; Tue, 20 Sep 2022 14:17:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BC02B6F3C8 Received: by mail-lf1-f42.google.com with SMTP id f9so3212701lfr.3 for ; Tue, 20 Sep 2022 04:17:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=k5PRgIlOEqL9z5lxrd536EwbIvExyRFccpLivROxV+Q=; b=YmhHBBaR4z3CFGBmG11oZ7xrmdqD5B/HaRMGjfj3HhfgIH3omG9ee7o/PjyXijliLe F3C7eiHo8p7u0us4+nNNkshG7rxDMIQsscs0jUy087UacyIWzO7r8qy5X+w1b8O5b+9T DLwQWDnYpmKMvJ3874a2kf7mK7hiJCI+QF5UupsaWaMaj7H+YRAJHs5VIhW9P9pAqQFt 6GQpuHW2CcWdtDELJc2Xig0bOBXrlYVWo5AacPNw8gGMSiV5nGtpyEHfxaqm7BK5eNcw pvmuJGE9/XFD2Lf79MRFoUCUrOjUSYi7pE8HAqsDTMJuwFDGpJqoOmL4W+eqYGmf2/zN a11A== X-Gm-Message-State: ACrzQf2ClZtvEdJcz8tXFJ81X26kK0xLJ61S3jkthkeeMzCkd9d4zd57 TBrirr4CkVl4uF59rxfIjLGJYO0GNUPAOQ== X-Google-Smtp-Source: AMsMyM5mvH0snShxV6JmFSKseXoMtbbp9ByQl7bIH6AYh6IA1XJsVJAc49wSTqm/pBV7vvWpvNzNRQ== X-Received: by 2002:a05:6512:2a8f:b0:49a:db9f:d498 with SMTP id dt15-20020a0565122a8f00b0049adb9fd498mr8404559lfb.435.1663672619622; Tue, 20 Sep 2022 04:16:59 -0700 (PDT) Received: from localhost.localdomain (128-69-252-100.broadband.corbina.ru. [128.69.252.100]) by smtp.gmail.com with ESMTPSA id b22-20020a056512305600b0049485e2cb91sm259317lfb.231.2022.09.20.04.16.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 20 Sep 2022 04:16:59 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Tue, 20 Sep 2022 14:16:45 +0300 Message-Id: <20220920111649.86190-1-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.32.1 (Apple Git-133) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2] Fix narrowing of unary minus. 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: Maksim Kokryashkin via Tarantool-patches Reply-To: Maksim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall (cherry picked from commit 1e6e8aaa20626ac94cf907c69b0452f76e9f5fa5) LuaJIT narrowing optimization during BC_UNM recording may ignore information about sign of zero for integer types of IR. So far the resulting value on a trace is not the same as for the interpreter. However, in DUALNUM mode with using of TValues containing integers the -0 value is represented in the same way as the 0 value and there is no difference between them. This patch fixes the non-DUALNUM mode behaviour. When the zero value is identified during recording it should be cast to number so IR_CONV is emitted. Also, this patch adds assertion guard checking that value on which operation of unary minus is performed isn't zero. Maxim Kokryashkin: * added the description and the test for the problem Resolves tarantool/tarantool#6976 Part of tarantool/tarantool#7230 --- Changes in v2: - Fixed test and commit message as per review by Sergey Branch and PR are updated. src/lj_opt_narrow.c | 9 +++- .../gh-6976-narrowing-of-unary-minus.test.lua | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua diff --git a/src/lj_opt_narrow.c b/src/lj_opt_narrow.c index cd96ca4b..bb61f97b 100644 --- a/src/lj_opt_narrow.c +++ b/src/lj_opt_narrow.c @@ -551,8 +551,13 @@ TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc) { rc = conv_str_tonum(J, rc, vc); if (tref_isinteger(rc)) { - if ((uint32_t)numberVint(vc) != 0x80000000u) - return emitir(IRTGI(IR_SUBOV), lj_ir_kint(J, 0), rc); + uint32_t k = (uint32_t)numberVint(vc); + if ((LJ_DUALNUM || k != 0) && k != 0x80000000u) { + TRef zero = lj_ir_kint(J, 0); + if (!LJ_DUALNUM) + emitir(IRTGI(IR_NE), rc, zero); + return emitir(IRTGI(IR_SUBOV), zero, rc); + } rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); } return emitir(IRTN(IR_NEG), rc, lj_ir_ksimd(J, LJ_KSIMD_NEG)); diff --git a/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua b/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua new file mode 100644 index 00000000..18778a55 --- /dev/null +++ b/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua @@ -0,0 +1,51 @@ +local tap = require('tap') +local test = tap.test('gh-6976-narrowing-of-unary-minus') +test:plan(2) + +jit.opt.start('hotloop=1', 'hotexit=1') + +local function check(routine) + jit.off() + jit.flush() + local interp_res = routine() + jit.on() + local jit_res = routine() + + for i = 1, #interp_res do + if interp_res[i] ~= jit_res[i] then + return false + end + end + + return true +end + +test:ok( + check( + function() + local res = require('table.new')(3, 0) + for _ = 1, 3 do + local zero = 0 + zero = -zero + table.insert(res, tostring(zero)) + end + return res + end + ), + 'incorrect recording for zero' +) + +test:ok( + check( + function() + local res = require('table.new')(3, 0) + for i = 2, 0, -1 do + table.insert(res, tostring(-i)) + end + return res + end + ), + 'assertion guard fail' +) + +os.exit(test:check() and 0 or 1) -- 2.32.1 (Apple Git-133)