Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0.
@ 2023-10-31 12:44 Sergey Kaplun via Tarantool-patches
  2023-10-31 15:17 ` Maxim Kokryashkin via Tarantool-patches
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2023-10-31 12:44 UTC (permalink / raw)
  To: Maxim Kokryashkin, Sergey Bronnikov; +Cc: tarantool-patches

From: Mike Pall <mike>

Reported by XmiliaH.

(cherry-picked from commit 7b994e0ee0399caf6319865bbac88ddf62129a36)

Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP arithmetic.
Its result is -0 instead of +0. This patch allows only x - (+0) ==> x
optimization.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#9145
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0
Tarantool PR: https://github.com/tarantool/tarantool/pull/9320
Related Issues:
* https://github.com/tarantool/tarantool/issues/9145
* https://github.com/LuaJIT/LuaJIT/pull/783

 src/lj_opt_fold.c                            |  3 +--
 test/tarantool-tests/lj-783-fold--0.test.lua | 28 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 test/tarantool-tests/lj-783-fold--0.test.lua

diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
index 09e6c87b..944a9ecc 100644
--- a/src/lj_opt_fold.c
+++ b/src/lj_opt_fold.c
@@ -1005,8 +1005,7 @@ LJFOLDF(simplify_numadd_xneg)
 LJFOLD(SUB any KNUM)
 LJFOLDF(simplify_numsub_k)
 {
-  lua_Number n = knumright;
-  if (n == 0.0)  /* x - (+-0) ==> x */
+  if (ir_knum(fright)->u64 == 0)  /* x - (+0) ==> x */
     return LEFTFOLD;
   return NEXTFOLD;
 }
diff --git a/test/tarantool-tests/lj-783-fold--0.test.lua b/test/tarantool-tests/lj-783-fold--0.test.lua
new file mode 100644
index 00000000..d6b4b493
--- /dev/null
+++ b/test/tarantool-tests/lj-783-fold--0.test.lua
@@ -0,0 +1,28 @@
+local tap = require('tap')
+
+-- Test file to demonstrate LuaJIT's incorrect fold optimization
+-- x - (-0) ==> x.
+-- See also https://github.com/LuaJIT/LuaJIT/issues/783.
+local test = tap.test('lj-783-fold--0'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(2)
+
+-- XXX: Use the variable to avoid folding during parsing.
+local minus_zero = -0
+local results = {}
+
+jit.opt.start('hotloop=1')
+
+for i = 1, 4 do
+  results[i] = tostring(minus_zero - (-0))
+end
+
+-- Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP
+-- arithmetic. Its result is -0 instead of +0.
+
+test:is(results[1], '0', 'correct VM value for -0 - (-0)')
+test:samevalues(results, '-0 folding in simplify_numsub_k')
+
+test:done(true)
-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches]  [PATCH luajit] Fix FOLD rule for x-0.
  2023-10-31 12:44 [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0 Sergey Kaplun via Tarantool-patches
@ 2023-10-31 15:17 ` Maxim Kokryashkin via Tarantool-patches
  2023-11-08 19:36 ` Sergey Bronnikov via Tarantool-patches
  2023-11-23  6:29 ` Igor Munkin via Tarantool-patches
  2 siblings, 0 replies; 4+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2023-10-31 15:17 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

[-- Attachment #1: Type: text/plain, Size: 2536 bytes --]


Hi, Sergey!
Thanks for the patch!
LGTM
--
Best regards,
Maxim Kokryashkin
 
  
>Вторник, 31 октября 2023, 15:49 +03:00 от Sergey Kaplun <skaplun@tarantool.org>:
> 
>From: Mike Pall <mike>
>
>Reported by XmiliaH.
>
>(cherry-picked from commit 7b994e0ee0399caf6319865bbac88ddf62129a36)
>
>Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP arithmetic.
>Its result is -0 instead of +0. This patch allows only x - (+0) ==> x
>optimization.
>
>Sergey Kaplun:
>* added the description and the test for the problem
>
>Part of tarantool/tarantool#9145
>---
>
>Branch:  https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0
>Tarantool PR:  https://github.com/tarantool/tarantool/pull/9320
>Related Issues:
>*  https://github.com/tarantool/tarantool/issues/9145
>*  https://github.com/LuaJIT/LuaJIT/pull/783
>
> src/lj_opt_fold.c | 3 +--
> test/tarantool-tests/lj-783-fold--0.test.lua | 28 ++++++++++++++++++++
> 2 files changed, 29 insertions(+), 2 deletions(-)
> create mode 100644 test/tarantool-tests/lj-783-fold--0.test.lua
>
>diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
>index 09e6c87b..944a9ecc 100644
>--- a/src/lj_opt_fold.c
>+++ b/src/lj_opt_fold.c
>@@ -1005,8 +1005,7 @@ LJFOLDF(simplify_numadd_xneg)
> LJFOLD(SUB any KNUM)
> LJFOLDF(simplify_numsub_k)
> {
>- lua_Number n = knumright;
>- if (n == 0.0) /* x - (+-0) ==> x */
>+ if (ir_knum(fright)->u64 == 0) /* x - (+0) ==> x */
>     return LEFTFOLD;
>   return NEXTFOLD;
> }
>diff --git a/test/tarantool-tests/lj-783-fold--0.test.lua b/test/tarantool-tests/lj-783-fold--0.test.lua
>new file mode 100644
>index 00000000..d6b4b493
>--- /dev/null
>+++ b/test/tarantool-tests/lj-783-fold--0.test.lua
>@@ -0,0 +1,28 @@
>+local tap = require('tap')
>+
>+-- Test file to demonstrate LuaJIT's incorrect fold optimization
>+-- x - (-0) ==> x.
>+-- See also  https://github.com/LuaJIT/LuaJIT/issues/783 .
>+local test = tap.test('lj-783-fold--0'):skipcond({
>+ ['Test requires JIT enabled'] = not jit.status(),
>+})
>+
>+test:plan(2)
>+
>+-- XXX: Use the variable to avoid folding during parsing.
>+local minus_zero = -0
>+local results = {}
>+
>+jit.opt.start('hotloop=1')
>+
>+for i = 1, 4 do
>+ results[i] = tostring(minus_zero - (-0))
>+end
>+
>+-- Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP
>+-- arithmetic. Its result is -0 instead of +0.
>+
>+test:is(results[1], '0', 'correct VM value for -0 - (-0)')
>+test:samevalues(results, '-0 folding in simplify_numsub_k')
>+
>+test:done(true)
>--
>2.42.0
 

[-- Attachment #2: Type: text/html, Size: 3613 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0.
  2023-10-31 12:44 [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0 Sergey Kaplun via Tarantool-patches
  2023-10-31 15:17 ` Maxim Kokryashkin via Tarantool-patches
@ 2023-11-08 19:36 ` Sergey Bronnikov via Tarantool-patches
  2023-11-23  6:29 ` Igor Munkin via Tarantool-patches
  2 siblings, 0 replies; 4+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2023-11-08 19:36 UTC (permalink / raw)
  To: Sergey Kaplun, Maxim Kokryashkin; +Cc: tarantool-patches

Hi, Sergey!
Thanks for the patch!
LGTM

On 10/31/23 15:44, Sergey Kaplun wrote:
> From: Mike Pall <mike>
>
> Reported by XmiliaH.
>
> (cherry-picked from commit 7b994e0ee0399caf6319865bbac88ddf62129a36)
>
> Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP arithmetic.
> Its result is -0 instead of +0. This patch allows only x - (+0) ==> x
> optimization.
>
> Sergey Kaplun:
> * added the description and the test for the problem
>
> Part of tarantool/tarantool#9145
> ---
>
> Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0
> Tarantool PR: https://github.com/tarantool/tarantool/pull/9320
> Related Issues:
> * https://github.com/tarantool/tarantool/issues/9145
> * https://github.com/LuaJIT/LuaJIT/pull/783
>
>   src/lj_opt_fold.c                            |  3 +--
>   test/tarantool-tests/lj-783-fold--0.test.lua | 28 ++++++++++++++++++++
>   2 files changed, 29 insertions(+), 2 deletions(-)
>   create mode 100644 test/tarantool-tests/lj-783-fold--0.test.lua
>
> diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
> index 09e6c87b..944a9ecc 100644
> --- a/src/lj_opt_fold.c
> +++ b/src/lj_opt_fold.c
> @@ -1005,8 +1005,7 @@ LJFOLDF(simplify_numadd_xneg)
>   LJFOLD(SUB any KNUM)
>   LJFOLDF(simplify_numsub_k)
>   {
> -  lua_Number n = knumright;
> -  if (n == 0.0)  /* x - (+-0) ==> x */
> +  if (ir_knum(fright)->u64 == 0)  /* x - (+0) ==> x */
>       return LEFTFOLD;
>     return NEXTFOLD;
>   }
> diff --git a/test/tarantool-tests/lj-783-fold--0.test.lua b/test/tarantool-tests/lj-783-fold--0.test.lua
> new file mode 100644
> index 00000000..d6b4b493
> --- /dev/null
> +++ b/test/tarantool-tests/lj-783-fold--0.test.lua
> @@ -0,0 +1,28 @@
> +local tap = require('tap')
> +
> +-- Test file to demonstrate LuaJIT's incorrect fold optimization
> +-- x - (-0) ==> x.
> +-- See also https://github.com/LuaJIT/LuaJIT/issues/783.
> +local test = tap.test('lj-783-fold--0'):skipcond({
> +  ['Test requires JIT enabled'] = not jit.status(),
> +})
> +
> +test:plan(2)
> +
> +-- XXX: Use the variable to avoid folding during parsing.
> +local minus_zero = -0
> +local results = {}
> +
> +jit.opt.start('hotloop=1')
> +
> +for i = 1, 4 do
> +  results[i] = tostring(minus_zero - (-0))
> +end
> +
> +-- Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP
> +-- arithmetic. Its result is -0 instead of +0.
> +
> +test:is(results[1], '0', 'correct VM value for -0 - (-0)')
> +test:samevalues(results, '-0 folding in simplify_numsub_k')
> +
> +test:done(true)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0.
  2023-10-31 12:44 [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0 Sergey Kaplun via Tarantool-patches
  2023-10-31 15:17 ` Maxim Kokryashkin via Tarantool-patches
  2023-11-08 19:36 ` Sergey Bronnikov via Tarantool-patches
@ 2023-11-23  6:29 ` Igor Munkin via Tarantool-patches
  2 siblings, 0 replies; 4+ messages in thread
From: Igor Munkin via Tarantool-patches @ 2023-11-23  6:29 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

Sergey,

I've checked the patchset into all long-term branches in
tarantool/luajit and bumped a new version in master, release/2.11 and
release/2.10.

On 31.10.23, Sergey Kaplun via Tarantool-patches wrote:
> From: Mike Pall <mike>
> 
> Reported by XmiliaH.
> 
> (cherry-picked from commit 7b994e0ee0399caf6319865bbac88ddf62129a36)
> 
> Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP arithmetic.
> Its result is -0 instead of +0. This patch allows only x - (+0) ==> x
> optimization.
> 
> Sergey Kaplun:
> * added the description and the test for the problem
> 
> Part of tarantool/tarantool#9145
> ---
> 
> Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0
> Tarantool PR: https://github.com/tarantool/tarantool/pull/9320
> Related Issues:
> * https://github.com/tarantool/tarantool/issues/9145
> * https://github.com/LuaJIT/LuaJIT/pull/783
> 
>  src/lj_opt_fold.c                            |  3 +--
>  test/tarantool-tests/lj-783-fold--0.test.lua | 28 ++++++++++++++++++++
>  2 files changed, 29 insertions(+), 2 deletions(-)
>  create mode 100644 test/tarantool-tests/lj-783-fold--0.test.lua
> 

<snipped>

> -- 
> 2.42.0
> 

-- 
Best regards,
IM

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-23  6:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 12:44 [Tarantool-patches] [PATCH luajit] Fix FOLD rule for x-0 Sergey Kaplun via Tarantool-patches
2023-10-31 15:17 ` Maxim Kokryashkin via Tarantool-patches
2023-11-08 19:36 ` Sergey Bronnikov via Tarantool-patches
2023-11-23  6:29 ` Igor Munkin via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox