Skip to content

Commit f0d2f50

Browse files
garet90romaindoumenc
authored andcommitted
runtime: add wasm bulk memory operations
The existing implementation uses loops to implement bulk memory operations such as memcpy and memclr. Now that bulk memory operations have been standardized and are implemented in all major browsers and engines (see https://webassembly.org/roadmap/), we should use them to improve performance. Updates golang#28360 Change-Id: I28df0e0350287d5e7e1d1c09a4064ea1054e7575 Reviewed-on: https://go-review.googlesource.com/c/go/+/444935 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Richard Musiol <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Richard Musiol <[email protected]> Reviewed-by: Richard Musiol <[email protected]>
1 parent c30bce7 commit f0d2f50

File tree

15 files changed

+59
-347
lines changed

15 files changed

+59
-347
lines changed

src/cmd/compile/internal/ir/symtab.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ var Syms struct {
5858
// Wasm
5959
WasmDiv *obj.LSym
6060
// Wasm
61-
WasmMove *obj.LSym
62-
// Wasm
63-
WasmZero *obj.LSym
64-
// Wasm
6561
WasmTruncS *obj.LSym
6662
// Wasm
6763
WasmTruncU *obj.LSym

src/cmd/compile/internal/ssa/_gen/Wasm.rules

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,9 @@
234234
(I64Store [s-8] dst (I64Load [s-8] src mem)
235235
(I64Store dst (I64Load src mem) mem))
236236

237-
// Adjust moves to be a multiple of 16 bytes.
238-
(Move [s] dst src mem)
239-
&& s > 16 && s%16 != 0 && s%16 <= 8 =>
240-
(Move [s-s%16]
241-
(OffPtr <dst.Type> dst [s%16])
242-
(OffPtr <src.Type> src [s%16])
243-
(I64Store dst (I64Load src mem) mem))
244-
(Move [s] dst src mem)
245-
&& s > 16 && s%16 != 0 && s%16 > 8 =>
246-
(Move [s-s%16]
247-
(OffPtr <dst.Type> dst [s%16])
248-
(OffPtr <src.Type> src [s%16])
249-
(I64Store [8] dst (I64Load [8] src mem)
250-
(I64Store dst (I64Load src mem) mem)))
251-
252237
// Large copying uses helper.
253-
(Move [s] dst src mem) && s%8 == 0 && logLargeCopy(v, s) =>
254-
(LoweredMove [s/8] dst src mem)
238+
(Move [s] dst src mem) && logLargeCopy(v, s) =>
239+
(LoweredMove [s] dst src mem)
255240

256241
// Lowering Zero instructions
257242
(Zero [0] _ mem) => mem
@@ -274,7 +259,7 @@
274259
(I64Store32 destptr (I64Const [0]) mem))
275260

276261
// Strip off any fractional word zeroing.
277-
(Zero [s] destptr mem) && s%8 != 0 && s > 8 =>
262+
(Zero [s] destptr mem) && s%8 != 0 && s > 8 && s < 32 =>
278263
(Zero [s-s%8] (OffPtr <destptr.Type> destptr [s%8])
279264
(I64Store destptr (I64Const [0]) mem))
280265

@@ -293,8 +278,8 @@
293278
(I64Store destptr (I64Const [0]) mem))))
294279

295280
// Large zeroing uses helper.
296-
(Zero [s] destptr mem) && s%8 == 0 && s > 32 =>
297-
(LoweredZero [s/8] destptr mem)
281+
(Zero [s] destptr mem) =>
282+
(LoweredZero [s] destptr mem)
298283

299284
// Lowering constants
300285
(Const64 ...) => (I64Const ...)

src/cmd/compile/internal/ssa/_gen/WasmOps.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func init() {
126126
{name: "LoweredInterCall", argLength: 2, reg: regInfo{inputs: []regMask{gp}, clobbers: callerSave}, aux: "CallOff", call: true}, // call fn by pointer. arg0=codeptr, arg1=mem, auxint=argsize, returns mem
127127

128128
{name: "LoweredAddr", argLength: 1, reg: gp11, aux: "SymOff", rematerializeable: true, symEffect: "Addr"}, // returns base+aux+auxint, arg0=base
129-
{name: "LoweredMove", argLength: 3, reg: regInfo{inputs: []regMask{gp, gp}}, aux: "Int64"}, // large move. arg0=dst, arg1=src, arg2=mem, auxint=len/8, returns mem
130-
{name: "LoweredZero", argLength: 2, reg: regInfo{inputs: []regMask{gp}}, aux: "Int64"}, // large zeroing. arg0=start, arg1=mem, auxint=len/8, returns mem
129+
{name: "LoweredMove", argLength: 3, reg: regInfo{inputs: []regMask{gp, gp}}, aux: "Int64"}, // large move. arg0=dst, arg1=src, arg2=mem, auxint=len, returns mem
130+
{name: "LoweredZero", argLength: 2, reg: regInfo{inputs: []regMask{gp}}, aux: "Int64"}, // large zeroing. arg0=start, arg1=mem, auxint=len, returns mem
131131

132132
{name: "LoweredGetClosurePtr", reg: gp01}, // returns wasm.REG_CTXT, the closure pointer
133133
{name: "LoweredGetCallerPC", reg: gp01, rematerializeable: true}, // returns the PC of the caller of the current function

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

Lines changed: 8 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ func InitConfig() {
206206
}
207207

208208
// Wasm (all asm funcs with special ABIs)
209-
ir.Syms.WasmMove = typecheck.LookupRuntimeVar("wasmMove")
210-
ir.Syms.WasmZero = typecheck.LookupRuntimeVar("wasmZero")
211209
ir.Syms.WasmDiv = typecheck.LookupRuntimeVar("wasmDiv")
212210
ir.Syms.WasmTruncS = typecheck.LookupRuntimeVar("wasmTruncS")
213211
ir.Syms.WasmTruncU = typecheck.LookupRuntimeVar("wasmTruncU")

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,13 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
149149
getValue32(s, v.Args[0])
150150
getValue32(s, v.Args[1])
151151
i32Const(s, int32(v.AuxInt))
152-
p := s.Prog(wasm.ACall)
153-
p.To = obj.Addr{Type: obj.TYPE_MEM, Name: obj.NAME_EXTERN, Sym: ir.Syms.WasmMove}
152+
s.Prog(wasm.AMemoryCopy)
154153

155154
case ssa.OpWasmLoweredZero:
156155
getValue32(s, v.Args[0])
156+
i32Const(s, 0)
157157
i32Const(s, int32(v.AuxInt))
158-
p := s.Prog(wasm.ACall)
159-
p.To = obj.Addr{Type: obj.TYPE_MEM, Name: obj.NAME_EXTERN, Sym: ir.Syms.WasmZero}
158+
s.Prog(wasm.AMemoryFill)
160159

161160
case ssa.OpWasmLoweredNilCheck:
162161
getValue64(s, v.Args[0])

src/cmd/internal/obj/wasm/a.out.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ const (
231231
AI64TruncSatF64S
232232
AI64TruncSatF64U
233233

234+
AMemoryInit
235+
ADataDrop
236+
AMemoryCopy
237+
AMemoryFill
238+
ATableInit
239+
AElemDrop
240+
ATableCopy
241+
ATableGrow
242+
ATableSize
243+
ATableFill
244+
234245
ALast // Sentinel: End of low-level WebAssembly instructions.
235246

236247
ARESUMEPOINT

src/cmd/internal/obj/wasm/anames.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/internal/obj/wasm/wasmobj.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,6 @@ var notUsePC_B = map[string]bool{
799799
"wasm_export_resume": true,
800800
"wasm_export_getsp": true,
801801
"wasm_pc_f_loop": true,
802-
"runtime.wasmMove": true,
803-
"runtime.wasmZero": true,
804802
"runtime.wasmDiv": true,
805803
"runtime.wasmTruncS": true,
806804
"runtime.wasmTruncU": true,
@@ -844,7 +842,7 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
844842
// Some functions use a special calling convention.
845843
switch s.Name {
846844
case "_rt0_wasm_js", "wasm_export_run", "wasm_export_resume", "wasm_export_getsp", "wasm_pc_f_loop",
847-
"runtime.wasmMove", "runtime.wasmZero", "runtime.wasmDiv", "runtime.wasmTruncS", "runtime.wasmTruncU", "memeqbody":
845+
"runtime.wasmDiv", "runtime.wasmTruncS", "runtime.wasmTruncU", "memeqbody":
848846
varDecls = []*varDecl{}
849847
useAssemblyRegMap()
850848
case "memchr", "memcmp":
@@ -1088,7 +1086,11 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
10881086
writeUleb128(w, align(p.As))
10891087
writeUleb128(w, uint64(p.To.Offset))
10901088

1091-
case ACurrentMemory, AGrowMemory:
1089+
case ACurrentMemory, AGrowMemory, AMemoryFill:
1090+
w.WriteByte(0x00)
1091+
1092+
case AMemoryCopy:
1093+
w.WriteByte(0x00)
10921094
w.WriteByte(0x00)
10931095

10941096
}

src/cmd/link/internal/wasm/asm.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ var wasmFuncTypes = map[string]*wasmFuncType{
6060
"wasm_export_resume": {Params: []byte{}}, //
6161
"wasm_export_getsp": {Results: []byte{I32}}, // sp
6262
"wasm_pc_f_loop": {Params: []byte{}}, //
63-
"runtime.wasmMove": {Params: []byte{I32, I32, I32}}, // dst, src, len
64-
"runtime.wasmZero": {Params: []byte{I32, I32}}, // ptr, len
6563
"runtime.wasmDiv": {Params: []byte{I64, I64}, Results: []byte{I64}}, // x, y -> x/y
6664
"runtime.wasmTruncS": {Params: []byte{F64}, Results: []byte{I64}}, // x -> int(x)
6765
"runtime.wasmTruncU": {Params: []byte{F64}, Results: []byte{I64}}, // x -> uint(x)

0 commit comments

Comments
 (0)