Skip to content

go/compile: x86-64: sub-optimal codegen less-than-int sized index from structured slice/array #74489

Open
@bupjae

Description

@bupjae

Go version

go version go1.25-devel_6c3b5a2 Thu Jul 3 18:43:56 2025 -0700 linux/amd64

Output of go env in your module/workspace:

Using service on https://go.godbolt.org/
Selected "x86-64 gc (tip)"

What did you do?

I entered following program

package main

func Foo(a [][4]byte, b []int, i int) int {
	return b[a[i][1]]
}

func main() {
}

https://go.godbolt.org/z/hfvezofxf

What did you see happen?

Codegen for a[i][1] part was following:

        LEAQ    (AX)(R9*4), DX
        LEAQ    1(DX), DX
        MOVBLZX (DX), AX

What did you expect to see?

One-line codegen like MOVBLZX 1(AX)(R9*4), AX was expected

The compiler really worked very hard to optimize codegen regarding various element size of slice a. However, it seems that it missed simple case - when the element size is 2, 4 or 8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone 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.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions