Prevent the compiler from removing exported variables during optimization.
Previously any global variable that was not used within the module was
removed during optimization. This commit fixes this issue.
@@ -84,10 +84,15 @@ function constant_propagation.run(module)
-- 3) Find out which constant globals should be propagated, and which unused constant globals
-- should be simply eliminated.
+ local is_exported = {}
+ for _, g_id in ipairs(module.exported_globals) do
+ is_exported[g_id] = true
+ end
+
local n_new_globals = 0
local new_global_id = {} -- { g_id => g_id? }
for i = 1, n_globals do
- if constant_initializer[i] and (n_reads[i] == 0 or n_writes[i] == 1) then
+ if constant_initializer[i] and not is_exported[i] and (n_reads[i] == 0 or n_writes[i] == 1) then
new_global_id[i] = false
else
n_new_globals = n_new_globals + 1
@@ -95,6 +100,10 @@ function constant_propagation.run(module)
end
end
+ for i, g_id in ipairs(module.exported_globals) do
+ module.exported_globals[i] = assert(new_global_id[g_id])
+ end
+
-- 4) Propagate the constant globals, and rename the existing ones accordingly
for _, func in ipairs(module.functions) do
