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 CCC8C713ABE; Wed, 29 Nov 2023 17:26:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CCC8C713ABE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1701268017; bh=MKb9GATDxO5cCfyXSRhAYaqaNVR40C2wTvGFNvmhu3s=; 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=d3DnzlOoRwMvak4UFdGr7NWkUozBdhitGZJ3ZGIV0MOgmaxW5LBmkVYjTj1O0McMF pUBj4as2mQvLPaR6Zm5aetpsEcBbtnMUS1ylEuhGz9BIyq4XK4+ICZ9OuAF+1rhOgC 1TlseJa8vjIu+yvC/jtIRhwzOXIVO3oAuEQ8SyN0= Received: from smtp43.i.mail.ru (smtp43.i.mail.ru [95.163.41.66]) (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 BAD2C70CC6C for ; Wed, 29 Nov 2023 17:26:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BAD2C70CC6C Received: by smtp43.i.mail.ru with esmtpa (envelope-from ) id 1r8LWg-001PqZ-2q; Wed, 29 Nov 2023 17:26:55 +0300 Content-Type: multipart/alternative; boundary="------------rRcjTy9Z0CHvoMW0lI2eoQ9E" Message-ID: <07734b67-027a-4cc8-9c8a-03cd77a54137@tarantool.org> Date: Wed, 29 Nov 2023 17:26:54 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: <20231128122112.16229-1-skaplun@tarantool.org> In-Reply-To: <20231128122112.16229-1-skaplun@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9D2A6479154BC7F418A5726D9682ECCAAA31AEB62BF693B13182A05F538085040E2979EC84F50FBBAC896CF8C213537F02EAF582831CC18ECA98253C75E726A19 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7466896EF24E80F12EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CD693CC314794D958638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80E9E18FCDD4565F2C39F585FA2FEE4D5117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD182CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE26055571C92BF10F7AAF5A6EB0CB4C2AD8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE36089696B24BB1D19AD7EC71F1DB88427C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB861051D4BA689FC2E808ACE2090B5E1725E5C173C3A84C3ED8438A78DFE0A9E089D37D7C0E48F6C8AA50765F7900637DBAA09C62585F9A4EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A5DA668BA87E46A36E7CFFC591C5D794AB36B452A93AEDFACFF87CCE6106E1FC07E67D4AC08A07B9B01DAA61796BF5227BCB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADBF74143AD284FC7177DD89D51EBB7742424CF958EAFF5D571004E42C50DC4CA955A7F0CF078B5EC49A30900B95165D3480A9008907CB2FD080C8A2A95C1AF3D7C567116CBC90AA7113B954E2D162535A322916C367ED30A11D7E09C32AA3244CD1D8982CF52788425F01B929C57FD9F3435BF7150578642F85A42E4C463514DC5DA084F8E80FEBD3FFA33E6B6B2F82C47A83BD0C44CE203720ABEDE4BBDD9CDD X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojxXIty/pLbahm1yV/U3AEOA== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769846B48D805F32B0BC896CF8C213537F07B0B9A7C4E99E59BEBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] Prevent CSE of a REF_BASE operand across IR_RETF. 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" This is a multi-part message in MIME format. --------------rRcjTy9Z0CHvoMW0lI2eoQ9E Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hello, Sergey thanks for the patch! LGTM with a three minor comments below On 11/28/23 15:21, Sergey Kaplun wrote: > From: Mike Pall > > Reported by XmiliaH. > > (cherry-picked from commit e73916d811710ab02a4dfe447d621c99f4e7186c) > > The RETF IR has a side effect: it shifts base when returning to a lower > frame, i.e., it affects `REF_BASE` IR (0000) (thus, we can say that this > IR is violating SSA form). So any optimization of IRs with `REF_BASE` as > an operand across RETF IR may lead to incorrect optimizations (see > details in the test file). > > This patch adds rules to the folding engine to prevent CSE across `IR_RETF` > for all possible IRs containing REF_BASE. > > 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-784-cse-ref-base-over-retf > Tarantool PR:https://github.com/tarantool/tarantool/pull/9421 > Related issues: > *https://github.com/LuaJIT/LuaJIT/issues/784 > *https://github.com/tarantool/tarantool/issues/9145 > > Interested reviewers can mention that only the `SUB any BASE` case is > tested. > The reason is that other cases are impossible to record in LuaJIT: > * EQ any BASE: EQ pgc REF_BASE IR for upvalues is emitted when > the open upvalue aliases a SSA slot, i.e., it belongs to the frame of > the currently executed function. In that case, if we want to emit RETF > IR, we need to leave this function. So we need to record the UCLO > bytecode, which is NIY in JIT. So, such a type of trace is impossible. > * SUB BASE any: SUB BASE fr is emitted for the recording of VARG Nit: fr -> frame or put in backticks if you refer to a variable in source code > bytecode, in case varargs are undefined on trace. We need a vararg > function to call to create an additional frame. But returning to lower > frames from a vararg function isn't implemented in LuaJIT -- either > the trace recording is stopped or the error is rased and the trace > isn't compiled. Also, IINM, fr operands will always be different for Nit: fr -> frame or put in backticks if you refer to a variable in source code > different frames, so there is no possible CSE here. > > So, these cases are needed to prevent any regressions in the future. > > Please correct me if I've missed something. > > src/lj_opt_fold.c | 11 +++ > .../lj-784-cse-ref-base-over-retf.test.lua | 86 +++++++++++++++++++ > 2 files changed, 97 insertions(+) > create mode 100644 test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua > > diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c > index c5f2232e..750f1c7e 100644 > --- a/src/lj_opt_fold.c > +++ b/src/lj_opt_fold.c > @@ -2313,6 +2313,17 @@ LJFOLDF(xload_kptr) > LJFOLD(XLOAD any any) > LJFOLDX(lj_opt_fwd_xload) > > +/* -- Frame handling ------------------------------------------------------ */ > + > +/* Prevent CSE of a REF_BASE operand across IR_RETF. */ > +LJFOLD(SUB any BASE) > +LJFOLD(SUB BASE any) > +LJFOLD(EQ any BASE) > +LJFOLDF(fold_base) > +{ > + return lj_opt_cselim(J, J->chain[IR_RETF]); > +} > + > /* -- Write barriers ------------------------------------------------------ */ > > /* Write barriers are amenable to CSE, but not across any incremental > diff --git a/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua b/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua > new file mode 100644 > index 00000000..095376fc > --- /dev/null > +++ b/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua > @@ -0,0 +1,86 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate incorrect FOLD optimization for IR > +-- with REF_BASE operand across IR RETF. > +-- See also,https://github.com/LuaJIT/LuaJIT/issues/784. > + > +local test = tap.test('lj-784-cse-ref-base-over-retf'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(1) > + > +-- The RETF IR has a side effect: it shifts base when returning to > +-- a lower frame, i.e., it affects `REF_BASE` IR (0000) (thus, we > +-- can say that this IR is violating SSA form). > +-- So any optimization of IRs with `REF_BASE` as an operand across > +-- RETF IR may lead to incorrect optimizations. > +-- In this test, SUB uref REF_BASE IR was eliminated, so instead > +-- the following trace: > +-- > +-- 0004 p32 SUB 0003 0000 > +-- 0005 > p32 UGT 0004 +32 > +-- ... > +-- 0009 > p32 RETF proto: 0x407dc118 [0x407dc194] > +-- ... > +-- 0012 p32 SUB 0003 0000 > +-- 0013 > p32 UGT 0012 +72 > +-- > +-- We got the following: > +-- > +-- 0004 p32 SUB 0003 0000 > +-- 0005 > p32 UGT 0004 +32 > +-- ... > +-- 0009 > p32 RETF proto: 0x41ffe0c0 [0x41ffe13c] > +-- ... > +-- 0012 > p32 UGT 0004 +72 > +-- > +-- As you can see, the 0012 SUB IR is eliminated because it is the > +-- same as the 0004 IR. This leads to incorrect assertion guards > +-- in the IR below. I would rephrase it to "assertion guards in the resulted IR" because there is no IR below the comment. > + > +local MAGIC = 42 > +-- XXX: simplify `jit.dump()` output. > +local fmod = math.fmod > + > +local function exit_with_retf(closure) > + -- Forcify stitch. Any NYI is OK here. > + fmod(1, 1) > + -- Call the closure so that we have emitted `uref - REF_BASE`. > + closure(0) > + -- Exit with `IR_RETF`. This will change `REF_BASE`. > +end > + > +local function sub_uref_base(closure) > + local open_upvalue > + if closure == nil then > + closure = function(val) > + local old = open_upvalue > + open_upvalue = val > + return old > + end > + -- First, create an additional frame, so we got the trace, > + -- where the open upvalue reference is always < `REF_BASE`. > + sub_uref_base(closure) > + end > + for _ = 1, 4 do > + -- `closure` function is inherited from the previous frame. > + exit_with_retf(closure) > + open_upvalue = MAGIC > + -- The open upvalue guard will use CSE over `IR_RETF` for > + -- `uref - REF_BASE`. `IR_RETF` changed the value of > + -- `REF_BASE`. > + -- Thus, the guards afterwards take the wrong IR as the first > + -- operand, so they are not failed, and the wrong value is > + -- returned from the trace. > + open_upvalue = closure(0) > + end > + return open_upvalue > +end > + > +jit.opt.start('hotloop=1') > + > +local res = sub_uref_base() > +test:is(res, MAGIC, 'no SUB uref REF_BASE CSE across RETF') > + > +test:done(true) --------------rRcjTy9Z0CHvoMW0lI2eoQ9E Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hello, Sergey

thanks for the patch!

LGTM with a three minor comments below

On 11/28/23 15:21, Sergey Kaplun wrote:
From: Mike Pall <mike>

Reported by XmiliaH.

(cherry-picked from commit e73916d811710ab02a4dfe447d621c99f4e7186c)

The RETF IR has a side effect: it shifts base when returning to a lower
frame, i.e., it affects `REF_BASE` IR (0000) (thus, we can say that this
IR is violating SSA form). So any optimization of IRs with `REF_BASE` as
an operand across RETF IR may lead to incorrect optimizations (see
details in the test file).

This patch adds rules to the folding engine to prevent CSE across `IR_RETF`
for all possible IRs containing REF_BASE.

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-784-cse-ref-base-over-retf
Tarantool PR: https://github.com/tarantool/tarantool/pull/9421
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/784
* https://github.com/tarantool/tarantool/issues/9145

Interested reviewers can mention that only the `SUB any BASE` case is
tested.
The reason is that other cases are impossible to record in LuaJIT:
* EQ any BASE: EQ pgc REF_BASE IR for upvalues is emitted when
  the open upvalue aliases a SSA slot, i.e., it belongs to the frame of
  the currently executed function. In that case, if we want to emit RETF
  IR, we need to leave this function. So we need to record the UCLO
  bytecode, which is NIY in JIT. So, such a type of trace is impossible.
* SUB BASE any: SUB BASE fr is emitted for the recording of VARG

Nit: fr -> frame

or put in backticks if you refer to a variable in source code

  bytecode, in case varargs are undefined on trace. We need a vararg
  function to call to create an additional frame. But returning to lower
  frames from a vararg function isn't implemented in LuaJIT -- either
  the trace recording is stopped or the error is rased and the trace
  isn't compiled. Also, IINM, fr operands will always be different for

Nit: fr -> frame

or put in backticks if you refer to a variable in source code


    

  different frames, so there is no possible CSE here.

So, these cases are needed to prevent any regressions in the future.

Please correct me if I've missed something.

 src/lj_opt_fold.c                             | 11 +++
 .../lj-784-cse-ref-base-over-retf.test.lua    | 86 +++++++++++++++++++
 2 files changed, 97 insertions(+)
 create mode 100644 test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua

diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
index c5f2232e..750f1c7e 100644
--- a/src/lj_opt_fold.c
+++ b/src/lj_opt_fold.c
@@ -2313,6 +2313,17 @@ LJFOLDF(xload_kptr)
 LJFOLD(XLOAD any any)
 LJFOLDX(lj_opt_fwd_xload)
 
+/* -- Frame handling ------------------------------------------------------ */
+
+/* Prevent CSE of a REF_BASE operand across IR_RETF. */
+LJFOLD(SUB any BASE)
+LJFOLD(SUB BASE any)
+LJFOLD(EQ any BASE)
+LJFOLDF(fold_base)
+{
+  return lj_opt_cselim(J, J->chain[IR_RETF]);
+}
+
 /* -- Write barriers ------------------------------------------------------ */
 
 /* Write barriers are amenable to CSE, but not across any incremental
diff --git a/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua b/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua
new file mode 100644
index 00000000..095376fc
--- /dev/null
+++ b/test/tarantool-tests/lj-784-cse-ref-base-over-retf.test.lua
@@ -0,0 +1,86 @@
+local tap = require('tap')
+
+-- Test file to demonstrate incorrect FOLD optimization for IR
+-- with REF_BASE operand across IR RETF.
+-- See also, https://github.com/LuaJIT/LuaJIT/issues/784.
+
+local test = tap.test('lj-784-cse-ref-base-over-retf'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(1)
+
+-- The RETF IR has a side effect: it shifts base when returning to
+-- a lower frame, i.e., it affects `REF_BASE` IR (0000) (thus, we
+-- can say that this IR is violating SSA form).
+-- So any optimization of IRs with `REF_BASE` as an operand across
+-- RETF IR may lead to incorrect optimizations.
+-- In this test, SUB uref REF_BASE IR was eliminated, so instead
+-- the following trace:
+--
+-- 0004    p32 SUB    0003  0000
+-- 0005 >  p32 UGT    0004  +32
+-- ...
+-- 0009 >  p32 RETF   proto: 0x407dc118  [0x407dc194]
+-- ...
+-- 0012    p32 SUB    0003  0000
+-- 0013 >  p32 UGT    0012  +72
+--
+-- We got the following:
+--
+-- 0004    p32 SUB    0003  0000
+-- 0005 >  p32 UGT    0004  +32
+-- ...
+-- 0009 >  p32 RETF   proto: 0x41ffe0c0  [0x41ffe13c]
+-- ...
+-- 0012 >  p32 UGT    0004  +72
+--
+-- As you can see, the 0012 SUB IR is eliminated because it is the
+-- same as the 0004 IR. This leads to incorrect assertion guards
+-- in the IR below.

I would rephrase it to "assertion guards in the resulted IR"

because there is no IR below the comment.

+
+local MAGIC = 42
+-- XXX: simplify `jit.dump()` output.
+local fmod =  math.fmod
+
+local function exit_with_retf(closure)
+  -- Forcify stitch. Any NYI is OK here.
+  fmod(1, 1)
+  -- Call the closure so that we have emitted `uref - REF_BASE`.
+  closure(0)
+  -- Exit with `IR_RETF`. This will change `REF_BASE`.
+end
+
+local function sub_uref_base(closure)
+  local open_upvalue
+  if closure == nil then
+    closure = function(val)
+      local old = open_upvalue
+      open_upvalue = val
+      return old
+    end
+    -- First, create an additional frame, so we got the trace,
+    -- where the open upvalue reference is always < `REF_BASE`.
+    sub_uref_base(closure)
+  end
+  for _ = 1, 4 do
+    -- `closure` function is inherited from the previous frame.
+    exit_with_retf(closure)
+    open_upvalue = MAGIC
+    -- The open upvalue guard will use CSE over `IR_RETF` for
+    -- `uref - REF_BASE`. `IR_RETF` changed the value of
+    -- `REF_BASE`.
+    -- Thus, the guards afterwards take the wrong IR as the first
+    -- operand, so they are not failed, and the wrong value is
+    -- returned from the trace.
+    open_upvalue = closure(0)
+  end
+  return open_upvalue
+end
+
+jit.opt.start('hotloop=1')
+
+local res = sub_uref_base()
+test:is(res, MAGIC, 'no SUB uref REF_BASE CSE across RETF')
+
+test:done(true)
--------------rRcjTy9Z0CHvoMW0lI2eoQ9E--