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
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
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. |
And @zdjones |
One more:
|
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)
}
} |
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
} |
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 |
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
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
What did you expect to see?
Both tricks work.
What did you see instead?
One of them doesn't work.
The text was updated successfully, but these errors were encountered: