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 DB8F86ECE3; Thu, 21 Jul 2022 19:49:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DB8F86ECE3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1658422163; bh=kVDjZ9NVzLjDHgJy7VarUSd35h8dr8D5UWGjaEMvTAs=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JQYm81+exnVgrftHm9X5f7i5L/TSqbL6JkbMEcO3nW+99T0EWVRPlJq9XNmsvEWa0 lhu39abQAtdXf1Q4Tkj3PNY0/XL3sOkSiCe2jjDIVfXLg9f6yo7rqXiyMxt9G6mrmB JOO/JmTRnfgnf7Pr2qJVKFTLKhopiMBx0qyO8isg= Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (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 204326ECE3 for ; Thu, 21 Jul 2022 19:49:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 204326ECE3 Received: by mail-lj1-f180.google.com with SMTP id e11so2512129ljl.4 for ; Thu, 21 Jul 2022 09:49:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=0dYrMfNCWYIlHteO8HB/N3VyfzH2Ki6a30wqikGDY+E=; b=uP+j68XxDnUsc2o805LFI+suLjcm9yWSwM+WuqU0rOhcwvMwt7CYPwMCgKnZYjqYu/ k0p/+jqXxpsumynekNTk6Xtb3TwEM5rTlLGUL1GjvfZmIT/RYLozTyz5d8qj0k6IEjZC iOoMazPob20z4sE/nAXzZc1GkNfmbviqLhAwmSRFPGYE0Zdr36UghjV/OOm4305fqhAx xopkf9oO7yDav9cXvF9F6q8J3qPJSbyYy8dZqEBlg3l97bgATSIwCUwpLTDov/FAgVIS akiYY6VWklsElI5d9dXPHKUAxIn9pDQ+7eyodeKgpBFNn52MWNi9psFznzkVZkI0+VcM 9MIA== X-Gm-Message-State: AJIora+dgSOX59QvdJJVC7GIfkvzh2qQB7TxDCCuPdy6s4kIuJhquTm/ hrv2EQRNEBQsmxZfn/OcilEb9gdSwqcArIs9 X-Google-Smtp-Source: AGRyM1upl69KnJUYkdA54R12e6ShY3k8lg/jNsHHGvgvqmUrM1uZK373kocNzsug/4DX7Bt8KXI9Dg== X-Received: by 2002:a05:651c:12cb:b0:25b:fa3f:c3f with SMTP id 11-20020a05651c12cb00b0025bfa3f0c3fmr20255412lje.364.1658422159879; Thu, 21 Jul 2022 09:49:19 -0700 (PDT) Received: from localhost.localdomain ([2a00:1370:8176:ec5:9b66:7279:f408:220e]) by smtp.gmail.com with ESMTPSA id w22-20020a05651c119600b0025d5ceeb0c5sm627843ljo.49.2022.07.21.09.49.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 09:49:19 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Thu, 21 Jul 2022 19:49:11 +0300 Message-Id: <20220721164915.61098-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v5] Fix math.min()/math.max() inconsistencies. 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: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall math.min()/math.max() could produce different results. Previously, these functions did not check number of arguments for x86/x64 architecture, which led to incorrect behavior in case of no arguments. This patch adds check for the number of arguments provided to math.min/max, and adds the corresponding test case for the mentioned issue. Also, it does some refactoring: 1. fcc is changed for min/max functions in ARM assembly from LO/HI (lower/upper or unordered) to LE/PL (lower/upper, equal or unoredered). 2. Several fold optimizations for min/max were removed or modified. Resolves tarantool/tarantool#6163 --- PR: https://github.com/tarantool/tarantool/pull/7455 Branch: https://github.com/tarantool/luajit/tree/fckxorg/gh-6163-min-max Side note: I have no idea how to test fold optimizations without checking mcode byte by byte, so I suggest to omit them. Side note: FCC changes have no visible effect on operation results, so I guess they just work a little bit faster that way. Again, I don't see any sensible way of testing them. src/lj_asm_arm.h | 6 +-- src/lj_asm_arm64.h | 6 +-- src/lj_opt_fold.c | 53 +++++++------------ src/lj_vmmath.c | 4 +- src/vm_arm.dasc | 4 +- src/vm_arm64.dasc | 4 +- src/vm_x64.dasc | 2 +- src/vm_x86.dasc | 2 +- .../gh-6163-jit-min-max.test.lua | 18 +++++++ 9 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 test/tarantool-tests/gh-6163-jit-min-max.test.lua diff --git a/src/lj_asm_arm.h b/src/lj_asm_arm.h index 4fd08b9e..84ab06c0 100644 --- a/src/lj_asm_arm.h +++ b/src/lj_asm_arm.h @@ -1664,8 +1664,8 @@ static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc) asm_intmin_max(as, ir, cc); } -#define asm_min(as, ir) asm_min_max(as, ir, CC_GT, CC_HI) -#define asm_max(as, ir) asm_min_max(as, ir, CC_LT, CC_LO) +#define asm_min(as, ir) asm_min_max(as, ir, CC_GT, CC_PL) +#define asm_max(as, ir) asm_min_max(as, ir, CC_LT, CC_LE) /* -- Comparisons --------------------------------------------------------- */ @@ -1857,7 +1857,7 @@ static void asm_hiop(ASMState *as, IRIns *ir) } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { as->curins--; /* Always skip the loword min/max. */ if (uselo || usehi) - asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_HI : CC_LO); + asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_PL : CC_LE); return; #elif LJ_HASFFI } else if ((ir-1)->o == IR_CONV) { diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h index da0ee4bb..1a60ab18 100644 --- a/src/lj_asm_arm64.h +++ b/src/lj_asm_arm64.h @@ -1593,7 +1593,7 @@ static void asm_fpmin_max(ASMState *as, IRIns *ir, A64CC fcc) Reg dest = (ra_dest(as, ir, RSET_FPR) & 31); Reg right, left = ra_alloc2(as, ir, RSET_FPR); right = ((left >> 8) & 31); left &= 31; - emit_dnm(as, A64I_FCSELd | A64F_CC(fcc), dest, left, right); + emit_dnm(as, A64I_FCSELd | A64F_CC(fcc), dest, right, left); emit_nm(as, A64I_FCMPd, left, right); } @@ -1605,8 +1605,8 @@ static void asm_min_max(ASMState *as, IRIns *ir, A64CC cc, A64CC fcc) asm_intmin_max(as, ir, cc); } -#define asm_max(as, ir) asm_min_max(as, ir, CC_GT, CC_HI) -#define asm_min(as, ir) asm_min_max(as, ir, CC_LT, CC_LO) +#define asm_min(as, ir) asm_min_max(as, ir, CC_LT, CC_PL) +#define asm_max(as, ir) asm_min_max(as, ir, CC_GT, CC_LE) /* -- Comparisons --------------------------------------------------------- */ diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c index 276dc040..07a52a4d 100644 --- a/src/lj_opt_fold.c +++ b/src/lj_opt_fold.c @@ -1774,8 +1774,6 @@ LJFOLDF(reassoc_intarith_k64) #endif } -LJFOLD(MIN MIN any) -LJFOLD(MAX MAX any) LJFOLD(BAND BAND any) LJFOLD(BOR BOR any) LJFOLDF(reassoc_dup) @@ -1785,6 +1783,15 @@ LJFOLDF(reassoc_dup) return NEXTFOLD; } +LJFOLD(MIN MIN any) +LJFOLD(MAX MAX any) +LJFOLDF(reassoc_dup_minmax) +{ + if (fins->op2 == fleft->op2) + return LEFTFOLD; /* (a o b) o b ==> a o b */ + return NEXTFOLD; +} + LJFOLD(BXOR BXOR any) LJFOLDF(reassoc_bxor) { @@ -1823,23 +1830,12 @@ LJFOLDF(reassoc_shift) return NEXTFOLD; } -LJFOLD(MIN MIN KNUM) -LJFOLD(MAX MAX KNUM) LJFOLD(MIN MIN KINT) LJFOLD(MAX MAX KINT) LJFOLDF(reassoc_minmax_k) { IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KNUM) { - lua_Number a = ir_knum(irk)->n; - lua_Number y = lj_vm_foldarith(a, knumright, fins->o - IR_ADD); - if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_knum(J, y); - return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ - } else if (irk->o == IR_KINT) { + if (irk->o == IR_KINT) { int32_t a = irk->i; int32_t y = kfold_intop(a, fright->i, fins->o); if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ @@ -1852,24 +1848,6 @@ LJFOLDF(reassoc_minmax_k) return NEXTFOLD; } -LJFOLD(MIN MAX any) -LJFOLD(MAX MIN any) -LJFOLDF(reassoc_minmax_left) -{ - if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) - return RIGHTFOLD; /* (b o1 a) o2 b ==> b; (a o1 b) o2 b ==> b */ - return NEXTFOLD; -} - -LJFOLD(MIN any MAX) -LJFOLD(MAX any MIN) -LJFOLDF(reassoc_minmax_right) -{ - if (fins->op1 == fright->op1 || fins->op1 == fright->op2) - return LEFTFOLD; /* a o2 (a o1 b) ==> a; a o2 (b o1 a) ==> a */ - return NEXTFOLD; -} - /* -- Array bounds check elimination -------------------------------------- */ /* Eliminate ABC across PHIs to handle t[i-1] forwarding case. @@ -1995,8 +1973,6 @@ LJFOLDF(comm_comp) LJFOLD(BAND any any) LJFOLD(BOR any any) -LJFOLD(MIN any any) -LJFOLD(MAX any any) LJFOLDF(comm_dup) { if (fins->op1 == fins->op2) /* x o x ==> x */ @@ -2004,6 +1980,15 @@ LJFOLDF(comm_dup) return fold_comm_swap(J); } +LJFOLD(MIN any any) +LJFOLD(MAX any any) +LJFOLDF(comm_dup_minmax) +{ + if (fins->op1 == fins->op2) /* x o x ==> x */ + return LEFTFOLD; + return NEXTFOLD; +} + LJFOLD(BXOR any any) LJFOLDF(comm_bxor) { diff --git a/src/lj_vmmath.c b/src/lj_vmmath.c index b231d3e8..08ccf467 100644 --- a/src/lj_vmmath.c +++ b/src/lj_vmmath.c @@ -50,8 +50,8 @@ double lj_vm_foldarith(double x, double y, int op) #if LJ_HASJIT case IR_ATAN2 - IR_ADD: return atan2(x, y); break; case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; - case IR_MIN - IR_ADD: return x > y ? y : x; break; - case IR_MAX - IR_ADD: return x < y ? y : x; break; + case IR_MIN - IR_ADD: return x < y ? x : y; break; + case IR_MAX - IR_ADD: return x > y ? x : y; break; #endif default: return x; } diff --git a/src/vm_arm.dasc b/src/vm_arm.dasc index 21f7fecb..6b511347 100644 --- a/src/vm_arm.dasc +++ b/src/vm_arm.dasc @@ -1718,8 +1718,8 @@ static void build_subroutines(BuildCtx *ctx) |.endif |.endmacro | - | math_minmax math_min, gt, hi - | math_minmax math_max, lt, lo + | math_minmax math_min, gt, pl + | math_minmax math_max, lt, le | |//-- String library ----------------------------------------------------- | diff --git a/src/vm_arm64.dasc b/src/vm_arm64.dasc index 313cc94f..80e795ae 100644 --- a/src/vm_arm64.dasc +++ b/src/vm_arm64.dasc @@ -1491,8 +1491,8 @@ static void build_subroutines(BuildCtx *ctx) | b <6 |.endmacro | - | math_minmax math_min, gt, hi - | math_minmax math_max, lt, lo + | math_minmax math_min, gt, pl + | math_minmax math_max, lt, le | |//-- String library ----------------------------------------------------- | diff --git a/src/vm_x64.dasc b/src/vm_x64.dasc index 974047d3..cbf5fb9b 100644 --- a/src/vm_x64.dasc +++ b/src/vm_x64.dasc @@ -1874,7 +1874,7 @@ static void build_subroutines(BuildCtx *ctx) | jmp ->fff_res | |.macro math_minmax, name, cmovop, sseop - | .ffunc name + | .ffunc_1 name | mov RAd, 2 |.if DUALNUM | mov RB, [BASE] diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc index 92140cec..93784814 100644 --- a/src/vm_x86.dasc +++ b/src/vm_x86.dasc @@ -2313,7 +2313,7 @@ static void build_subroutines(BuildCtx *ctx) | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0. | |.macro math_minmax, name, cmovop, sseop - | .ffunc name + | .ffunc_1 name | mov RA, 2 | cmp dword [BASE+4], LJ_TISNUM |.if DUALNUM diff --git a/test/tarantool-tests/gh-6163-jit-min-max.test.lua b/test/tarantool-tests/gh-6163-jit-min-max.test.lua new file mode 100644 index 00000000..809c5268 --- /dev/null +++ b/test/tarantool-tests/gh-6163-jit-min-max.test.lua @@ -0,0 +1,18 @@ +local tap = require('tap') +jit.off() +jit.flush() + +local test = tap.test('gh-6163-jit-min-max') +test:plan(1) +-- +-- gh-6163: math.min/math.max success with no args +-- +local function filler() + return 1, 1, 1 +end + +filler() +local r, _ = pcall(function() math.min() end) +test:ok(false == r) + +os.exit(test:check() and 0 or 1) -- 2.36.1