Skip to content

Commit c136076

Browse files
Try allowing globals to be imported in ctor eval
1 parent 7c6eb7c commit c136076

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/tools/wasm-ctor-eval.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ class EvallingImportResolver : public ImportResolver {
7373
public:
7474
EvallingImportResolver() = default;
7575

76+
// We throw FailToEvalException on reading these. Provide a stub value that
77+
// passes import validation.
7678
Literals* getGlobalOrNull(ImportNames name, Type type) const override {
77-
throw FailToEvalException("Accessed imported global");
79+
auto [it, _] = stubLiterals.try_emplace(type, Literals({Literal(type)}));
80+
return &it->second;
7881
}
82+
83+
private:
84+
mutable std::unordered_map<Type, Literals> stubLiterals;
7985
};
8086

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

559565
for (auto& oldGlobal : oldGlobals) {
566+
if (oldGlobal->imported()) {
567+
continue;
568+
}
560569
// Serialize the global's value. While doing so, pass in the name of this
561570
// global, as we may be able to reuse the global as the defining global
562571
// for the value. See getSerialization() for more details.
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
(module
2+
;; an imported global that isn't accessed doesn't stop us from optimizing
23
(import "import" "global" (global $imported i32))
3-
(func $test1 (export "test1")
4-
;; This should be safe to eval in theory, but the imported global stops us,
5-
;; so this function will not be optimized out.
6-
;; TODO: perhaps if we never use that global that is ok?
4+
(global $g (mut i32) (i32.const 0))
5+
(func $setg (export "setg")
6+
(drop (i32.const 1))
7+
(global.set $g
8+
(i32.add (i32.const 1) (i32.const 2))
9+
)
10+
)
11+
12+
(func $keepalive (export "keepalive") (result i32)
13+
;; Keep the global alive so we can see its value.
14+
(global.get $g)
715
)
816
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test1
1+
setg
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(module
2-
(type $0 (func))
3-
(export "test1" (func $test1))
4-
(func $test1 (type $0)
5-
(nop)
2+
(type $0 (func (result i32)))
3+
(global $g (mut i32) (i32.const 3))
4+
(export "keepalive" (func $keepalive))
5+
(func $keepalive (type $0) (result i32)
6+
(global.get $g)
67
)
78
)

0 commit comments

Comments
 (0)