Skip to content

Commit b439c5e

Browse files
committed
Revert "Followup to #6061"
This reverts commit 2a4c278.
1 parent d401275 commit b439c5e

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/passes/OnceReduction.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -486,16 +486,14 @@ struct OnceReduction : public Pass {
486486
ExpressionManipulator::nop(body);
487487
continue;
488488
}
489-
// The early-exit logic is the first item, followed by the global setting,
490-
// and we've ruled out the case of there being nothing else after those,
491-
// so there are at least 3 items.
492-
assert(list.size() >= 3);
493-
// We consider the first item in the payload. Anything further would be
494-
// much more difficult to analyze.
495-
auto* payloadStart = list[2];
496-
if (auto* call = payloadStart->dynCast<Call>()) {
489+
if (list.size() != 3) {
490+
// Something non-trivial; too many items for us to consider.
491+
continue;
492+
}
493+
auto* payload = list[2];
494+
if (auto* call = payload->dynCast<Call>()) {
497495
if (optInfo.onceFuncs.at(call->target).is()) {
498-
// This "once" function immediately calls another. We do not need the
496+
// All this "once" function does is call another. We do not need the
499497
// early-exit logic in this one, then, because of the following
500498
// reasoning. We are comparing these forms:
501499
//
@@ -504,29 +502,27 @@ struct OnceReduction : public Pass {
504502
// if (!foo$once) return; // two lines of
505503
// foo$once = 1; // early-exit code
506504
// bar();
507-
// ..
508505
// }
509506
//
510507
// to
511508
//
512509
// // AFTER
513510
// function foo() {
514511
// bar();
515-
// ..
516512
// }
517513
//
518514
// The question is whether different behavior can be observed between
519515
// those two. There are two cases, when we enter foo:
520516
//
521-
// 1. foo has been entered before. Then we early-exit in BEFORE, and
517+
// 1. foo has been called before. Then we early-exit in BEFORE, and
522518
// in AFTER we call bar which will early-exit (since foo was
523519
// called, which means bar was at least entered, which set its
524520
// global; bar might be on the stack, if it called foo, so it has
525521
// not necessarily fully executed - this is a tricky situation to
526522
// handle in general, like recursive imports of modules in various
527523
// languages - but we do know bar has been *entered*, which means
528524
// the global was set).
529-
// 2. foo has never been entered before. In this case in BEFORE we set
525+
// 2. foo has never been called before. In this case in BEFORE we set
530526
// the global and call bar, and in AFTER we also call bar.
531527
//
532528
// Thus, the behavior is the same, and we can remove the early-exit

test/lit/passes/once-reduction.wast

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,13 @@
15911591

15921592

15931593
;; CHECK: (func $once (type $0)
1594-
;; CHECK-NEXT: (nop)
1595-
;; CHECK-NEXT: (nop)
1594+
;; CHECK-NEXT: (if
1595+
;; CHECK-NEXT: (global.get $once)
1596+
;; CHECK-NEXT: (return)
1597+
;; CHECK-NEXT: )
1598+
;; CHECK-NEXT: (global.set $once
1599+
;; CHECK-NEXT: (i32.const 1)
1600+
;; CHECK-NEXT: )
15961601
;; CHECK-NEXT: (call $once.1)
15971602
;; CHECK-NEXT: (call $once.2)
15981603
;; CHECK-NEXT: (call $import
@@ -1605,9 +1610,6 @@
16051610
(return)
16061611
)
16071612
(global.set $once (i32.const 1))
1608-
;; We immediately call another "once" function, so we can remove the early-
1609-
;; exit logic before us. (Note that $once.1 and $once.2 call us, but there
1610-
;; we cannot remove anything because of the risk of infinite looping.)
16111613
(call $once.1)
16121614
;; We cannot remove this second call. While $once.1 calls $once.2, we may
16131615
;; be in this situation: a call started at $once.1, which calls $once

0 commit comments

Comments
 (0)