Skip to content

Commit c0b040d

Browse files
author
Jesse Chen
committed
fix: source map generation and print with depth
1 parent 5beebc6 commit c0b040d

File tree

6 files changed

+126
-14
lines changed

6 files changed

+126
-14
lines changed

src/passes/Print.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Print out text in s-expression format
1919
//
2020

21+
#include <cstdint>
2122
#include <ir/iteration.h>
2223
#include <ir/module-utils.h>
2324
#include <ir/table-utils.h>
@@ -2532,7 +2533,7 @@ struct PrintExpressionContents
25322533
// internal contents and the nested children.
25332534
struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
25342535
std::ostream& o;
2535-
unsigned indent = 0;
2536+
uint32_t indent = 0U;
25362537

25372538
bool minify;
25382539
const char* maybeSpace;
@@ -2547,6 +2548,10 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
25472548
Module* currModule = nullptr;
25482549
Function* currFunction = nullptr;
25492550
Function::DebugLocation lastPrintedLocation;
2551+
uint32_t lastPrintedIndent =
2552+
static_cast<uint32_t>(-1); ///< last print indent, child will not duplicate
2553+
///< the debug location, siblings will not miss
2554+
///< the debug location
25502555
bool debugInfo;
25512556

25522557
// Used to print delegate's depth argument when it throws to the caller
@@ -2560,10 +2565,17 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
25602565
}
25612566

25622567
void printDebugLocation(const Function::DebugLocation& location) {
2563-
if (lastPrintedLocation == location) {
2568+
// ;; debug location
2569+
// (parent
2570+
// (child) ;; no debug location
2571+
// )
2572+
// ;; debug location
2573+
// (siblings)
2574+
if (lastPrintedLocation == location && indent > lastPrintedIndent) {
25642575
return;
25652576
}
25662577
lastPrintedLocation = location;
2578+
lastPrintedIndent = indent;
25672579
auto fileName = currModule->debugInfoFileNames[location.fileIndex];
25682580
o << ";;@ " << fileName << ":" << location.lineNumber << ":"
25692581
<< location.columnNumber << '\n';

src/wasm-binary.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,6 @@ class WasmBinaryWriter {
13431343
void writeDylinkSection();
13441344
void writeLegacyDylinkSection();
13451345

1346-
void initializeDebugInfo();
13471346
void writeSourceMapProlog();
13481347
void writeSourceMapEpilog();
13491348
void writeDebugLocation(const Function::DebugLocation& loc);
@@ -1404,7 +1403,6 @@ class WasmBinaryWriter {
14041403
std::vector<std::pair<size_t, const Function::DebugLocation*>>
14051404
sourceMapLocations;
14061405
size_t sourceMapLocationsSizeAtSectionStart;
1407-
Function::DebugLocation lastDebugLocation;
14081406

14091407
std::unique_ptr<ImportInfo> importInfo;
14101408

src/wasm-stack.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "wasm-binary.h"
2525
#include "wasm-traversal.h"
2626
#include "wasm.h"
27+
#include <cstdint>
2728

2829
namespace wasm {
2930

@@ -154,7 +155,10 @@ class BinaryInstWriter : public OverriddenVisitor<BinaryInstWriter> {
154155
template<typename SubType>
155156
class BinaryenIRWriter : public Visitor<BinaryenIRWriter<SubType>> {
156157
public:
157-
BinaryenIRWriter(Function* func) : func(func) {}
158+
BinaryenIRWriter(Function* func)
159+
: func(func), lastDebugLocation({static_cast<uint32_t>(-1),
160+
static_cast<uint32_t>(-1),
161+
static_cast<uint32_t>(-1)}) {}
158162

159163
void write();
160164

@@ -168,6 +172,10 @@ class BinaryenIRWriter : public Visitor<BinaryenIRWriter<SubType>> {
168172

169173
protected:
170174
Function* func = nullptr;
175+
Function::DebugLocation lastDebugLocation; ///< last debug location
176+
uint32_t lastDebugLocationDepth =
177+
static_cast<uint32_t>(-1); ///< last debug location depth
178+
uint32_t depth = 0U; ///< depth of current write cursor
171179

172180
private:
173181
void emit(Expression* curr) { static_cast<SubType*>(this)->emit(curr); }
@@ -239,6 +247,7 @@ void BinaryenIRWriter<SubType>::visit(Expression* curr) {
239247
// unreachable block is a source of unreachability, which means we don't need
240248
// to emit an extra `unreachable` before the end of the block to prevent type
241249
// errors.
250+
depth++;
242251
bool hasUnreachableChild = false;
243252
for (auto* child : ValueChildIterator(curr)) {
244253
visit(child);
@@ -251,7 +260,22 @@ void BinaryenIRWriter<SubType>::visit(Expression* curr) {
251260
// `curr` is not reachable, so don't emit it.
252261
return;
253262
}
254-
emitDebugLocation(curr);
263+
264+
if (func != nullptr) {
265+
const auto& debugLocationIterator = func->debugLocations.find(curr);
266+
if (debugLocationIterator != func->debugLocations.cend()) {
267+
if (lastDebugLocation != debugLocationIterator->second ||
268+
depth < lastDebugLocationDepth) {
269+
emitDebugLocation(curr);
270+
lastDebugLocationDepth = depth;
271+
lastDebugLocation = debugLocationIterator->second;
272+
}
273+
}
274+
} else {
275+
emitDebugLocation(
276+
curr); // for global expression write, no func value, directly write it
277+
}
278+
depth--;
255279
// Control flow requires special handling, but most instructions can be
256280
// emitted directly after their children.
257281
if (Properties::isControlFlowStructure(curr)) {

src/wasm/wasm-binary.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ void WasmBinaryWriter::write() {
4343

4444
writeDylinkSection();
4545

46-
initializeDebugInfo();
4746
if (sourceMap) {
4847
writeSourceMapProlog();
4948
}
@@ -1120,9 +1119,6 @@ void WasmBinaryWriter::writeSymbolMap() {
11201119
file.close();
11211120
}
11221121

1123-
void WasmBinaryWriter::initializeDebugInfo() {
1124-
lastDebugLocation = {0, /* lineNumber = */ 1, 0};
1125-
}
11261122

11271123
void WasmBinaryWriter::writeSourceMapProlog() {
11281124
*sourceMap << "{\"version\":3,\"sources\":[";
@@ -1304,12 +1300,8 @@ void WasmBinaryWriter::writeDylinkSection() {
13041300
}
13051301

13061302
void WasmBinaryWriter::writeDebugLocation(const Function::DebugLocation& loc) {
1307-
if (loc == lastDebugLocation) {
1308-
return;
1309-
}
13101303
auto offset = o.size();
13111304
sourceMapLocations.emplace_back(offset, &loc);
1312-
lastDebugLocation = loc;
13131305
}
13141306

13151307
void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {

test/fib-dbg.wasm.fromBinary

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
(i32.const 0)
134134
)
135135
)
136+
;;@ fib.c:3:0
136137
(if
137138
(local.get $6)
138139
(block
@@ -156,6 +157,7 @@
156157
)
157158
)
158159
)
160+
;;@ fib.c:8:0
159161
(loop $label$4
160162
(block $label$5
161163
;;@ fib.c:4:0
@@ -172,12 +174,14 @@
172174
(i32.const 1)
173175
)
174176
)
177+
;;@ fib.c:3:0
175178
(local.set $7
176179
(i32.eq
177180
(local.get $9)
178181
(local.get $0)
179182
)
180183
)
184+
;;@ fib.c:3:0
181185
(if
182186
(local.get $7)
183187
(block
@@ -201,6 +205,7 @@
201205
)
202206
)
203207
)
208+
;;@ fib.c:3:0
204209
(br $label$4)
205210
)
206211
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
;; RUN: wasm-opt %s -o %t.wasm -osm %t.map -g -q
2+
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s
3+
4+
(module
5+
(func $foo (param $x i32) (param $y i32)
6+
;;@ src.cpp:1:1
7+
(if
8+
(i32.add
9+
(local.get $x)
10+
;;@ src.cpp:2:1
11+
(local.get $y)
12+
)
13+
;;@ src.cpp:3:1
14+
(return)
15+
)
16+
;;@ src.cpp:1:1
17+
(block
18+
;;@ src.cpp:4:1
19+
(drop
20+
(i32.add
21+
(local.get $x)
22+
;;@ src.cpp:5:1
23+
(local.get $y)
24+
)
25+
)
26+
(drop
27+
(i32.sub
28+
(local.get $x)
29+
(local.get $y)
30+
)
31+
)
32+
)
33+
(drop
34+
(i32.mul
35+
(local.get $x)
36+
;;@ src.cpp:6:1
37+
(local.get $y)
38+
)
39+
)
40+
(return)
41+
)
42+
)
43+
44+
;; CHECK: (func $foo (param $x i32) (param $y i32)
45+
;; CHECK-NEXT: ;;@ src.cpp:1:1
46+
;; CHECK-NEXT: (if
47+
;; CHECK-NEXT: (i32.add
48+
;; CHECK-NEXT: (local.get $x)
49+
;; CHECK-NEXT: ;;@ src.cpp:2:1
50+
;; CHECK-NEXT: (local.get $y)
51+
;; CHECK-NEXT: )
52+
;; CHECK-NEXT: ;;@ src.cpp:3:1
53+
;; CHECK-NEXT: (return)
54+
;; CHECK-NEXT: )
55+
;; CHECK-NEXT: ;;@ src.cpp:4:1
56+
;; CHECK-NEXT: (drop
57+
;; CHECK-NEXT: (i32.add
58+
;; CHECK-NEXT: (local.get $x)
59+
;; CHECK-NEXT: ;;@ src.cpp:5:1
60+
;; CHECK-NEXT: (local.get $y)
61+
;; CHECK-NEXT: )
62+
;; CHECK-NEXT: )
63+
;; CHECK-NEXT: ;;@ src.cpp:4:1
64+
;; CHECK-NEXT: (drop
65+
;; CHECK-NEXT: (i32.sub
66+
;; CHECK-NEXT: (local.get $x)
67+
;; CHECK-NEXT: (local.get $y)
68+
;; CHECK-NEXT: )
69+
;; CHECK-NEXT: )
70+
;; CHECK-NEXT: ;;@ src.cpp:1:1
71+
;; CHECK-NEXT: (drop
72+
;; CHECK-NEXT: (i32.mul
73+
;; CHECK-NEXT: (local.get $x)
74+
;; CHECK-NEXT: ;;@ src.cpp:6:1
75+
;; CHECK-NEXT: (local.get $y)
76+
;; CHECK-NEXT: )
77+
;; CHECK-NEXT: )
78+
;; CHECK-NEXT: ;;@ src.cpp:1:1
79+
;; CHECK-NEXT: (return)
80+
;; CHECK-NEXT: )
81+
;; CHECK-NEXT:)

0 commit comments

Comments
 (0)