Skip to content
Draft
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
11 changes: 10 additions & 1 deletion src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ class EvallingImportResolver : public ImportResolver {
public:
EvallingImportResolver() = default;

// We throw FailToEvalException on reading these. Provide a stub value that
// passes import validation.
Literals* getGlobalOrNull(ImportNames name, Type type) const override {
throw FailToEvalException("Accessed imported global");
auto [it, _] = stubLiterals.try_emplace(type, Literals({Literal(type)}));
return &it->second;
}

private:
mutable std::unordered_map<Type, Literals> stubLiterals;
};

class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
Expand Down Expand Up @@ -557,6 +563,9 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
wasm->updateMaps();

for (auto& oldGlobal : oldGlobals) {
if (oldGlobal->imported()) {
continue;
}
// Serialize the global's value. While doing so, pass in the name of this
// global, as we may be able to reuse the global as the defining global
// for the value. See getSerialization() for more details.
Expand Down
16 changes: 12 additions & 4 deletions test/ctor-eval/global-get-init.wast
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
(module
;; an imported global that isn't accessed doesn't stop us from optimizing
(import "import" "global" (global $imported i32))
(func $test1 (export "test1")
;; This should be safe to eval in theory, but the imported global stops us,
;; so this function will not be optimized out.
;; TODO: perhaps if we never use that global that is ok?
(global $g (mut i32) (i32.const 0))
(func $setg (export "setg")
(drop (i32.const 1))
(global.set $g
(i32.add (i32.const 1) (i32.const 2))
)
)

(func $keepalive (export "keepalive") (result i32)
;; Keep the global alive so we can see its value.
(global.get $g)
)
)
2 changes: 1 addition & 1 deletion test/ctor-eval/global-get-init.wast.ctors
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test1
setg
9 changes: 5 additions & 4 deletions test/ctor-eval/global-get-init.wast.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(module
(type $0 (func))
(export "test1" (func $test1))
(func $test1 (type $0)
(nop)
(type $0 (func (result i32)))
(global $g (mut i32) (i32.const 3))
(export "keepalive" (func $keepalive))
(func $keepalive (type $0) (result i32)
(global.get $g)
)
)
Loading