Skip to content

Commit

Permalink
Fixes?
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Sep 20, 2024
1 parent 4111ce8 commit f92899c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
42 changes: 37 additions & 5 deletions src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ static void updateLocationSet(std::set<Function::DebugLocation>& locations,
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 +79,17 @@ 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)];
}
}
}
// TODO: Do we need something like updateLocationSet here?
}
ret->module = func->module;
ret->base = func->base;
ret->noFullInline = func->noFullInline;
Expand Down Expand Up @@ -181,7 +195,6 @@ void copyModuleItems(const Module& in, Module& out) {
// to map file name indices from this modules to file name indices in
// the target module.
std::optional<std::vector<Index>> fileIndexMap;
// TODO: Update this with symbol names
if (!in.debugInfoFileNames.empty()) {
std::unordered_map<std::string, Index> debugInfoFileIndices;
for (Index i = 0; i < out.debugInfoFileNames.size(); i++) {
Expand All @@ -200,8 +213,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
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.
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
4 changes: 2 additions & 2 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2506,9 +2506,9 @@ void PrintSExpression::printDebugLocation(
o << ";;@ " << fileName << ":" << location->lineNumber << ":"
<< location->columnNumber;

if (location->nameIndex.has_value()) {
if (location->symbolNameIndex.has_value()) {
auto symbolName =
currModule->debugInfoSymbolNames[*(location->nameIndex)];
currModule->debugInfoSymbolNames[*(location->symbolNameIndex)];
o << ":" << symbolName;
}

Expand Down
6 changes: 3 additions & 3 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2072,10 +2072,10 @@ class Function : public Importable {
// Source maps debugging info: map expression nodes to their file, line, col.
struct DebugLocation {
BinaryLocation fileIndex, lineNumber, columnNumber;
std::optional<BinaryLocation> nameIndex;
std::optional<BinaryLocation> symbolNameIndex;
bool operator==(const DebugLocation& other) const {
return fileIndex == other.fileIndex && lineNumber == other.lineNumber &&
columnNumber == other.columnNumber && nameIndex == other.nameIndex;
columnNumber == other.columnNumber && symbolNameIndex == other.symbolNameIndex;
}
bool operator!=(const DebugLocation& other) const {
return !(*this == other);
Expand All @@ -2085,7 +2085,7 @@ class Function : public Importable {
: lineNumber != other.lineNumber ? lineNumber < other.lineNumber
: columnNumber != other.columnNumber
? columnNumber < other.columnNumber
: nameIndex < other.nameIndex;
: symbolNameIndex < other.symbolNameIndex;
}
};
// One can explicitly set the debug location of an expression to
Expand Down
21 changes: 11 additions & 10 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ void WasmBinaryWriter::writeSourceMapEpilog() {
BinaryLocation lastFileIndex = 0;
BinaryLocation lastLineNumber = 1;
BinaryLocation lastColumnNumber = 0;
BinaryLocation lastNameIndex = 0;
BinaryLocation lastSymbolNameIndex = 0;
for (const auto& [offset, loc] : sourceMapLocations) {
if (lastOffset > 0) {
*sourceMap << ",";
Expand All @@ -1276,9 +1276,10 @@ void WasmBinaryWriter::writeSourceMapEpilog() {
writeBase64VLQ(*sourceMap, int32_t(loc->columnNumber - lastColumnNumber));
lastColumnNumber = loc->columnNumber;

if (loc->nameIndex) {
writeBase64VLQ(*sourceMap, int32_t(*loc->nameIndex - lastNameIndex));
lastNameIndex = *loc->nameIndex;
if (loc->symbolNameIndex) {
writeBase64VLQ(*sourceMap,
int32_t(*loc->symbolNameIndex - lastSymbolNameIndex));
lastSymbolNameIndex = *loc->symbolNameIndex;
}
}
}
Expand Down Expand Up @@ -2938,12 +2939,12 @@ void WasmBinaryReader::readSourceMapHeader() {
uint32_t lineNumber =
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
uint32_t columnNumber = readBase64VLQ(*sourceMap);
std::optional<BinaryLocation> nameIndex;
std::optional<BinaryLocation> symbolNameIndex;
peek = sourceMap->peek();
if (!(peek == ',' || peek == '\"')) {
nameIndex = readBase64VLQ(*sourceMap);
symbolNameIndex = readBase64VLQ(*sourceMap);
}
nextDebugLocation = {fileIndex, lineNumber, columnNumber, nameIndex};
nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
nextDebugLocationHasDebugInfo = true;
}
}
Expand Down Expand Up @@ -2998,13 +2999,13 @@ void WasmBinaryReader::readNextDebugLocation() {
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
uint32_t columnNumber = nextDebugLocation.columnNumber + columnNumberDelta;

std::optional<BinaryLocation> nameIndex;
std::optional<BinaryLocation> symbolNameIndex;
peek = sourceMap->peek();
if (!(peek == ',' || peek == '\"')) {
nameIndex = readBase64VLQ(*sourceMap);
symbolNameIndex = readBase64VLQ(*sourceMap);
}

nextDebugLocation = {fileIndex, lineNumber, columnNumber, nameIndex};
nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
nextDebugLocationHasDebugInfo = true;
}
}
Expand Down

0 comments on commit f92899c

Please sign in to comment.