Commit 8b3939d32a for golang
commit 8b3939d32aaf5e27293155e7d817c29ce5109a13
Author: Michael Haddon <michael@politepixels.io>
Date: Thu Nov 26 01:36:16 2026 +0000
cmd/compile: rename a use to the phi inserted in the use's block
When reestablishSSA inserts a phi at the top of the block of a use it
is renaming, it still replaces the use with the block's downward
exposed definition. That can be a reload that shuffle placed later in
the same block, so the use comes before the definition of its
argument. The arm, arm64, loong64, mips, mips64 and riscv64 code
generators omit the extension of a narrow LoadReg, so on a path that
did not reload the register, the value is used unextended.
Use the inserted phi, as is already done for later uses in that
block. Also check, after scheduling, that a value's arguments from
its own block come before it.
Add a test.
Fixes #83398
Change-Id: Ibabe48d6ab2dd64ae81de553757454074ed64efe
GitHub-Last-Rev: 0bc506ec69774480ab3758bc584096af30889969
GitHub-Pull-Request: golang/go#83399
Reviewed-on: https://go-review.googlesource.com/c/go/+/866345
Reviewed-by: Daniel Morsing <daniel.morsing@gmail.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/src/cmd/compile/internal/ssacompile/check.go b/src/cmd/compile/internal/ssacompile/check.go
index caedf40f03..9eb65a761b 100644
--- a/src/cmd/compile/internal/ssacompile/check.go
+++ b/src/cmd/compile/internal/ssacompile/check.go
@@ -248,6 +248,10 @@ func checkFunc(f *ssa.Func) {
if arg.Type.IsMemory() && i != len(v.Args)-1 {
f.Fatalf("value %s has non-final memory arg (%d < %d)", v.LongString(), i, len(v.Args)-1)
}
+ // After scheduling, args from the same block must come before v
+ if f.Scheduled && arg.Block == b && !valueMark[arg.ID] {
+ f.Fatalf("arg %d of value %s is not defined before it, arg=%s", i, v.LongString(), arg.LongString())
+ }
}
}
diff --git a/src/cmd/compile/internal/ssacompile/regalloc.go b/src/cmd/compile/internal/ssacompile/regalloc.go
index a564773ff2..dabe898cc6 100644
--- a/src/cmd/compile/internal/ssacompile/regalloc.go
+++ b/src/cmd/compile/internal/ssacompile/regalloc.go
@@ -2626,6 +2626,10 @@ func (e *edgeState) reestablishSSA(exposedDownwards []contentRecord) {
}
}
ddef, _ := downwardDef.Get(search.ID)
+ if use.op() != ssaop.OpPhi && search == use.block() {
+ // The phi we inserted reaches the use; the downward def may follow it
+ ddef, _ = topDef.Get(search.ID)
+ }
if f.Pass.Debug > ssa.RegDebug && ddef != use.value() {
fmt.Printf("replacing v%d with v%d in %#s\n", use.value().ID, ddef.ID, use)
}
diff --git a/test/fixedbugs/issue83398.go b/test/fixedbugs/issue83398.go
new file mode 100644
index 0000000000..55e0f4f773
--- /dev/null
+++ b/test/fixedbugs/issue83398.go
@@ -0,0 +1,33 @@
+// run
+
+// Copyright 2026 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.
+
+package main
+
+//go:noinline
+func f(a, b int8, c, d bool) int8 {
+ v := a * b
+ if c {
+ if d {
+ g()
+ }
+ h(int32(v))
+ }
+ return v
+}
+
+//go:noinline
+func g() {}
+
+//go:noinline
+func h(x int32) {
+ if x != 64 {
+ panic(x)
+ }
+}
+
+func main() {
+ f(0x40, 0x41, true, false)
+}