Skip to content

cmd/compile: prove uses signed isPowerOfTwo for testing unsigned constants #76688

Closed

Description

@Jorropo

Go version

go version go1.26-devel_f22d37d574 Mon Dec 1 14:59:40 2025 -0800 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v3'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/tmp/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/hugo/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build310673283=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/hugo/k/go/src/go.mod'
GOMODCACHE='/home/hugo/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/hugo/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/hugo/k/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/hugo/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/home/hugo/k/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26-devel_f22d37d574 Mon Dec 1 14:59:40 2025 -0800'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Compile this code

package a

func f(x, y uint64) uint64 {
	if x > 1 || y != 1<<63 {
		return 42
	}
	return x * y
}

with go build -gcflags=-d=ssa/prove/debug=1 a.go

What did you see happen?

Rewrote Mul v21 into CondSelect; v7 is bool

What did you expect to see?

Nothing, theses lines of code:

if xl.umin == xl.umax && isPowerOfTwo(int64(xl.umin)) ||
xl.min == xl.max && isPowerOfTwo(xl.min) ||
yl.umin == yl.umax && isPowerOfTwo(int64(yl.umin)) ||
yl.min == yl.max && isPowerOfTwo(yl.min) {
// 0,1 * a power of two is better done as a shift
break
}

are supposed to prevent rewriting multiplies to condselect for constant powers of two since opt should translate them to a left shift after prove does constant argument folding from limits.

However it uses a signed test for unsigned numbers which gives an incorrect result for 1 << (bitsize-1).

Activity

  1. self-assigned this
    on Dec 4, 2025
  2. changed the title [-]cmd/compile:[/-] [+]cmd/compile: prove uses signed `isPowerOfTwo` for testing unsigned constants[/+] on Dec 4, 2025
  3. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Dec 4, 2025
  4. gopherbot commented on Dec 4, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/726680 mentions this issue: cmd/compile: use isUnsignedPowerOfTwo rather than isPowerOfTwo for unsigneds

  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Dec 4, 2025
  6. added this to the Go1.26 milestone on Dec 4, 2025
  7. added a commit that references this issue on Dec 4, 2025
    7b67b68
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.NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions