Skip to content

Commit 5d050e7

Browse files
authored
ConstraintAnalysis: Fix some trivial asserts (#8893)
1 parent c01b697 commit 5d050e7

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

src/ir/constraint.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,7 @@ void BasicBlockConstraintMap::set(Index index, const Constraint& c) {
288288

289289
// Clear the old state.
290290
eraseStaleRefs(index);
291-
map[index].setProvesNothing();
292-
// Note that this last line puts us in a temporarily invalid state: we do not
293-
// normally store a constraint of proves-nothing in the map. Doing it this
294-
// way, until approximateAnd adds the constraint, is more efficient than
295-
// erasing and re-adding.
291+
map.erase(index);
296292

297293
// Apply the constraint.
298294
approximateAnd(index, c);

src/passes/ConstraintAnalysis.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,14 @@ struct ConstraintAnalysis
187187
// of course not needed at this stage.)
188188
auto& constraints = block->contents.startConstraints;
189189
for (auto** currp : block->contents.actions) {
190-
applyToConstraints(*currp, constraints);
191-
if (constraints.unreachable) {
190+
if (!constraints.unreachable) {
191+
applyToConstraints(*currp, constraints);
192+
optimizeExpression(currp, constraints);
193+
} else {
194+
// This is unreachable code: just mark it so.
192195
*currp = Builder(*getModule()).makeUnreachable();
193196
refinalize = true;
194197
}
195-
optimizeExpression(currp, constraints);
196198
}
197199
}
198200

test/lit/passes/constraint-analysis.wast

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,4 +3472,62 @@
34723472
(br $loop)
34733473
)
34743474
)
3475+
3476+
;; CHECK: (func $unreachable-loop (type $1)
3477+
;; CHECK-NEXT: (local $x i32)
3478+
;; CHECK-NEXT: (unreachable)
3479+
;; CHECK-NEXT: (unreachable)
3480+
;; CHECK-NEXT: )
3481+
;; OPTIN: (func $unreachable-loop (type $1)
3482+
;; OPTIN-NEXT: (local $x i32)
3483+
;; OPTIN-NEXT: (unreachable)
3484+
;; OPTIN-NEXT: (unreachable)
3485+
;; OPTIN-NEXT: )
3486+
(func $unreachable-loop
3487+
(local $x i32)
3488+
;; The entire loop is unreachable. The control flow here should not cause any
3489+
;; internal errors, and we can just optimize this to unreachable.
3490+
(unreachable)
3491+
(local.set $x
3492+
(loop $loop (result i32)
3493+
(i32.const 0)
3494+
)
3495+
)
3496+
)
3497+
3498+
;; CHECK: (func $set-silly (type $0) (param $x i32)
3499+
;; CHECK-NEXT: (local.set $x
3500+
;; CHECK-NEXT: (local.get $x)
3501+
;; CHECK-NEXT: )
3502+
;; CHECK-NEXT: (if
3503+
;; CHECK-NEXT: (i32.const 0)
3504+
;; CHECK-NEXT: (then
3505+
;; CHECK-NEXT: )
3506+
;; CHECK-NEXT: )
3507+
;; CHECK-NEXT: )
3508+
;; OPTIN: (func $set-silly (type $0) (param $x i32)
3509+
;; OPTIN-NEXT: (local.set $x
3510+
;; OPTIN-NEXT: (local.get $x)
3511+
;; OPTIN-NEXT: )
3512+
;; OPTIN-NEXT: (if
3513+
;; OPTIN-NEXT: (i32.const 0)
3514+
;; OPTIN-NEXT: (then
3515+
;; OPTIN-NEXT: )
3516+
;; OPTIN-NEXT: )
3517+
;; OPTIN-NEXT: )
3518+
(func $set-silly (param $x i32)
3519+
;; Setting x to x adds no information, and we do not store it in the IR. This
3520+
;; must not lead to internal errors (we have asserts on not storing
3521+
;; "proves-nothing" in the internal map, and if we wrote this, it would be
3522+
;; that).
3523+
(local.set $x
3524+
(local.get $x)
3525+
)
3526+
;; Add some control flow to exercise the assert.
3527+
(if
3528+
(i32.const 0)
3529+
(then)
3530+
)
3531+
)
34753532
)
3533+

0 commit comments

Comments
 (0)