Commit 89aae5f8ab for golang

commit 89aae5f8ab68a8ac3c05a067b7155f84ab8c5f92
Author: Michael Haddon <michael@politepixels.io>
Date:   Thu Nov 12 16:13:20 2026 +0000

    cmd/compile: put stack offsets into wasm loads and stores

    On wasm, a load or store at an offset from SP, such as for the
    arguments and results of a call, adds the offset to SP and then loads
    or stores at offset 0.

    Use SP as the address and put the offset into the load or store, as
    LoadReg and StoreReg do. This also covers the parameters, results and
    locals of the function, whose offsets are only known after the stack
    frame is laid out, and floating point and SIMD values.

    This makes the code in binaries 7% to 9% smaller, and binaries about
    5% smaller. Run time improves by up to a few percent.

    Add an assembly check.

    goos: js
    goarch: wasm
    pkg: hash/crc32
                                        │   old.txt   │              new.txt              │
                                        │   sec/op    │   sec/op     vs base              │
    CRC32/poly=IEEE/size=40/align=0       41.35n ± 2%   38.10n ± 1%  -7.87% (p=0.000 n=8)
    CRC32/poly=IEEE/size=1kB/align=0      392.2n ± 1%   390.3n ± 1%       ~ (p=0.130 n=8)
    CRC32/poly=Koopman/size=40/align=0    83.86n ± 1%   80.83n ± 3%  -3.61% (p=0.013 n=8)
    CRC32/poly=Koopman/size=1kB/align=0   1.982µ ± 2%   1.982µ ± 2%       ~ (p=0.798 n=8)
    geomean                               227.9n        220.9n       -3.05%

    pkg: text/template
                             │   old.txt   │              new.txt              │
                             │   sec/op    │   sec/op     vs base              │
    ExecuteBuiltins/eq         1.221µ ± 3%   1.181µ ± 2%  -3.24% (p=0.021 n=8)
    ExecuteBuiltins/eqMulti    1.628µ ± 3%   1.596µ ± 2%  -1.94% (p=0.038 n=8)
    ExecuteBuiltins/lt         1.060µ ± 2%   1.028µ ± 2%  -3.02% (p=0.001 n=8)
    ExecuteBuiltins/not        752.6n ± 2%   730.8n ± 2%  -2.89% (p=0.005 n=8)
    ExecuteBuiltins/printf     1.708µ ± 3%   1.652µ ± 2%       ~ (p=0.099 n=8)
    ExecuteBuiltins/userFunc   1.362µ ± 6%   1.293µ ± 3%  -5.10% (p=0.000 n=8)
    ExecutePrintString         1.833µ ± 3%   1.788µ ± 1%  -2.46% (p=0.000 n=8)
    ExecuteMethodCall          29.69µ ± 4%   28.40µ ± 2%  -4.35% (p=0.028 n=8)
    geomean                    1.940µ        1.876µ       -3.29%

    Updates #65440
    Fixes #83028

    Change-Id: I3f31dec58847be061b1aa1db57285661bd4486ad
    GitHub-Last-Rev: db2614864053a2f880e85b52c80508a60bfe5cc9
    GitHub-Pull-Request: golang/go#83029
    Reviewed-on: https://go-review.googlesource.com/c/go/+/862901
    TryBot-Bypass: David Chase <drchase@google.com>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
    Reviewed-by: David Chase <drchase@google.com>
    Reviewed-by: Cherry Mui <cherryyz@google.com>

diff --git a/src/cmd/compile/internal/wasm/ssa.go b/src/cmd/compile/internal/wasm/ssa.go
index e9c9ecc4c7..400aef83a9 100644
--- a/src/cmd/compile/internal/wasm/ssa.go
+++ b/src/cmd/compile/internal/wasm/ssa.go
@@ -323,12 +323,26 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
 		setReg(s, v.Reg0()) // move result from wasm stack to register local

 	case ssaop.OpWasmI64Store8, ssaop.OpWasmI64Store16, ssaop.OpWasmI64Store32, ssaop.OpWasmI64Store, ssaop.OpWasmF32Store, ssaop.OpWasmF64Store:
+		if to, ok := stackAddr(s, v.Args[0], v.AuxInt); ok {
+			getReg(s, wasm.REG_SP)
+			getValue64(s, v.Args[1])
+			p := s.Prog(v.Op.Asm())
+			p.To = to
+			break
+		}
 		getValue32(s, v.Args[0])
 		getValue64(s, v.Args[1])
 		p := s.Prog(v.Op.Asm())
 		p.To = obj.Addr{Type: obj.TYPE_CONST, Offset: v.AuxInt}

 	case ssaop.OpWasmV128Store:
+		if to, ok := stackAddr(s, v.Args[0], v.AuxInt); ok {
+			getReg(s, wasm.REG_SP)
+			getValue128(s, v.Args[1])
+			p := s.Prog(v.Op.Asm())
+			p.To = to
+			break
+		}
 		getValue32(s, v.Args[0])
 		getValue128(s, v.Args[1])
 		p := s.Prog(v.Op.Asm())
@@ -444,6 +458,11 @@ func ssaGenValueOnStack(s *ssagen.State, v *ssa.Value, extend bool) {

 	case ssaop.OpWasmI64Load8U, ssaop.OpWasmI64Load8S, ssaop.OpWasmI64Load16U, ssaop.OpWasmI64Load16S,
 		ssaop.OpWasmI64Load32U, ssaop.OpWasmI64Load32S, ssaop.OpWasmI64Load, ssaop.OpWasmF32Load, ssaop.OpWasmF64Load, ssaop.OpWasmV128Load:
+		if from, ok := stackAddr(s, v.Args[0], v.AuxInt); ok {
+			p := s.Prog(v.Op.Asm())
+			p.From = from
+			break
+		}
 		getValue32(s, v.Args[0])
 		p := s.Prog(v.Op.Asm())
 		p.From = obj.Addr{Type: obj.TYPE_CONST, Offset: v.AuxInt}
@@ -551,6 +570,18 @@ func isAlready32(v *ssa.Value) bool {
 	}
 }

+// stackAddr returns the operand of a load or store at v+off and true,
+// or false if v is not an offset from SP whose generation was delayed.
+func stackAddr(s *ssagen.State, v *ssa.Value, off int64) (obj.Addr, bool) {
+	if !v.OnWasmStack || v.Op != ssaop.OpWasmLoweredAddr || v.Args[0].Op != ssaop.OpSP {
+		return obj.Addr{}, false
+	}
+	s.OnWasmStackSkipped--
+	a := obj.Addr{Type: obj.TYPE_MEM, Reg: wasm.REG_SP}
+	ssagen.AddAux2(&a, v, v.AuxInt+off)
+	return a, true
+}
+
 func getValue32(s *ssagen.State, v *ssa.Value) {
 	if v.OnWasmStack {
 		s.OnWasmStackSkipped--
diff --git a/test/codegen/wasmstack.go b/test/codegen/wasmstack.go
new file mode 100644
index 0000000000..4f23e6e3e5
--- /dev/null
+++ b/test/codegen/wasmstack.go
@@ -0,0 +1,40 @@
+// asmcheck
+
+// 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 codegen
+
+// Loads and stores on the stack put the offset into the instruction.
+
+//go:noinline
+func wasmStackCallee(a, b, c int) int {
+	return a + b + c
+}
+
+func wasmStackArgs(x int) int {
+	// wasm:`I64Store 8\(SP\)` `I64Store 16\(SP\)` -`I64ExtendI32U`
+	return wasmStackCallee(x, x+1, x+2)
+}
+
+func wasmStackResults(x, y int) (int, int) {
+	// wasm:`I64Store .*~r0\+\d+\(SP\)` `I64Store .*~r1\+\d+\(SP\)` -`I64ExtendI32U`
+	return y, x
+}
+
+func wasmStackFloatResult(x float64) float64 {
+	// wasm:`F64Store .*~r0\+\d+\(SP\)` -`I64ExtendI32U`
+	return x * 2
+}
+
+//go:noinline
+func wasmStackUse(p *[4]int) {}
+
+func wasmStackLocal(x int) int {
+	var a [4]int
+	// wasm:`I64Store .*a\+\d+\(SP\)` -`I64ExtendI32U`
+	a[2] = x
+	wasmStackUse(&a)
+	return a[3]
+}