summaryrefslogtreecommitdiff
path: root/src/passes
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes')
-rw-r--r--src/passes/Metrics.cpp1
-rw-r--r--src/passes/MinifyImportsAndExports.cpp1
-rw-r--r--src/passes/Print.cpp26
-rw-r--r--src/passes/RemoveUnusedModuleElements.cpp33
4 files changed, 54 insertions, 7 deletions
diff --git a/src/passes/Metrics.cpp b/src/passes/Metrics.cpp
index af5a62697..a408ccf95 100644
--- a/src/passes/Metrics.cpp
+++ b/src/passes/Metrics.cpp
@@ -66,6 +66,7 @@ struct Metrics
counts["[imports]"] = imports.getNumImports();
counts["[funcs]"] = imports.getNumDefinedFunctions();
counts["[globals]"] = imports.getNumDefinedGlobals();
+ counts["[events]"] = imports.getNumDefinedEvents();
counts["[exports]"] = module->exports.size();
// add memory and table
if (module->memory.exists) {
diff --git a/src/passes/MinifyImportsAndExports.cpp b/src/passes/MinifyImportsAndExports.cpp
index 23dd2a21a..043cfb588 100644
--- a/src/passes/MinifyImportsAndExports.cpp
+++ b/src/passes/MinifyImportsAndExports.cpp
@@ -159,6 +159,7 @@ private:
};
ModuleUtils::iterImportedGlobals(*module, processImport);
ModuleUtils::iterImportedFunctions(*module, processImport);
+ ModuleUtils::iterImportedEvents(*module, processImport);
if (minifyExports) {
// Minify the exported names.
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index fb85023f0..9dbf3cc1f 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -1660,6 +1660,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
case ExternalKind::Global:
o << "global";
break;
+ case ExternalKind::Event:
+ o << "event";
+ break;
case ExternalKind::Invalid:
WASM_UNREACHABLE();
}
@@ -1805,6 +1808,25 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
o << maybeNewLine;
}
+ void visitEvent(Event* curr) {
+ doIndent(o, indent);
+ if (curr->imported()) {
+ o << '(';
+ emitImportHeader(curr);
+ }
+ o << "(event ";
+ printName(curr->name, o);
+ o << maybeSpace << "(attr " << curr->attribute << ')' << maybeSpace << '(';
+ printMinor(o, "param");
+ for (auto& param : curr->params) {
+ o << ' ' << printType(param);
+ }
+ o << "))";
+ if (curr->imported()) {
+ o << ')';
+ }
+ o << maybeNewLine;
+ }
void printTableHeader(Table* curr) {
o << '(';
printMedium(o, "table") << ' ';
@@ -1948,12 +1970,16 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
*curr, [&](Global* global) { visitGlobal(global); });
ModuleUtils::iterImportedFunctions(
*curr, [&](Function* func) { visitFunction(func); });
+ ModuleUtils::iterImportedEvents(*curr,
+ [&](Event* event) { visitEvent(event); });
ModuleUtils::iterDefinedMemories(
*curr, [&](Memory* memory) { visitMemory(memory); });
ModuleUtils::iterDefinedTables(*curr,
[&](Table* table) { visitTable(table); });
ModuleUtils::iterDefinedGlobals(
*curr, [&](Global* global) { visitGlobal(global); });
+ ModuleUtils::iterDefinedEvents(*curr,
+ [&](Event* event) { visitEvent(event); });
for (auto& child : curr->exports) {
doIndent(o, indent);
visitExport(child.get());
diff --git a/src/passes/RemoveUnusedModuleElements.cpp b/src/passes/RemoveUnusedModuleElements.cpp
index c96ce1a8c..20c30d271 100644
--- a/src/passes/RemoveUnusedModuleElements.cpp
+++ b/src/passes/RemoveUnusedModuleElements.cpp
@@ -15,9 +15,9 @@
*/
//
-// Removes module elements that are are never used: functions and globals,
-// which may be imported or not, and function types (which we merge
-// and remove if unneeded)
+// Removes module elements that are are never used: functions, globals, and
+// events, which may be imported or not, and function types (which we merge and
+// remove if unneeded)
//
#include <memory>
@@ -30,7 +30,7 @@
namespace wasm {
-enum class ModuleElementKind { Function, Global };
+enum class ModuleElementKind { Function, Global, Event };
typedef std::pair<ModuleElementKind, Name> ModuleElement;
@@ -68,7 +68,7 @@ struct ReachabilityAnalyzer : public PostWalker<ReachabilityAnalyzer> {
if (!func->imported()) {
walk(func->body);
}
- } else {
+ } else if (curr.first == ModuleElementKind::Global) {
// if not imported, it has an init expression we need to walk
auto* global = module->getGlobal(curr.second);
if (!global->imported()) {
@@ -122,6 +122,7 @@ struct ReachabilityAnalyzer : public PostWalker<ReachabilityAnalyzer> {
struct FunctionTypeAnalyzer : public PostWalker<FunctionTypeAnalyzer> {
std::vector<Function*> functions;
std::vector<CallIndirect*> indirectCalls;
+ std::vector<Event*> events;
void visitFunction(Function* curr) {
if (curr->type.is()) {
@@ -129,6 +130,8 @@ struct FunctionTypeAnalyzer : public PostWalker<FunctionTypeAnalyzer> {
}
}
+ void visitEvent(Event* curr) { events.push_back(curr); }
+
void visitCallIndirect(CallIndirect* curr) { indirectCalls.push_back(curr); }
};
@@ -139,11 +142,11 @@ struct RemoveUnusedModuleElements : public Pass {
: rootAllFunctions(rootAllFunctions) {}
void run(PassRunner* runner, Module* module) override {
- optimizeGlobalsAndFunctions(module);
+ optimizeGlobalsAndFunctionsAndEvents(module);
optimizeFunctionTypes(module);
}
- void optimizeGlobalsAndFunctions(Module* module) {
+ void optimizeGlobalsAndFunctionsAndEvents(Module* module) {
std::vector<ModuleElement> roots;
// Module start is a root.
if (module->start.is()) {
@@ -169,6 +172,8 @@ struct RemoveUnusedModuleElements : public Pass {
roots.emplace_back(ModuleElementKind::Function, curr->value);
} else if (curr->kind == ExternalKind::Global) {
roots.emplace_back(ModuleElementKind::Global, curr->value);
+ } else if (curr->kind == ExternalKind::Event) {
+ roots.emplace_back(ModuleElementKind::Event, curr->value);
} else if (curr->kind == ExternalKind::Memory) {
exportsMemory = true;
} else if (curr->kind == ExternalKind::Table) {
@@ -215,6 +220,17 @@ struct RemoveUnusedModuleElements : public Pass {
}),
v.end());
}
+ {
+ auto& v = module->events;
+ v.erase(std::remove_if(v.begin(),
+ v.end(),
+ [&](const std::unique_ptr<Event>& curr) {
+ return analyzer.reachable.count(
+ ModuleElement(ModuleElementKind::Event,
+ curr->name)) == 0;
+ }),
+ v.end());
+ }
module->updateMaps();
// Handle the memory and table
if (!exportsMemory && !analyzer.usesMemory) {
@@ -272,6 +288,9 @@ struct RemoveUnusedModuleElements : public Pass {
for (auto* call : analyzer.indirectCalls) {
call->fullType = canonicalize(call->fullType);
}
+ for (auto* event : analyzer.events) {
+ event->type = canonicalize(event->type);
+ }
// remove no-longer used types
module->functionTypes.erase(
std::remove_if(module->functionTypes.begin(),