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
17 changes: 17 additions & 0 deletions src/ir/import-name.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@

#include <ostream>

#include "support/hash.h"
#include "support/name.h"

namespace wasm {

struct ImportNames {
Name module;
Name name;

bool operator==(const ImportNames& other) const {
return module == other.module && name == other.name;
}
};

} // namespace wasm

namespace std {

template<> struct hash<wasm::ImportNames> {
std::size_t operator()(const wasm::ImportNames& importNames) const noexcept {
size_t val = hash<wasm::Name>{}(importNames.module);
wasm::rehash(val, importNames.name);
return val;
}
};

} // namespace std

#endif // wasm_ir_import_name_h
12 changes: 10 additions & 2 deletions src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,22 @@ class EvallingImportResolver : public ImportResolver {
throw FailToEvalException{"Imported table access."};
}

// We assume that each tag import is distinct. This is wrong if the same tag
// instantiation is imported twice with different import names.
Tag* getTagOrNull(ImportNames name,
const Signature& signature) const override {
Fatal() << "getTagOrNull not implemented in ctor-eval.";
return nullptr;
auto [it, inserted] = importedTags.try_emplace(name, Tag{});
if (inserted) {
auto& tag = it->second;
tag.type = HeapType(signature);
}

return &it->second;
}

private:
mutable Literals stubLiteral;
mutable std::unordered_map<ImportNames, Tag> importedTags;
};

class EvallingRuntimeTable : public RuntimeTable {
Expand Down
16 changes: 16 additions & 0 deletions test/ctor-eval/tag-import.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(module
;; an imported tag that isn't accessed doesn't stop us from optimizing
(import "import" "tag" (tag $imported))
(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)
)
)
1 change: 1 addition & 0 deletions test/ctor-eval/tag-import.wast.ctors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setg
8 changes: 8 additions & 0 deletions test/ctor-eval/tag-import.wast.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(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