Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/passes/ConstraintAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,6 @@ struct ConstraintAnalysis
return;
}

// See above on binary action counting limits.
if (auto* binary = set->value->dynCast<Binary>()) {
if (binaryActionCounts[binary]++ >= MaxBinaryActions) {
constraints.setProvesNothing(set->index);
return;
}
}
Comment on lines -572 to -577

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wasn't this catch-all widening sufficient to prevent the problem? Because the expression we have here isn't a Binary? Could we fix it by just handling any kind of expression here instead of just Binary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that we didn't handle this wrt a fallthrough. That is, we computed the fallthrough after, perhaps producing a Binary then - after this check.

That was silly and also inefficient...


// Look at the fallthrough. It is valid to do so, because our constraints
// only track two things, constants and locals. For a constant, it does
// not change while falling through. For a local, the only way for the
Expand All @@ -600,8 +592,37 @@ struct ConstraintAnalysis
// opportunity to write any other value while falling through. (And, any
// local.tee appearing here would have been reached earlier in the
// traversal, and handled.)
auto* value =
Properties::getFallthrough(set->value, getPassOptions(), *getModule());
auto* value = set->value;
while (1) {
if (value->is<LocalSet>()) {
// We stop at the first tee: we don't need to look any further, and
// will just apply that local's values to ourselves, saving repeated
// work.
break;
}
auto* next = Properties::getImmediateFallthrough(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there an optional parameter to getFallthrough that will give us this NoTee behavior without us having to reimplement the loop?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, but it also skips BrIfs:

enum class FallthroughBehavior { AllowTeeBrIf, NoTeeBrIf };

We could perhaps make that flag more refined, but I'm not sure it's worth it.

value, getPassOptions(), *getModule());
if (value == next) {
break;
} else {
value = next;
}
}

// Now that we know the value, check binary action counting limits (see
// above).
if (auto* binary = value->dynCast<Binary>()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to continue restricting this to Binary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this "precise" to the things it needs to handle. There is nothing else where we have this issue other than a Binary, atm (and I'm not sure we ever will?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess all infinite analysis loops would involve a Binary expression somewhere. But it seems somewhat arbitrary to attach the counter to only Binary expressions. Maybe instead of counting the number of times we analyze particular expressions, we can count the number of times we analyze each basic block. That would be even more precise and less arbitrary. I guess we could do that in a follow-up, though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it makes sense as a Binary is where x = x + 1 happens (the + 1). Other things only flow around data.

With that said, maybe it is nicer to count basic block operations? Worth thinking about.

// The count may exceed the limit sometimes, but add a hard assert on
// never going up so high it is likely doing an unbounded computation.
auto& count = binaryActionCounts[binary];
assert(count < MaxBinaryActions * 10);
count++;
if (count >= MaxBinaryActions) {
constraints.setProvesNothing(set->index);
return;
}
}

constraints.set(set->index, value);
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/lit/passes/constraint-analysis-loops.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1905,4 +1905,59 @@
)
)
)

;; CHECK: (func $increment-tee (type $0)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local $y i32)
;; CHECK-NEXT: (loop $label1
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (then
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (local.tee $x
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $label1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $increment-tee
(local $x i32)
(local $y i32)
;; A loop, where $y is incremented but there is a tee in the middle. The loop
;; is unbounded (the exit condition is never hit), so we must be careful to
;; not keep calculating 1,2,3, without limit. The tee in the middle should not
;; confuse us: we apply the +=1 operation to x directly, but y reads it
;; through the tee. We should stop calculating anything about both rather than
;; hang for a long time.
(loop $label1
(if
(i32.gt_s
(local.get $y)
(local.get $x)
)
(then
(unreachable)
)
)
(local.set $y
(local.tee $x
(i32.add
(local.get $y)
(i32.const 1)
)
)
)
(br $label1)
)
)
)
Loading