Skip to content

Commit b3c5faf

Browse files
committed
Updates after review
1 parent 824eda0 commit b3c5faf

7 files changed

Lines changed: 115 additions & 59 deletions

File tree

src/ir/module-utils.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,28 @@
2424

2525
namespace wasm::ModuleUtils {
2626

27+
// Update the file name indices when moving a set of debug locations from one
28+
// module to another.
2729
static void updateLocationSet(std::set<Function::DebugLocation>& locations,
28-
std::vector<Index>* indexMap) {
30+
std::vector<Index>& fileIndexMap) {
2931
std::set<Function::DebugLocation> updatedLocations;
3032

3133
for (auto iter : locations) {
32-
iter.fileIndex = (*indexMap)[iter.fileIndex];
34+
iter.fileIndex = fileIndexMap[iter.fileIndex];
3335
updatedLocations.insert(iter);
3436
}
3537
locations.clear();
3638
std::swap(locations, updatedLocations);
3739
}
3840

3941
// Copies a function into a module. If newName is provided it is used as the
40-
// name of the function (otherwise the original name is copied). If indexMap is
41-
// specified, it is used to rename source map filename indices when copying the
42-
// function from one module to another one.
42+
// name of the function (otherwise the original name is copied). If fileIndexMap
43+
// is specified, it is used to rename source map filename indices when copying
44+
// the function from one module to another one.
4345
Function* copyFunction(Function* func,
4446
Module& out,
4547
Name newName,
46-
std::vector<Index>* indexMap) {
48+
std::optional<std::vector<Index>> fileIndexMap) {
4749
auto ret = std::make_unique<Function>();
4850
ret->name = newName.is() ? newName : func->name;
4951
ret->type = func->type;
@@ -57,12 +59,12 @@ Function* copyFunction(Function* func,
5759
ret->prologLocation = func->prologLocation;
5860
ret->epilogLocation = func->epilogLocation;
5961
// Update file indices if needed
60-
if (indexMap) {
62+
if (fileIndexMap) {
6163
for (auto& iter : ret->debugLocations) {
62-
iter.second.fileIndex = (*indexMap)[iter.second.fileIndex];
64+
iter.second.fileIndex = (*fileIndexMap)[iter.second.fileIndex];
6365
}
64-
updateLocationSet(ret->prologLocation, indexMap);
65-
updateLocationSet(ret->epilogLocation, indexMap);
66+
updateLocationSet(ret->prologLocation, *fileIndexMap);
67+
updateLocationSet(ret->epilogLocation, *fileIndexMap);
6668
}
6769
ret->module = func->module;
6870
ret->base = func->base;
@@ -165,13 +167,27 @@ DataSegment* copyDataSegment(const DataSegment* segment, Module& out) {
165167

166168
// Copies named toplevel module items (things of kind ModuleItemKind). See
167169
// copyModule() for something that also copies exports, the start function, etc.
168-
// The indexMap is used to update source map information when copying functions
169-
// from one module to another.
170-
void copyModuleItems(const Module& in,
171-
Module& out,
172-
std::vector<Index>* indexMap) {
170+
// The fileIndexMap is used to update source map information when copying
171+
// functions from one module to another.
172+
void copyModuleItems(const Module& in, Module& out) {
173+
std::vector<Index> fileIndexMap;
174+
std::unordered_map<std::string, Index> debugInfoFileIndices;
175+
for (Index i = 0; i < out.debugInfoFileNames.size(); i++) {
176+
debugInfoFileIndices[out.debugInfoFileNames[i]] = i;
177+
}
178+
for (Index i = 0; i < in.debugInfoFileNames.size(); i++) {
179+
std::string file = in.debugInfoFileNames[i];
180+
auto iter = debugInfoFileIndices.find(file);
181+
if (iter == debugInfoFileIndices.end()) {
182+
Index index = out.debugInfoFileNames.size();
183+
out.debugInfoFileNames.push_back(file);
184+
debugInfoFileIndices[file] = index;
185+
}
186+
fileIndexMap.push_back(debugInfoFileIndices[file]);
187+
}
188+
173189
for (auto& curr : in.functions) {
174-
copyFunction(curr.get(), out, Name(), indexMap);
190+
copyFunction(curr.get(), out, Name(), fileIndexMap);
175191
}
176192
for (auto& curr : in.globals) {
177193
copyGlobal(curr.get(), out);

src/ir/module-utils.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
namespace wasm::ModuleUtils {
2525

2626
// Copies a function into a module. If newName is provided it is used as the
27-
// name of the function (otherwise the original name is copied). If indexMap is
27+
// name of the function (otherwise the original name is copied). If fileIndexMap is
2828
// specified, it is used to rename source map filename indices when copying the
2929
// function from one module to another one.
3030
Function* copyFunction(Function* func,
3131
Module& out,
3232
Name newName = Name(),
33-
std::vector<Index>* indexMap = 0);
33+
std::optional<std::vector<Index>> fileIndexMap = std::nullopt);
3434

3535
Global* copyGlobal(Global* global, Module& out);
3636

@@ -46,11 +46,7 @@ DataSegment* copyDataSegment(const DataSegment* segment, Module& out);
4646

4747
// Copies named toplevel module items (things of kind ModuleItemKind). See
4848
// copyModule() for something that also copies exports, the start function, etc.
49-
// The indexMap is used to update source map information when copying functions
50-
// from one module to another.
51-
void copyModuleItems(const Module& in,
52-
Module& out,
53-
std::vector<Index>* indexMap = 0);
49+
void copyModuleItems(const Module& in, Module& out);
5450

5551
void copyModule(const Module& in, Module& out);
5652

src/tools/wasm-merge.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,9 @@ void renameInputItems(Module& input) {
324324
}
325325

326326
void copyModuleContents(Module& input, Name inputName) {
327-
// Source map filename index mapping
328-
std::vector<Index> indexMap;
329-
std::unordered_map<std::string, Index> debugInfoFileIndices;
330-
for (Index i = 0; i < merged.debugInfoFileNames.size(); i++) {
331-
debugInfoFileIndices[merged.debugInfoFileNames[i]] = i;
332-
}
333-
for (Index i = 0; i < input.debugInfoFileNames.size(); i++) {
334-
std::string file = input.debugInfoFileNames[i];
335-
auto iter = debugInfoFileIndices.find(file);
336-
if (iter == debugInfoFileIndices.end()) {
337-
Index index = merged.debugInfoFileNames.size();
338-
merged.debugInfoFileNames.push_back(file);
339-
debugInfoFileIndices[file] = index;
340-
}
341-
indexMap.push_back(debugInfoFileIndices[file]);
342-
}
343-
344-
// Copy the regular module items (functions, globals) etc. which we
327+
// First, copy the regular module items (functions, globals) etc. which we
345328
// have proper names for, and can just copy.
346-
ModuleUtils::copyModuleItems(input, merged, &indexMap);
329+
ModuleUtils::copyModuleItems(input, merged);
347330

348331
// We must handle exports in a special way, as we need to note their origin
349332
// module as we copy them in (also, they are not importable or exportable, so

src/tools/wasm-metadce.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ int main(int argc, const char* argv[]) {
497497
ModuleReader reader;
498498
reader.setDWARF(debugInfo);
499499
try {
500-
reader.read(options.extra["infile"], wasm, input);
500+
reader.read(options.extra["infile"], wasm, inputSourceMapFilename);
501501
} catch (ParseException& p) {
502502
p.dump(std::cerr);
503503
Fatal() << "error in parsing wasm input";

test/lit/merge/sourcemap.wat

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
22

3-
;; RUN: wasm-merge %s first %s.second second -S -o - | filecheck %s
3+
;; RUN: wasm-merge %s first %s.second second -S -o - | filecheck %s --check-prefix=CHECK-TEXT
4+
;; RUN: wasm-as %s -o %t.wasm --source-map %t.map
5+
;; RUN: wasm-as %s.second -o %t.second.wasm --source-map %t.second.map
6+
;; RUN: wasm-merge %t.wasm first --input-source-map %t.map %t.second.wasm second --input-source-map %t.second.map -o %t.merged.wasm --output-source-map %t.merged.map
7+
;; RUN: wasm-dis %t.merged.wasm --source-map %t.merged.map -o - | filecheck %s --check-prefix=CHECK-BIN
48

59
;; Test that sourcemap information is preserved
610

@@ -12,22 +16,42 @@
1216
;;@ a:3:1
1317
)
1418
)
15-
;; CHECK: (type $0 (func))
19+
;; CHECK-TEXT: (type $0 (func))
1620

17-
;; CHECK: (export "f" (func $0))
21+
;; CHECK-TEXT: (export "f" (func $0))
1822

19-
;; CHECK: (export "g" (func $0_1))
23+
;; CHECK-TEXT: (export "g" (func $0_1))
2024

21-
;; CHECK: ;;@ a:1:1
22-
;; CHECK-NEXT: (func $0
23-
;; CHECK-NEXT: ;;@ a:2:1
24-
;; CHECK-NEXT: (nop)
25-
;; CHECK-NEXT: ;;@ a:3:1
26-
;; CHECK-NEXT: )
25+
;; CHECK-TEXT: ;;@ a:1:1
26+
;; CHECK-TEXT-NEXT: (func $0
27+
;; CHECK-TEXT-NEXT: ;;@ a:2:1
28+
;; CHECK-TEXT-NEXT: (nop)
29+
;; CHECK-TEXT-NEXT: ;;@ a:3:1
30+
;; CHECK-TEXT-NEXT: )
2731

28-
;; CHECK: ;;@ b:1:2
29-
;; CHECK-NEXT: (func $0_1
30-
;; CHECK-NEXT: ;;@ b:2:2
31-
;; CHECK-NEXT: (nop)
32-
;; CHECK-NEXT: ;;@ b:3:2
33-
;; CHECK-NEXT: )
32+
;; CHECK-TEXT: ;;@ b:1:2
33+
;; CHECK-TEXT-NEXT: (func $0_1
34+
;; CHECK-TEXT-NEXT: ;;@ b:2:2
35+
;; CHECK-TEXT-NEXT: (nop)
36+
;; CHECK-TEXT-NEXT: ;;@ b:3:2
37+
;; CHECK-TEXT-NEXT: )
38+
39+
;; CHECK-BIN: (type $0 (func))
40+
41+
;; CHECK-BIN: (export "f" (func $0))
42+
43+
;; CHECK-BIN: (export "g" (func $1))
44+
45+
;; CHECK-BIN: ;;@ a:1:1
46+
;; CHECK-BIN-NEXT: (func $0
47+
;; CHECK-BIN-NEXT: ;;@ a:2:1
48+
;; CHECK-BIN-NEXT: (nop)
49+
;; CHECK-BIN-NEXT: ;;@ a:3:1
50+
;; CHECK-BIN-NEXT: )
51+
52+
;; CHECK-BIN: ;;@ b:1:2
53+
;; CHECK-BIN-NEXT: (func $1
54+
;; CHECK-BIN-NEXT: ;;@ b:2:2
55+
;; CHECK-BIN-NEXT: (nop)
56+
;; CHECK-BIN-NEXT: ;;@ b:3:2
57+
;; CHECK-BIN-NEXT: )

test/lit/metadce/sourcemap.wat

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
;; RUN: wasm-metadce %s --graph-file %s.json -S -o - | filecheck %s
3+
;; RUN: wasm-as %s -o %t.wasm --source-map %t.map
4+
;; RUN: wasm-metadce %t.wasm --input-source-map %t.map --graph-file %s.json -o %t.out.wasm --output-source-map %t.out.map
5+
;; RUN: wasm-dis %t.out.wasm --source-map %t.out.map -o - | filecheck %s
6+
7+
;; Test that sourcemap information is preserved
8+
9+
(module
10+
;;@ a:1:1
11+
(func (export "f")
12+
;;@ a:2:1
13+
(nop)
14+
;;@ a:3:1
15+
)
16+
)
17+
;; CHECK: (type $0 (func))
18+
19+
;; CHECK: (export "f" (func $0))
20+
21+
;; CHECK: ;;@ a:1:1
22+
;; CHECK-NEXT: (func $0
23+
;; CHECK-NEXT: ;;@ a:2:1
24+
;; CHECK-NEXT: (nop)
25+
;; CHECK-NEXT: ;;@ a:3:1
26+
;; CHECK-NEXT: )
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"name": "root",
4+
"reaches": ["f"],
5+
"root": true
6+
},
7+
{
8+
"name": "f",
9+
"export": "f"
10+
}
11+
]

0 commit comments

Comments
 (0)