Commit 71f2cd330d for golang
commit 71f2cd330d6ed233fc6090775f391c6b262d6215
Author: Carlos Amedee <carlos@golang.org>
Date: Wed Oct 28 17:15:39 2026 -0400
cmd/go: define GOWASM_stackswitch for Wasm assembly
When GOARCH=wasm and GOWASM enables stackswitch, pass
-D GOWASM_stackswitch to the assembler. Later CLs (starting with
"runtime, cmd/internal/obj/wasm: switch goroutines with Wasm
continuations") use it in runtime/asm_wasm.s to omit the
non-stack-switching mcall and morestack (defined instead in
runtime/stackswitch_wasm.s) and in runtime/rt0_*_wasm.s to select the
stack-switching code in the entry points and runtime.exit.
Unlike GOAMD64 or GOPPC64, GOWASM is a list of features rather than a
level, so there is one macro per enabled feature: today only
stackswitch. The legacy satconv and signext features are always
enabled and get no macro. The macro must agree with
buildcfg.GOWASM.StackSwitch, which the wasm.stackswitch build tag and
the rest of the toolchain use, so cmd/go parses GOWASM with the new
buildcfg.ParseGowasm, as it parses GOARM64 with buildcfg.ParseGoarm64,
rather than splitting the string itself. It relies on the earlier CL
"cmd/go: compute arch tool tags from go.env values" for GOWASM set in
go.env to produce the right tool tags.
The gated regions are a few functions in shared assembly files, so a
macro, like GOAMD64_* and GOARM_*, is simpler than splitting those
files by build tag.
For #81963
Change-Id: I2c450d6c4a83e40ccae64fbb4d771d637618b399
Reviewed-on: https://go-review.googlesource.com/c/go/+/862684
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Auto-Submit: Carlos Amedee <carlos@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
index 42f5c0f8ad..04c77eff13 100644
--- a/src/cmd/go/internal/cfg/cfg.go
+++ b/src/cmd/go/internal/cfg/cfg.go
@@ -137,6 +137,9 @@ func defaultContext() build.Context {
if archEnv != "" && !strings.Contains(archEnv, ",") && !slices.Contains(ctxt.ToolTags, Goarch+"."+archEnv) {
ctxt.ToolTags = append(ctxt.ToolTags, Goarch+"."+archEnv)
}
+ if _, err := buildcfg.ParseGowasm(GOWASM); err != nil && buildcfg.Error == nil {
+ buildcfg.Error = err
+ }
// The go/build rule for whether cgo is enabled is:
// 1. If $CGO_ENABLED is set, respect it.
diff --git a/src/cmd/go/internal/work/gc.go b/src/cmd/go/internal/work/gc.go
index 86c7fa26c0..3136e4b645 100644
--- a/src/cmd/go/internal/work/gc.go
+++ b/src/cmd/go/internal/work/gc.go
@@ -477,6 +477,19 @@ func asmArgs(a *Action, p *load.Package) []any {
}
}
+ if cfg.Goarch == "wasm" {
+ // Define GOWASM_stackswitch from cfg.GOWASM, parsed with
+ // buildcfg.ParseGowasm so it agrees with buildcfg.GOWASM.StackSwitch
+ // in the rest of the toolchain (the legacy satconv and signext
+ // features are always enabled and get no macro). Any parse error is
+ // reported by buildcfg.Check / BuildInit; gating on err == nil here
+ // keeps this in sync with buildcfg.GogoarchTags.
+ f, err := buildcfg.ParseGowasm(cfg.GOWASM)
+ if err == nil && f.StackSwitch {
+ args = append(args, "-D", "GOWASM_stackswitch")
+ }
+ }
+
return args
}
diff --git a/src/cmd/go/internal/work/init.go b/src/cmd/go/internal/work/init.go
index ea6a9e7cf4..1bcfc51946 100644
--- a/src/cmd/go/internal/work/init.go
+++ b/src/cmd/go/internal/work/init.go
@@ -16,6 +16,7 @@ import (
"cmd/internal/quoted"
"errors"
"fmt"
+ "internal/buildcfg"
"internal/platform"
"os"
"os/exec"
@@ -104,6 +105,9 @@ func BuildInit(ld *modload.Loader) {
}
modload.Init(ld)
+ if _, err := buildcfg.ParseGowasm(cfg.GOWASM); err != nil {
+ base.Fatal(err)
+ }
instrumentInit()
buildModeInit()
initCompilerConcurrencyPool()
diff --git a/src/cmd/go/testdata/script/tooltags.txt b/src/cmd/go/testdata/script/tooltags.txt
index 3ef20c78f8..6398b7b6a5 100644
--- a/src/cmd/go/testdata/script/tooltags.txt
+++ b/src/cmd/go/testdata/script/tooltags.txt
@@ -112,9 +112,28 @@ go list -f '{{context.ToolTags}}'
stdout 'wasm32.satconv wasm32.signext'
! stdout 'wasm32.stackswitch'
+# GOWASM=stackswitch defines GOWASM_stackswitch for the assembler, and no
+# other GOWASM value defines a GOWASM_ macro.
+env GOOS=js
+env GOARCH=wasm
+env GOWASM=stackswitch
+go build -a -n runtime
+stderr 'GOWASM_stackswitch'
+env GOWASM=satconv,stackswitch
+go list -f '{{context.ToolTags}}'
+stdout 'wasm\.stackswitch( |\])'
+go build -a -n runtime
+stderr 'GOWASM_stackswitch'
+env GOWASM=satconv
+go build -a -n runtime
+! stderr 'GOWASM_'
+env GOWASM=
+go build -a -n runtime
+! stderr 'GOWASM_'
+
# Setting GOARCH=wasm and GOWASM=satconv,stackswitch via GOENV must derive
-# the wasm.* tool tags (selecting stackswitch_wasmx.go), without leaking
-# host-arch tool tags.
+# the wasm.* tool tags (selecting stackswitch_wasmx.go) and define
+# GOWASM_stackswitch, without leaking host-arch tool tags.
env GOOS=
env GOARCH=
env GOWASM=
@@ -126,6 +145,13 @@ stdout 'wasm\.satconv wasm\.signext wasm\.stackswitch\]'
go list -f '{{.GoFiles}}' runtime
stdout 'stackswitch_wasmx\.go'
! stdout 'stackswitch_stub\.go'
+go build -a -n runtime
+stderr 'GOWASM_stackswitch'
+
+# An invalid GOWASM in GOENV is rejected.
+cp badwasm.env $GOENV
+! go build -n runtime
+stderr 'go: invalid GOWASM: no such feature "bogus"'
-- amd64.env --
GOOS=linux
@@ -142,3 +168,8 @@ package p
GOOS=js
GOARCH=wasm
GOWASM=satconv,stackswitch
+
+-- badwasm.env --
+GOOS=js
+GOARCH=wasm
+GOWASM=stackswitch,bogus
diff --git a/src/internal/buildcfg/cfg.go b/src/internal/buildcfg/cfg.go
index 78cc5a45a1..fc93b07016 100644
--- a/src/internal/buildcfg/cfg.go
+++ b/src/internal/buildcfg/cfg.go
@@ -397,13 +397,13 @@ func goriscv64() int {
return v
}
-// gowasmFeatures describes the WebAssembly features that GOWASM enables.
+// GowasmFeatures describes the WebAssembly features that GOWASM enables.
// The legacy satconv and signext features are always enabled.
-type gowasmFeatures struct {
+type GowasmFeatures struct {
StackSwitch bool
}
-func (f gowasmFeatures) String() string {
+func (f GowasmFeatures) String() string {
var flags []string
if f.StackSwitch {
flags = append(flags, "stackswitch")
@@ -411,10 +411,10 @@ func (f gowasmFeatures) String() string {
return strings.Join(flags, ",")
}
-// parseGowasm parses a GOWASM value: a comma-separated list of features.
+// ParseGowasm parses a GOWASM value: a comma-separated list of features.
// It returns the features it recognizes, and an error for the first feature it
// doesn't.
-func parseGowasm(v string) (f gowasmFeatures, err error) {
+func ParseGowasm(v string) (f GowasmFeatures, err error) {
for opt := range strings.SplitSeq(v, ",") {
switch opt {
case "satconv":
@@ -434,8 +434,8 @@ func parseGowasm(v string) (f gowasmFeatures, err error) {
return f, err
}
-func gowasm() gowasmFeatures {
- f, err := parseGowasm(envOr("GOWASM", ""))
+func gowasm() GowasmFeatures {
+ f, err := ParseGowasm(envOr("GOWASM", ""))
if err != nil {
Error = err
}
@@ -559,7 +559,7 @@ func GogoarchTags(goarch, archEnv string) []string {
}
return list
case "wasm", "wasm32":
- f, err := parseGowasm(archEnv)
+ f, err := ParseGowasm(archEnv)
var list []string
// SatConv is always enabled
list = append(list, goarch+".satconv")
diff --git a/src/internal/buildcfg/cfg_test.go b/src/internal/buildcfg/cfg_test.go
index 34a56b4a6c..d3773bae3e 100644
--- a/src/internal/buildcfg/cfg_test.go
+++ b/src/internal/buildcfg/cfg_test.go
@@ -160,6 +160,35 @@ func TestGogoarchTags(t *testing.T) {
}
}
+func TestParseGowasm(t *testing.T) {
+ tests := []struct {
+ in string
+ want GowasmFeatures
+ wantErr string
+ }{
+ {"", GowasmFeatures{}, ""},
+ {"stackswitch", GowasmFeatures{StackSwitch: true}, ""},
+ {"satconv,stackswitch", GowasmFeatures{StackSwitch: true}, ""},
+ {",stackswitch,", GowasmFeatures{StackSwitch: true}, ""},
+ {"bogus", GowasmFeatures{}, `invalid GOWASM: no such feature "bogus"`},
+ {"stackswitch,bogus", GowasmFeatures{StackSwitch: true}, `invalid GOWASM: no such feature "bogus"`},
+ {"first,second", GowasmFeatures{}, `invalid GOWASM: no such feature "first"`},
+ }
+ for _, tt := range tests {
+ got, err := ParseGowasm(tt.in)
+ if got != tt.want {
+ t.Errorf("ParseGowasm(%q) = %+v, want %+v", tt.in, got, tt.want)
+ }
+ if tt.wantErr == "" {
+ if err != nil {
+ t.Errorf("ParseGowasm(%q) unexpected error: %v", tt.in, err)
+ }
+ } else if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
+ t.Errorf("ParseGowasm(%q) error = %v, want %q", tt.in, err, tt.wantErr)
+ }
+ }
+}
+
var goodFIPS = []string{
"v1.0.0",
"v1.0.1",