Skip to content

Commit 5d36dab

Browse files
committed
Enable StackIR optimizations in release mode and for generating WAT
In WebAssembly/binaryen#6568, StackIR was changed from a set of passes to a global option. This meant that the `true` argument passed into _BinaryenModuleAllocateAndWriteStackIR (to optimize the StackIR) no longer had an effect. This commit restores that and also runs the optimizations under the same {optimize,shrink}Levels as wasm-opt.
1 parent 41a697f commit 5d36dab

File tree

221 files changed

+86421
-92528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+86421
-92528
lines changed

cli/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,10 @@ export async function main(argv, options) {
945945
try {
946946
// use superset text format when extension is `.wast`.
947947
// Otherwise use official stack IR format (wat).
948+
binaryen.setOptimizeStackIR(true);
948949
out = opts.textFile?.endsWith(".wast")
949950
? binaryenModule.emitText()
950-
: binaryenModule.emitStackIR(true);
951+
: binaryenModule.emitStackIR();
951952
} catch (e) {
952953
crash("emitText", e);
953954
}

src/module.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,22 @@ export class Module {
25032503
binaryen._BinaryenSetFastMath(on);
25042504
}
25052505

2506+
getGenerateStackIR(): bool {
2507+
return binaryen._BinaryenGetGenerateStackIR();
2508+
}
2509+
2510+
setGenerateStackIR(on: bool): void {
2511+
binaryen._BinaryenSetGenerateStackIR(on);
2512+
}
2513+
2514+
getOptimizeStackIR(): bool {
2515+
return binaryen._BinaryenGetOptimizeStackIR();
2516+
}
2517+
2518+
setOptimizeStackIR(on: bool): void {
2519+
binaryen._BinaryenSetOptimizeStackIR(on);
2520+
}
2521+
25062522
getPassArgument(key: string): string | null {
25072523
let cStr = this.allocStringCached(key);
25082524
let ptr = binaryen._BinaryenGetPassArgument(cStr);
@@ -2594,6 +2610,11 @@ export class Module {
25942610
this.setFastMath(true);
25952611
this.clearPassArguments();
25962612

2613+
// OptimizationOptions#parse in src/tools/optimization-options.h
2614+
const stackIR = optimizeLevel >= 2 || shrinkLevel >= 1;
2615+
this.setGenerateStackIR(stackIR);
2616+
this.setOptimizeStackIR(stackIR);
2617+
25972618
// Tweak inlining limits based on optimization levels
25982619
if (optimizeLevel >= 2 && shrinkLevel == 0) {
25992620
this.setAlwaysInlineMaxSize(12);

tests/compiler/NonNullable.debug.wat

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,38 @@
105105
end
106106
end
107107
end
108-
block $while-break|1
109-
loop $while-continue|1
110-
local.get $len
111-
local.tee $7
112-
i32.const 1
113-
i32.sub
114-
local.set $len
115-
local.get $7
108+
loop $while-continue|1
109+
local.get $len
110+
local.tee $7
111+
i32.const 1
112+
i32.sub
113+
local.set $len
114+
local.get $7
115+
if
116+
local.get $ptr1
117+
i32.load16_u
118+
local.set $a
119+
local.get $ptr2
120+
i32.load16_u
121+
local.set $b
122+
local.get $a
123+
local.get $b
124+
i32.ne
116125
if
117-
local.get $ptr1
118-
i32.load16_u
119-
local.set $a
120-
local.get $ptr2
121-
i32.load16_u
122-
local.set $b
123126
local.get $a
124127
local.get $b
125-
i32.ne
126-
if
127-
local.get $a
128-
local.get $b
129-
i32.sub
130-
return
131-
end
132-
local.get $ptr1
133-
i32.const 2
134-
i32.add
135-
local.set $ptr1
136-
local.get $ptr2
137-
i32.const 2
138-
i32.add
139-
local.set $ptr2
140-
br $while-continue|1
128+
i32.sub
129+
return
141130
end
131+
local.get $ptr1
132+
i32.const 2
133+
i32.add
134+
local.set $ptr1
135+
local.get $ptr2
136+
i32.const 2
137+
i32.add
138+
local.set $ptr2
139+
br $while-continue|1
142140
end
143141
end
144142
i32.const 0

tests/compiler/NonNullable.release.wat

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,10 @@
330330
i32.const 0
331331
call $~lib/string/String.__eq
332332
i32.eqz
333-
local.set $0
334333
global.get $~lib/memory/__stack_pointer
335334
i32.const 8
336335
i32.add
337336
global.set $~lib/memory/__stack_pointer
338-
local.get $0
339337
)
340338
(func $NonNullable/assertNonNull<~lib/string/String>
341339
global.get $~lib/memory/__stack_pointer

tests/compiler/assert-nonnull.release.wat

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@
174174
i32.store
175175
local.get $0
176176
i32.load
177-
local.set $0
178177
global.get $~lib/memory/__stack_pointer
179178
i32.const 8
180179
i32.add
@@ -183,7 +182,6 @@
183182
i32.const 4
184183
i32.add
185184
global.set $~lib/memory/__stack_pointer
186-
local.get $0
187185
return
188186
end
189187
i32.const 34256
@@ -453,7 +451,6 @@
453451
i32.eqz
454452
br_if $folding-inner1
455453
global.get $~lib/memory/__stack_pointer
456-
local.set $1
457454
global.get $~lib/memory/__stack_pointer
458455
local.get $0
459456
i32.store offset=4
@@ -468,7 +465,6 @@
468465
global.get $~lib/memory/__stack_pointer
469466
local.get $0
470467
i32.store
471-
local.get $1
472468
local.get $0
473469
i32.load
474470
local.tee $0
@@ -535,7 +531,6 @@
535531
i32.eqz
536532
br_if $folding-inner1
537533
global.get $~lib/memory/__stack_pointer
538-
local.set $1
539534
global.get $~lib/memory/__stack_pointer
540535
local.get $0
541536
i32.store offset=4
@@ -550,7 +545,6 @@
550545
global.get $~lib/memory/__stack_pointer
551546
local.get $0
552547
i32.store
553-
local.get $1
554548
local.get $0
555549
i32.load
556550
local.tee $0
@@ -623,7 +617,6 @@
623617
local.get $0
624618
i32.load
625619
call_indirect (type $1)
626-
drop
627620
unreachable
628621
end
629622
i32.const 34256
@@ -662,7 +655,6 @@
662655
local.get $0
663656
i32.store
664657
global.get $~lib/memory/__stack_pointer
665-
local.set $1
666658
local.get $0
667659
i32.eqz
668660
if
@@ -673,13 +665,11 @@
673665
call $~lib/builtins/abort
674666
unreachable
675667
end
676-
local.get $1
677668
local.get $0
678669
i32.store offset=4
679670
local.get $0
680671
i32.load
681672
call_indirect (type $1)
682-
drop
683673
unreachable
684674
end
685675
i32.const 34256
@@ -723,7 +713,6 @@
723713
local.get $0
724714
i32.load
725715
call_indirect (type $1)
726-
drop
727716
unreachable
728717
end
729718
i32.const 34256
@@ -785,7 +774,6 @@
785774
local.get $0
786775
i32.load
787776
call_indirect (type $1)
788-
drop
789777
unreachable
790778
end
791779
i32.const 34256
@@ -837,7 +825,6 @@
837825
local.get $0
838826
i32.load
839827
call_indirect (type $1)
840-
drop
841828
unreachable
842829
end
843830
i32.const 34256

0 commit comments

Comments
 (0)