diff options
author | Austin Clements <austin@google.com> | 2018-01-10 16:28:58 -0500 |
---|---|---|
committer | Austin Clements <austin@google.com> | 2018-03-08 22:25:25 +0000 |
commit | 669db2cef55321b0fe354b8bf9212245dc9c6aed (patch) | |
tree | 348588330befc30190b6df63a172752df3ed0eb8 /test/prove.go | |
parent | 2e9cf5f66e4dccbb9676ebbabd7d36db4f2825a1 (diff) | |
download | go-git-669db2cef55321b0fe354b8bf9212245dc9c6aed.tar.gz |
cmd/compile: make prove pass use unsatisfiability
Currently the prove pass uses implication queries. For each block, it
collects the set of branch conditions leading to that block, and
queries this fact table for whether any of these facts imply the
block's own branch condition (or its inverse). This works remarkably
well considering it doesn't do any deduction on these facts, but it
has various downsides:
1. It requires an implementation both of adding facts to the table and
determining implications. These are very nearly duals of each
other, but require separate implementations. Likewise, the process
of asserting facts of dominating branch conditions is very nearly
the dual of the process of querying implied branch conditions.
2. It leads to less effective use of derived facts. For example, the
prove pass currently derives facts about the relations between len
and cap, but can't make use of these unless a branch condition is
in the exact form of a derived fact. If one of these derived facts
contradicts another fact, it won't notice or make use of this.
This CL changes the approach of the prove pass to instead use
*contradiction* instead of implication. Rather than ever querying a
branch condition, it simply adds branch conditions to the fact table.
If this leads to a contradiction (specifically, it makes the fact set
unsatisfiable), that branch is impossible and can be cut. As a result,
1. We can eliminate the code for determining implications
(factsTable.get disappears entirely). Also, there is now a single
implementation of visiting and asserting branch conditions, since
we don't have to flip them around to treat them as facts in one
place and queries in another.
2. Derived facts can be used effectively. It doesn't matter *why* the
fact table is unsatisfiable; a contradiction in any of the facts is
enough.
3. As an added benefit, it's now quite easy to avoid traversing beyond
provably-unreachable blocks. In contrast, the current
implementation always visits all blocks.
The prove pass already has nearly all of the mechanism necessary to
compute unsatisfiability, which means this both simplifies the code
and makes it more powerful.
The only complication is that the current implication procedure has a
hack for dealing with the 0 <= Args[0] condition of OpIsInBounds and
OpIsSliceInBounds. We replace this with asserting the appropriate fact
when we process one of these conditions. This seems much cleaner
anyway, and works because we can now take advantage of derived facts.
This has no measurable effect on compiler performance.
Effectiveness:
There is exactly one condition in all of std and cmd that this fails
to prove that the old implementation could: (int64(^uint(0)>>1) < x)
in encoding/gob. This can never be true because x is an int, and it's
basically coincidence that the old code gets this. (For example, it
fails to prove the similar (x < ^int64(^uint(0)>>1)) condition that
immediately precedes it, and even though the conditions are logically
unrelated, it wouldn't get the second one if it hadn't first processed
the first!)
It does, however, prove a few dozen additional branches. These come
from facts that are added to the fact table about the relations
between len and cap. These were almost never queried directly before,
but could lead to contradictions, which the unsat-based approach is
able to use.
There are exactly two branches in std and cmd that this implementation
proves in the *other* direction. This sounds scary, but is okay
because both occur in already-unreachable blocks, so it doesn't matter
what we chose. Because the fact table logic is sound but incomplete,
it fails to prove that the block isn't reachable, even though it is
able to prove that both outgoing branches are impossible. We could
turn these blocks into BlockExit blocks, but it doesn't seem worth the
trouble of the extra proof effort for something that happens twice in
all of std and cmd.
Tests:
This CL updates test/prove.go to change the expected messages because
it can no longer give a "reason" why it proved or disproved a
condition. It also adds a new test of a branch it couldn't prove
before.
It mostly guts test/sliceopt.go, removing everything related to slice
bounds optimizations and moving a few relevant tests to test/prove.go.
Much of this test is actually unreachable. The new prove pass figures
this out and doesn't try to prove anything about the unreachable
parts. The output on the unreachable parts is already suspect because
anything can be proved at that point, so it's really just a regression
test for an algorithm the compiler no longer uses.
This is a step toward fixing #23354. That issue is quite easy to fix
once we can use derived facts effectively.
Change-Id: Ia48a1b9ee081310579fe474e4a61857424ff8ce8
Reviewed-on: https://go-review.googlesource.com/87478
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/prove.go')
-rw-r--r-- | test/prove.go | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/test/prove.go b/test/prove.go index e89ab3f8d8..2f4fa5d308 100644 --- a/test/prove.go +++ b/test/prove.go @@ -11,11 +11,11 @@ import "math" func f0(a []int) int { a[0] = 1 - a[0] = 1 // ERROR "Proved boolean IsInBounds$" + a[0] = 1 // ERROR "Proved IsInBounds$" a[6] = 1 - a[6] = 1 // ERROR "Proved boolean IsInBounds$" + a[6] = 1 // ERROR "Proved IsInBounds$" + a[5] = 1 // ERROR "Proved IsInBounds$" a[5] = 1 // ERROR "Proved IsInBounds$" - a[5] = 1 // ERROR "Proved boolean IsInBounds$" return 13 } @@ -23,24 +23,24 @@ func f1(a []int) int { if len(a) <= 5 { return 18 } - a[0] = 1 // ERROR "Proved non-negative bounds IsInBounds$" - a[0] = 1 // ERROR "Proved boolean IsInBounds$" + a[0] = 1 // ERROR "Proved IsInBounds$" + a[0] = 1 // ERROR "Proved IsInBounds$" a[6] = 1 - a[6] = 1 // ERROR "Proved boolean IsInBounds$" + a[6] = 1 // ERROR "Proved IsInBounds$" + a[5] = 1 // ERROR "Proved IsInBounds$" a[5] = 1 // ERROR "Proved IsInBounds$" - a[5] = 1 // ERROR "Proved boolean IsInBounds$" return 26 } func f1b(a []int, i int, j uint) int { if i >= 0 && i < len(a) { - return a[i] // ERROR "Proved non-negative bounds IsInBounds$" + return a[i] // ERROR "Proved IsInBounds$" } if i >= 10 && i < len(a) { - return a[i] // ERROR "Proved non-negative bounds IsInBounds$" + return a[i] // ERROR "Proved IsInBounds$" } if i >= 10 && i < len(a) { - return a[i] // ERROR "Proved non-negative bounds IsInBounds$" + return a[i] // ERROR "Proved IsInBounds$" } if i >= 10 && i < len(a) { // todo: handle this case return a[i-10] @@ -64,7 +64,7 @@ func f1c(a []int, i int64) int { func f2(a []int) int { for i := range a { a[i+1] = i - a[i+1] = i // ERROR "Proved boolean IsInBounds$" + a[i+1] = i // ERROR "Proved IsInBounds$" } return 34 } @@ -84,15 +84,14 @@ func f4a(a, b, c int) int { if a > b { // ERROR "Disproved Greater64$" return 50 } - if a < b { // ERROR "Proved boolean Less64$" + if a < b { // ERROR "Proved Less64$" return 53 } - if a == b { // ERROR "Disproved boolean Eq64$" + // We can't get to this point and prove knows that, so + // there's no message for the next (obvious) branch. + if a != a { return 56 } - if a > b { // ERROR "Disproved boolean Greater64$" - return 59 - } return 61 } return 63 @@ -127,8 +126,8 @@ func f4c(a, b, c int) int { func f4d(a, b, c int) int { if a < b { if a < c { - if a < b { // ERROR "Proved boolean Less64$" - if a < c { // ERROR "Proved boolean Less64$" + if a < b { // ERROR "Proved Less64$" + if a < c { // ERROR "Proved Less64$" return 87 } return 89 @@ -218,8 +217,8 @@ func f6e(a uint8) int { func f7(a []int, b int) int { if b < len(a) { a[b] = 3 - if b < len(a) { // ERROR "Proved boolean Less64$" - a[b] = 5 // ERROR "Proved boolean IsInBounds$" + if b < len(a) { // ERROR "Proved Less64$" + a[b] = 5 // ERROR "Proved IsInBounds$" } } return 161 @@ -242,7 +241,7 @@ func f9(a, b bool) int { if a { return 1 } - if a || b { // ERROR "Disproved boolean Arg$" + if a || b { // ERROR "Disproved Arg$" return 2 } return 3 @@ -260,22 +259,22 @@ func f10(a string) int { func f11a(a []int, i int) { useInt(a[i]) - useInt(a[i]) // ERROR "Proved boolean IsInBounds$" + useInt(a[i]) // ERROR "Proved IsInBounds$" } func f11b(a []int, i int) { useSlice(a[i:]) - useSlice(a[i:]) // ERROR "Proved boolean IsSliceInBounds$" + useSlice(a[i:]) // ERROR "Proved IsSliceInBounds$" } func f11c(a []int, i int) { useSlice(a[:i]) - useSlice(a[:i]) // ERROR "Proved boolean IsSliceInBounds$" + useSlice(a[:i]) // ERROR "Proved IsSliceInBounds$" } func f11d(a []int, i int) { useInt(a[2*i+7]) - useInt(a[2*i+7]) // ERROR "Proved boolean IsInBounds$" + useInt(a[2*i+7]) // ERROR "Proved IsInBounds$" } func f12(a []int, b int) { @@ -305,7 +304,7 @@ func f13a(a, b, c int, x bool) int { } } if x { - if a > 12 { // ERROR "Proved boolean Greater64$" + if a > 12 { // ERROR "Proved Greater64$" return 5 } } @@ -327,7 +326,7 @@ func f13b(a int, x bool) int { } } if x { - if a == -9 { // ERROR "Proved boolean Eq64$" + if a == -9 { // ERROR "Proved Eq64$" return 9 } } @@ -349,7 +348,7 @@ func f13b(a int, x bool) int { func f13c(a int, x bool) int { if a < 90 { if x { - if a < 90 { // ERROR "Proved boolean Less64$" + if a < 90 { // ERROR "Proved Less64$" return 13 } } @@ -450,7 +449,7 @@ func f14(p, q *int, a []int) { j := *q i2 := *p useInt(a[i1+j]) - useInt(a[i2+j]) // ERROR "Proved boolean IsInBounds$" + useInt(a[i2+j]) // ERROR "Proved IsInBounds$" } func f15(s []int, x int) { @@ -460,11 +459,32 @@ func f15(s []int, x int) { func f16(s []int) []int { if len(s) >= 10 { - return s[:10] // ERROR "Proved non-negative bounds IsSliceInBounds$" + return s[:10] // ERROR "Proved IsSliceInBounds$" } return nil } +func f17(b []int) { + for i := 0; i < len(b); i++ { + useSlice(b[i:]) // Learns i <= len + // This tests for i <= cap, which we can only prove + // using the derived relation between len and cap. + // This depends on finding the contradiction, since we + // don't query this condition directly. + useSlice(b[:i]) // ERROR "Proved IsSliceInBounds$" + } +} + +func sm1(b []int, x int) { + // Test constant argument to slicemask. + useSlice(b[2:8]) // ERROR "Proved slicemask not needed$" + // Test non-constant argument with known limits. + // Right now prove only uses the unsigned limit. + if uint(cap(b)) > 10 { + useSlice(b[2:]) // ERROR "Proved slicemask not needed$" + } +} + //go:noinline func useInt(a int) { } |