Skip to content

cmd/compile: zcse pass does not honor EarlyOk marking #81571

Closed

Description

@randall77

See the last comment on #78997. We sometimes lift an instruction that is arch-gated above the test for the arch gate.

if cpu.HasFeature {
    cpu.UseFeature...
}

We can't lift cpu.UseFeature above the if.

This has been solved for the licm (loop invariant code motion) pass, as part of fixing #78997.
It was always solved for cse, as we only common-subexpression eliminate two expressions if one dominates the other. That dominating condition prevents us from moving anything above its feature check.
However, it is not solved for zcse, the zero-argument common-subexpression pass. It lifts things very aggressively to the function entry. I don't think we had any zero-arg gated instructions previously, but now we have some (exactly 1 generic op, I think).

Repro (amd64);

func f() int {
	n := 0
	if archsimd.X86.AVX512() {
		var v archsimd.Int32x16
		n += int(v.ReduceSum())
	}
	if archsimd.X86.AVX512() {
		var v archsimd.Int32x16
		n += int(v.ReduceSum())
	}
	return n
}
GOARCH=amd64 GOEXPERIMENT=simd ../bin/go build -gcflags=-S ~/gowork/tmp3.go

the arch-independent ZeroSIMD op (which lowers to VMOVDQU64) gets lifted to the start of the function.
With only a single if, that op eventually gets moved by the tighten pass back inside the conditional. It requires 2 ifs to defeat the tighten pass and prevent it from masking the underlying bug.

I think we need to teach zcse to use the EarlyOk flags from #78997, and don't mark ZeroSIMD as EarlyOk.

Activity

  1. 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

  2. added a commit that references this issue on Sep 17, 2026
    7f7ce37
  3. gopherbot commented on Sep 18, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/835125 mentions this issue: cmd/compile: restrict simd test to amd64

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions