Skip to content
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

Support 5 segment source mappings #6795

Merged
merged 40 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bc6dd69
Handle single-segment source mapping in source map header decoder
osa1 Jul 31, 2024
a12e4ad
Support 5-segment source mappings
osa1 Jul 31, 2024
363a03e
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Aug 7, 2024
d8555c5
Revert formatting change
osa1 Aug 7, 2024
3b986eb
Fix formatting
osa1 Aug 7, 2024
2015e88
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Sep 16, 2024
9465b2e
Revert debug change
osa1 Sep 16, 2024
4076b7a
Apply suggestions from code review
osa1 Sep 19, 2024
b63ec19
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Sep 20, 2024
6171fe9
Add test
osa1 Sep 20, 2024
0a9d251
Implement parsing and printing
osa1 Sep 20, 2024
6858dac
Format
osa1 Sep 20, 2024
4111ce8
Fix parsing
osa1 Sep 20, 2024
f92899c
Fixes?
osa1 Sep 20, 2024
8675c23
Revert lit test, wasm-opt doesn't generate symbol names
osa1 Sep 20, 2024
8a35d16
Fix formatting
osa1 Sep 20, 2024
0051cbb
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Sep 25, 2024
56abcb5
Fix wat parsing
osa1 Sep 25, 2024
7013707
Fix wat parsing a little bit more
osa1 Sep 25, 2024
cd2ac9d
Fix wat parsing even more
osa1 Sep 25, 2024
f70be71
Update lit test
osa1 Sep 25, 2024
ef71d81
Fix symbol name index updates
osa1 Sep 25, 2024
7e284ed
Improve test
osa1 Sep 25, 2024
c18d339
Formatting
osa1 Sep 25, 2024
7b59358
Refactor weird code
osa1 Sep 26, 2024
c96ad9a
Implement a TODO
osa1 Sep 26, 2024
25e1d80
Update merge test
osa1 Sep 27, 2024
95e24c7
Update metadce test
osa1 Sep 27, 2024
5dac139
Add inlining test
osa1 Sep 27, 2024
7d6529d
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Sep 27, 2024
836d387
Revert formatting change
osa1 Sep 27, 2024
1c4157c
Update lit output
osa1 Sep 27, 2024
a278429
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Oct 1, 2024
0098182
Address some of the comments
osa1 Oct 1, 2024
415bb88
Formatting
osa1 Oct 1, 2024
2f8cf68
Remove local
osa1 Oct 1, 2024
b1dff53
Merge remote-tracking branch 'origin/main' into 5_segment_source_mapp…
osa1 Oct 1, 2024
8a9ee2d
Revert C API changes
osa1 Oct 1, 2024
c54b226
Update comment
osa1 Oct 1, 2024
a721d72
Fix typo
osa1 Oct 1, 2024
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
16 changes: 16 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5662,6 +5662,14 @@ void BinaryenModuleInterpret(BinaryenModuleRef module) {
ModuleRunner instance(*(Module*)module, &interface, {});
}

BinaryenIndex BinaryenModuleAddDebugInfoSymbolName(BinaryenModuleRef module,
const char* symbolname) {
auto& debugInfoSymbolNames = ((Module*)module)->debugInfoSymbolNames;
BinaryenIndex index = debugInfoSymbolNames.size();
debugInfoSymbolNames.push_back(symbolname);
return index;
}

BinaryenIndex BinaryenModuleAddDebugInfoFileName(BinaryenModuleRef module,
const char* filename) {
auto& debugInfoFileNames = ((Module*)module)->debugInfoFileNames;
Expand All @@ -5670,6 +5678,14 @@ BinaryenIndex BinaryenModuleAddDebugInfoFileName(BinaryenModuleRef module,
return index;
}

const char* BinaryenModuleGetDebugInfoSymbolName(BinaryenModuleRef module,
BinaryenIndex index) {
const auto& debugInfoSymbolNames = ((Module*)module)->debugInfoSymbolNames;
return index < debugInfoSymbolNames.size()
? debugInfoSymbolNames.at(index).c_str()
: nullptr;
}
osa1 marked this conversation as resolved.
Show resolved Hide resolved

const char* BinaryenModuleGetDebugInfoFileName(BinaryenModuleRef module,
BinaryenIndex index) {
const auto& debugInfoFileNames = ((Module*)module)->debugInfoFileNames;
Expand Down
57 changes: 53 additions & 4 deletions src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,40 @@ static void updateLocationSet(std::set<Function::DebugLocation>& locations,
std::swap(locations, updatedLocations);
}

static void updateSymbolSet(std::set<Function::DebugLocation>& locations,
osa1 marked this conversation as resolved.
Show resolved Hide resolved
std::vector<Index>& symbolIndexMap) {
std::set<Function::DebugLocation> updatedLocations;

for (auto iter : locations) {
if (iter.symbolNameIndex.has_value()) {
osa1 marked this conversation as resolved.
Show resolved Hide resolved
iter.symbolNameIndex = symbolIndexMap[*iter.symbolNameIndex];
}
updatedLocations.insert(iter);
}
locations.clear();
std::swap(locations, updatedLocations);
}

// Copies a function into a module. If newName is provided it is used as the
// name of the function (otherwise the original name is copied). If fileIndexMap
// is specified, it is used to rename source map filename indices when copying
// the function from one module to another one.
osa1 marked this conversation as resolved.
Show resolved Hide resolved
Function* copyFunction(Function* func,
Module& out,
Name newName,
std::optional<std::vector<Index>> fileIndexMap) {
auto ret = copyFunctionWithoutAdd(func, out, newName, fileIndexMap);
std::optional<std::vector<Index>> fileIndexMap,
std::optional<std::vector<Index>> symbolNameIndexMap) {
auto ret = copyFunctionWithoutAdd(
func, out, newName, fileIndexMap, symbolNameIndexMap);
return out.addFunction(std::move(ret));
}

std::unique_ptr<Function>
copyFunctionWithoutAdd(Function* func,
Module& out,
Name newName,
std::optional<std::vector<Index>> fileIndexMap) {
std::optional<std::vector<Index>> fileIndexMap,
std::optional<std::vector<Index>> symbolNameIndexMap) {
auto ret = std::make_unique<Function>();
ret->name = newName.is() ? newName : func->name;
ret->hasExplicitName = func->hasExplicitName;
Expand All @@ -76,6 +93,18 @@ copyFunctionWithoutAdd(Function* func,
updateLocationSet(ret->prologLocation, *fileIndexMap);
updateLocationSet(ret->epilogLocation, *fileIndexMap);
}
if (symbolNameIndexMap) {
for (auto& iter : ret->debugLocations) {
if (iter.second) {
if (iter.second->symbolNameIndex.has_value()) {
iter.second->symbolNameIndex =
(*symbolNameIndexMap)[*(iter.second->symbolNameIndex)];
}
}
updateSymbolSet(ret->prologLocation, *symbolNameIndexMap);
updateSymbolSet(ret->epilogLocation, *symbolNameIndexMap);
}
}
ret->module = func->module;
ret->base = func->base;
ret->noFullInline = func->noFullInline;
Expand Down Expand Up @@ -199,8 +228,27 @@ void copyModuleItems(const Module& in, Module& out) {
}
}

std::optional<std::vector<Index>> symbolNameIndexMap;
if (!in.debugInfoSymbolNames.empty()) {
std::unordered_map<std::string, Index> debugInfoSymbolNameIndices;
for (Index i = 0; i < out.debugInfoSymbolNames.size(); i++) {
debugInfoSymbolNameIndices[out.debugInfoSymbolNames[i]] = i;
}
symbolNameIndexMap.emplace();
for (Index i = 0; i < in.debugInfoSymbolNames.size(); i++) {
std::string file = in.debugInfoSymbolNames[i];
auto iter = debugInfoSymbolNameIndices.find(file);
if (iter == debugInfoSymbolNameIndices.end()) {
Index index = out.debugInfoSymbolNames.size();
out.debugInfoSymbolNames.push_back(file);
debugInfoSymbolNameIndices[file] = index;
}
symbolNameIndexMap->push_back(debugInfoSymbolNameIndices[file]);
}
}

for (auto& curr : in.functions) {
copyFunction(curr.get(), out, Name(), fileIndexMap);
copyFunction(curr.get(), out, Name(), fileIndexMap, symbolNameIndexMap);
}
for (auto& curr : in.globals) {
copyGlobal(curr.get(), out);
Expand Down Expand Up @@ -241,6 +289,7 @@ void copyModule(const Module& in, Module& out) {
out.start = in.start;
out.customSections = in.customSections;
out.debugInfoFileNames = in.debugInfoFileNames;
out.debugInfoSymbolNames = in.debugInfoSymbolNames;
out.features = in.features;
}

Expand Down
14 changes: 8 additions & 6 deletions src/ir/module-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ namespace wasm::ModuleUtils {
// name of the function (otherwise the original name is copied). If fileIndexMap
// is specified, it is used to rename source map filename indices when copying
// the function from one module to another one.
osa1 marked this conversation as resolved.
Show resolved Hide resolved
Function*
copyFunction(Function* func,
Module& out,
Name newName = Name(),
std::optional<std::vector<Index>> fileIndexMap = std::nullopt);
Function* copyFunction(
Function* func,
Module& out,
Name newName = Name(),
std::optional<std::vector<Index>> fileIndexMap = std::nullopt,
std::optional<std::vector<Index>> symbolNameIndexMap = std::nullopt);

// As above, but does not add the copy to the module.
std::unique_ptr<Function> copyFunctionWithoutAdd(
Function* func,
Module& out,
Name newName = Name(),
std::optional<std::vector<Index>> fileIndexMap = std::nullopt);
std::optional<std::vector<Index>> fileIndexMap = std::nullopt,
std::optional<std::vector<Index>> symbolNameIndexMap = std::nullopt);

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

Expand Down
30 changes: 27 additions & 3 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
typeNames;
const std::unordered_map<Index, Index>& implicitElemIndices;

std::unordered_map<std::string_view, Index> debugSymbolNameIndices;
std::unordered_map<std::string_view, Index> debugFileIndices;

// The index of the current module element.
Expand Down Expand Up @@ -1777,11 +1778,34 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
}
contents = contents.substr(lineSize + 1);

lexer = Lexer(contents);
auto colSize = contents.find(':');
auto rest = std::string_view();
osa1 marked this conversation as resolved.
Show resolved Hide resolved
if (colSize == contents.npos) {
colSize = contents.size();
if (colSize == 0) {
return;
}
} else {
rest = contents.substr(colSize + 1);
}
lexer = Lexer(contents.substr(0, colSize));
auto col = lexer.takeU32();
if (!col || !lexer.empty()) {
if (!col) {
return;
}
contents = rest;

std::optional<BinaryLocation> symbolNameIndex;
if (!contents.empty()) {
auto symbolName = contents;
auto [it, inserted] = debugSymbolNameIndices.insert(
{symbolName, debugSymbolNameIndices.size()});
if (inserted) {
assert(wasm.debugInfoSymbolNames.size() == it->second);
wasm.debugInfoSymbolNames.push_back(std::string(symbolName));
}
symbolNameIndex = it->second;
}

// TODO: If we ever parallelize the parse, access to
// `wasm.debugInfoFileNames` will have to be protected by a lock.
Expand All @@ -1792,7 +1816,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
wasm.debugInfoFileNames.push_back(std::string(file));
}
irBuilder.setDebugLocation(
Function::DebugLocation({it->second, *line, *col}));
Function::DebugLocation({it->second, *line, *col, symbolNameIndex}));
}

Result<> makeBlock(Index pos,
Expand Down
10 changes: 9 additions & 1 deletion src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,15 @@ void PrintSExpression::printDebugLocation(
} else {
auto fileName = currModule->debugInfoFileNames[location->fileIndex];
o << ";;@ " << fileName << ":" << location->lineNumber << ":"
<< location->columnNumber << '\n';
<< location->columnNumber;

if (location->symbolNameIndex.has_value()) {
osa1 marked this conversation as resolved.
Show resolved Hide resolved
auto symbolName =
currModule->debugInfoSymbolNames[*(location->symbolNameIndex)];
o << ":" << symbolName;
}

o << '\n';
}
doIndent(o, indent);
}
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,7 @@ class WasmBinaryReader {
// Debug information reading helpers
void setDebugLocations(std::istream* sourceMap_) { sourceMap = sourceMap_; }
std::unordered_map<std::string, Index> debugInfoFileIndices;
std::unordered_map<std::string, Index> debugInfoSymbolNameIndices;
void readNextDebugLocation();
void readSourceMapHeader();

Expand Down
15 changes: 9 additions & 6 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2076,19 +2076,21 @@ class Function : public Importable {
// Source maps debugging info: map expression nodes to their file, line, col.
osa1 marked this conversation as resolved.
Show resolved Hide resolved
struct DebugLocation {
BinaryLocation fileIndex, lineNumber, columnNumber;
std::optional<BinaryLocation> symbolNameIndex;
bool operator==(const DebugLocation& other) const {
return fileIndex == other.fileIndex && lineNumber == other.lineNumber &&
columnNumber == other.columnNumber;
columnNumber == other.columnNumber &&
symbolNameIndex == other.symbolNameIndex;
}
bool operator!=(const DebugLocation& other) const {
return !(*this == other);
}
bool operator<(const DebugLocation& other) const {
return fileIndex != other.fileIndex
? fileIndex < other.fileIndex
: lineNumber != other.lineNumber
? lineNumber < other.lineNumber
: columnNumber < other.columnNumber;
return fileIndex != other.fileIndex ? fileIndex < other.fileIndex
: lineNumber != other.lineNumber ? lineNumber < other.lineNumber
: columnNumber != other.columnNumber
? columnNumber < other.columnNumber
: symbolNameIndex < other.symbolNameIndex;
}
};
// One can explicitly set the debug location of an expression to
Expand Down Expand Up @@ -2300,6 +2302,7 @@ class Module {

// Source maps debug info.
std::vector<std::string> debugInfoFileNames;
std::vector<std::string> debugInfoSymbolNames;

// `features` are the features allowed to be used in this module and should be
// respected regardless of the value of`hasFeaturesSection`.
Expand Down
Loading
Loading