Skip to content

Commit

Permalink
Revert "runtime: add wasm bulk memory operations" by Frank
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan committed Sep 11, 2023
1 parent 91c77a5 commit 85bdb1e
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 66 deletions.
4 changes: 4 additions & 0 deletions src/cmd/compile/internal/ir/symtab.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ var Syms struct {
// Wasm
WasmDiv *obj.LSym
// Wasm
WasmMove *obj.LSym
// Wasm
WasmZero *obj.LSym
// Wasm
WasmTruncS *obj.LSym
// Wasm
WasmTruncU *obj.LSym
Expand Down
25 changes: 20 additions & 5 deletions src/cmd/compile/internal/ssa/_gen/Wasm.rules
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,24 @@
(I64Store [s-8] dst (I64Load [s-8] src mem)
(I64Store dst (I64Load src mem) mem))

// Adjust moves to be a multiple of 16 bytes.
(Move [s] dst src mem)
&& s > 16 && s%16 != 0 && s%16 <= 8 =>
(Move [s-s%16]
(OffPtr <dst.Type> dst [s%16])
(OffPtr <src.Type> src [s%16])
(I64Store dst (I64Load src mem) mem))
(Move [s] dst src mem)
&& s > 16 && s%16 != 0 && s%16 > 8 =>
(Move [s-s%16]
(OffPtr <dst.Type> dst [s%16])
(OffPtr <src.Type> src [s%16])
(I64Store [8] dst (I64Load [8] src mem)
(I64Store dst (I64Load src mem) mem)))

// Large copying uses helper.
(Move [s] dst src mem) && logLargeCopy(v, s) =>
(LoweredMove [s] dst src mem)
(Move [s] dst src mem) && s%8 == 0 && logLargeCopy(v, s) =>
(LoweredMove [s/8] dst src mem)

// Lowering Zero instructions
(Zero [0] _ mem) => mem
Expand All @@ -259,7 +274,7 @@
(I64Store32 destptr (I64Const [0]) mem))

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

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

// Large zeroing uses helper.
(Zero [s] destptr mem) =>
(LoweredZero [s] destptr mem)
(Zero [s] destptr mem) && s%8 == 0 && s > 32 =>
(LoweredZero [s/8] destptr mem)

// Lowering constants
(Const64 ...) => (I64Const ...)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/ssa/_gen/WasmOps.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func init() {
{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

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

{name: "LoweredGetClosurePtr", reg: gp01}, // returns wasm.REG_CTXT, the closure pointer
{name: "LoweredGetCallerPC", reg: gp01, rematerializeable: true}, // returns the PC of the caller of the current function
Expand Down
81 changes: 73 additions & 8 deletions src/cmd/compile/internal/ssa/rewriteWasm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/cmd/compile/internal/ssagen/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func InitConfig() {
}

// Wasm (all asm funcs with special ABIs)
ir.Syms.WasmMove = typecheck.LookupRuntimeVar("wasmMove")
ir.Syms.WasmZero = typecheck.LookupRuntimeVar("wasmZero")
ir.Syms.WasmDiv = typecheck.LookupRuntimeVar("wasmDiv")
ir.Syms.WasmTruncS = typecheck.LookupRuntimeVar("wasmTruncS")
ir.Syms.WasmTruncU = typecheck.LookupRuntimeVar("wasmTruncU")
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/compile/internal/wasm/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,14 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
getValue32(s, v.Args[0])
getValue32(s, v.Args[1])
i32Const(s, int32(v.AuxInt))
s.Prog(wasm.AMemoryCopy)
p := s.Prog(wasm.ACall)
p.To = obj.Addr{Type: obj.TYPE_MEM, Name: obj.NAME_EXTERN, Sym: ir.Syms.WasmMove}

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

case ssa.OpWasmLoweredNilCheck:
getValue64(s, v.Args[0])
Expand Down
11 changes: 0 additions & 11 deletions src/cmd/internal/obj/wasm/a.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,6 @@ const (
AI64TruncSatF64S
AI64TruncSatF64U

AMemoryInit
ADataDrop
AMemoryCopy
AMemoryFill
ATableInit
AElemDrop
ATableCopy
ATableGrow
ATableSize
ATableFill

ALast // Sentinel: End of low-level WebAssembly instructions.

ARESUMEPOINT
Expand Down
10 changes: 0 additions & 10 deletions src/cmd/internal/obj/wasm/anames.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/cmd/internal/obj/wasm/wasmobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ var notUsePC_B = map[string]bool{
"runtime.gcWriteBarrier6": true,
"runtime.gcWriteBarrier7": true,
"runtime.gcWriteBarrier8": true,
"runtime.wasmMove": true,
"runtime.wasmZero": true,
"runtime.wasmDiv": true,
"runtime.wasmTruncS": true,
"runtime.wasmTruncU": true,
Expand Down Expand Up @@ -962,7 +964,8 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
// Some functions use a special calling convention.
switch s.Name {
case "_rt0_wasm_js", "_rt0_wasm_wasip1", "wasm_export_run", "wasm_export_resume", "wasm_export_getsp",
"wasm_pc_f_loop", "runtime.wasmDiv", "runtime.wasmTruncS", "runtime.wasmTruncU", "memeqbody":
"wasm_pc_f_loop", "runtime.wasmMove", "runtime.wasmZero", "runtime.wasmDiv", "runtime.wasmTruncS",
"runtime.wasmTruncU", "memeqbody":
varDecls = []*varDecl{}
useAssemblyRegMap()
case "memchr", "memcmp":
Expand Down Expand Up @@ -1216,11 +1219,7 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
writeUleb128(w, align(p.As))
writeUleb128(w, uint64(p.To.Offset))

case ACurrentMemory, AGrowMemory, AMemoryFill:
w.WriteByte(0x00)

case AMemoryCopy:
w.WriteByte(0x00)
case ACurrentMemory, AGrowMemory:
w.WriteByte(0x00)

}
Expand Down
16 changes: 9 additions & 7 deletions src/cmd/link/internal/wasm/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ func readWasmImport(ldr *loader.Loader, s loader.Sym) obj.WasmImport {
}

var wasmFuncTypes = map[string]*wasmFuncType{
"_rt0_wasm_js": {Params: []byte{}}, //
"_rt0_wasm_wasip1": {Params: []byte{}}, //
"wasm_export__start": {}, //
"wasm_export_run": {Params: []byte{I32, I32}}, // argc, argv
"wasm_export_resume": {Params: []byte{}}, //
"wasm_export_getsp": {Results: []byte{I32}}, // sp
"wasm_pc_f_loop": {Params: []byte{}}, //
"_rt0_wasm_js": {Params: []byte{}}, //
"_rt0_wasm_wasip1": {Params: []byte{}}, //
"wasm_export__start": {}, //
"wasm_export_run": {Params: []byte{I32, I32}}, // argc, argv
"wasm_export_resume": {Params: []byte{}}, //
"wasm_export_getsp": {Results: []byte{I32}}, // sp
"wasm_pc_f_loop": {Params: []byte{}},
"runtime.wasmMove": {Params: []byte{I32, I32, I32}}, // dst, src, len
"runtime.wasmZero": {Params: []byte{I32, I32}}, // ptr, len //
"runtime.wasmDiv": {Params: []byte{I64, I64}, Results: []byte{I64}}, // x, y -> x/y
"runtime.wasmTruncS": {Params: []byte{F64}, Results: []byte{I64}}, // x -> int(x)
"runtime.wasmTruncU": {Params: []byte{F64}, Results: []byte{I64}}, // x -> uint(x)
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/asm_wasm.s
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,10 @@ TEXT NAME(SB), WRAPPER, $MAXSIZE-48; \
I64Load stackArgs+16(FP); \
I32WrapI64; \
I64Load stackArgsSize+24(FP); \
I64Const $3; \
I64ShrU; \
I32WrapI64; \
MemoryCopy; \
Call runtime·wasmMove(SB); \
End; \
\
MOVD f+8(FP), CTXT; \
Expand Down
33 changes: 26 additions & 7 deletions src/runtime/memclr_wasm.s
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ TEXT runtime·memclrNoHeapPointers(SB), NOSPLIT, $0-16
MOVD ptr+0(FP), R0
MOVD n+8(FP), R1

Get R0
I32WrapI64
I32Const $0
Get R1
I32WrapI64
MemoryFill
RET
loop:
Loop
Get R1
I64Eqz
If
RET
End

Get R0
I32WrapI64
I64Const $0
I64Store8 $0

Get R0
I64Const $1
I64Add
Set R0

Get R1
I64Const $1
I64Sub
Set R1

Br loop
End
UNDEF
Loading

0 comments on commit 85bdb1e

Please sign in to comment.