Skip to content
Merged
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
23 changes: 17 additions & 6 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1245,14 +1245,20 @@ void WasmBinaryWriter::writeSourceMapProlog() {
std::map<Index, Index> oldToNewIndex;

// Collect all used symbol name indexes.
for (auto& func : wasm->functions) {
for (auto& [_, location] : func->debugLocations) {
auto prepareIndexMap =
[&](const std::optional<Function::DebugLocation>& location) {
if (location && location->symbolNameIndex) {
uint32_t oldIndex = *location->symbolNameIndex;
assert(oldIndex < wasm->debugInfoSymbolNames.size());
oldToNewIndex[oldIndex] = 0; // placeholder
}
};
for (auto& func : wasm->functions) {
for (auto& [_, location] : func->debugLocations) {
prepareIndexMap(location);
}
prepareIndexMap(func->prologLocation);
prepareIndexMap(func->epilogLocation);
}

// Create the new list of names and the mapping from old to new indices.
Expand All @@ -1263,13 +1269,18 @@ void WasmBinaryWriter::writeSourceMapProlog() {
}

// Update all debug locations to point to the new indices.
auto updateIndex = [&](std::optional<Function::DebugLocation>& location) {
if (location && location->symbolNameIndex) {
uint32_t oldIndex = *location->symbolNameIndex;
location->symbolNameIndex = oldToNewIndex[oldIndex];
}
};
for (auto& func : wasm->functions) {
for (auto& [_, location] : func->debugLocations) {
if (location && location->symbolNameIndex) {
uint32_t oldIndex = *location->symbolNameIndex;
location->symbolNameIndex = oldToNewIndex[oldIndex];
}
updateIndex(location);
}
updateIndex(func->prologLocation);
updateIndex(func->epilogLocation);
}

// Replace the old symbol names with the new, pruned list.
Expand Down
10 changes: 9 additions & 1 deletion test/lit/source-map-names.wast
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
;; After --remove-unused-module-elements, the output source map's 'names' field
;; should NOT contain 'unused'

;; OUT-MAP: "names":["used","used2"]
;; OUT-MAP: "names":["used","used2","usedProlog"]

(module
(export "used" (func $used))
(export "used2" (func $used2))
(export "usedProlog" (func $usedProlog))

(func $unused
;;@ src.cpp:1:1:unused
Expand All @@ -29,4 +30,11 @@
;;@ src.cpp:3:1:used2
(nop)
)

;; OUT-WAST: ;;@ src.cpp:4:1:usedProlog
;; OUT-WAST-NEXT: (func
;;@ src.cpp:4:1:usedProlog
(func $usedProlog
(nop)
)
)
Loading