Skip to content
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
13 changes: 11 additions & 2 deletions src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ class EvallingModuleRunner;

class EvallingImportResolver : public ImportResolver {
public:
EvallingImportResolver() = default;
EvallingImportResolver() : stubLiteral({Literal(0)}){};

// Return an unused stub value. We throw FailToEvalException on reading any
// imported globals. We ignore the type and return an i32 literal since some
// types can't be created anyway (e.g. ref none).
Literals* getGlobalOrNull(ImportNames name, Type type) const override {
throw FailToEvalException("Accessed imported global");
return &stubLiteral;
}

private:
mutable Literals stubLiteral;
};

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
18 changes: 13 additions & 5 deletions test/ctor-eval/global-get-init.wast
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
(module
(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?
;; an imported global that isn't accessed doesn't stop us from optimizing
(import "import" "global" (global $imported (ref i31)))
(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