]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: set bits.OnesCount's limits to [0, 64]
authorJorropo <jorropo.pgm@gmail.com>
Sun, 9 Mar 2025 13:38:44 +0000 (14:38 +0100)
committerJorropo <jorropo.pgm@gmail.com>
Wed, 12 Mar 2025 02:52:29 +0000 (19:52 -0700)
Change-Id: I2f60de836f58ef91baae856f44d8f73c190326f2
Reviewed-on: https://go-review.googlesource.com/c/go/+/656158
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>

src/cmd/compile/internal/ssa/prove.go
test/prove_popcount.go [new file with mode: 0644]

index d1d851be91c1490da9b93bf56dfa6c490182fb39..9fedbdbbc987ee0489a2271d9c425e579bb889eb 100644 (file)
@@ -1645,13 +1645,13 @@ func initLimit(v *Value) limit {
                lim = lim.signedMinMax(math.MinInt32, math.MaxInt32)
 
        // math/bits intrinsics
-       case OpCtz64, OpBitLen64:
+       case OpCtz64, OpBitLen64, OpPopCount64:
                lim = lim.unsignedMax(64)
-       case OpCtz32, OpBitLen32:
+       case OpCtz32, OpBitLen32, OpPopCount32:
                lim = lim.unsignedMax(32)
-       case OpCtz16, OpBitLen16:
+       case OpCtz16, OpBitLen16, OpPopCount16:
                lim = lim.unsignedMax(16)
-       case OpCtz8, OpBitLen8:
+       case OpCtz8, OpBitLen8, OpPopCount8:
                lim = lim.unsignedMax(8)
 
        // bool to uint8 conversion
diff --git a/test/prove_popcount.go b/test/prove_popcount.go
new file mode 100644 (file)
index 0000000..430df01
--- /dev/null
@@ -0,0 +1,37 @@
+// errorcheck -0 -d=ssa/prove/debug=1
+
+//go:build amd64.v3 || arm64
+
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// FIXME(@Jorropo): this file exists because I havn't yet bothered to
+// make prove work on the pure go function call fallback.
+// My idea was to wait until CL 637936 is merged, then we can always emit
+// the PopCount SSA operation and translate them to pure function calls
+// in late-opt.
+
+package main
+
+import "math/bits"
+
+func onesCountsBounds(x uint64, ensureAllBranchesCouldHappen func() bool) int {
+       z := bits.OnesCount64(x)
+       if ensureAllBranchesCouldHappen() && z > 64 { // ERROR "Disproved Less64$"
+               return 42
+       }
+       if ensureAllBranchesCouldHappen() && z <= 64 { // ERROR "Proved Leq64$"
+               return 4242
+       }
+       if ensureAllBranchesCouldHappen() && z < 0 { // ERROR "Disproved Less64$"
+               return 424242
+       }
+       if ensureAllBranchesCouldHappen() && z >= 0 { // ERROR "Proved Leq64$"
+               return 42424242
+       }
+       return z
+}
+
+func main() {
+}