[Tarantool-patches] [PATCH luajit] Actually implement maxirconst trace limit.

Sergey Kaplun skaplun at tarantool.org
Wed Dec 29 15:27:31 MSK 2021


From: Mike Pall <mike>

Suggested by spacewander.

(cherry picked from 9ff94c4a1fcec2c310dcb092da694f23186e23)

`maxirconst` should restrict the amount of IR constants for 1 trace.
Nevertheless, its value isn't checked anywhere.

This patch adds the corresponding check after instruction recording.

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

Part of tarantool/tarantool#6548
---

Issue: https://github.com/LuaJIT/LuaJIT/issues/430
Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-430-maxirconst-actually-implement-full-ci
Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/lj-430-maxirconst-actually-implement-full-ci

 src/lj_record.c                               |  5 ++-
 .../lj-430-maxirconst.test.lua                | 43 +++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 test/tarantool-tests/lj-430-maxirconst.test.lua

diff --git a/src/lj_record.c b/src/lj_record.c
index 42af09e5..f839ade2 100644
--- a/src/lj_record.c
+++ b/src/lj_record.c
@@ -2470,8 +2470,9 @@ void lj_record_ins(jit_State *J)
 #undef rbv
 #undef rcv
 
-  /* Limit the number of recorded IR instructions. */
-  if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord])
+  /* Limit the number of recorded IR instructions and constants. */
+  if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord] ||
+      J->cur.nk < REF_BIAS-(IRRef)J->param[JIT_P_maxirconst])
     lj_trace_err(J, LJ_TRERR_TRACEOV);
 }
 
diff --git a/test/tarantool-tests/lj-430-maxirconst.test.lua b/test/tarantool-tests/lj-430-maxirconst.test.lua
new file mode 100644
index 00000000..1829b37d
--- /dev/null
+++ b/test/tarantool-tests/lj-430-maxirconst.test.lua
@@ -0,0 +1,43 @@
+-- XXX: avoid any other traces compilation due to hotcount
+-- collisions for predictible results.
+jit.off()
+jit.flush()
+
+-- Disabled on *BSD due to #4819.
+require('utils').skipcond(jit.os == 'BSD', 'Disabled due to #4819')
+
+local tap = require('tap')
+
+local test = tap.test('lj-430-maxirconst')
+test:plan(2)
+
+-- XXX: trace always has at least 3 IR constants: for nil, false
+-- and true.
+jit.opt.start('hotloop=1', 'maxirconst=3')
+
+-- This function has only 3 IR constant.
+local function irconst3()
+end
+
+-- This function has 4 IR constants before optimizations.
+local function irconst4()
+  local _ = 42
+end
+
+local ntrace_old = misc.getmetrics().jit_trace_num
+jit.on()
+irconst3()
+irconst3()
+jit.off()
+test:ok(ntrace_old + 1 == misc.getmetrics().jit_trace_num,
+	'trace number increases')
+
+ntrace_old = misc.getmetrics().jit_trace_num
+jit.on()
+irconst4()
+irconst4()
+jit.off()
+test:ok(ntrace_old == misc.getmetrics().jit_trace_num,
+	'trace number is the same')
+
+os.exit(test:check() and 0 or 1)
-- 
2.34.1



More information about the Tarantool-patches mailing list