Skip to content

Commit 93f40ae

Browse files
authored
Update names field in source maps (#8068)
This scans the code after optimizations and removes unused function names from 'names' field in the source map, reducing its size. Emscripten has not been generating 'names' field so far, but after emscripten-core/emscripten#25870, it will generate the field in case `llvm-dwarfdump` supports a new option `--filter-child-tag`.
1 parent 0fdf002 commit 93f40ae

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
#include <algorithm>
1818
#include <fstream>
19-
#include <iomanip>
2019

21-
#include "ir/eh-utils.h"
2220
#include "ir/module-utils.h"
2321
#include "ir/names.h"
2422
#include "ir/table-utils.h"
2523
#include "ir/type-updating.h"
2624
#include "pass.h"
2725
#include "support/bits.h"
28-
#include "support/debug.h"
2926
#include "support/stdckdint.h"
3027
#include "support/string.h"
3128
#include "wasm-annotations.h"
@@ -1242,6 +1239,43 @@ void WasmBinaryWriter::writeSourceMapProlog() {
12421239
}
12431240
}
12441241

1242+
// Remove unused function names from 'names' field.
1243+
if (!wasm->debugInfoSymbolNames.empty()) {
1244+
std::vector<std::string> newSymbolNames;
1245+
std::map<Index, Index> oldToNewIndex;
1246+
1247+
// Collect all used symbol name indexes.
1248+
for (auto& func : wasm->functions) {
1249+
for (auto& [_, location] : func->debugLocations) {
1250+
if (location && location->symbolNameIndex) {
1251+
uint32_t oldIndex = *location->symbolNameIndex;
1252+
assert(oldIndex < wasm->debugInfoSymbolNames.size());
1253+
oldToNewIndex[oldIndex] = 0; // placeholder
1254+
}
1255+
}
1256+
}
1257+
1258+
// Create the new list of names and the mapping from old to new indices.
1259+
uint32_t index = 0;
1260+
for (auto& [oldIndex, newIndex] : oldToNewIndex) {
1261+
newSymbolNames.push_back(wasm->debugInfoSymbolNames[oldIndex]);
1262+
newIndex = index++;
1263+
}
1264+
1265+
// Update all debug locations to point to the new indices.
1266+
for (auto& func : wasm->functions) {
1267+
for (auto& [_, location] : func->debugLocations) {
1268+
if (location && location->symbolNameIndex) {
1269+
uint32_t oldIndex = *location->symbolNameIndex;
1270+
location->symbolNameIndex = oldToNewIndex[oldIndex];
1271+
}
1272+
}
1273+
}
1274+
1275+
// Replace the old symbol names with the new, pruned list.
1276+
wasm->debugInfoSymbolNames = std::move(newSymbolNames);
1277+
}
1278+
12451279
auto writeOptionalString = [&](const char* name, const std::string& str) {
12461280
if (!str.empty()) {
12471281
*sourceMap << "\"" << name << "\":\"" << str << "\",";
@@ -1269,10 +1303,6 @@ void WasmBinaryWriter::writeSourceMapProlog() {
12691303
writeStringVector("sourcesContent", wasm->debugInfoSourcesContent);
12701304
}
12711305

1272-
// TODO: This field is optional; maybe we should omit if it's empty.
1273-
// TODO: Binaryen actually does not correctly preserve symbol names when it
1274-
// rewrites the mappings. We should maybe just drop them, or else handle
1275-
// them correctly.
12761306
writeStringVector("names", wasm->debugInfoSymbolNames);
12771307

12781308
*sourceMap << "\"mappings\":\"";

test/lit/source-map-names.wast

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
;; RUN: wasm-opt %s --remove-unused-module-elements -o %t.wasm -osm %t.map
2+
;; RUN: wasm-dis %t.wasm --source-map %t.map | filecheck %s --check-prefix OUT-WAST
3+
;; RUN: cat %t.map | filecheck %s --check-prefix OUT-MAP
4+
5+
;; After --remove-unused-module-elements, the output source map's 'names' field
6+
;; should NOT contain 'unused'
7+
8+
;; OUT-MAP: "names":["used","used2"]
9+
10+
(module
11+
(export "used" (func $used))
12+
(export "used2" (func $used2))
13+
14+
(func $unused
15+
;;@ src.cpp:1:1:unused
16+
(nop)
17+
)
18+
19+
(func $used
20+
;; OUT-WAST: ;;@ src.cpp:2:1:used
21+
;; OUT-WAST-NEXT: (nop)
22+
;;@ src.cpp:2:1:used
23+
(nop)
24+
)
25+
26+
(func $used2
27+
;; OUT-WAST: ;;@ src.cpp:3:1:used
28+
;; OUT-WAST-NEXT: (nop)
29+
;;@ src.cpp:3:1:used2
30+
(nop)
31+
)
32+
)

0 commit comments

Comments
 (0)