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: some subsequent bounds checks are not eliminated #50365

Open
tdakkota opened this issue Dec 27, 2021 · 3 comments
Open

cmd/compile: some subsequent bounds checks are not eliminated #50365

tdakkota opened this issue Dec 27, 2021 · 3 comments

Comments

@tdakkota
Copy link

@tdakkota tdakkota commented Dec 27, 2021

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

$ go version
go version devel go1.18-088bb4b Tue Nov 2 06:25:39 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

https://go.dev/play/p/eyem-XNTdye
https://go.godbolt.org/z/bMTGW9E7W

What did you expect to see?

Since i and size are always positive numbers,

i*size+1 is not equal to zero and will be always greater than i*size, so

data[i*size]

check can be eliminated.

If i*size+1 overflows or i*size+1 >= len(data), data[i*size+1] will panic before accessing data[i*size].

What did you see instead?

Compiler emits two checks.

@tdakkota tdakkota changed the title cmd/compile: some subsequent bound checks are not eliminated cmd/compile: some subsequent bounds checks are not eliminated Dec 27, 2021
@randall77
Copy link
Contributor

@randall77 randall77 commented Dec 27, 2021

Since i and size are always positive numbers,

isize+1 is not equal to zero and will be always greater than isize, so

data[i*size]
check can be eliminated.

That's not always true. For instance in int8 space, 5*51+1 == 0, but 5*51 == -1. I think the size==2 case is safe, but probably most sizes (including 5) aren't. The numbers are larger for int32 and int64 but the same situation happens.

@tdakkota
Copy link
Author

@tdakkota tdakkota commented Dec 28, 2021

Isn't it true for all even non-zero size (size > 0 && size % 2 == 0)?

@randall77
Copy link
Contributor

@randall77 randall77 commented Dec 28, 2021

Yes, I think all size%2==0 are ok, with i*size+1 at least.

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
3 participants