-
Notifications
You must be signed in to change notification settings - Fork 828
Update names field in source maps #8068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
3c636de
484554c
f636186
38d6742
3a68a12
93d135a
a55a844
6a53061
d0a70ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,16 +16,13 @@ | |
|
|
||
| #include <algorithm> | ||
| #include <fstream> | ||
| #include <iomanip> | ||
|
|
||
| #include "ir/eh-utils.h" | ||
| #include "ir/module-utils.h" | ||
| #include "ir/names.h" | ||
| #include "ir/table-utils.h" | ||
| #include "ir/type-updating.h" | ||
| #include "pass.h" | ||
| #include "support/bits.h" | ||
| #include "support/debug.h" | ||
| #include "support/stdckdint.h" | ||
| #include "support/string.h" | ||
| #include "wasm-annotations.h" | ||
|
|
@@ -1242,6 +1239,44 @@ void WasmBinaryWriter::writeSourceMapProlog() { | |
| } | ||
| } | ||
|
|
||
| // Remove unused function names from 'names' field. | ||
| if (!wasm->debugInfoSymbolNames.empty()) { | ||
| std::vector<std::string> newSymbolNames; | ||
| std::unordered_map<Index, Index> oldToNewIndex; | ||
|
|
||
| // Collect all used symbol name indexes. | ||
| for (auto& func : wasm->functions) { | ||
| for (auto& pair : func->debugLocations) { | ||
| if (pair.second && pair.second->symbolNameIndex) { | ||
| uint32_t oldIndex = *pair.second->symbolNameIndex; | ||
| assert(oldIndex < wasm->debugInfoSymbolNames.size()); | ||
| oldToNewIndex[oldIndex] = 0; // placeholder | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Create the new list of names and the mapping from old to new indices. | ||
| uint32_t newIndex = 0; | ||
| for (auto& pair : oldToNewIndex) { | ||
|
||
| // pair.first is the oldIndex | ||
| newSymbolNames.push_back(wasm->debugInfoSymbolNames[pair.first]); | ||
| pair.second = newIndex++; // update placeholder to new index | ||
| } | ||
|
|
||
| // Update all debug locations to point to the new indices. | ||
| for (auto& func : wasm->functions) { | ||
| for (auto& pair : func->debugLocations) { | ||
aheejin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (pair.second && pair.second->symbolNameIndex) { | ||
| uint32_t oldIndex = *pair.second->symbolNameIndex; | ||
| pair.second->symbolNameIndex = oldToNewIndex[oldIndex]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Replace the old symbol names with the new, pruned list. | ||
| wasm->debugInfoSymbolNames = std::move(newSymbolNames); | ||
| } | ||
|
|
||
| auto writeOptionalString = [&](const char* name, const std::string& str) { | ||
| if (!str.empty()) { | ||
| *sourceMap << "\"" << name << "\":\"" << str << "\","; | ||
|
|
@@ -1269,10 +1304,6 @@ void WasmBinaryWriter::writeSourceMapProlog() { | |
| writeStringVector("sourcesContent", wasm->debugInfoSourcesContent); | ||
| } | ||
|
|
||
| // TODO: This field is optional; maybe we should omit if it's empty. | ||
| // TODO: Binaryen actually does not correctly preserve symbol names when it | ||
| // rewrites the mappings. We should maybe just drop them, or else handle | ||
| // them correctly. | ||
| writeStringVector("names", wasm->debugInfoSymbolNames); | ||
|
|
||
| *sourceMap << "\"mappings\":\""; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ;; RUN: wasm-opt %s --remove-unused-module-elements -o %t.wasm -osm %t.map | ||
| ;; RUN: wasm-dis %t.wasm --source-map %t.map | filecheck %s --check-prefix OUT-WAST | ||
| ;; RUN: cat %t.map | filecheck %s --check-prefix OUT-MAP | ||
|
|
||
| ;; After --remove-unused-module-elements, the output source map's 'names' field | ||
| ;; should NOT contain 'unused' | ||
|
|
||
| ;; OUT-MAP: "names":["used"] | ||
|
|
||
| (module | ||
| (export "used" (func $used)) | ||
|
|
||
| (func $unused | ||
| ;;@ src.cpp:1:1:unused | ||
| (nop) | ||
| ) | ||
|
|
||
| (func $used | ||
| ;; OUT-WAST: ;;@ src.cpp:2:1:used | ||
| ;; OUT-WAST-NEXT: (nop) | ||
| ;;@ src.cpp:2:1:used | ||
| (nop) | ||
| ) | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add another used symbol, to get some coverage of the order of the symbols being deterministic? (not great coverage, but it might help)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done: a55a844 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you even do something like
[_, location]here instead of having to usepair.second?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done