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

[StackIR] Support source maps and DWARF with StackIR #6564

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 7 additions & 3 deletions src/wasm-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,21 @@ class StackIRToBinaryWriter {
public:
StackIRToBinaryWriter(WasmBinaryWriter& parent,
BufferWithRandomAccess& o,
Function* func)
: writer(parent, o, func, false /* sourceMap */, false /* DWARF */),
func(func) {}
Function* func,
bool sourceMap = false,
bool DWARF = false)
: parent(parent), writer(parent, o, func, sourceMap, DWARF), func(func),
sourceMap(sourceMap) {}

void write();

MappedLocals& getMappedLocals() { return writer.mappedLocals; }

private:
WasmBinaryWriter& parent;
BinaryInstWriter writer;
Function* func;
bool sourceMap;
};

std::ostream& printStackIR(std::ostream& o, Module* module, bool optimize);
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ void WasmBinaryWriter::writeFunctions() {
size_t start = o.size();
BYN_TRACE("writing" << func->name << std::endl);
// Emit Stack IR if present, and if we can
if (func->stackIR && !sourceMap && !DWARF) {
if (func->stackIR) {
BYN_TRACE("write Stack IR\n");
StackIRToBinaryWriter writer(*this, o, func);
StackIRToBinaryWriter writer(*this, o, func, sourceMap, DWARF);
writer.write();
if (debugInfo) {
funcMappedLocals[func->name] = std::move(writer.getMappedLocals());
Expand Down
12 changes: 12 additions & 0 deletions src/wasm/wasm-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,9 @@ StackInst* StackIRGenerator::makeStackInst(StackInst::Op op,
}

void StackIRToBinaryWriter::write() {
if (func->prologLocation.size()) {
parent.writeDebugLocation(*func->prologLocation.begin());
}
writer.mapLocalsAndEmitHeader();
// Stack to track indices of catches within a try
SmallVector<Index, 4> catchIndexStack;
Expand All @@ -2795,7 +2798,13 @@ void StackIRToBinaryWriter::write() {
case StackInst::IfBegin:
case StackInst::LoopBegin:
case StackInst::TryTableBegin: {
if (sourceMap) {
parent.writeDebugLocation(inst->origin, func);
}
writer.visit(inst->origin);
if (sourceMap) {
parent.writeDebugLocationEnd(inst->origin, func);
}
break;
}
case StackInst::TryEnd:
Expand Down Expand Up @@ -2830,6 +2839,9 @@ void StackIRToBinaryWriter::write() {
WASM_UNREACHABLE("unexpected op");
}
}
if (func->epilogLocation.size()) {
parent.writeDebugLocation(*func->epilogLocation.begin());
}
writer.emitFunctionEnd();
}

Expand Down
4 changes: 3 additions & 1 deletion test/lit/source-map.wast
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
;; RUN: wasm-opt %s -o %t.wasm -osm %t.map -g -q
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s

;; RUN: wasm-opt %s --new-wat-parser -S -o - | filecheck %s
;; Also test with StackIR.
;; RUN: wasm-opt %s --generate-stack-ir -o %t.wasm -osm %t.map -g -q
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s

(module
;;@ src.cpp:0:1
Expand Down
Loading
Loading