Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion code.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func (t *translator) readCodeForFunction(fn *funcCompiler) error {
fn.pushConst(r)
}
}
fn.memMayGrow()
}

case 0x0f: // return
Expand Down Expand Up @@ -720,7 +721,7 @@ func (t *translator) readCodeForFunction(fn *funcCompiler) error {
&ast.BinaryExpr{
X: &ast.CallExpr{
Fun: newID("len"),
Args: []ast.Expr{t.memory.selector}},
Args: []ast.Expr{fn.mem()}},
Op: token.SHR,
Y: literal16,
}, t.memory.stype()))
Expand All @@ -732,6 +733,7 @@ func (t *translator) readCodeForFunction(fn *funcCompiler) error {
Sel: newID("Grow")},
Args: []ast.Expr{literal0, newID("maxMem")}},
t.memory.stype()))
fn.memMayGrow()

default:
needsUnsafe("shared memory")
Expand All @@ -742,6 +744,7 @@ func (t *translator) readCodeForFunction(fn *funcCompiler) error {
&ast.UnaryExpr{Op: token.AND, X: t.memory.selector},
literal0, newID("maxMem")}},
t.memory.stype()))
fn.memMayGrow()
}

case 0x40: // memory.grow
Expand Down Expand Up @@ -773,6 +776,7 @@ func (t *translator) readCodeForFunction(fn *funcCompiler) error {
newID("maxMem")}},
t.memory.stype()))
}
fn.memMayGrow()

case 0x41: // i32.const
e, err := t.constI32()
Expand Down
14 changes: 8 additions & 6 deletions code_atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (t *translator) readOpcodeAtomic(fn *funcCompiler) error {
_, err = t.in.ReadByte() // 0x00
fn.helpers.add("atomic_fence")
fn.emit(&ast.ExprStmt{X: &ast.CallExpr{Fun: newID("atomic_fence")}})
fn.memMayGrow()
return nil
}

Expand Down Expand Up @@ -170,6 +171,7 @@ func (t *translator) readOpcodeAtomic(fn *funcCompiler) error {
default:
return fmt.Errorf("unsupported opcode (atomic): 0xFE 0x%02X", code)
}
fn.memMayGrow()
return nil
}

Expand All @@ -179,7 +181,7 @@ func (fn *funcCompiler) atomicLoad(typ, name string, offset uint64) ast.Expr {
fn.helpers.add(name)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr},
Args: []ast.Expr{fn.mem(), addr},
}, typ)
}

Expand All @@ -193,7 +195,7 @@ func (fn *funcCompiler) atomicStore(name string, offset uint64) ast.Stmt {
fn.helpers.add("atomic_ptr" + bits)
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, val},
Args: []ast.Expr{fn.mem(), addr, val},
}}
}

Expand All @@ -207,7 +209,7 @@ func (fn *funcCompiler) atomicRmw(typ, name string, offset uint64) ast.Expr {
fn.helpers.add("atomic_ptr" + bits)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, val},
Args: []ast.Expr{fn.mem(), addr, val},
}, typ)
}

Expand All @@ -223,7 +225,7 @@ func (fn *funcCompiler) atomicCmpxchg(typ, name string, offset uint64) ast.Expr
fn.helpers.add("atomic_ptr" + bits)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, old, new},
Args: []ast.Expr{fn.mem(), addr, old, new},
}, typ)
}

Expand All @@ -237,7 +239,7 @@ func (fn *funcCompiler) atomicNotify(name string, offset uint64) ast.Expr {
return &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{
fn.memory.selector, addr, count,
fn.mem(), addr, count,
modSel(newID("waiters"))}}
}

Expand All @@ -255,6 +257,6 @@ func (fn *funcCompiler) atomicWait(name string, offset uint64) ast.Expr {
return &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{
fn.memory.selector, addr, exp, timeout,
fn.mem(), addr, exp, timeout,
modSel(newID("waiters"))}}
}
8 changes: 4 additions & 4 deletions code_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (t *translator) readOpcodeExtended(fn *funcCompiler) error {
X: &ast.CallExpr{
Fun: newID("memory_init"),
Args: []ast.Expr{
t.memory.selector,
fn.mem(),
t.dataExpr(int(i)), dst, src, n}}})

case 0x09: // data.drop
Expand Down Expand Up @@ -75,7 +75,7 @@ func (t *translator) readOpcodeExtended(fn *funcCompiler) error {
X: &ast.CallExpr{
Fun: newID("memory_copy"),
Args: []ast.Expr{
t.memory.selector,
fn.mem(),
dst, src, n}}})

case 0x0b: // memory.fill
Expand All @@ -93,15 +93,15 @@ func (t *translator) readOpcodeExtended(fn *funcCompiler) error {
X: &ast.CallExpr{
Fun: newID("memory_zero"),
Args: []ast.Expr{
t.memory.selector,
fn.mem(),
dest, n}}})
} else {
fn.helpers.add("memory_fill")
fn.emit(&ast.ExprStmt{
X: &ast.CallExpr{
Fun: newID("memory_fill"),
Args: []ast.Expr{
t.memory.selector,
fn.mem(),
dest, val, n}}})
}

Expand Down
116 changes: 113 additions & 3 deletions fncompiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"go/ast"
"go/token"
"slices"
"strconv"
"strings"

Expand All @@ -21,6 +22,12 @@ type funcCompiler struct {
labels int
temps int

memSlice *ast.ParenExpr
memReloads []memReload
memReads int
memFirstTop int
memLastTop int

provided bool
}

Expand Down Expand Up @@ -132,7 +139,7 @@ func (fn *funcCompiler) branch(n uint64) (stmts []ast.Stmt) {
// Returns an expression that loads a byte from memory (an l-value).
func (fn *funcCompiler) load8(offset uint64) ast.Expr {
return &ast.IndexExpr{
X: fn.memory.selector,
X: fn.mem(),
Index: fn.popAddr(offset)}
}

Expand All @@ -145,7 +152,7 @@ func (fn *funcCompiler) load(typ string, offset uint64) (expr ast.Expr) {
fn.helpers.add("load" + bits)
expr = &ast.CallExpr{
Fun: newID("load" + bits),
Args: []ast.Expr{fn.memory.selector, addr}}
Args: []ast.Expr{fn.mem(), addr}}

switch {
case strings.HasPrefix(typ, "float"):
Expand Down Expand Up @@ -180,7 +187,7 @@ func (fn *funcCompiler) store(typ string, offset uint64) ast.Stmt {
fn.helpers.add("store" + bits)
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: newID("store" + bits),
Args: []ast.Expr{fn.memory.selector, addr, val}}}
Args: []ast.Expr{fn.mem(), addr, val}}}
}

// Pushes expr (a literal, constant or materialized temporary) to the value stack.
Expand Down Expand Up @@ -597,7 +604,110 @@ func (fn *funcCompiler) newLabel() *ast.Ident {
return id
}

const loopWeight = 8

func (fn *funcCompiler) inLoop() bool {
for i := range fn.blocks {
if fn.blocks[i].loopPos != 0 {
return true
}
}
return false
}

type memReload struct {
body *ast.BlockStmt
pos int
topIdx int
prevReads int
inLoop bool
}

func (fn *funcCompiler) mem() ast.Expr {
if *noopt || fn.blocks.top().unreachable {
return fn.memory.selector
}
if fn.memSlice == nil {
fn.memSlice = &ast.ParenExpr{X: fn.memory.selector}
}
topIdx := len(fn.blocks[0].body.List)
if len(fn.blocks) > 1 {
topIdx--
}
if fn.memReads == 0 {
fn.memFirstTop = topIdx
}
if fn.inLoop() {
fn.memReads += loopWeight
} else {
fn.memReads += 1
}
fn.memLastTop = topIdx
return fn.memSlice
}

func (fn *funcCompiler) memMayGrow() {
blk := fn.blocks.top()
if *noopt || fn.memory == nil || blk.unreachable {
return
}
fn.memReloads = append(fn.memReloads, memReload{
body: blk.body,
pos: len(blk.body.List),
topIdx: len(fn.blocks[0].body.List) - 1,
prevReads: fn.memReads,
inLoop: fn.inLoop(),
})
}

func (fn *funcCompiler) memFinish() {
if fn.memReads == 0 {
return
}

cost := 1
var reloads []memReload
for _, r := range fn.memReloads {
// Skip reloads before the definition, and after the last use:
// only a loop can reach a use that comes earlier.
if fn.memFirstTop <= r.topIdx && r.topIdx <= fn.memLastTop &&
(r.inLoop || r.prevReads < fn.memReads) {
if r.inLoop {
cost += loopWeight
} else {
cost += 1
}
reloads = append(reloads, r)
}
}
if cost >= fn.memReads {
return
}

mem := newID("mem")
fn.memSlice.X = mem

// Backwards, so positions stay valid.
for _, r := range slices.Backward(reloads) {
r.body.List = slices.Insert(r.body.List, r.pos, ast.Stmt(&ast.AssignStmt{
Tok: token.ASSIGN,
Lhs: []ast.Expr{mem},
Rhs: []ast.Expr{fn.memory.selector},
}))
}

// Define at the top level, before the statement with the first use.
top := fn.blocks[0].body
top.List = slices.Insert(top.List, fn.memFirstTop, ast.Stmt(&ast.AssignStmt{
Tok: token.DEFINE,
Lhs: []ast.Expr{mem},
Rhs: []ast.Expr{fn.memory.selector},
}))
}

func (fn *funcCompiler) cleanup() {
fn.memFinish()

// Sanity checks.
passes.CheckMaterialized(fn.decl)

Expand Down
1 change: 1 addition & 0 deletions ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
//
// Method code may have:
// - module selector (m)
// - local memory slice (mem)
// - local variables, arguments (v+number)
// - materialized temporaries (t+number)
// - temporary variables (p+number)
Expand Down
6 changes: 6 additions & 0 deletions regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func Test_regression_store_grow(t *testing.T) {
if got := m.Xsize(); got != 2 {
t.Errorf("size = %d, want 2", got)
}
if got := m.Xgrow_mid(); got != 12345 {
t.Errorf("grow_mid() = %d, want 12345 (local mem must reload after memory.grow)", got)
}
if got := m.Xsize(); got != 3 {
t.Errorf("size = %d, want 3", got)
}
}

func Test_regression_oob_trap(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion testdata/loops/loops.go

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

16 changes: 14 additions & 2 deletions testdata/regression/store_grow/store_grow.go

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

Binary file modified testdata/regression/store_grow/store_grow.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions testdata/regression/store_grow/store_grow.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
(func (export "test") (result i32)
(i32.store (i32.const 0) (call $alloc))
(i32.load (i32.const 0)))
(func (export "grow_mid") (result i32) (local $off i32)
(local.set $off (i32.shl (memory.size) (i32.const 16)))
(i32.store (local.get $off) (call $alloc))
(i32.load (local.get $off)))
(func (export "size") (result i32)
(memory.size)))
Loading