Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: missed obvious BCE #42603

Closed
josharian opened this issue Nov 13, 2020 · 3 comments
Closed

cmd/compile: missed obvious BCE #42603

josharian opened this issue Nov 13, 2020 · 3 comments

Comments

@josharian
Copy link
Contributor

@josharian josharian commented Nov 13, 2020

Simplified from some real code:

func f(b []byte, x int, p *uint16) {
	switch x {
	case 0:
		if len(b) < 20 {
			return
		}
		*p = binary.BigEndian.Uint16(b[:2])
	case 1:
		if len(b) < 40 {
			return
		}
		*p = binary.BigEndian.Uint16(b[:2])
	}
}

The compiler inserts a bounds check on the first binary.BigEndian.Uint16(b[:2]), but not the second one. If you remove the case 1 case, the compiler can eliminate the bounds check!

cc @zdjones @randall77

@zdjones
Copy link
Contributor

@zdjones zdjones commented Nov 27, 2020

A quick look at the SSA that prove sees, and my intuition says that prove should indeed be removing both checks. I will look into this more.

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 27, 2020

Change https://golang.org/cl/273670 mentions this issue: cmd/compile: use list to keep track of slice len/cap in factsTable

Loading

@cuonglm
Copy link
Member

@cuonglm cuonglm commented Nov 27, 2020

The problem is that we only keep 1 slice len/cap SSA value in fact table, so when updating fact table for it, we don't update in all places. In the meantime, you can do:

func f(b []byte, x int, p *uint16) {
	l := len(b)
	switch x {
	case 0:
		if l < 20 {
			return
		}
		*p = binary.BigEndian.Uint16(b[:2])
	case 1:
		if l < 40 {
			return
		}
		*p = binary.BigEndian.Uint16(b[:2])
	}
}

to help compiler proving them.

Loading

@dmitshur dmitshur added this to the Backlog milestone Nov 30, 2020
@gopherbot gopherbot closed this in 5e804ba Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants