Skip to content

Commit 1a483a2

Browse files
authored
Optimize normally with debug info (#1927)
* optimize normally with debug info - some of it may be removed, but that's the price of higher optimization levels, and by optimizing normally in profiling and -g2 etc. builds they are more comparable to normal ones, yielding better data * copy debug locations automatically in replaceCurrent in wasm-traversal, so optimization passes at least by default will preserve debuggability
1 parent 87a6512 commit 1a483a2

File tree

7 files changed

+102
-106
lines changed

7 files changed

+102
-106
lines changed

src/passes/pass.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ void PassRunner::addDefaultFunctionOptimizationPasses() {
156156
add("flatten");
157157
add("local-cse");
158158
}
159-
if (!options.debugInfo) { // debug info must be preserved, do not dce it
160-
add("dce");
161-
}
159+
add("dce");
162160
add("remove-unused-brs");
163161
add("remove-unused-names");
164162
add("optimize-instructions");
@@ -215,14 +213,12 @@ void PassRunner::addDefaultGlobalOptimizationPrePasses() {
215213

216214
void PassRunner::addDefaultGlobalOptimizationPostPasses() {
217215
// inlining/dae+optimizing can remove debug annotations
218-
if (!options.debugInfo) {
219-
if (options.optimizeLevel >= 2 || options.shrinkLevel >= 1) {
220-
add("dae-optimizing");
221-
}
222-
// inline when working hard, and when not preserving debug info
223-
if (options.optimizeLevel >= 2 || options.shrinkLevel >= 2) {
224-
add("inlining-optimizing");
225-
}
216+
if (options.optimizeLevel >= 2 || options.shrinkLevel >= 1) {
217+
add("dae-optimizing");
218+
}
219+
// inline when working hard, and when not preserving debug info
220+
if (options.optimizeLevel >= 2 || options.shrinkLevel >= 2) {
221+
add("inlining-optimizing");
226222
}
227223
add("duplicate-function-elimination"); // optimizations show more functions as duplicate
228224
add("remove-unused-module-elements");

src/wasm-traversal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ struct Walker : public VisitorType {
302302
// just one visit*() method is called by the traversal; if you replace a node,
303303
// and you want to process the output, you must do that explicitly).
304304
Expression* replaceCurrent(Expression* expression) {
305+
// Copy debug info, if present.
306+
if (currFunction) {
307+
auto& debugLocations = currFunction->debugLocations;
308+
if (!debugLocations.empty()) {
309+
auto* curr = getCurrent();
310+
auto iter = debugLocations.find(curr);
311+
if (iter != debugLocations.end()) {
312+
auto location = iter->second;
313+
debugLocations.erase(iter);
314+
debugLocations[expression] = location;
315+
}
316+
}
317+
}
305318
return *replacep = expression;
306319
}
307320

test/debugInfo.fromasm

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,36 @@
2929
(i32.const 1)
3030
)
3131
)
32-
(func $i32s-rem (; 2 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
33-
(if (result i32)
34-
(local.get $1)
35-
(i32.rem_s
36-
(local.get $0)
37-
(local.get $1)
38-
)
39-
(i32.const 0)
40-
)
41-
)
42-
(func $opts (; 3 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
43-
;;@ even-opted.cpp:1:0
44-
(local.set $0
45-
(i32.add
46-
(local.get $0)
47-
(local.get $1)
48-
)
49-
)
50-
;;@ even-opted.cpp:2:0
51-
(local.set $1
52-
(i32.shr_s
53-
(local.get $1)
54-
(local.get $0)
55-
)
56-
)
32+
(func $opts (; 2 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
33+
(local $2 i32)
5734
;;@ even-opted.cpp:3:0
5835
(i32.add
59-
(call $i32s-rem
60-
(local.get $0)
61-
(local.get $1)
36+
(if (result i32)
37+
(local.tee $2
38+
;;@ even-opted.cpp:2:0
39+
(local.tee $1
40+
(i32.shr_s
41+
(local.get $1)
42+
;;@ even-opted.cpp:1:0
43+
(local.tee $0
44+
(i32.add
45+
(local.get $0)
46+
(local.get $1)
47+
)
48+
)
49+
)
50+
)
51+
)
52+
(i32.rem_s
53+
(local.get $0)
54+
(local.get $2)
55+
)
56+
(i32.const 0)
6257
)
6358
(local.get $1)
6459
)
6560
)
66-
(func $fib (; 4 ;) (; has Stack IR ;) (param $0 i32) (result i32)
61+
(func $fib (; 3 ;) (; has Stack IR ;) (param $0 i32) (result i32)
6762
(local $1 i32)
6863
(local $2 i32)
6964
(local $3 i32)
@@ -126,7 +121,7 @@
126121
;;@ fib.c:8:0
127122
(local.get $1)
128123
)
129-
(func $switch_reach (; 5 ;) (; has Stack IR ;) (param $0 i32) (result i32)
124+
(func $switch_reach (; 4 ;) (; has Stack IR ;) (param $0 i32) (result i32)
130125
(local $1 i32)
131126
(local.set $1
132127
(block $__rjto$0 (result i32)
@@ -175,7 +170,7 @@
175170
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
176171
(local.get $1)
177172
)
178-
(func $nofile (; 6 ;) (; has Stack IR ;)
173+
(func $nofile (; 5 ;) (; has Stack IR ;)
179174
;;@ (unknown):1337:0
180175
(call $nofile)
181176
)

test/debugInfo.fromasm.clamp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,36 @@
2929
(i32.const 1)
3030
)
3131
)
32-
(func $i32s-rem (; 2 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
33-
(if (result i32)
34-
(local.get $1)
35-
(i32.rem_s
36-
(local.get $0)
37-
(local.get $1)
38-
)
39-
(i32.const 0)
40-
)
41-
)
42-
(func $opts (; 3 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
43-
;;@ even-opted.cpp:1:0
44-
(local.set $0
45-
(i32.add
46-
(local.get $0)
47-
(local.get $1)
48-
)
49-
)
50-
;;@ even-opted.cpp:2:0
51-
(local.set $1
52-
(i32.shr_s
53-
(local.get $1)
54-
(local.get $0)
55-
)
56-
)
32+
(func $opts (; 2 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
33+
(local $2 i32)
5734
;;@ even-opted.cpp:3:0
5835
(i32.add
59-
(call $i32s-rem
60-
(local.get $0)
61-
(local.get $1)
36+
(if (result i32)
37+
(local.tee $2
38+
;;@ even-opted.cpp:2:0
39+
(local.tee $1
40+
(i32.shr_s
41+
(local.get $1)
42+
;;@ even-opted.cpp:1:0
43+
(local.tee $0
44+
(i32.add
45+
(local.get $0)
46+
(local.get $1)
47+
)
48+
)
49+
)
50+
)
51+
)
52+
(i32.rem_s
53+
(local.get $0)
54+
(local.get $2)
55+
)
56+
(i32.const 0)
6257
)
6358
(local.get $1)
6459
)
6560
)
66-
(func $fib (; 4 ;) (; has Stack IR ;) (param $0 i32) (result i32)
61+
(func $fib (; 3 ;) (; has Stack IR ;) (param $0 i32) (result i32)
6762
(local $1 i32)
6863
(local $2 i32)
6964
(local $3 i32)
@@ -126,7 +121,7 @@
126121
;;@ fib.c:8:0
127122
(local.get $1)
128123
)
129-
(func $switch_reach (; 5 ;) (; has Stack IR ;) (param $0 i32) (result i32)
124+
(func $switch_reach (; 4 ;) (; has Stack IR ;) (param $0 i32) (result i32)
130125
(local $1 i32)
131126
(local.set $1
132127
(block $__rjto$0 (result i32)
@@ -175,7 +170,7 @@
175170
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
176171
(local.get $1)
177172
)
178-
(func $nofile (; 6 ;) (; has Stack IR ;)
173+
(func $nofile (; 5 ;) (; has Stack IR ;)
179174
;;@ (unknown):1337:0
180175
(call $nofile)
181176
)

test/debugInfo.fromasm.clamp.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/debugInfo.fromasm.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/debugInfo.fromasm.read-written

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,38 @@
3232
(i32.const 1)
3333
)
3434
)
35-
(func $i32s-rem (; 2 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
36-
(if (result i32)
37-
(local.get $1)
38-
(i32.rem_s
39-
(local.get $0)
40-
(local.get $1)
41-
)
42-
(i32.const 0)
43-
)
44-
)
45-
(func $opts (; 3 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
46-
;;@ even-opted.cpp:1:0
47-
(local.set $0
48-
(i32.add
49-
(local.get $0)
50-
(local.get $1)
51-
)
52-
)
53-
;;@ even-opted.cpp:2:0
54-
(local.set $1
55-
(i32.shr_s
56-
(local.get $1)
57-
(local.get $0)
58-
)
59-
)
35+
(func $opts (; 2 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
36+
(local $2 i32)
6037
;;@ even-opted.cpp:3:0
6138
(i32.add
62-
(call $i32s-rem
63-
(local.get $0)
64-
(local.get $1)
39+
;;@ even-opted.cpp:1:0
40+
(if (result i32)
41+
(local.tee $2
42+
(local.tee $1
43+
(i32.shr_s
44+
;;@ even-opted.cpp:2:0
45+
(local.get $1)
46+
;;@ even-opted.cpp:1:0
47+
(local.tee $0
48+
(i32.add
49+
(local.get $0)
50+
(local.get $1)
51+
)
52+
)
53+
)
54+
)
55+
)
56+
;;@ even-opted.cpp:3:0
57+
(i32.rem_s
58+
(local.get $0)
59+
(local.get $2)
60+
)
61+
(i32.const 0)
6562
)
6663
(local.get $1)
6764
)
6865
)
69-
(func $fib (; 4 ;) (type $1) (param $0 i32) (result i32)
66+
(func $fib (; 3 ;) (type $1) (param $0 i32) (result i32)
7067
(local $1 i32)
7168
(local $2 i32)
7269
(local $3 i32)
@@ -130,7 +127,7 @@
130127
;;@ fib.c:8:0
131128
(local.get $1)
132129
)
133-
(func $switch_reach (; 5 ;) (type $1) (param $0 i32) (result i32)
130+
(func $switch_reach (; 4 ;) (type $1) (param $0 i32) (result i32)
134131
(local $1 i32)
135132
(local.set $1
136133
(block $label$1 (result i32)
@@ -179,7 +176,7 @@
179176
;;@ /tmp/emscripten_test_binaryen2_28hnAe/src.c:59950:0
180177
(local.get $1)
181178
)
182-
(func $nofile (; 6 ;) (type $2)
179+
(func $nofile (; 5 ;) (type $2)
183180
;;@ (unknown):1337:0
184181
(call $nofile)
185182
)

0 commit comments

Comments
 (0)