Skip to content

Commit 90b9b94

Browse files
committed
loads may trap, do not remove them in vacuum unless the pass options allow that
1 parent 5c4c6ba commit 90b9b94

10 files changed

Lines changed: 449 additions & 63 deletions

src/passes/Vacuum.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ struct Vacuum : public WalkerPass<PostWalker<Vacuum>> {
7373
case Expression::Id::UnreachableId: return curr; // always needed
7474

7575
case Expression::Id::LoadId: {
76-
if (!resultUsed) {
76+
// it is ok to remove a load if the result is not used, and it has no
77+
// side effects (the load itself may trap, if we are not ignoring such things)
78+
if (!resultUsed && !EffectAnalyzer(getPassOptions(), curr).hasSideEffects()) {
7779
return curr->cast<Load>()->ptr;
7880
}
7981
return curr;

test/emcc_hello_world.fromasm

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,17 +2819,21 @@
28192819
)
28202820
(i32.const 10)
28212821
)
2822-
(set_local $6
2823-
(i32.add
2824-
(get_local $3)
2825-
(i32.shl
2822+
(drop
2823+
(i32.load offset=4
2824+
(tee_local $6
28262825
(i32.add
2827-
(i32.load8_s
2828-
(get_local $6)
2826+
(get_local $3)
2827+
(i32.shl
2828+
(i32.add
2829+
(i32.load8_s
2830+
(get_local $6)
2831+
)
2832+
(i32.const -48)
2833+
)
2834+
(i32.const 3)
28292835
)
2830-
(i32.const -48)
28312836
)
2832-
(i32.const 3)
28332837
)
28342838
)
28352839
)
@@ -3157,17 +3161,21 @@
31573161
)
31583162
(i32.const 10)
31593163
)
3160-
(set_local $6
3161-
(i32.add
3162-
(get_local $3)
3163-
(i32.shl
3164+
(drop
3165+
(i32.load offset=4
3166+
(tee_local $6
31643167
(i32.add
3165-
(i32.load8_s
3166-
(get_local $6)
3168+
(get_local $3)
3169+
(i32.shl
3170+
(i32.add
3171+
(i32.load8_s
3172+
(get_local $6)
3173+
)
3174+
(i32.const -48)
3175+
)
3176+
(i32.const 3)
31673177
)
3168-
(i32.const -48)
31693178
)
3170-
(i32.const 3)
31713179
)
31723180
)
31733181
)
@@ -3885,8 +3893,12 @@
38853893
)
38863894
(br $__rjti$4)
38873895
)
3888-
(set_local $5
3889-
(get_local $19)
3896+
(drop
3897+
(i32.load offset=4
3898+
(tee_local $5
3899+
(get_local $19)
3900+
)
3901+
)
38903902
)
38913903
(i32.store8
38923904
(get_local $40)
@@ -3935,8 +3947,12 @@
39353947
)
39363948
(br $__rjti$5)
39373949
)
3938-
(set_local $5
3939-
(get_local $19)
3950+
(drop
3951+
(i32.load offset=4
3952+
(tee_local $5
3953+
(get_local $19)
3954+
)
3955+
)
39403956
)
39413957
(i32.store
39423958
(get_local $41)
@@ -3993,6 +4009,11 @@
39934009
(get_global $tempDoublePtr)
39944010
(get_local $15)
39954011
)
4012+
(drop
4013+
(i32.load
4014+
(get_global $tempDoublePtr)
4015+
)
4016+
)
39964017
(set_local $31
39974018
(if (result i32)
39984019
(i32.lt_s
@@ -4045,6 +4066,11 @@
40454066
(get_global $tempDoublePtr)
40464067
(get_local $15)
40474068
)
4069+
(drop
4070+
(i32.load
4071+
(get_global $tempDoublePtr)
4072+
)
4073+
)
40484074
(set_local $7
40494075
(block $do-once49 (result i32)
40504076
(if (result i32)

test/emcc_hello_world.fromasm.clamp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,17 +2843,21 @@
28432843
)
28442844
(i32.const 10)
28452845
)
2846-
(set_local $6
2847-
(i32.add
2848-
(get_local $3)
2849-
(i32.shl
2846+
(drop
2847+
(i32.load offset=4
2848+
(tee_local $6
28502849
(i32.add
2851-
(i32.load8_s
2852-
(get_local $6)
2850+
(get_local $3)
2851+
(i32.shl
2852+
(i32.add
2853+
(i32.load8_s
2854+
(get_local $6)
2855+
)
2856+
(i32.const -48)
2857+
)
2858+
(i32.const 3)
28532859
)
2854-
(i32.const -48)
28552860
)
2856-
(i32.const 3)
28572861
)
28582862
)
28592863
)
@@ -3181,17 +3185,21 @@
31813185
)
31823186
(i32.const 10)
31833187
)
3184-
(set_local $6
3185-
(i32.add
3186-
(get_local $3)
3187-
(i32.shl
3188+
(drop
3189+
(i32.load offset=4
3190+
(tee_local $6
31883191
(i32.add
3189-
(i32.load8_s
3190-
(get_local $6)
3192+
(get_local $3)
3193+
(i32.shl
3194+
(i32.add
3195+
(i32.load8_s
3196+
(get_local $6)
3197+
)
3198+
(i32.const -48)
3199+
)
3200+
(i32.const 3)
31913201
)
3192-
(i32.const -48)
31933202
)
3194-
(i32.const 3)
31953203
)
31963204
)
31973205
)
@@ -3909,8 +3917,12 @@
39093917
)
39103918
(br $__rjti$4)
39113919
)
3912-
(set_local $5
3913-
(get_local $19)
3920+
(drop
3921+
(i32.load offset=4
3922+
(tee_local $5
3923+
(get_local $19)
3924+
)
3925+
)
39143926
)
39153927
(i32.store8
39163928
(get_local $40)
@@ -3959,8 +3971,12 @@
39593971
)
39603972
(br $__rjti$5)
39613973
)
3962-
(set_local $5
3963-
(get_local $19)
3974+
(drop
3975+
(i32.load offset=4
3976+
(tee_local $5
3977+
(get_local $19)
3978+
)
3979+
)
39643980
)
39653981
(i32.store
39663982
(get_local $41)
@@ -4017,6 +4033,11 @@
40174033
(get_global $tempDoublePtr)
40184034
(get_local $15)
40194035
)
4036+
(drop
4037+
(i32.load
4038+
(get_global $tempDoublePtr)
4039+
)
4040+
)
40204041
(set_local $31
40214042
(if (result i32)
40224043
(i32.lt_s
@@ -4069,6 +4090,11 @@
40694090
(get_global $tempDoublePtr)
40704091
(get_local $15)
40714092
)
4093+
(drop
4094+
(i32.load
4095+
(get_global $tempDoublePtr)
4096+
)
4097+
)
40724098
(set_local $7
40734099
(block $do-once49 (result i32)
40744100
(if (result i32)

test/emcc_hello_world.fromasm.imprecise

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,17 +2763,21 @@
27632763
)
27642764
(i32.const 10)
27652765
)
2766-
(set_local $6
2767-
(i32.add
2768-
(get_local $3)
2769-
(i32.shl
2766+
(drop
2767+
(i32.load offset=4
2768+
(tee_local $6
27702769
(i32.add
2771-
(i32.load8_s
2772-
(get_local $6)
2770+
(get_local $3)
2771+
(i32.shl
2772+
(i32.add
2773+
(i32.load8_s
2774+
(get_local $6)
2775+
)
2776+
(i32.const -48)
2777+
)
2778+
(i32.const 3)
27732779
)
2774-
(i32.const -48)
27752780
)
2776-
(i32.const 3)
27772781
)
27782782
)
27792783
)
@@ -3102,17 +3106,21 @@
31023106
)
31033107
(i32.const 10)
31043108
)
3105-
(set_local $6
3106-
(i32.add
3107-
(get_local $3)
3108-
(i32.shl
3109+
(drop
3110+
(i32.load offset=4
3111+
(tee_local $6
31093112
(i32.add
3110-
(i32.load8_s
3111-
(get_local $6)
3113+
(get_local $3)
3114+
(i32.shl
3115+
(i32.add
3116+
(i32.load8_s
3117+
(get_local $6)
3118+
)
3119+
(i32.const -48)
3120+
)
3121+
(i32.const 3)
31123122
)
3113-
(i32.const -48)
31143123
)
3115-
(i32.const 3)
31163124
)
31173125
)
31183126
)
@@ -3830,8 +3838,12 @@
38303838
)
38313839
(br $__rjti$4)
38323840
)
3833-
(set_local $5
3834-
(get_local $19)
3841+
(drop
3842+
(i32.load offset=4
3843+
(tee_local $5
3844+
(get_local $19)
3845+
)
3846+
)
38353847
)
38363848
(i32.store8
38373849
(get_local $40)
@@ -3880,8 +3892,12 @@
38803892
)
38813893
(br $__rjti$5)
38823894
)
3883-
(set_local $5
3884-
(get_local $19)
3895+
(drop
3896+
(i32.load offset=4
3897+
(tee_local $5
3898+
(get_local $19)
3899+
)
3900+
)
38853901
)
38863902
(i32.store
38873903
(get_local $41)

test/passes/vacuum.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,17 @@
247247
)
248248
(unreachable)
249249
)
250+
(func $load-may-have-side-effects (type $3) (result i32)
251+
(i64.ge_s
252+
(block $block (result i64)
253+
(drop
254+
(i64.load32_s
255+
(i32.const 678585719)
256+
)
257+
)
258+
(i64.const 2912825531628789796)
259+
)
260+
(i64.const 0)
261+
)
262+
)
250263
)

test/passes/vacuum.wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,20 @@
564564
)
565565
(unreachable)
566566
)
567+
(func $load-may-have-side-effects (result i32)
568+
(i64.ge_s
569+
(block (result i64)
570+
(drop
571+
(i64.eq
572+
(i64.load32_s
573+
(i32.const 678585719)
574+
)
575+
(i64.const 8097879367757079605)
576+
)
577+
)
578+
(i64.const 2912825531628789796)
579+
)
580+
(i64.const 0)
581+
)
582+
)
567583
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func (result i32)))
3+
(memory $0 0)
4+
(func $load-would-normally-have-side-effects (type $0) (result i32)
5+
(i64.ge_s
6+
(i64.const 2912825531628789796)
7+
(i64.const 0)
8+
)
9+
)
10+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(module
2+
(func $load-would-normally-have-side-effects (result i32)
3+
(i64.ge_s
4+
(block (result i64)
5+
(drop
6+
(i64.eq
7+
(i64.load32_s
8+
(i32.const 678585719)
9+
)
10+
(i64.const 8097879367757079605)
11+
)
12+
)
13+
(i64.const 2912825531628789796)
14+
)
15+
(i64.const 0)
16+
)
17+
)
18+
)
19+

0 commit comments

Comments
 (0)