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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@josharian
Copy link
Contributor

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 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.

@gopherbot
Copy link

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

@cuonglm
Copy link
Member

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.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2020
@dmitshur dmitshur added this to the Backlog milestone Nov 30, 2020
@golang golang locked and limited conversation to collaborators Feb 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

5 participants