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 D117F6FA3BE; Fri, 24 Nov 2023 15:50:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D117F6FA3BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1700830202; bh=PrNRncuFmR1wWhe4/krZBZBqevSSlCY0ppbj5R7KOO0=; 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=LEuO2RLJCuRwu42ndgzh5JghBDkHhDmJBE+6YSra5P3Wg/oZFQhHIL3r5x/7Yl/x8 RDcyZyQ2l3p2jm8TDtwocr6TIhNN+rXwsRyeyN204BtMQFFUOwoMBdWQEZ0sfhibrd BRHSZv3uqOa4D/gYm+P8qcKJlWp6abGsTdIxkLeg= Received: from smtp56.i.mail.ru (smtp56.i.mail.ru [95.163.41.94]) (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 21A5F6FA3A5 for ; Fri, 24 Nov 2023 15:50:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 21A5F6FA3A5 Received: by smtp56.i.mail.ru with esmtpa (envelope-from ) id 1r6VdA-00Fh5c-15; Fri, 24 Nov 2023 15:50:00 +0300 Message-ID: <47007825-9906-4f23-b719-79d822672e32@tarantool.org> Date: Fri, 24 Nov 2023 15:50:00 +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: <20231114150455.2850-1-skaplun@tarantool.org> In-Reply-To: <20231114150455.2850-1-skaplun@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD91D460360092162747FECD3F9344D284EA1319F5150E6282C182A05F538085040128CFAC3CEE8CCF7E587A8C23070B58066F52CE4FFFF98B0CCFDCEA8DAB3986F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78E88BD1CA827EF00C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE7233EAFDDCC869EB0EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38B73AB1701401CD87156FCEC9F793A90074D8B2817E6E5567ACC7F00164DA146DAFE8445B8C89999728AA50765F79006375FFD5C25497261569FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E97D2AE7161E217F117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF4CF1817FC635140EBA3038C0950A5D36C8A9BA7A39EFB766D91E3A1F190DE8FDBA3038C0950A5D36D5E8D9A59859A8B63770467DA350783E76E601842F6C81A1F004C906525384303E02D724532EE2C3F43C7A68FF6260569E8FC8737B5C224952D31B9D28593E51E827F84554CEF50127C277FBC8AE2E8BF1175FABE1C0F9B6AAAE862A0553A39223F8577A6DFFEA7C14C5F97DFE9D033043847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: 0D63561A33F958A569F1576CAFC0E4D2D27DFD16E09156B8C0650D9864682BE8F87CCE6106E1FC07E67D4AC08A07B9B00A6B3CD6EB70C818BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF83CB3894992BDE8C80E2DC6CB27D9EB196E225C8269259B6679A3FAB3BFD2DCC16D5D591715857EC6E159192F517E6717124AD964AD985113D9B79E82A3F6B2AE48CAC7CA610320002C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojXb+1cQU8cAOawpbFom76Ew== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769590B67D4E202729CE587A8C23070B58060497F7EF99AAC8FEBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] Fix FOLD rule for BUFHDR append. 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" Hello, Sergey thanks for the patch! LGTM with a nit below. On 11/14/23 18:04, Sergey Kaplun wrote: > From: Mike Pall > > Reported by XmiliaH. > > (cherry-picked from commit bc1bdbf620f58f0978385828bc51272903601e17) > > `bufput_append()` may fold `BUFHDR RESET` + `BUFPUT` IRs to `BUFHDR > APPEND` even if the right operand (`BUFSTR`) is the PHI. If it's not the > last IR in the `BUFSTR` chain, this may lead to an incorrect resulting > value in the buffer, which contains a longer string since `APPEND` is > used instead of `RESET`. > > This patch adds the corresponding check inside the fold rule. > > 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-791-fold-bufhdr-append > Tarantool PR: https://github.com/tarantool/tarantool/pull/9369 > Relate issues: > * https://github.com/LuaJIT/LuaJIT/issues/791 > * https://github.com/tarantool/tarantool/issues/9145 > > src/lj_opt_fold.c | 3 +- > .../lj-791-fold-bufhdr-append.test.lua | 54 +++++++++++++++++++ > 2 files changed, 56 insertions(+), 1 deletion(-) > create mode 100644 test/tarantool-tests/lj-791-fold-bufhdr-append.test.lua > > diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c > index 944a9ecc..910cbc14 100644 > --- a/src/lj_opt_fold.c > +++ b/src/lj_opt_fold.c > @@ -584,7 +584,8 @@ LJFOLDF(bufput_append) > if ((J->flags & JIT_F_OPT_FWD) && > !(fleft->op2 & IRBUFHDR_APPEND) && > fleft->prev == fright->op2 && > - fleft->op1 == IR(fright->op2)->op1) { > + fleft->op1 == IR(fright->op2)->op1 && > + !(irt_isphi(fright->t) && IR(fright->op2)->prev)) { > IRRef ref = fins->op1; > IR(ref)->op2 = (fleft->op2 | IRBUFHDR_APPEND); /* Modify BUFHDR. */ > IR(ref)->op1 = fright->op1; > diff --git a/test/tarantool-tests/lj-791-fold-bufhdr-append.test.lua b/test/tarantool-tests/lj-791-fold-bufhdr-append.test.lua > new file mode 100644 > index 00000000..b2422159 > --- /dev/null > +++ b/test/tarantool-tests/lj-791-fold-bufhdr-append.test.lua > @@ -0,0 +1,54 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate the incorrect LuaJIT's optimization > +-- `bufput_append()` for BUFPUT IR. > +-- See also https://github.com/LuaJIT/LuaJIT/issues/791. > + > +local test = tap.test('lj-791-fold-bufhdr-append'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(1) > + > +local EMPTY_STR = '' > +local prefix = 'Lu' > +local result > + > +jit.opt.start('hotloop=1') > + > +-- The interesting part of IRs is the following (non-GC64 mode): > +-- 0006 str BUFSTR 0005 0003 > +-- 0007 > str SLOAD #2 T > +-- 0008 p32 BUFHDR [0x400004a0] RESET > +-- 0009 p32 BUFPUT 0008 "Lu" > +-- 0010 p32 BUFPUT 0009 0007 > +-- 0011 + str BUFSTR 0010 0008 > +-- 0012 + int ADD 0001 +1 > +-- 0013 > int LE 0012 +5 > +-- 0014 > --- LOOP ------------ > +-- 0015 p32 BUFHDR [0x400004a0] RESET > + > +-- The instruction to be folded is the following: > +-- 0016 p32 BUFPUT 0015 0011 > +-- > +-- The 0011 operand is PHI, which is not the last IR in the BUFSTR > +-- chain (`ir->prev = REF_BIAS + 0006`). Folding this IR leads to > +-- this resulting IR: > +-- p32 BUFHDR 0010 APPEND > +-- Which appends to buffer instead of reseting, so the resulting After rebase on tarantool/master and running LuaJIT-codespell: reseting ==> resetting > +-- string contains one more symbol. > + > +-- XXX: Use 5 iterations to run variant part of the loop. > +for _ = 1, 5 do > + result = prefix .. 'a' > + -- We need a non-constant string to be appended to prevent more > + -- aggressive optimizations. Use an empty string for > + -- convenience. Also, use a constant string in the first operand > + -- in the concatenation operator for more readable `jit.dump` > + -- output. > + prefix = 'Lu' .. EMPTY_STR > +end > + > +test:is(result, 'Lua', 'skipped BUFPUT APPEND optimization for PHIs') > + > +test:done(true)