Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoとSIMDとWasmの今。

Avatar for asuka asuka
June 06, 2026

 GoとSIMDとWasmの今。

Kyoto.go #65 オフラインLT会@はてな
https://kyotogo.connpass.com/event/392816/

Avatar for asuka

asuka

June 06, 2026

More Decks by asuka

Other Decks in Technology

Transcript

  1. Go1.26のリリースノートにSIMDに関する記述がある 👀 私,気になります! New experimental simd/archsimd package 2 Go 1.26

    introduces a new experimental simd/archsimd package, which can be enabled by setting the environment variable GOEXPERIMENT=simd at build time. This package provides access to architecture-specific SIMD operations. It is currently available on the amd64 architecture and supports 128-bit, 256-bit, and 512-bit vector types, such as Int8x16 and Float64x8, with operations such as Int8x16.Add. The API is not yet considered stable. We intend to provide support for other architectures in future versions, but the API intentionally architecture-specific and thus non-portable. In addition, we plan to develop a high-level portable SIMD package in the future. See the package documentation and the proposal issue for more details.
  2. - 1つの命令で,複数のデータを処理する処理の仕組み (ベクトル演算命令) - CPUアーキテクチャ依存の仕組み - AVX-512とか聞いたことあるかも ? e.g. Wasmには,SIMD用のv128という128ビットの型が用意されている

    - 128ビットに詰められる値 - 64ビットを2個 : float64/int32を2個まとめて計算 - 32ビットを4個 : float32/int32を4個まとめて計算 - 16ビットを8個 : int16を8個まとめて計算 - 8ビットを16個 : int8を16個まとめて計算 SIMD (Single Instruction, Multiple Data) 3
  3. SIMDの計算例 AとBの内容を足してCを作る - A = [1, 2, 3, 4] -

    B = [10, 20, 30, 40] - C = [11, 22, 33, 44] スカラー演算 : for i in 0..4 { C[i] = A[i] + B[i] } // 4回計算する ベクトル演算 : C = A + B // 1回計算する → 理論上は4倍早い!! 4
  4. e.g. Rust(Yew) vs JavaScript(React) — マンデルブロ集合で実測したWebAssembly のリアルな速度差 SIMDの計算例 5 両者を同条件(Worker

    × 4 プール再利用、release ビルド、同じ画面サイズ)で揃えた上で、改めて計 測した結果がこちらです。 参考までに、初期画面と深ズーム時のスクリーンショットを添えておきます。 シーン React 版 Frame Yew 版 Frame 比 Zoom 1.9x(初期画面) 12.0 ms 10.0 ms Yew 1.2x Zoom 約 380x(中ズーム) 170.4 ms 156.6 ms Yew 1.09x Zoom 約 72,000x(深ズーム) 170.3 ms 153.3 ms Yew 1.11x SIMD使って速く 出来ないかな
  5. https://github.com/plumchang/yew-fractal/pull/1 SIMDの計算例 6 SIMD無 フレーム生成 : 94.5ms 11fps SIMD有 フレーム生成

    : 28.3ms 35fps 約3倍高速化!! f64 → v128 f32x4で計算 (倍精度じゃなくても大丈夫そう )
  6. Go1.26のSIMDを試すには 1. ビルド時にGOEXPERIMENT=simdを指定 2. 現状はARCH=amd64のみサポート 7 import ( "fmt" "simd/archsimd"

    ) func main() { var a archsimd.Float324 = archsimd.LoadFloat324&4]float321, 2, 3, 4 var b archsimd.Float324 = archsimd.BroadcastFloat3242 c := a.Mul(b) fmt.Println(c) } $ GOEXPERIMENT=simd GOOS=darwin GOARCH=amd64 go build -o simd main.go $ ./simd {2,4,6,8}
  7. architecture-specific SIMD intrinsics under a GOEXPERIMENT - 2段階の対応を予定 a. 低レイヤーのアーキテクチャ

    API実装 b. 高レイヤーの移植性の高いベクター API実装 - Go1.26ではamd64の低レイヤーアーキテクチャを実装 a. simd/archsimd - archsimd.Float32x4 - archsimd.Float64x2 - archsimd.Int8x16 - archsimd.Int16x8 - etc. e.g. Wasm i16x8.extadd_pairwise_i8x16_s 互換性がありそう GoのSIMD (1.26) 8
  8. GoのSIMD (future) architecture and vector-size agnostic SIMD intrinsics under a

    GOEXPERIMENT - 2週間前にAccepted🎉 - 高レイヤーの移植性の高い(=アーキテクチャに依存しない)ベクターAPI実装 - simd.Float32s - simd.Float64s - simd.Int8s - simd.Int16s - etc. 9 In this version, the supported vector methods are those in the intersection of the wasm SIMD API and the current amd64 SIMD API; this is a small but still useful set of operations. Starting small also simplifies the emulation for those platforms that do not yet support SIMD, and also increases the chance that newly implemented SIMD support for some other architecture will easily be compatible. サイズを意識しなくともよい Wasmとamd64の共通命令 のサポートからスタート!!
  9. GoとWasm Node.jsからWASIp1をそのまま実行できる (Guleコード不要) ※ Node.js ≠ ブラウザ 11 import {

    readFile } from "node:fs/promises"; import { WASI } from "node:wasi"; const wasi = new WASI version: "preview1", args: [], env: {}, preopens: {} }); const bytes = await readFile(new URL"main.wasm", import.meta.url)); const wasm = await WebAssembly.instantiate(bytes, wasi.getImportObject()); wasi.start(wasm.instance); Node.jsがWASIp1をサポートしている node --experimental-wasi-unstable-preview1 example.ts Denoも2.8からサポート deno run -R=. example.ts
  10. $ whoami asuka / @a_skua • 株式会社モニクル/SWE ◦ Monicle Techbook

    vol.2 技術書典19 • Wasm関連の同人誌・商業誌の執筆 ◦ 実践入門WebAssembly ◦ WebAssembly System Interface入門 ◦ Wasm Cookbook vol.2 技術書典17 ◦ Wasm Cookbook vol.3 技術書典18 ◦ Wasm Cookbook vol.4 技術書典19 ◦ Wasm Cookbook vol.5 技術書典20 ◦ Wasm Cookbook vol.6 技術書典NT 12 商業誌 Cookbookシリーズ ごちWASI