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: loops prevent bounds checking elimination #40704

Open
elichai opened this issue Aug 11, 2020 · 2 comments
Open

cmd/compile: loops prevent bounds checking elimination #40704

elichai opened this issue Aug 11, 2020 · 2 comments

Comments

@elichai
Copy link

@elichai elichai commented Aug 11, 2020

What version of Go are you using (go version)?

$ go version
go1.14

Does this issue reproduce with the latest release?

Yes

What did you do?

If I do some copies between arrays with a loop like:

func Convert(a *[32]byte, b *[4]uint64) {
	for i := 0; i < 4; i++ {
		binary.LittleEndian.PutUint64(a[i*8:], b[i])
	}
}

it bounds check on each array on every iteration.

but if I unroll it, it can figure out that it will never go out of bounds and eliminate the checks:

func Convert(a *[32]byte, b *[4]uint64) {
		binary.LittleEndian.PutUint64(a[:], b[0])
		binary.LittleEndian.PutUint64(a[8:], b[1])
		binary.LittleEndian.PutUint64(a[16:], b[2])
		binary.LittleEndian.PutUint64(a[24:], b[3])
}

godbolt: https://godbolt.org/z/E3c5r4

This means that when implementing performance critical code (ie cryptography) I find myself unrolling loops of 8-16 because they end up on pprof which really sucks for readability

@randall77 randall77 added this to the Unplanned milestone Aug 11, 2020
@randall77
Copy link
Contributor

@randall77 randall77 commented Aug 11, 2020

We don't propagate known ranges through multiplies at the moment. Shouldn't be too hard to do in prove.go.

@mdempsky mdempsky changed the title loops prevent bounds checking elimination cmd/compile: loops prevent bounds checking elimination Aug 12, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 17, 2020

Change https://golang.org/cl/248701 mentions this issue: cmd/compile: add support for Lsh64*64 and Mul64 to IsSliceInBounds

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
4 participants
You can’t perform that action at this time.