ravi
ravi copied to clipboard
Implement escape analysis to improve performance
By doing escape analysis it should be possible to determine if a local variable can escape from a function. This would allow variables that are of primitive type to use the JIT function stack rather than Lua stack.
As per Roberto - only possible way for a variable to escape is via closures being created. So we need to scan the up-values of a closure to determine if any locals are escaping. All up-values get flattened (i.e. any up-values up the chain gets referenced in the closure being created) - so searching the closure's list of up-values is enough.
Example:
> function x()
>> local i: integer
>> function y()
>> local j: integer
>> function z()
>> return i+j
>> end
>> end
>> end
> ravi.dumplua(x)
function <stdin:1,9> (5 instructions at 0x7ffc4500fac0)
0 params, 2 slots, 1 upvalue, 1 local, 1 constant, 1 function
1 [2] LOADNIL 0 0
2 [2] LOADIZ 0
3 [8] CLOSURE 1 0 ; 0x7ffc4500fdb0
4 [3] SETTABUP 0 -1 1 ; _ENV "y"
5 [9] RETURN 0 1
constants (1) for 0x7ffc4500fac0:
1 "y"
locals (1) for 0x7ffc4500fac0:
0 i 3 6
upvalues (1) for 0x7ffc4500fac0:
0 _ENV 0 0
function <stdin:3,8> (5 instructions at 0x7ffc4500fdb0)
0 params, 2 slots, 2 upvalues, 1 local, 1 constant, 1 function
1 [4] LOADNIL 0 0
2 [4] LOADIZ 0
3 [7] CLOSURE 1 0 ; 0x7ffc4500ff60
4 [5] SETTABUP 0 -1 1 ; _ENV "z"
5 [8] RETURN 0 1
constants (1) for 0x7ffc4500fdb0:
1 "z"
locals (1) for 0x7ffc4500fdb0:
0 j 3 6
upvalues (2) for 0x7ffc4500fdb0:
0 _ENV 0 0
1 i 1 0
function <stdin:5,7> (5 instructions at 0x7ffc4500ff60)
0 params, 2 slots, 2 upvalues, 0 locals, 0 constants, 0 functions
1 [6] GETUPVAL 0 0 ; i
2 [6] GETUPVAL 1 1 ; j
3 [6] ADDII 0 0 1
4 [6] RETURN 0 2
5 [7] RETURN 0 1
constants (0) for 0x7ffc4500ff60:
locals (0) for 0x7ffc4500ff60:
upvalues (2) for 0x7ffc4500ff60:
0 i 0 1
1 j 1 0
>