Skip to content
Open
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: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ Current Trunk
-------------

- Add support for non-nullable table types and initialization expressions for
tables. This comes with a breaking change to C API: `BinaryenAddTable` takes
an additional `BinaryenExpressionRef` parameter to provide an initialization
expression. This may be set to NULL for tables without an initializer. In JS
this parameter is optional and so is not breaking. (#8405)
tables. This comes with a breaking change to C API: `BinaryenAddTable` takes
an additional `BinaryenExpressionRef` parameter to provide an initialization
expression. This may be set to NULL for tables without an initializer. In JS
this parameter is optional and so is not breaking. (#8405)
- MinifyImportsAndExports now has a new output format using JSON. This was
changed while fixing bugs with colliding module names (to avoid two breaking
changes to the output). (#8550)

v128
----
Expand Down
54 changes: 45 additions & 9 deletions src/passes/MinifyImportsAndExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <ir/names.h>
#include <pass.h>
#include <shared-constants.h>
#include <support/string.h>
#include <wasm.h>

namespace wasm {
Expand All @@ -62,14 +63,19 @@ struct MinifyImportsAndExports : public Pass {
void run(Module* module) override {
// Minify the imported names.
Names::MinifiedNameGenerator names;
std::map<Name, Name> oldToNew;
std::map<Name, Name> newToOld;
auto process = [&](Name& name) {
auto iter = oldToNew.find(name);
// Use a key of (module, base) for the old values, to handle colliding
// basenames between modules.
std::map<std::pair<Name, Name>, Name> oldToNew;
std::map<Name, std::pair<Name, Name>> newToOld;
// Process a name. This can be the basename of an import, or the full name
// of an export. The module name is only used for imports.
auto process = [&](Name& name, Name module = Name()) {
std::pair<Name, Name> key(module, name);
auto iter = oldToNew.find(key);
if (iter == oldToNew.end()) {
auto newName = names.getName();
oldToNew[name] = newName;
newToOld[newName] = name;
oldToNew[key] = newName;
newToOld[newName] = key;
name = newName;
} else {
name = iter->second;
Expand All @@ -82,7 +88,7 @@ struct MinifyImportsAndExports : public Pass {
// and wasi, but not custom user things.
if (minifyModules || curr->module == ENV ||
curr->module.startsWith("wasi_")) {
process(curr->base);
process(curr->base, curr->module);
}
});

Expand All @@ -93,10 +99,40 @@ struct MinifyImportsAndExports : public Pass {
}
}
module->updateMaps();

// Emit the mapping.
for (auto& [new_, old] : newToOld) {
std::cout << old.str << " => " << new_.str << '\n';
std::cout << "{\n";
std::cout << " \"imports\": [";
bool first = true;
for (auto& [new_, key] : newToOld) {
if (key.first) {
if (first) {
first = false;
} else {
std::cout << ',';
}
std::cout << "\n [";
String::printEscaped(std::cout, key.first.str) << ", ";
String::printEscaped(std::cout, key.second.str) << ", ";
String::printEscaped(std::cout, new_.str) << "]";
}
}
std::cout << "\n ],\n\"exports\": [";
first = true;
for (auto& [new_, key] : newToOld) {
if (!key.first) {
if (first) {
first = false;
} else {
std::cout << ',';
}
std::cout << "\n [";
String::printEscaped(std::cout, key.second.str) << ", ";
String::printEscaped(std::cout, new_.str) << "]";
}
}
std::cout << "\n ]\n";
std::cout << "}\n";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does binaryen write json elsewhere? If so mabye we can make utilities for dumping this kind of data?


if (minifyModules) {
doMinifyModules(module);
Expand Down
22 changes: 15 additions & 7 deletions test/passes/minify-imports-and-exports-and-modules.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
memory => a
table => b
longname1 => c
longname2 => d
longname3 => e
longname4 => f
{
"imports": [
["env", "memory", "a"],
["env", "table", "b"],
["env", "longname1", "c"],
["env", "longname2", "d"],
["env", "longname3", "e"],
["other", "longname3", "f"],
["other", "longname4", "g"]
],
"exports": [
]
}
(module
(type $0 (func))
(import "a" "a" (memory $0 256 256))
(import "a" "b" (table $0 4 funcref))
(import "a" "c" (func $internal1))
(import "a" "d" (func $internal2))
(import "a" "e" (func $internal3))
(import "a" "f" (func $internal4))
(import "a" "f" (func $internal3b))
(import "a" "g" (func $internal4))
)
13 changes: 8 additions & 5 deletions test/passes/minify-imports-and-exports-and-modules.wast
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(module
(import "env" "memory" (memory $0 256 256))
(import "env" "table" (table $0 4 funcref))
(import "env" "longname1" (func $internal1))
(import "env" "longname2" (func $internal2))
(import "env" "longname3" (func $internal3))
(import "env" "memory" (memory $0 256 256))
(import "env" "table" (table $0 4 funcref))
(import "env" "longname1" (func $internal1))
(import "env" "longname2" (func $internal2))

(import "env" "longname3" (func $internal3)) ;; \ note overlap
(import "other" "longname3" (func $internal3b)) ;; / in basename

(import "other" "longname4" (func $internal4))
)
Loading
Loading