Skip to content

Commit de9a868

Browse files
authored
fix a dce fuzz bug where if changed to unreachable but didn't propagate that effect up. also add set_global support in dce (#1218)
1 parent 58d13ae commit de9a868

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

src/ast/type-updating.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ struct TypeUpdater : public ExpressionStackWalker<TypeUpdater, UnifiedExpression
266266
}
267267
}
268268
}
269+
270+
// efficiently update the type of an if, given the data we know. this
271+
// can remove a concrete type and turn the if unreachable when it is
272+
// unreachable
273+
void maybeUpdateTypeToUnreachable(If* curr) {
274+
if (!isConcreteWasmType(curr->type)) {
275+
return; // nothing concrete to change to unreachable
276+
}
277+
curr->finalize();
278+
if (curr->type == unreachable) {
279+
propagateTypesUp(curr);
280+
}
281+
}
269282
};
270283

271284
} // namespace wasm

src/passes/DeadCodeElimination.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct DeadCodeElimination : public WalkerPass<PostWalker<DeadCodeElimination>>
216216
replaceCurrent(curr->condition);
217217
}
218218
// the if may have had a type, but can now be unreachable, which allows more reduction outside
219-
curr->finalize();
219+
typeUpdater.maybeUpdateTypeToUnreachable(curr);
220220
}
221221

222222
static void scan(DeadCodeElimination* self, Expression** currp) {
@@ -351,6 +351,10 @@ struct DeadCodeElimination : public WalkerPass<PostWalker<DeadCodeElimination>>
351351
blockifyReachableOperands({ curr->value }, curr->type);
352352
}
353353

354+
void visitSetGlobal(SetGlobal* curr) {
355+
blockifyReachableOperands({ curr->value }, curr->type);
356+
}
357+
354358
void visitLoad(Load* curr) {
355359
blockifyReachableOperands({ curr->ptr }, curr->type);
356360
}

test/passes/dce.txt

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -444,28 +444,19 @@
444444
)
445445
)
446446
(func $replace-with-unreachable-affects-parent (type $5) (param $var$0 f32) (param $var$1 i64)
447-
(block $top
448-
(drop
449-
(f32.load offset=4
450-
(block (result i32)
451-
(drop
452-
(i64.const 0)
453-
)
454-
(if
455-
(block $block (result i32)
456-
(call $replace-with-unreachable-affects-parent
457-
(f32.const 1)
458-
(i64.const -15917430362925035)
459-
)
460-
(i32.const 1)
461-
)
462-
(unreachable)
463-
(unreachable)
464-
)
465-
)
447+
(drop
448+
(i64.const 0)
449+
)
450+
(if
451+
(block $block (result i32)
452+
(call $replace-with-unreachable-affects-parent
453+
(f32.const 1)
454+
(i64.const -15917430362925035)
466455
)
456+
(i32.const 1)
467457
)
468458
(unreachable)
459+
(unreachable)
469460
)
470461
)
471462
(func $replace-block-changes-later-when-if-goes (type $1)
@@ -488,3 +479,27 @@
488479
(i32.const 0)
489480
)
490481
)
482+
(module
483+
(type $0 (func))
484+
(global $global (mut f64) (f64.const 0))
485+
(memory $0 0)
486+
(func $0 (type $0)
487+
(if
488+
(i32.const 0)
489+
(unreachable)
490+
(unreachable)
491+
)
492+
)
493+
)
494+
(module
495+
(type $0 (func))
496+
(memory $0 0)
497+
(func $0 (type $0)
498+
(local $local f64)
499+
(if
500+
(i32.const 0)
501+
(unreachable)
502+
(unreachable)
503+
)
504+
)
505+
)

test/passes/dce.wast

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,29 @@
708708
(i32.const 0)
709709
)
710710
)
711+
;; if goes to unreachable, need to propagate that up to the set_global
712+
(module
713+
(global $global (mut f64) (f64.const 0))
714+
(func $0
715+
(set_global $global
716+
(if (result f64)
717+
(i32.const 0)
718+
(unreachable)
719+
(unreachable)
720+
)
721+
)
722+
)
723+
)
724+
(module
725+
(func $0
726+
(local $local f64)
727+
(set_local $local
728+
(if (result f64)
729+
(i32.const 0)
730+
(unreachable)
731+
(unreachable)
732+
)
733+
)
734+
)
735+
)
736+

0 commit comments

Comments
 (0)