Re: confused about down-recursion

  • From: "Dario d'Andrea" <dariodandrea1995@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 4 Jul 2019 12:10:14 +0200

Hi Tweyseo,
did you manage to understand how LuaJIT behaves with down-recursion? Could
you explain it here (or is there anyone that can explain it)?

to answer your question:
- "And another confusion was why there was 3 different guards for "if v ==
0" ?"
my guess is that it is because there is a call unroll factor of 3, hence
the code (including guards) is replicated 3 times in the trace. See default
parameters at lj_jit.h.



Cheers,
Dario




Il giorno mar 30 apr 2019 alle ore 05:36 twey seo <tweyseo@xxxxxxx> ha
scritto:

I found the case for down-recursion:

require("jit.dump").on("six", "t1.dlog")--require("jit.v").on("t1.log")--
---
local function f1(v)
    if v == 0 then return 0 end
    f1(v - 1)
end

for i = 1, 40 do
    f1(i)
end


v.log:

[TRACE   1 t1.lua:3 up-recursion]
[TRACE --- (1/3) t1.lua:4 -- down-recursion, restarting at t1.lua:6]
[TRACE   2 t1.lua:6 down-recursion]


dump.log:

---- TRACE 1 start t1.lua:3
---- TRACE 1 IR
....        SNAP   #0   [ ---- ---- ]
0001 >  num SLOAD  #1    T
....        SNAP   #1   [ ---- ---- ]
0002 >  num NE     0001  +0
....        SNAP   #2   [ ---- ---- ]
0003    fun SLOAD  #0    R
0004 >  fun EQ     0003  t1.lua:3
0005    num SUB    0001  +1
....        SNAP   #3   [ t1.lua:3|---- t1.lua:3|---- ]
0006 >  num NE     0005  +0
0007    num SUB    0005  +1
....        SNAP   #4   [ t1.lua:3|---- t1.lua:3|0005 t1.lua:3|---- ]
0008 >  num NE     0007  +0
0009    num SUB    0007  +1
....        SNAP   #5   [ t1.lua:3|---- t1.lua:3|0005 t1.lua:3|0007
t1.lua:3|0009 ]
---- TRACE 1 stop -> up-recursion

---- TRACE 1 exit 3
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 2 start 1/3 t1.lua:4
---- TRACE 2 abort t1.lua:6 -- down-recursion, restarting

---- TRACE 2 start t1.lua:6
---- TRACE 2 IR
....        SNAP   #0   [ ---- ]
....        SNAP   #1   [ ---- ]
0002 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #2   [ ---- ---- ]
0003 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #3   [ ---- ---- ]
0004 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #4   [ ---- ]
---- TRACE 2 stop -> down-recursion

---- TRACE 2 exit 3


But i was confused with this behavior, someone who can explain it for me?
And another confusion was why there was 3 different guards for "if v == 0"
?



-- 
*Dario d'Andrea [image: https://www.linkedin.com/in/dariodandrea/]
<https://www.linkedin.com/in/dariodandrea/>*
MSc Computer Engineering
+39 3391138471

Other related posts: