Skip to content

cmd/compile: instructions being lifted before arch support tests #78997

Closed

Description

@randall77
package main

import "math/bits"

func f(x uint64) int {
	r := 0
	for range 5 {
		r += bits.OnesCount64(x)
	}
	return r
}
GOAMD64=v1 go build -gcflags=-S test.go

In the assembly output, the POPCNTQ instruction has been lifted to before the start of the loop. We can't do that, unless we also lift the x86HasPOPCNT test that wraps POPCNTQ instructions in v1 mode.

This is a consequence of https://go-review.googlesource.com/c/go/+/697235 . Maybe we should revert it. (It was also responsible for #78892. I'm not sure what else might be lurking.)

Activity

  1. Jorropo commented on Apr 28, 2026

    @Jorropo
    Member

    I think we need something like faultOnNilArg0 but for CPUID bits.

  2. gabyhelp commented on Apr 28, 2026

    @gabyhelp
  3. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Apr 28, 2026
  4. randall77 commented on Apr 28, 2026

    @randall77
    ContributorAuthor

    I think we need something like faultOnNilArg0 but for CPUID bits.

    That's a possibility. I was thinking it might be safer to go in the other direction - mark instructions we know are safe to lift. That might be more verbose though.

  5. randall77 commented on Apr 28, 2026

    @randall77
    ContributorAuthor

    This issue reveals a pretty fundamental flaw in our SSA representation. There's no ordering edge between a test that an instruction is ok to use, and the instruction itself. It would be really nice if there was an SSA edge that represented that ordering somehow.
    We have a similar problem with nil checks, which we kind of hack around. It would be nice if there was an SSA edge from the nil check to the load/store that requires it.

    Not sure how to do that.

  6. Jorropo commented on Apr 28, 2026

    @Jorropo
    Member

    In an other issue I wanted to add nilcheck to the mem chain but this has other negative downsides.

  7. prattmic commented on Apr 29, 2026

    @prattmic
    Member

    cc @golang/compiler

  8. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Apr 29, 2026
  9. Jorropo commented on Apr 29, 2026

    @Jorropo
    Member

    I was working on a patch to lift up all possible instructions from the likely edge of a branch to before branche even if they are only used in the likely branch (basically pulls the whole graph up until the first mem producer or phi node and any of it's transient consumer).
    But this has the same issue.

  10. prattmic commented on Apr 29, 2026

    @prattmic
    Member

    This is a consequence of https://go-review.googlesource.com/c/go/+/697235

    This is new in 1.27, so I'm tentatively marking this as a release blocker while we decide what to do.

  11. added this to the Go1.27 milestone on Apr 29, 2026
  12. 1 remaining item

  13. randall77 commented on Apr 29, 2026

    @randall77
    ContributorAuthor

    Two more tricky cases:

    func f(n int, x uint64) uint64 {
    	var r uint64
    	for range n {
    		if archsimd.X86.AVX() {
    			v := archsimd.BroadcastUint64x2(x)
    			v = v.Mul(v)
    			r += v.GetElem(0)
    		}
    	}
    	return r
    }
    

    It is trickier because that's a user-level guarding test, not one inserted by the compiler.

    func f(n int, x uint64) uint64 {
    	var r uint64
    	if !archsimd.X86.AVX() {
    		n = 0
    	}
    	for range n {
    		v := archsimd.BroadcastUint64x2(x)
    		v = v.Mul(v)
    		r += v.GetElem(0)
    	}
    	return r
    }
    

    This one is tricky because it doesn't even have the test in the loop. Nevertheless, we still can't lift the arch-gated instructions out of the loop.

  14. gopherbot commented on May 1, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/772962 mentions this issue: cmd/compile: make lifting ops out of loops opt-in instead of opt-out

  15. moved this from Todo to In Progress in Go Compiler / Runtime / Commandon May 1, 2026
  16. gopherbot commented on May 1, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/771481 mentions this issue: cmd/compile: test for issue 78997

  17. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    and removed
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on May 5, 2026
  18. JunyangShao commented on May 20, 2026

    @JunyangShao
    Contributor

    @randall77 do we have any update on this?

  19. randall77 commented on May 20, 2026

    @randall77
    ContributorAuthor

    The CL stack just needs review. Feel free.
    (It's a bug fix, so it doesn't need to happen today.)

  20. added a commit that references this issue on May 26, 2026
  21. added a commit that references this issue on May 26, 2026
    f934246
  22. AWSjswinney commented on Sep 16, 2026

    @AWSjswinney
    Contributor

    @randall77

    This issue still exists on the latest master branch when you bury the feature gate in another condition or use two feature gates. I noticed this when working adding SVE support but it affects AVX and PMULL tests too. Here are some AI generated test cases:

    amd64

    // Minimal reproducer: an AVX512 instruction executes on the path taken
    // because AVX512 is absent.
    //
    // GOARCH=amd64 GOEXPERIMENT=simd go build -gcflags=-S
    //
    // The two guarded regions each need a 512-bit zero. cse merges the two into one
    // value, and it is then materialized on every path reaching the merge --
    // including the path out of the failed guard. On amd64 a vector zero is a
    // reference to the fixed all-zeros register, which is free as an operand but
    // costs a move when it has to be materialized into a specific register, and at
    // 512 bits that move is VMOVDQU64, which is EVEX-encoded and needs AVX512F.
    //
    // Verified on master 5c51011e82, default GOAMD64=v1:
    //
    //	0x000a 00010  JEQ       73                        <- AVX512 absent
    //	0x000c 00012  VMOVDQU64 Z15, Z0                   <- inside the guard: fine
    //	...
    //	0x0049 00073  VMOVDQU64 Z15, Z0                   <- the absent path: faults
    //
    // One guarded region is not enough, and neither is one region plus a value to
    // merge with: both keep the move inside the guard. Two regions are the minimum.
    //
    // The 256-bit form does the same with VMOVDQU (VEX, needs AVX). The 128-bit
    // form is harmless, because there the move is MOVUPS, which is SSE2 and part of
    // the amd64 baseline.
    package main
    
    import (
    	"fmt"
    
    	"simd/archsimd"
    )
    
    //go:noinline
    func twoRegions() int {
    	n := 0
    	if archsimd.X86.AVX512() {
    		var v archsimd.Int32x16
    		n += int(v.ReduceSum())
    	}
    	if archsimd.X86.AVX512() {
    		var w archsimd.Int32x16
    		n += int(w.ReduceSum())
    	}
    	return n
    }
    
    func main() {
    	fmt.Println("OK", twoRegions(), archsimd.X86.AVX512())
    }

    arm64

    // Negative result: a NEON feature gate does not reproduce the escape.
    //
    // GOEXPERIMENT=simd go build -gcflags=-S
    //
    // PMULL is an Advanced SIMD (Neon) instruction behind an optional feature, with
    // a real check -- archsimd.ARM64.PMULL() -- and pmull is already in
    // internal/cpu's arm64 options, so GODEBUG=cpu.pmull=off works on an unmodified
    // toolchain. It looked like a way to demonstrate the class without SVE.
    //
    // It does not work. On master 5c51011e82, VPMULL stays inside its guard in all
    // three shapes below:
    //
    //	oneRegion   flag load 4, TBZ 12, VMOVI 16, VPMULL 20
    //	twoRegions  VMOVI hoisted to 0 (baseline Neon, harmless); VPMULL 20 and 56,
    //	            each after its own TBZ
    //	selectable  flag load 4, TBZ 12, VPMULL 20
    //
    // Two reasons. VPMULL takes two vector arguments, and zcse only relocates
    // argument-less values, so mechanism 1 cannot reach it. And branchelim declines
    // to if-convert these regions, so mechanism 2 does not either.
    //
    // The conclusion is that arm64's exposure comes specifically from SVE having
    // argument-less ops that are real instructions -- ScalableVectorLen is RDVL, and
    // a scalable or predicate ZeroSIMD is ZDUP or PFALSE. Neon itself has no feature
    // check because it is the arm64 baseline, and its 128-bit zero is VMOVI, which
    // is always legal wherever it lands.
    package main
    
    import (
    	"fmt"
    
    	"simd/archsimd"
    )
    
    //go:noinline
    func oneRegion() uint64 {
    	if archsimd.ARM64.PMULL() {
    		var a, b archsimd.Uint64x2
    		return a.CarrylessMultiplyEven(b).GetElem(0)
    	}
    	return 0
    }
    
    //go:noinline
    func twoRegions(c bool) uint64 {
    	var n uint64
    	if c {
    		if archsimd.ARM64.PMULL() {
    			var a, b archsimd.Uint64x2
    			n += a.CarrylessMultiplyEven(b).GetElem(0)
    		}
    	}
    	if archsimd.ARM64.PMULL() {
    		var a, b archsimd.Uint64x2
    		n += a.CarrylessMultiplyEven(b).GetElem(0)
    	}
    	return n
    }
    
    //go:noinline
    func selectable(c bool) uint64 {
    	var n uint64
    	if c {
    		n = 1
    	}
    	if archsimd.ARM64.PMULL() {
    		var a, b archsimd.Uint64x2
    		n += a.CarrylessMultiplyEven(b).GetElem(0)
    	}
    	return n
    }
    
    func main() {
    	fmt.Println("OK", oneRegion(), twoRegions(true), selectable(true), archsimd.ARM64.PMULL())
    }
  23. gopherbot commented on Sep 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/833984 mentions this issue: cmd/compile: teach zcse to not lift arch-feature-gated instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions