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: elide unnecessary copies #54613

Open
dominikh opened this issue Aug 23, 2022 · 1 comment
Open

cmd/compile: elide unnecessary copies #54613

dominikh opened this issue Aug 23, 2022 · 1 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
Milestone

Comments

@dominikh
Copy link
Member

dominikh commented Aug 23, 2022

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

$ go version
go version devel go1.20-8d57f4dcef Sat Aug 20 00:56:30 2022 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes, reproduces on Go 1.19.

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

package pkg

func fn1() byte {
	arr := [5]byte{1, 2, 3, 4, 5}
	return arr[0] | arr[1] | arr[2] | arr[3] | arr[4]
}

func fn2() byte {
	arr1 := [5]byte{1, 2, 3, 4, 5}
	arr2 := arr1
	arr3 := arr1
	arr4 := arr1
	arr5 := arr1
	return arr1[0] | arr2[1] | arr3[2] | arr4[3] | arr5[4]
}

What did you expect to see?

Both functions to compile the same.

What did you see instead?

fn2 really does copy arr 4 times. The compiler could elide these copies, turning arr2–5 into aliases, without affecting the behavior of the program.

command-line-arguments.fn1 STEXT nosplit size=73 args=0x0 locals=0x10 funcid=0x0 align=0x0
        0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:3)      TEXT    command-line-arguments.fn1(SB), NOSPLIT|ABIInternal, $16-0
        0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:3)      SUBQ    $16, SP
        0x0004 00004 (/home/dominikh/prj/src/example.com/foo.go:3)      MOVQ    BP, 8(SP)
        0x0009 00009 (/home/dominikh/prj/src/example.com/foo.go:3)      LEAQ    8(SP), BP
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:3)      FUNCDATA        $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:3)      FUNCDATA        $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:4)      MOVL    $67305985, command-line-arguments.arr+3(SP)
        0x0016 00022 (/home/dominikh/prj/src/example.com/foo.go:4)      MOVB    $5, command-line-arguments.arr+7(SP)
        0x001b 00027 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVBLZX command-line-arguments.arr+3(SP), AX
        0x0020 00032 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVBLZX command-line-arguments.arr+4(SP), CX
        0x0025 00037 (/home/dominikh/prj/src/example.com/foo.go:5)      ORL     CX, AX
        0x0027 00039 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVBLZX command-line-arguments.arr+5(SP), CX
        0x002c 00044 (/home/dominikh/prj/src/example.com/foo.go:5)      ORL     CX, AX
        0x002e 00046 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVBLZX command-line-arguments.arr+6(SP), CX
        0x0033 00051 (/home/dominikh/prj/src/example.com/foo.go:5)      ORL     CX, AX
        0x0035 00053 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVL    $5, CX
        0x003a 00058 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVBLZX CL, CX
        0x003d 00061 (/home/dominikh/prj/src/example.com/foo.go:5)      ORL     CX, AX
        0x003f 00063 (/home/dominikh/prj/src/example.com/foo.go:5)      MOVQ    8(SP), BP
        0x0044 00068 (/home/dominikh/prj/src/example.com/foo.go:5)      ADDQ    $16, SP
        0x0048 00072 (/home/dominikh/prj/src/example.com/foo.go:5)      RET
        0x0000 48 83 ec 10 48 89 6c 24 08 48 8d 6c 24 08 c7 44  H...H.l$.H.l$..D
        0x0010 24 03 01 02 03 04 c6 44 24 07 05 0f b6 44 24 03  $......D$....D$.
        0x0020 0f b6 4c 24 04 09 c8 0f b6 4c 24 05 09 c8 0f b6  ..L$.....L$.....
        0x0030 4c 24 06 09 c8 b9 05 00 00 00 0f b6 c9 09 c8 48  L$.............H
        0x0040 8b 6c 24 08 48 83 c4 10 c3                       .l$.H....
command-line-arguments.fn2 STEXT nosplit size=121 args=0x0 locals=0x20 funcid=0x0 align=0x0
        0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:8)      TEXT    command-line-arguments.fn2(SB), NOSPLIT|ABIInternal, $32-0
        0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:8)      SUBQ    $32, SP
        0x0004 00004 (/home/dominikh/prj/src/example.com/foo.go:8)      MOVQ    BP, 24(SP)
        0x0009 00009 (/home/dominikh/prj/src/example.com/foo.go:8)      LEAQ    24(SP), BP
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:8)      FUNCDATA        $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:8)      FUNCDATA        $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x000e 00014 (/home/dominikh/prj/src/example.com/foo.go:9)      MOVL    $67305985, command-line-arguments.arr1+19(SP)
        0x0016 00022 (/home/dominikh/prj/src/example.com/foo.go:9)      MOVB    $5, command-line-arguments.arr1+23(SP)
        0x001b 00027 (/home/dominikh/prj/src/example.com/foo.go:10)     MOVL    command-line-arguments.arr1+19(SP), CX
        0x001f 00031 (/home/dominikh/prj/src/example.com/foo.go:10)     MOVBLZX command-line-arguments.arr1+23(SP), DX
        0x0024 00036 (/home/dominikh/prj/src/example.com/foo.go:10)     MOVL    CX, command-line-arguments.arr2+14(SP)
        0x0028 00040 (/home/dominikh/prj/src/example.com/foo.go:10)     MOVB    DL, command-line-arguments.arr2+18(SP)
        0x002c 00044 (/home/dominikh/prj/src/example.com/foo.go:11)     MOVL    command-line-arguments.arr1+19(SP), CX
        0x0030 00048 (/home/dominikh/prj/src/example.com/foo.go:11)     MOVBLZX command-line-arguments.arr1+23(SP), DX
        0x0035 00053 (/home/dominikh/prj/src/example.com/foo.go:11)     MOVL    CX, command-line-arguments.arr3+9(SP)
        0x0039 00057 (/home/dominikh/prj/src/example.com/foo.go:11)     MOVB    DL, command-line-arguments.arr3+13(SP)
        0x003d 00061 (/home/dominikh/prj/src/example.com/foo.go:12)     MOVL    command-line-arguments.arr1+19(SP), CX
        0x0041 00065 (/home/dominikh/prj/src/example.com/foo.go:12)     MOVBLZX command-line-arguments.arr1+23(SP), DX
        0x0046 00070 (/home/dominikh/prj/src/example.com/foo.go:12)     MOVL    CX, command-line-arguments.arr4+4(SP)
        0x004a 00074 (/home/dominikh/prj/src/example.com/foo.go:12)     MOVB    DL, command-line-arguments.arr4+8(SP)
        0x004e 00078 (/home/dominikh/prj/src/example.com/foo.go:13)     MOVBLZX command-line-arguments.arr1+23(SP), AX
        0x0053 00083 (/home/dominikh/prj/src/example.com/foo.go:14)     MOVBLZX command-line-arguments.arr1+19(SP), CX
        0x0058 00088 (/home/dominikh/prj/src/example.com/foo.go:14)     MOVBLZX command-line-arguments.arr2+15(SP), DX
        0x005d 00093 (/home/dominikh/prj/src/example.com/foo.go:14)     ORL     CX, DX
        0x005f 00095 (/home/dominikh/prj/src/example.com/foo.go:14)     MOVBLZX command-line-arguments.arr3+11(SP), CX
        0x0064 00100 (/home/dominikh/prj/src/example.com/foo.go:14)     ORL     DX, CX
        0x0066 00102 (/home/dominikh/prj/src/example.com/foo.go:14)     MOVBLZX command-line-arguments.arr4+7(SP), DX
        0x006b 00107 (/home/dominikh/prj/src/example.com/foo.go:14)     ORL     CX, DX
        0x006d 00109 (/home/dominikh/prj/src/example.com/foo.go:14)     ORL     DX, AX
        0x006f 00111 (/home/dominikh/prj/src/example.com/foo.go:14)     MOVQ    24(SP), BP
        0x0074 00116 (/home/dominikh/prj/src/example.com/foo.go:14)     ADDQ    $32, SP
        0x0078 00120 (/home/dominikh/prj/src/example.com/foo.go:14)     RET
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 23, 2022
@dr2chase
Copy link
Contributor

dr2chase commented Aug 24, 2022

@golang/compiler

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 24, 2022
@randall77 randall77 added this to the Unplanned milestone Aug 24, 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
Status: Todo
Development

No branches or pull requests

5 participants