Skip to content

Commit 42acc22

Browse files
authored
support fixed (non-relocatable) segments in wasm-merge. also a few printing fixes for multiple segments, which we never really printed that prettily (#1316)
1 parent 0d5a39f commit 42acc22

29 files changed

+440
-29
lines changed

src/passes/Print.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,20 +741,18 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
741741
printTableHeader(curr);
742742
o << maybeNewLine;
743743
}
744-
if (curr->segments.empty()) return;
745-
doIndent(o, indent);
746744
for (auto& segment : curr->segments) {
747745
// Don't print empty segments
748746
if (segment.data.empty()) continue;
747+
doIndent(o, indent);
749748
printOpening(o, "elem ", true);
750749
visit(segment.offset);
751750
for (auto name : segment.data) {
752751
o << ' ';
753752
printName(name);
754753
}
755-
o << ')';
754+
o << ")\n";
756755
}
757-
o << maybeNewLine;
758756
}
759757
void printMemoryHeader(Memory* curr) {
760758
printOpening(o, "memory") << ' ';

src/support/name.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ struct Name : public cashew::IString {
4040
Name(const std::string& str) : cashew::IString(str.c_str(), false) {}
4141

4242
friend std::ostream& operator<<(std::ostream& o, Name name) {
43-
assert(name.str);
44-
return o << '$' << name.str; // reference interpreter requires we prefix all names
43+
if (name.str) {
44+
return o << '$' << name.str; // reference interpreter requires we prefix all names
45+
} else {
46+
return o << "(null Name)";
47+
}
4548
}
4649

4750
static Name fromInt(size_t i) {

src/tools/wasm-merge.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,28 @@ struct Mergeable {
123123
memoryBaseGlobals.insert(name);
124124
});
125125
if (memoryBaseGlobals.size() == 0) {
126-
Fatal() << "no memory base was imported";
126+
// add one
127+
auto* import = new Import;
128+
import->name = MEMORY_BASE;
129+
import->module = ENV;
130+
import->base = MEMORY_BASE;
131+
import->kind = ExternalKind::Global;
132+
import->globalType = i32;
133+
wasm.addImport(import);
134+
memoryBaseGlobals.insert(import->name);
127135
}
128136
findImportsByBase(wasm, TABLE_BASE, [&](Name name) {
129137
tableBaseGlobals.insert(name);
130138
});
131139
if (tableBaseGlobals.size() == 0) {
132-
Fatal() << "no table base was imported";
140+
auto* import = new Import;
141+
import->name = TABLE_BASE;
142+
import->module = ENV;
143+
import->base = TABLE_BASE;
144+
import->kind = ExternalKind::Global;
145+
import->globalType = i32;
146+
wasm.addImport(import);
147+
tableBaseGlobals.insert(import->name);
133148
}
134149
}
135150

@@ -169,7 +184,8 @@ struct Mergeable {
169184
// ensure a relocatable segment exists, of the proper size, including
170185
// the dylink bump applied into it, standardized into the form of
171186
// not using a dylink section and instead having enough zeros at
172-
// the end. this makes linking much simpler.
187+
// the end. this makes linking much simpler.ta
188+
// there may be other non-relocatable segments too.
173189
template<typename T, typename U, typename Segment>
174190
void standardizeSegment(Module& wasm, T& what, Index size, U zero, Name globalName) {
175191
Segment* relocatable = nullptr;
@@ -194,9 +210,10 @@ struct Mergeable {
194210
ensureSize(what, relocatable->data.size());
195211
}
196212

197-
// copies a relocatable segment from the input to the output
213+
// copies a relocatable segment from the input to the output, and
214+
// copies the non-relocatable ones as well
198215
template<typename T, typename V>
199-
void copySegment(T& output, T& input, V updater) {
216+
void copySegments(T& output, T& input, V updater) {
200217
for (auto& inputSegment : input.segments) {
201218
Expression* inputOffset = inputSegment.offset;
202219
if (inputOffset->is<GetGlobal>()) {
@@ -213,6 +230,9 @@ struct Mergeable {
213230
}
214231
}
215232
WASM_UNREACHABLE(); // we must find a relocatable one in the output, as we standardized
233+
} else {
234+
// this is a non-relocatable one. just copy it.
235+
output.segments.push_back(inputSegment);
216236
}
217237
}
218238
}
@@ -405,8 +425,8 @@ struct InputMergeable : public ExpressionStackWalker<InputMergeable, Visitor<Inp
405425
}
406426

407427
// memory&table: we place the new memory segments at a higher position. after the existing ones.
408-
copySegment(outputMergeable.wasm.memory, wasm.memory, [](char x) -> char { return x; });
409-
copySegment(outputMergeable.wasm.table, wasm.table, [&](Name x) -> Name { return fNames[x]; });
428+
copySegments(outputMergeable.wasm.memory, wasm.memory, [](char x) -> char { return x; });
429+
copySegments(outputMergeable.wasm.table, wasm.table, [&](Name x) -> Name { return fNames[x]; });
410430

411431
// update the new contents about to be merged in
412432
walkModule(&wasm);

test/merge/dylib.wasm.combined

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
(global $global$2 i32 (i32.const 0))
1919
(global $global$0$0 (mut i32) (i32.const 0))
2020
(global $global$1$0 (mut i32) (i32.const 0))
21-
2221
(data (get_global $import$0) "hello, world!\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
2322
(export "__post_instantiate" (func $__post_instantiate))
2423
(export "_main" (func $_main))

test/merge/dylib.wasm.combined.finalized

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
(global $global$2 i32 (i32.const 0))
1919
(global $global$0$0 (mut i32) (i32.const 0))
2020
(global $global$1$0 (mut i32) (i32.const 0))
21-
2221
(data (i32.const 1024) "hello, world!\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
2322
(export "__post_instantiate" (func $__post_instantiate))
2423
(export "_main" (func $_main))

test/merge/dylib.wasm.combined.finalized.opt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
(global $global$2 i32 (i32.const 0))
1111
(global $global$0$0 (mut i32) (i32.const 0))
1212
(global $global$1$0 (mut i32) (i32.const 0))
13-
1413
(data (i32.const 1024) "hello, world!")
1514
(export "__post_instantiate" (func $__post_instantiate))
1615
(export "_main" (func $_main))

test/merge/dylib.wasm.combined.opt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
(global $global$2 i32 (i32.const 0))
1414
(global $global$0$0 (mut i32) (i32.const 0))
1515
(global $global$1$0 (mut i32) (i32.const 0))
16-
1716
(data (get_global $import$0) "hello, world!")
1817
(export "__post_instantiate" (func $__post_instantiate))
1918
(export "_main" (func $_main))

test/merge/fusing.wast.combined

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
(import "env" "tableBase" (global $tableBase$0 i32))
1010
(global $a-global i32 (i32.const 0))
1111
(global $b-global f64 (f64.const 2.14281428))
12-
1312
(data (get_global $memoryBase) "")
1413
(export "foo" (func $foo-func))
1514
(export "aglobal" (global $a-global))

test/merge/fusing.wast.combined.finalized

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
(import "env" "tableBase" (global $tableBase$0 i32))
1010
(global $a-global i32 (i32.const 0))
1111
(global $b-global f64 (f64.const 2.14281428))
12-
1312
(data (i32.const 1024) "")
1413
(export "foo" (func $foo-func))
1514
(export "aglobal" (global $a-global))

test/merge/fusing.wast.combined.finalized.opt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
(import "env" "table" (table 8 anyfunc))
55
(global $a-global i32 (i32.const 0))
66
(global $b-global f64 (f64.const 2.14281428))
7-
87
(export "foo" (func $foo-func))
98
(export "aglobal" (global $a-global))
109
(export "bar" (func $bar-func))

0 commit comments

Comments
 (0)