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
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@go101
Copy link

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

@rasky @brtzsnr @dr2chase

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

mvdan commented Aug 24, 2020

And @zdjones

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 24, 2020
@go101
Copy link
Author

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

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. 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