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 68A5999FA18; Wed, 24 Jan 2024 17:15:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 68A5999FA18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1706105756; bh=bLWMAbAeIVFzNEwqp5QmCjw6O4vwLhMmHDXHKGPmusc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=J4UNjOzoi7JSamCfsqdWXC9OLGvhyP9mDP76YsM1aO4EGAI5mNQcQmwbINEm2uUXP H4D+15nZTFvpiQvhu9+XeibunuFrYKNRNDLiPxjkVXCrbITZmIbClkQIq5vcOHxe2h CZhbqOghlHqq3r0qHWe8ALSvS3mC7Kl3CGvWepqo= Received: from smtp40.i.mail.ru (smtp40.i.mail.ru [95.163.41.81]) (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 3F5C832B7C1 for ; Wed, 24 Jan 2024 17:15:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3F5C832B7C1 Received: by smtp40.i.mail.ru with esmtpa (envelope-from ) id 1rSe2G-00F816-1Z; Wed, 24 Jan 2024 17:15:24 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Wed, 24 Jan 2024 17:11:08 +0300 Message-ID: <8abb9f75b9f4b8459a6495fedb40da7da5334c9c.1706104777.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD96A58C36AA2E996497FC6886252B15AA45B61F22128DB277C182A05F538085040E8EC40A4F5AF129AE033E4A99736FD6F19596D0670BA5497DBBF9DFD3A90B350 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE74E2C4641A2CB07F2EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637F4EDC07301919F278638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8A73762C2E1646E0A41CB11056A6D8E63117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC292D688DDAD4E7BC389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC821E93C0F2A571C7BF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C643FE6A0CAC512C72D242C3BD2E3F4C64AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C3E4A64BF2429643C8BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CFED8438A78DFE0A9E1DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C361DD96311B40C2D435872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-C1DE0DAB: 0D63561A33F958A552F19E98A9FBB3E59CA07F7D11ADFBAABC9D70AD38F4B40CF87CCE6106E1FC07E67D4AC08A07B9B0D23BF7408B3F90229C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFD1FA3E906B65079DFAAFB14219CEA484024882C4526887A321892E6856F1CAD245F8ED8E1771A93D4BEAD515961AA0A7CFAA700343C5BFAD927B7AFBAAFA7464A74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojjPo90oGNxwJ0Mg6f37szRw== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A76971D968936EDA4AA2E033E4A99736FD6F6ECF975C7BB2667FDEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit 1/2] Optimize table.new() with constant args to (sinkable) IR_TNEW. 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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Thanks to Peter Cawley. (cherry picked from commit d1236a4caa999b29e774ef5103df3b424d821d9b) This patch adds optimization for calls of `table.new()` with constant argument refs when asize is in the proper range (i.e., is less than IR operand width). The call is replaced with IR TNEW. This opens up opportunities for other optimizations in the pipeline. Sergey Kaplun: * added the description and the test for the feature Part of tarantool/tarantool#9595 --- src/lj_ffrecord.c | 9 ++ .../lj-1128-table-new-opt-tnew.test.lua | 112 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 test/tarantool-tests/lj-1128-table-new-opt-tnew.test.lua diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c index 99a6b918..7c6e3590 100644 --- a/src/lj_ffrecord.c +++ b/src/lj_ffrecord.c @@ -1065,6 +1065,15 @@ static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd) { TRef tra = lj_opt_narrow_toint(J, J->base[0]); TRef trh = lj_opt_narrow_toint(J, J->base[1]); + if (tref_isk(tra) && tref_isk(trh)) { + int32_t a = IR(tref_ref(tra))->i; + if (a < 0x7fff) { + uint32_t hbits = hsize2hbits(IR(tref_ref(trh))->i); + a = a > 0 ? a+1 : 0; + J->base[0] = emitir(IRTG(IR_TNEW, IRT_TAB), (uint32_t)a, hbits); + return; + } + } J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh); UNUSED(rd); } diff --git a/test/tarantool-tests/lj-1128-table-new-opt-tnew.test.lua b/test/tarantool-tests/lj-1128-table-new-opt-tnew.test.lua new file mode 100644 index 00000000..805e6de6 --- /dev/null +++ b/test/tarantool-tests/lj-1128-table-new-opt-tnew.test.lua @@ -0,0 +1,112 @@ +local tap = require('tap') +local test = tap.test('lj-1128-table-new-opt-tnew'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), + ['Disabled on *BSD due to #4819'] = jit.os == 'BSD', +}) + +-- Test LuaJIT optimization when a call to the `lj_tab_new_ah()` +-- is replaced with the corresponding TNEW IR. +-- See also https://github.com/LuaJIT/LuaJIT/issues/1128. + +local jparse = require('utils').jit.parse + +-- API follows the semantics of `lua_createtable()`. +local table_new = require('table.new') + +-- `hbits` for different `hsizes`, see for details. +local HBITS = { + [1] = 1, + [3] = 2, +} + +-- XXX: Avoid any other traces compilation due to hotcount +-- collisions for predictable results. +jit.off() +jit.flush() + +test:plan(10) + +jit.on() +jit.opt.start('hotloop=1') +jparse.start('i') + +local anchor + +for _ = 1, 4 do + anchor = table_new(1, 1) +end + +local traces = jparse.finish() +jit.off() + +test:ok(type(anchor) == 'table', 'base result') +test:ok(traces[1]:has_ir(('TNEW.*#2.*#%d'):format(HBITS[1])), 'base IR value') + +jit.flush() +jit.on() +-- XXX: Reset hotcounters. +jit.opt.start('hotloop=1') +jparse.start('i') + +for _ = 1, 4 do + anchor = table_new(0, 0) +end + +traces = jparse.finish() +jit.off() + +test:ok(type(anchor) == 'table', 'base result') +test:ok(traces[1]:has_ir('TNEW.*#0.*#0'), '0 asize, 0 hsize') + +jit.flush() +jit.on() +-- XXX: Reset hotcounters. +jit.opt.start('hotloop=1') +jparse.start('i') + +for _ = 1, 4 do + anchor = table_new(0, 3) +end + +traces = jparse.finish() +jit.off() + +test:ok(type(anchor) == 'table', 'base result') +test:ok(traces[1]:has_ir(('TNEW.*#0.*#%d'):format(HBITS[3])), + '3 hsize -> 2 hbits') + +jit.flush() +jit.on() +-- XXX: Reset hotcounters. +jit.opt.start('hotloop=1') +jparse.start('i') + +for _ = 1, 4 do + anchor = table_new(-1, 0) +end + +traces = jparse.finish() +jit.off() + +test:ok(type(anchor) == 'table', 'base result') +test:ok(traces[1]:has_ir('TNEW.*#0.*#0'), 'negative asize') + +jit.flush() +jit.on() +-- XXX: Reset hotcounters. +jit.opt.start('hotloop=1') +jparse.start('i') + +for _ = 1, 4 do + anchor = table_new(0xffff, 0) +end + +traces = jparse.finish() +jit.off() + +test:ok(type(anchor) == 'table', 'base result') +-- Test that TNEW isn't emitted for `asize` bigger than the IR +-- operand width (>0x8000). +test:ok(not traces[1]:has_ir('TNEW'), 'asize out of range') + +test:done(true) -- 2.43.0