Skip to content

feat: add static Go 1.27 simd/archsimd backend (part of #17) - #18

Open
hazyhaar wants to merge 1 commit into
cloudwego:mainfrom
hazyhaar:feat/go127-simd
Open

hazyhaar wants to merge 1 commit into
cloudwego:mainfrom
hazyhaar:feat/go127-simd

Conversation

@hazyhaar

@hazyhaar hazyhaar commented Sep 26, 2026 •

Copy link
Copy Markdown

Description

Part of #17.

This pull request adds an ahead-of-time SIMD backend for Go 1.27+ on amd64 gated by go1.27 && goexperiment.simd (simd/archsimd), avoiding runtime executable memory mapping (W^X / MemoryDenyWriteExecute=yes) and the Go 1.27 sonic/loader symbol-table initialization crash when building with GOEXPERIMENT=simd.

Scope with Respect to #17

  • Covered by this PR (GOEXPERIMENT=simd on Go 1.27+ amd64): Static Go simd/archsimd AVX2 kernels (internal/simd), with automatic fallback to encoding/base64 on non-AVX2 amd64 CPUs. No runtime mmap/mprotect(PROT_EXEC) and no bytedance/sonic/loader initialization occur on this path.
  • Remaining in [BUG] Fatal crash at init under Go 1.27 (runtime.moduledataverify1) and panic under W^X (MemoryDenyWriteExecute) #17 (Go <= 1.26 default builds and Go 1.27 without GOEXPERIMENT=simd):
    • On Go <= 1.26 ((!go1.27 || !goexperiment.simd) && amd64 && !noasm && !appengine), base64x keeps the existing internal/native + bytedance/sonic/loader path even if GOEXPERIMENT=simd is set, because bytedance/sonic (v1.15.0, gated by go1.17 && !go1.27) links _subr__b64decode via //go:linkname and emits direct C-ABI calls from its JIT decoder. Under strict W^X (MemoryDenyWriteExecute=yes) on Go <= 1.26, building with -tags noasm works around the issue by selecting base64x_fallback.go (and disabling sonic's JIT), while the default-build Go 1.26 W^X case itself remains open in [BUG] Fatal crash at init under Go 1.27 (runtime.moduledataverify1) and panic under W^X (MemoryDenyWriteExecute) #17.
    • Default Go 1.27 builds without GOEXPERIMENT=simd remain dependent on an upstream bytedance/sonic/loader update for Go 1.27's pcHeader / moduledata layout.

Key Changes

  • Go 1.27+ Static Backend (base64x_simd_amd64.go): Conditioned on //go:build go1.27 && goexperiment.simd && amd64 && !noasm && !appengine, dispatching to AVX2 simd/archsimd routines for standard and URL alphabets (padded and raw), CRLF handling, zero-allocation in-place decoding, and native/native.h-compatible JSON escape unescaping (\r, \n, \/, ASCII \uXXXX) with src-indexed base64.CorruptInputError offsets.
  • Static AVX2 Kernel (internal/simd): Ahead-of-time vector encode/decode routines (b64_gen.go) targeting Go 1.27's simd/archsimd API, with scalar fallback (b64_nosimd_gen.go) and destination capacity guards for 32-byte vector stores.
  • Native Loader Exclusion under go1.27 && goexperiment.simd: Guarded base64x_native.go, base64x_subr_native_amd64.go, base64x_error_native_amd64_test.go, and internal/native (including avx2, sse, and .tmpl templates) with !go1.27 || !goexperiment.simd so GOEXPERIMENT=simd go test -race ./... passes across all packages of the root module on Go 1.27+ without initializing sonic/loader, while Go <= 1.26 root-package builds remain on internal/native.
  • _subr__b64decode / _subr__b64encode Semantics: Documented in base64x_simd_amd64.go and base64x_subr_amd64.go that both uintptr symbols remain declared (preserving linkability) and stay 0 under go1.27 && goexperiment.simd (where sonic's !go1.27 JIT is inactive), matching -tags noasm and appengine builds.
  • License Header Compliance (.licenserc.yaml): Placed Apache-2.0 headers above //go:build directives and added internal/simd/*.go (b64_gen.go, b64_nosimd_gen.go) to paths-ignore alongside internal/native/{avx2,sse}/*.go.

Verification

  • GOEXPERIMENT=simd go test -race -count=3 ./... (Go 1.27.2 linux/amd64)
  • go test -tags noasm -race -count=3 ./... and go test -tags appengine -race -count=1 ./...
  • Go 1.26.6 (go test -race ./... and GOEXPERIMENT=simd go test -race . ./internal/native) and Go 1.20.14 (go test -race ./...)
  • Cross-compilation checks for linux/arm64 (./...) and linux/riscv64 (.)
  • Benchmarks on Intel Core i9-14900K (-cpu=1, 0 B/op, 0 allocs/op, superseding the preliminary [BUG] Fatal crash at init under Go 1.27 (runtime.moduledataverify1) and panic under W^X (MemoryDenyWriteExecute) #17 numbers):
    • Root 4 kB (GOEXPERIMENT=simd go test -cpu=1 -bench=BenchmarkSimd -benchmem .): BenchmarkSimdEncode_4kB 7.72 GB/s, BenchmarkSimdDecode_4kB 5.56 GB/s
    • ./bench (cd ./bench && GOEXPERIMENT=simd go test -cpu=1 -bench=. -benchmem -run=none ./...): BenchmarkEncoderBase64x_128B 4.81 GB/s, 4kB 7.73 GB/s, 256kB 7.85 GB/s, 1MB 7.80 GB/s (vs encoding/base64 2.27–2.47 GB/s), and BenchmarkDecoderBase64x (64 B input) 1.09 GB/s

@CLAassistant

CLAassistant commented Sep 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@dajiaohuang

dajiaohuang commented Oct 22, 2026 •

Copy link
Copy Markdown

Scope clarification: issue #17 separately reproduces the W^X failure with Go 1.26. This PR's static backend is gated by goexperiment.simd, while its description says Go 1.26 and earlier continue using the existing loader. Thus the issue's MemoryDenyWriteExecute=yes repro on Go 1.26 appears to remain on the dynamic-loader path and is not covered by this patch. Is this PR intended to address only the Go 1.27 initialization crash? If so, could the Go 1.26 W^X case remain explicitly tracked rather than treating all of #17 as resolved by this PR?

Add a static Go 1.27 simd/archsimd AVX2 backend under
go1.27 && goexperiment.simd so amd64 builds with GOEXPERIMENT=simd on
Go 1.27+ encode and decode without runtime executable page mapping
(W^X / MemoryDenyWriteExecute=yes) or sonic/loader.

- Gate the static backend on go1.27 && goexperiment.simd and guard
  internal/native (including avx2/sse subpackages and templates) with
  !go1.27 || !goexperiment.simd so Go <= 1.26 stays on internal/native
  (preserving _subr__b64decode for sonic's !go1.27 JIT) while Go 1.27+
  passes GOEXPERIMENT=simd go test ./... across all root-module packages.
- Match native/native.h JSON mode unescaping (\r, \n, \/, ASCII \uXXXX),
  raw CRLF acceptance, zero-alloc in-place decoding, and src-indexed
  base64.CorruptInputError offsets.
- Document that _subr__b64decode and _subr__b64encode remain 0 under
  go1.27 && goexperiment.simd, matching -tags noasm and appengine builds.
- Place Apache-2.0 license headers before build constraints and ignore
  generated internal/simd/*.go files in .licenserc.yaml.

Part of cloudwego#17
@hazyhaar hazyhaar changed the title feat: add static Go 1.27 simd/archsimd backend (fixes #17) feat: add static Go 1.27 simd/archsimd backend (part of #17) Nov 2, 2026
@hazyhaar

hazyhaar commented Nov 2, 2026

Copy link
Copy Markdown
Author

@dajiaohuang Thank you for the review — updated the PR title, description, and commit footer from Fixes #17 to Part of #17 so the remaining cases stay tracked in #17.

Concretely:

  1. What this PR covers (go1.27 && goexperiment.simd on amd64): On Go 1.27+ with GOEXPERIMENT=simd, base64x uses the static ahead-of-time simd/archsimd backend (base64x_simd_amd64.go + internal/simd) and does not compile or initialize internal/native / bytedance/sonic/loader. That avoids both the Go 1.27 runtime.moduledataverify1 crash and runtime executable memory mapping (W^X / MemoryDenyWriteExecute=yes) under GOEXPERIMENT=simd.
  2. Why Go <= 1.26 default builds (and Go 1.27 without GOEXPERIMENT=simd) remain open in [BUG] Fatal crash at init under Go 1.27 (runtime.moduledataverify1) and panic under W^X (MemoryDenyWriteExecute) #17: In bytedance/sonic (v1.15.0, gated by go1.17 && !go1.27), internal/rt/base64_amd64.go and internal/decoder/jitdec/assembler_regabi_amd64.go go:linkname _subr__b64decode and emit a direct C-ABI CALL to that raw machine-code entrypoint from the JIT decoder. Gating the static backend on go1.27 && goexperiment.simd (and internal/native on !go1.27 || !goexperiment.simd) keeps _subr__b64decode populated on Go <= 1.26 even if GOEXPERIMENT=simd is set, while on Go 1.27+ sonic's JIT is not built (!go1.27), so leaving _subr__b64decode and _subr__b64encode at 0 under go1.27 && goexperiment.simd (matching -tags noasm and appengine) is safe. On Go <= 1.26, W^X environments can be worked around by building the binary with -tags noasm (which selects base64x_fallback.go and also disables sonic's JIT); the default-build Go 1.26 W^X case itself remains open in [BUG] Fatal crash at init under Go 1.27 (runtime.moduledataverify1) and panic under W^X (MemoryDenyWriteExecute) #17.

This update also includes three fixes alongside the scope change:

  • Guarded internal/native (including avx2/sse and .tmpl templates) with !go1.27 || !goexperiment.simd so GOEXPERIMENT=simd go test -race ./... passes across all packages of the root module on Go 1.27+ without triggering internal/native's init().
  • Aligned JSONStdEncoding / JSONURLEncoding unescaping (\r, \n, \/, ASCII \uXXXX, raw CRLF, src-indexed base64.CorruptInputError offsets) and zero-allocation in-place decoding on the static SIMD path with native/native.h.
  • Fixed the skywalking-eyes header check by placing Apache-2.0 headers before //go:build constraints and adding internal/simd/*.go (b64_gen.go, b64_nosimd_gen.go) to .licenserc.yaml paths-ignore alongside internal/native/{avx2,sse}/*.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants