Skip to content

cmd/compile: simd operations not marked as hoistable #79984

Description

@balasanjay

Go version

N/A

Output of go env in your module/workspace:

N/A

What did you do?

I wrote a simple base64 encoder: https://go.dev/play/p/SaXeuZMg6Xy

I used some relatively simple pure archsimd functions inside my core encoding loop (e.g. LoadUint8x64Array and BroadcastUint32x16).

What did you see happen?

For encodeSIMD1, I see a fairly messy disassembly for the inside of the loop:

Disassembly
command-line-arguments_encodeSIMD1_pc105:
        VMOVDQU64       (DI), Z1
        MOVL    $264305664, DX
        VMOVD   DX, X2
        VPBROADCASTD    X2, Z2
        MOVL    $67108928, R9
        VMOVD   R9, X3
        VPBROADCASTD    X3, Z3
        MOVL    $4129776, R10
        VMOVD   R10, X4
        VPBROADCASTD    X4, Z4
        MOVL    $16777232, R11
        VMOVD   R11, X5
        VPBROADCASTD    X5, Z5
        NOP
        LEAQ    command-line-arguments..autotmp_43(SP), R12
        MOVUPS  X15, (R12)
        MOVUPS  X15, 16(R12)
        MOVUPS  X15, 32(R12)
        MOVUPS  X15, 48(R12)
        MOVB    $1, command-line-arguments..autotmp_43(SP)
        MOVW    $258, command-line-arguments..autotmp_43+2(SP)
        MOVL    $67437316, command-line-arguments..autotmp_43+4(SP)
        MOVQ    $723682103795385863, R12
        MOVQ    R12, command-line-arguments..autotmp_43+8(SP)
        MOVQ    $1157723140823845901, R13
        MOVQ    R13, command-line-arguments..autotmp_43+16(SP)
        MOVQ    $1591764177852305939, R15
        MOVQ    R15, command-line-arguments..autotmp_43+24(SP)
        MOVQ    $2025805214880765977, R15
        MOVQ    R15, command-line-arguments..autotmp_43+32(SP)
        MOVQ    $2459846251909226015, R15
        MOVQ    R15, command-line-arguments..autotmp_43+40(SP)
        MOVQ    $2893887288937686053, R15
        MOVQ    R15, command-line-arguments..autotmp_43+48(SP)
        MOVQ    $3327928325966146091, R15
        MOVQ    R15, command-line-arguments..autotmp_43+56(SP)
        VMOVDQU64       command-line-arguments..autotmp_43(SP), Z6
        VPERMB  Z1, Z6, Z1
        VPANDD  Z1, Z2, Z2
        VPMULHUW        Z3, Z2, Z2
        VPANDD  Z1, Z4, Z1
        VPMULLW Z5, Z1, Z1
        VPORD   Z1, Z2, Z1
        VPERMB  Z0, Z1, Z1
        NOP
        NOP
        NOP
        NOP
        NOP
        VMOVDQU64       Z1, (AX)
        ADDQ    $-64, CX
        MOVQ    CX, command-line-arguments..autotmp_54+128(SP)
        NEGQ    CX
        SARQ    $63, CX
        ANDL    $64, CX
        ADDQ    CX, AX
        ADDQ    $-64, BX
        ADDQ    $-48, R8
        ADDQ    $48, DI
        ADDQ    $-48, SI
        MOVQ    command-line-arguments..autotmp_54+128(SP), CX
command-line-arguments_encodeSIMD1_pc432:
        CMPQ    SI, $64
        JLT     command-line-arguments_encodeSIMD1_pc460
        NOP
        CMPQ    BX, $63
        JHI     command-line-arguments_encodeSIMD1_pc105

For encodeSIMD2, I manually hoisted some of the magic constants out of the loop. This cleaned up the codegen a lot.

Disassembly
command-line-arguments_encodeSIMD2_pc317:
        VMOVDQU64       (DI), Z6
        VPERMB  Z6, Z5, Z6
        VPANDD  Z0, Z6, Z7
        VPMULHUW        Z1, Z7, Z7
        VPANDD  Z2, Z6, Z6
        VPMULLW Z3, Z6, Z6
        VPORD   Z6, Z7, Z6
        VPERMB  Z4, Z6, Z6
        NOP
        NOP
        VMOVDQU64       Z6, (AX)
        ADDQ    $-64, CX
        MOVQ    CX, DX
        NEGQ    DX
        SARQ    $63, DX
        ANDL    $64, DX
        ADDQ    DX, AX
        ADDQ    $-64, BX
        ADDQ    $-48, R8
        ADDQ    $48, DI
        ADDQ    $-48, SI
command-line-arguments_encodeSIMD2_pc407:
        CMPQ    SI, $64
        JLT     command-line-arguments_encodeSIMD2_pc424
        NOP
        CMPQ    BX, $63
        JHI     command-line-arguments_encodeSIMD2_pc317

What did you expect to see?

I expected to see similar codegen for encodeSIMD1 and encodeSIMD2. The compiler should hoist these operations out of the loop, I've seen it do the same for other operations, it should be possible for these SIMD operations as well.

Metadata

Metadata

Assignees

Labels

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.

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions