Skip to content

Commit eef5f8d

Browse files
committed
cmd/compile: enforce that locals are always accessed with SP base register
After CL 678937, we could have a situation where the value of the stack pointer is in both SP and another register. We need to make sure that regalloc picks SP when issuing a reference to local variables; the assembler expects that. Fixes #74836 Change-Id: I2ac73ece6eb44b4a78c1369f8a69e51ab9748754 Reviewed-on: https://go-review.googlesource.com/c/go/+/692395 Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Mark Freeman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent e071617 commit eef5f8d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/cmd/compile/internal/ssa/regalloc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,12 @@ func (s *regAllocState) regalloc(f *Func) {
15831583
mask &^= desired.avoid
15841584
}
15851585
}
1586+
if mask&s.values[v.Args[i.idx].ID].regs&(1<<s.SPReg) != 0 {
1587+
// Prefer SP register. This ensures that local variables
1588+
// use SP as their base register (instead of a copy of the
1589+
// stack pointer living in another register). See issue 74836.
1590+
mask = 1 << s.SPReg
1591+
}
15861592
args[i.idx] = s.allocValToReg(v.Args[i.idx], mask, true, v.Pos)
15871593
}
15881594

test/fixedbugs/issue74836.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile
2+
3+
// Copyright 2025 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
type T struct {
10+
a [20]int
11+
}
12+
13+
func f(x [4]int) {
14+
g(T{}, x)
15+
}
16+
17+
func g(t T, x [4]int)

0 commit comments

Comments
 (0)