Skip to content

Commit 2738190

Browse files
committed
wasm-merge: preserve explicit names
- only write explicit function names - when merging modules, the name of types, globals and tags in all modules but the first were lost
1 parent 766c029 commit 2738190

File tree

7 files changed

+46
-28
lines changed

7 files changed

+46
-28
lines changed

src/ir/module-splitting.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,9 @@ void ModuleSplitter::exportImportFunction(Name funcName) {
395395
// Import the function if it is not already imported into the secondary
396396
// module.
397397
if (secondary.getFunctionOrNull(funcName) == nullptr) {
398-
auto func =
399-
Builder::makeFunction(funcName, primary.getFunction(funcName)->type, {});
398+
auto primaryFunc = primary.getFunction(funcName);
399+
auto func = Builder::makeFunction(funcName, primaryFunc->type, {});
400+
func->hasExplicitName = primaryFunc->hasExplicitName;
400401
func->module = config.importNamespace;
401402
func->base = exportName;
402403
secondary.addFunction(std::move(func));
@@ -542,7 +543,7 @@ void ModuleSplitter::setupTablePatching() {
542543
placeholder->base = std::to_string(index);
543544
placeholder->name = Names::getValidFunctionName(
544545
primary, std::string("placeholder_") + placeholder->base.toString());
545-
placeholder->hasExplicitName = false;
546+
placeholder->hasExplicitName = true;
546547
placeholder->type = secondaryFunc->type;
547548
elem = placeholder->name;
548549
primary.addFunction(std::move(placeholder));

src/ir/module-utils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Function* copyFunction(Function* func,
4848
std::optional<std::vector<Index>> fileIndexMap) {
4949
auto ret = std::make_unique<Function>();
5050
ret->name = newName.is() ? newName : func->name;
51+
ret->hasExplicitName = func->hasExplicitName && !newName.is();
5152
ret->type = func->type;
5253
ret->vars = func->vars;
5354
ret->localNames = func->localNames;
@@ -77,6 +78,7 @@ Function* copyFunction(Function* func,
7778
Global* copyGlobal(Global* global, Module& out) {
7879
auto* ret = new Global();
7980
ret->name = global->name;
81+
ret->hasExplicitName = global->hasExplicitName;
8082
ret->type = global->type;
8183
ret->mutable_ = global->mutable_;
8284
ret->module = global->module;
@@ -93,6 +95,7 @@ Global* copyGlobal(Global* global, Module& out) {
9395
Tag* copyTag(Tag* tag, Module& out) {
9496
auto* ret = new Tag();
9597
ret->name = tag->name;
98+
ret->hasExplicitName = tag->hasExplicitName;
9699
ret->sig = tag->sig;
97100
ret->module = tag->module;
98101
ret->base = tag->base;
@@ -209,6 +212,12 @@ void copyModuleItems(const Module& in, Module& out) {
209212
for (auto& curr : in.dataSegments) {
210213
copyDataSegment(curr.get(), out);
211214
}
215+
216+
for (auto& [type, names] : in.typeNames) {
217+
if (!out.typeNames.count(type)) {
218+
out.typeNames[type] = names;
219+
}
220+
}
212221
}
213222

214223
void copyModule(const Module& in, Module& out) {
@@ -222,7 +231,6 @@ void copyModule(const Module& in, Module& out) {
222231
out.customSections = in.customSections;
223232
out.debugInfoFileNames = in.debugInfoFileNames;
224233
out.features = in.features;
225-
out.typeNames = in.typeNames;
226234
}
227235

228236
void clearModule(Module& wasm) {

src/wasm/wasm-binary.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -864,19 +864,27 @@ void WasmBinaryWriter::writeNames() {
864864

865865
// function names
866866
{
867-
auto substart =
868-
startSubsection(BinaryConsts::CustomSections::Subsection::NameFunction);
869-
o << U32LEB(indexes.functionIndexes.size());
870-
Index emitted = 0;
871-
auto add = [&](Function* curr) {
872-
o << U32LEB(emitted);
873-
writeEscapedName(curr->name.str);
874-
emitted++;
867+
std::vector<std::pair<Index, Function*>> functionsWithNames;
868+
Index checked = 0;
869+
auto check = [&](Function* curr) {
870+
if (curr->hasExplicitName) {
871+
functionsWithNames.push_back({checked, curr});
872+
}
873+
checked++;
875874
};
876-
ModuleUtils::iterImportedFunctions(*wasm, add);
877-
ModuleUtils::iterDefinedFunctions(*wasm, add);
878-
assert(emitted == indexes.functionIndexes.size());
879-
finishSubsection(substart);
875+
ModuleUtils::iterImportedFunctions(*wasm, check);
876+
ModuleUtils::iterDefinedFunctions(*wasm, check);
877+
assert(checked == indexes.functionIndexes.size());
878+
if (functionsWithNames.size() > 0) {
879+
auto substart =
880+
startSubsection(BinaryConsts::CustomSections::Subsection::NameFunction);
881+
o << U32LEB(functionsWithNames.size());
882+
for (auto& [index, global] : functionsWithNames) {
883+
o << U32LEB(index);
884+
writeEscapedName(global->name.str);
885+
}
886+
finishSubsection(substart);
887+
}
880888
}
881889

882890
// local names

src/wasm/wasm-s-parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) {
11811181
// make a new function
11821182
currFunction = std::unique_ptr<Function>(
11831183
Builder(wasm).makeFunction(name, std::move(params), type, std::move(vars)));
1184+
currFunction->hasExplicitName = hasExplicitName;
11841185
currFunction->profile = profile;
11851186

11861187
// parse body

test/lit/merge/names.wat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
;; CHECK: (type $2 (func (param (ref $t))))
99

10-
;; CHECK: (type $3 (struct (field i64) (field i32)))
10+
;; CHECK: (type $u (struct (field $c i64) (field $d i32)))
1111

12-
;; CHECK: (type $4 (func (param (ref $3))))
12+
;; CHECK: (type $4 (func (param (ref $u))))
1313

1414
;; CHECK: (global $global$0 i32 (i32.const 0))
1515

16-
;; CHECK: (global $global$1 i32 (i32.const 0))
16+
;; CHECK: (global $glob2 i32 (i32.const 0))
1717

1818
;; CHECK: (global $global$2 i32 (i32.const 0))
1919

@@ -39,7 +39,7 @@
3939

4040
;; CHECK: (tag $tag$1)
4141

42-
;; CHECK: (tag $tag$2)
42+
;; CHECK: (tag $tag2)
4343

4444
;; CHECK: (tag $tag$3)
4545

@@ -71,21 +71,21 @@
7171

7272
;; CHECK: (export "f2" (func $func2))
7373

74-
;; CHECK: (export "f3" (func $1_3))
74+
;; CHECK: (export "f3" (func $4))
7575

7676
;; CHECK: (export "t2" (table $table2))
7777

7878
;; CHECK: (export "t3" (table $3))
7979

80-
;; CHECK: (export "g2" (global $global$1))
80+
;; CHECK: (export "g2" (global $glob2))
8181

8282
;; CHECK: (export "g3" (global $global$0))
8383

84-
;; CHECK: (export "tag2" (tag $tag$2))
84+
;; CHECK: (export "tag2" (tag $tag2))
8585

8686
;; CHECK: (export "tag3" (tag $tag$3))
8787

88-
;; CHECK: (export "func2" (func $2_3))
88+
;; CHECK: (export "func2" (func $5))
8989

9090
;; CHECK: (func $func0
9191
;; CHECK-NEXT: (nop)
@@ -127,10 +127,10 @@
127127
;; CHECK-NEXT: (nop)
128128
;; CHECK-NEXT: )
129129

130-
;; CHECK: (func $1_3
130+
;; CHECK: (func $4
131131
;; CHECK-NEXT: (nop)
132132
;; CHECK-NEXT: )
133133

134-
;; CHECK: (func $2_3 (param $0 (ref $3))
134+
;; CHECK: (func $5 (param $0 (ref $u))
135135
;; CHECK-NEXT: (nop)
136136
;; CHECK-NEXT: )

test/passes/func-metrics.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func: func_a
244244
Block : 1
245245
Call : 5
246246
start: func_a
247-
[removable-bytes-without-it]: 57
247+
[removable-bytes-without-it]: 60
248248
[total] : 0
249249
(module
250250
(type $0 (func))
@@ -274,7 +274,7 @@ func: 0
274274
[vars] : 0
275275
GlobalGet : 1
276276
export: stackSave (0)
277-
[removable-bytes-without-it]: 62
277+
[removable-bytes-without-it]: 79
278278
[total] : 0
279279
(module
280280
(type $0 (func (result i32)))
-3 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)