Constant-folding and dead-code elimination are not working correctly on agni
Tapple Gao
The compiler on agni is not doing as much constant-folding and dead-code elimination as the CLI tools. Take this script:
print(gcinfo())
local a = "a" .."a".."a".."a".."a".."a".."a".."a".."a".."a"
.."a".."a".."a".."a".."a".."a".."a".."a".."a".."a".."a"
print(gcinfo())
Variable
a
should be constant-folded away, then eliminated as dead code, as it is unusedOn simulator version Luau 2026-01-30.21516666911 , this prints
[4:29 PM] Object: 1.4892578125
[4:29 PM] Object: 1.525390625
The two numbers are different, so something entered memory in-between the two gcinfo calls
However, when run locally, it prints:
$ ./slua memtest.luau
38
38
The two numbers are the same, indicating nothing happened in memory in-between the two calls.
Looking at the bytecode compiled locally:
$ ./slua-compile memtest.luau
Function 0 (??):
1: print(gcinfo())
GETIMPORT R0 1 [print]
GETIMPORT R1 3 [gcinfo]
CALL R1 0 -1
CALL R0 -1 0
4: print(gcinfo())
GETIMPORT R0 1 [print]
GETIMPORT R1 3 [gcinfo]
CALL R1 0 -1
CALL R0 -1 0
5:
RETURN R0 0
nothing at all happens in between the two print statements. No allocation, no string concatenation, nothing
I can't grab the bytecode for the simulator-compiled script, but it's clearly doing something differently
Log In
WolfGang Senizen
The optimizer is enabled and does "work" it seems that some optimizations themselves are not being performed.
--!optimize 0
local a = false
if a then
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAA")
end
print(ll.GetUsedMemory())
Prints
1453
--!optimize 1
local a = false
if a then
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAA")
end
print(ll.GetUsedMemory())
Prints
1390
Not having the optimize directive comment also prints
1390
implying the default is at level 1However some optimizations do not seem to be occuring, for instance
local a = "a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
.."a"
print(ll.GetUsedMemory())
Prints
1541
, regardless of optimization level.however compiling this with slua locally using the binaries the
a
variable is removed.