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 missing BCE cases #40987

Open
go101 opened this issue Aug 23, 2020 · 7 comments
Open

cmd/compile: some missing BCE cases #40987

go101 opened this issue Aug 23, 2020 · 7 comments

Comments

@go101
Copy link

@go101 go101 commented Aug 23, 2020

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

$ go version
go version go1.15 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package foo

func f(a []int) {
	//a = a[:256] // works
	_ = a[255] // doesn't work
	for i := 0; i < 50000; i++ {
	    _ = a[byte(i)] // BCEed?
	}
}

func g(a []int) {
	//a = a[:4096] // works
	_ = a[4095] // doesn't work
	for i := 0; i < 50000; i++ {
	    _ = a[i&4095] // BCEed?
	}
}

What did you expect to see?

Both tricks work.

What did you see instead?

One of them doesn't work.

@go101
Copy link
Author

@go101 go101 commented Aug 23, 2020

And this:

package foo

func f1(a []int) (r int) {
	n := len(a)
	for i := 0; i < n - 3; i += 4 {
		r += a[i] // BCEed 
	}
	return r
}

func f2(a []int) (r int) {
	n := len(a)
	for i := 0; i <= n - 4; i += 4 {
		r += a[i] // not BCEed
	}
	return r
}

I rememberd that before it behaved the inverse.

@randall77
Copy link
Contributor

@randall77 randall77 commented Aug 23, 2020

@randall77 randall77 added this to the Unplanned milestone Aug 23, 2020
@mvdan
Copy link
Member

@mvdan mvdan commented Aug 24, 2020

And @zdjones

@go101
Copy link
Author

@go101 go101 commented Mar 3, 2021

One more:

func ComparePackagePaths(pa, pb string) {
	var n = len(pa)
	if n > len(pb) {
		n = len(pb)
		pa, pb = pb, pa
	}
	if n <= len(pb) { // len(pa) <= len(pb) will get BCE
		for i := 0; i < len(pa); i++ {
			_, _ = pa[i], 
			pb[i] // bound check here
		}
	}
}

@go101
Copy link
Author

@go101 go101 commented Jun 7, 2021

One more, all bounds checks could be removed in this test:

package allocate

import "testing"
import "reflect"

func init() {
	s3 := Filter3(buildOrginalData())
	s4 := Filter4(buildOrginalData())
	if !reflect.DeepEqual(s3, s4) {
		panic("s3 != s4")
	}
}

func buildOrginalData() []int {
	s := make([]int, 1024)
	for i := range s {
		s[i] = i
	}
	return s
}

func check(v int) bool {
	return v%2 == 0
}

// Lose elements.
func Filter3(data []int) []int {
	var k = 0
	for _, v := range data {
		if check(v) {
			data[k] = v // Found IsInBounds
			k++
		}
	}
	return data[:k] // Found IsSliceInBounds
}

// Keep all elements.
func Filter4(data []int) []int {
	var k = 0
	for i, v := range data {
		if check(v) {
			data[i] = data[k] // Found IsInBounds
			data[k] = v
			k++
		}
	}
	return data[:k] // Found IsSliceInBounds
}

var r3 []int
func BenchmarkFilter3(b *testing.B) {
	data := buildOrginalData()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		r3 = Filter3(data)
	}
}

var r4 []int
func BenchmarkFilter4(b *testing.B) {
	data := buildOrginalData()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		r4 = Filter4(data)
	}
}

@go101
Copy link
Author

@go101 go101 commented Jul 4, 2021

Another:

func foo(s [10]uint, n uint) {
	i := n % uint(len(s))
	s[i] = i // Found IsInBounds
}

func bar(s []uint, n uint) {
	i := n % uint(len(s))
	s[i] = i // BCEed
}

@go101
Copy link
Author

@go101 go101 commented Aug 3, 2021

package main

func f3(s []int) {
	for i := len(s) - 1; i >= 0; i-- {
		//_ = s[i]
		_ = s[i:len(s)]
		_ = s[:i+1] // Found IsSliceInBounds
	}
}


func f5(s []int) {
	for i := 0; i < len(s) - 1; i += 2 {
		//_ = s[i]
		_ = s[i:len(s)]
		_ = s[:i+1] // Found IsSliceInBounds
		_ = s[i+1]  // Found IsInBounds
	}
}

func main() {}

By turning on the _ = s[i] lines, the two Found IsSliceInBounds messages will disappear, but the Found IsInBounds message is still there.

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