Skip to content

cmd/compile: loops prevent bounds checking elimination #40704

@elichai

Description

@elichai

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions