Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: GETIMPORT constant pre-process #42

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions Source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ local function luau_newsettings()
errorHandling = true,
generalizedIteration = true,
allowProxyErrors = false,
}
useImportConstants = false,
staticEnvironment = {},
}
end

local function luau_validatesettings(luau_settings)
Expand All @@ -172,6 +174,26 @@ local function luau_validatesettings(luau_settings)
assert(type(luau_settings.errorHandling) == "boolean", "luau_settings.errorHandling should be a boolean")
assert(type(luau_settings.generalizedIteration) == "boolean", "luau_settings.generalizedIteration should be a boolean")
assert(type(luau_settings.allowProxyErrors) == "boolean", "luau_settings.allowProxyErrors should be a boolean")
assert(type(luau_settings.staticEnvironment) == "table", "luau_settings.staticEnvironment should be a table")
assert(type(luau_settings.useImportConstants) == "boolean", "luau_settings.useImportConstants should be a boolean")
end

local function assertImport(import, message)
if import == nil then
error(message, 0)
end
return import
end

local function resolveImportConstant(static, count, k0, k1, k2)
if count == 1 then
return assertImport(static[k0], `Could not resolve import constant: {k0}\nMake sure the import is defined in staticEnvironment.`)
elseif count == 2 then
return assertImport(static[k0][k1], `Could not resolve import constant: {k0}.{k1}\nMake sure the import is defined in staticEnvironment.`)
elseif count == 3 then
return assertImport(static[k0][k1][k2], `Could not resolve import constant: {k0}.{k1}.{k2}\nMake sure the import is defined in staticEnvironment.`)
end
return error(`Could not resolve import constant, unknown size: {count}`)
end

local function luau_deserialize(bytecode, luau_settings)
Expand Down Expand Up @@ -327,6 +349,12 @@ local function luau_deserialize(bytecode, luau_settings)
inst.K1 = k[id1 + 1]
inst.K2 = k[id2 + 1]
end
if luau_settings.useImportConstants then
inst.K = resolveImportConstant(
luau_settings.staticEnvironment,
count, inst.K0, inst.K1, inst.K2
)
end
elseif kmode == 5 then --// AUX boolean low 1 bit
inst.K = bit32_extract(inst.aux, 0, 1) == 1
inst.KN = bit32_extract(inst.aux, 31, 1) == 1
Expand Down Expand Up @@ -642,16 +670,19 @@ local function luau_load(module, env, luau_settings)
end
end
elseif op == 12 then --[[ GETIMPORT ]]
local count = inst.KC
local k0 = inst.K0
local import = extensions[k0] or env[k0]

if count == 1 then
stack[inst.A] = import
elseif count == 2 then
stack[inst.A] = import[inst.K1]
elseif count == 3 then
stack[inst.A] = import[inst.K1][inst.K2]
if luau_settings.useImportConstants then
stack[inst.A] = inst.K
else
local count = inst.KC
local k0 = inst.K0
local import = extensions[k0] or env[k0]
if count == 1 then
stack[inst.A] = import
elseif count == 2 then
stack[inst.A] = import[inst.K1]
elseif count == 3 then
stack[inst.A] = import[inst.K1][inst.K2]
end
end

pc += 1 --// adjust for aux
Expand Down
1 change: 1 addition & 0 deletions tests/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ FIU_TESTCASES {
"Specs/extensions",
"Specs/nativeNamecall",
"Specs/vectorLib",
"Specs/importConstants",
}},
{"Benchmarks", {
// Fiu Benchmark Tests
Expand Down
64 changes: 64 additions & 0 deletions tests/Specs/importConstants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--!ctx Luau

local ok, compileResult = Luau.compile([[
local a = A
local b = Lib.B
local c = Lib.List.C

assert(a() == "A")
assert(b() == "Lib.B")
assert(c() == "Lib.List.C")
]], {
optimizationLevel = 2,
debugLevel = 2,
coverageLevel = 0,
vectorLib = nil,
vectorCtor = nil,
vectorType = nil
})

if not ok then
error(compileResult)
end

local settings = Fiu.luau_newsettings()

local calledConstants = {}

settings.useImportConstants = true
settings.staticEnvironment = {
assert = assert,
A = function()
calledConstants.A = true
return "A"
end,
Lib = {
B = function()
calledConstants.B = true
return "Lib.B"
end,
List = {
C = function()
calledConstants.C = true
return "Lib.List.C"
end
}
}
}

local func, _ = Fiu.luau_load(Fiu.luau_deserialize(compileResult, settings), {}, settings)

func()

assert(calledConstants.A, "A was not called")
assert(calledConstants.B, "B was not called")
assert(calledConstants.C, "C was not called")

settings.staticEnvironment = {}

local success, message = pcall(Fiu.luau_deserialize, compileResult, settings)

assert(success == false, "luau_deserialize should have errored")
assert(MATCH(message, "Could not resolve import constant: A\nMake sure the import is defined in staticEnvironment."))

OK()
Loading