Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2762404

Browse files
authored
wazevo(frontend): faster non-imported global access (#1889)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 68729c0 commit 2762404

File tree

19 files changed

+339
-201
lines changed

19 files changed

+339
-201
lines changed

internal/engine/compiler/engine.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ func (e *moduleEngine) ResolveImportedFunction(index, indexInImportedModule wasm
656656
e.functions[index] = imported.functions[indexInImportedModule]
657657
}
658658

659+
// GetGlobalValue implements the same method as documented on wasm.ModuleEngine.
660+
func (e *moduleEngine) GetGlobalValue(wasm.Index) (lo, hi uint64) {
661+
panic("BUG: GetGlobalValue should never be called on compiler mode")
662+
}
663+
664+
// OwnsGlobals implements the same method as documented on wasm.ModuleEngine.
665+
func (e *moduleEngine) OwnsGlobals() bool { return false }
666+
659667
// ResolveImportedMemory implements wasm.ModuleEngine.
660668
func (e *moduleEngine) ResolveImportedMemory(wasm.ModuleEngine) {}
661669

internal/engine/interpreter/interpreter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ type moduleEngine struct {
8787
parentEngine *engine
8888
}
8989

90+
// GetGlobalValue implements the same method as documented on wasm.ModuleEngine.
91+
func (e *moduleEngine) GetGlobalValue(wasm.Index) (lo, hi uint64) {
92+
panic("BUG: GetGlobalValue should never be called on interpreter mode")
93+
}
94+
95+
// OwnsGlobals implements the same method as documented on wasm.ModuleEngine.
96+
func (e *moduleEngine) OwnsGlobals() bool { return false }
97+
9098
// callEngine holds context per moduleEngine.Call, and shared across all the
9199
// function calls originating from the same moduleEngine.Call execution.
92100
//

internal/engine/wazevo/backend/backend_test.go

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,34 +1937,26 @@ L2:
19371937
L1 (SSA Block: blk0):
19381938
mov x128?, x0
19391939
mov x129?, x1
1940-
ldr x130?, [x129?, #0x8]
1941-
ldr w131?, [x130?, #0x8]
1942-
ldr x132?, [x129?, #0x10]
1943-
ldr x133?, [x132?, #0x8]
1944-
ldr x134?, [x129?, #0x18]
1945-
ldr s135?, [x134?, #0x8]
1946-
ldr x136?, [x129?, #0x20]
1947-
ldr d137?, [x136?, #0x8]
1940+
ldr w130?, [x129?, #0x8]
1941+
ldr x131?, [x129?, #0x18]
1942+
ldr s132?, [x129?, #0x28]
1943+
ldr d133?, [x129?, #0x38]
19481944
str x129?, [x128?, #0x8]
19491945
mov x0, x128?
19501946
mov x1, x129?
19511947
bl f1
1952-
ldr x138?, [x129?, #0x8]
1953-
ldr w139?, [x138?, #0x8]
1954-
ldr x140?, [x129?, #0x10]
1955-
ldr x141?, [x140?, #0x8]
1956-
ldr x142?, [x129?, #0x18]
1957-
ldr s143?, [x142?, #0x8]
1958-
ldr x144?, [x129?, #0x20]
1959-
ldr d145?, [x144?, #0x8]
1960-
mov v3.8b, v145?.8b
1961-
mov v2.8b, v143?.8b
1962-
mov x3, x141?
1963-
mov x2, x139?
1964-
mov v1.8b, v137?.8b
1965-
mov v0.8b, v135?.8b
1966-
mov x1, x133?
1967-
mov x0, x131?
1948+
ldr w134?, [x129?, #0x8]
1949+
ldr x135?, [x129?, #0x18]
1950+
ldr s136?, [x129?, #0x28]
1951+
ldr d137?, [x129?, #0x38]
1952+
mov v3.8b, v137?.8b
1953+
mov v2.8b, v136?.8b
1954+
mov x3, x135?
1955+
mov x2, x134?
1956+
mov v1.8b, v133?.8b
1957+
mov v0.8b, v132?.8b
1958+
mov x1, x131?
1959+
mov x0, x130?
19681960
ret
19691961
`,
19701962
afterFinalizeARM64: `
@@ -1974,29 +1966,21 @@ L1 (SSA Block: blk0):
19741966
orr x27, xzr, #0x20
19751967
str x27, [sp, #-0x10]!
19761968
str x1, [sp, #0x10]
1977-
ldr x8, [x1, #0x8]
1978-
ldr w8, [x8, #0x8]
1969+
ldr w8, [x1, #0x8]
19791970
str w8, [sp, #0x2c]
1980-
ldr x9, [x1, #0x10]
1981-
ldr x9, [x9, #0x8]
1971+
ldr x9, [x1, #0x18]
19821972
str x9, [sp, #0x24]
1983-
ldr x10, [x1, #0x18]
1984-
ldr s8, [x10, #0x8]
1973+
ldr s8, [x1, #0x28]
19851974
str s8, [sp, #0x20]
1986-
ldr x10, [x1, #0x20]
1987-
ldr d9, [x10, #0x8]
1975+
ldr d9, [x1, #0x38]
19881976
str d9, [sp, #0x18]
19891977
str x1, [x0, #0x8]
19901978
bl f1
19911979
ldr x8, [sp, #0x10]
1992-
ldr x9, [x8, #0x8]
1993-
ldr w9, [x9, #0x8]
1994-
ldr x10, [x8, #0x10]
1995-
ldr x10, [x10, #0x8]
1996-
ldr x11, [x8, #0x18]
1997-
ldr s8, [x11, #0x8]
1998-
ldr x8, [x8, #0x20]
1999-
ldr d9, [x8, #0x8]
1980+
ldr w9, [x8, #0x8]
1981+
ldr x10, [x8, #0x18]
1982+
ldr s8, [x8, #0x28]
1983+
ldr d9, [x8, #0x38]
20001984
mov v3.8b, v9.8b
20011985
mov v2.8b, v8.8b
20021986
mov x3, x10
@@ -2022,36 +2006,28 @@ L1 (SSA Block: blk0):
20222006
afterLoweringARM64: `
20232007
L1 (SSA Block: blk0):
20242008
mov x129?, x1
2025-
ldr x131?, [x129?, #0x8]
2026-
orr w141?, wzr, #0x1
2027-
str w141?, [x131?, #0x8]
2028-
ldr x133?, [x129?, #0x10]
2029-
orr x140?, xzr, #0x2
2030-
str x140?, [x133?, #0x8]
2031-
ldr x135?, [x129?, #0x18]
2032-
ldr s139?, #8; b 8; data.f32 3.000000
2033-
str s139?, [x135?, #0x8]
2034-
ldr x137?, [x129?, #0x20]
2035-
ldr d138?, #8; b 16; data.f64 4.000000
2036-
str d138?, [x137?, #0x8]
2009+
orr w137?, wzr, #0x1
2010+
str w137?, [x129?, #0x8]
2011+
orr x136?, xzr, #0x2
2012+
str x136?, [x129?, #0x18]
2013+
ldr s135?, #8; b 8; data.f32 3.000000
2014+
str s135?, [x129?, #0x28]
2015+
ldr d134?, #8; b 16; data.f64 4.000000
2016+
str d134?, [x129?, #0x38]
20372017
ret
20382018
`,
20392019
afterFinalizeARM64: `
20402020
L1 (SSA Block: blk0):
20412021
stp x30, xzr, [sp, #-0x10]!
20422022
str xzr, [sp, #-0x10]!
2043-
ldr x8, [x1, #0x8]
2044-
orr w9, wzr, #0x1
2045-
str w9, [x8, #0x8]
2046-
ldr x8, [x1, #0x10]
2047-
orr x9, xzr, #0x2
2048-
str x9, [x8, #0x8]
2049-
ldr x8, [x1, #0x18]
2023+
orr w8, wzr, #0x1
2024+
str w8, [x1, #0x8]
2025+
orr x8, xzr, #0x2
2026+
str x8, [x1, #0x18]
20502027
ldr s8, #8; b 8; data.f32 3.000000
2051-
str s8, [x8, #0x8]
2052-
ldr x8, [x1, #0x20]
2028+
str s8, [x1, #0x28]
20532029
ldr d8, #8; b 16; data.f64 4.000000
2054-
str d8, [x8, #0x8]
2030+
str d8, [x1, #0x38]
20552031
add sp, sp, #0x10
20562032
ldr x30, [sp], #0x10
20572033
ret

internal/engine/wazevo/frontend/frontend_test.go

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,15 +1181,11 @@ blk0: (exec_ctx:i64, module_ctx:i64, v2:i32)
11811181
m: testcases.GlobalsGet.Module,
11821182
exp: `
11831183
blk0: (exec_ctx:i64, module_ctx:i64)
1184-
v2:i64 = Load module_ctx, 0x8
1185-
v3:i32 = Load v2, 0x8
1186-
v4:i64 = Load module_ctx, 0x10
1187-
v5:i64 = Load v4, 0x8
1188-
v6:i64 = Load module_ctx, 0x18
1189-
v7:f32 = Load v6, 0x8
1190-
v8:i64 = Load module_ctx, 0x20
1191-
v9:f64 = Load v8, 0x8
1192-
Jump blk_ret, v3, v5, v7, v9
1184+
v2:i32 = Load module_ctx, 0x8
1185+
v3:i64 = Load module_ctx, 0x18
1186+
v4:f32 = Load module_ctx, 0x28
1187+
v5:f64 = Load module_ctx, 0x38
1188+
Jump blk_ret, v2, v3, v4, v5
11931189
`,
11941190
},
11951191
{
@@ -1198,18 +1194,14 @@ blk0: (exec_ctx:i64, module_ctx:i64)
11981194
exp: `
11991195
blk0: (exec_ctx:i64, module_ctx:i64)
12001196
v2:i32 = Iconst_32 0x1
1201-
v3:i64 = Load module_ctx, 0x8
1202-
Store v2, v3, 0x8
1203-
v4:i64 = Iconst_64 0x2
1204-
v5:i64 = Load module_ctx, 0x10
1205-
Store v4, v5, 0x8
1206-
v6:f32 = F32const 3.000000
1207-
v7:i64 = Load module_ctx, 0x18
1208-
Store v6, v7, 0x8
1209-
v8:f64 = F64const 4.000000
1210-
v9:i64 = Load module_ctx, 0x20
1211-
Store v8, v9, 0x8
1212-
Jump blk_ret, v2, v4, v6, v8
1197+
Store v2, module_ctx, 0x8
1198+
v3:i64 = Iconst_64 0x2
1199+
Store v3, module_ctx, 0x18
1200+
v4:f32 = F32const 3.000000
1201+
Store v4, module_ctx, 0x28
1202+
v5:f64 = F64const 4.000000
1203+
Store v5, module_ctx, 0x38
1204+
Jump blk_ret, v2, v3, v4, v5
12131205
`,
12141206
},
12151207
{
@@ -1220,50 +1212,34 @@ signatures:
12201212
sig1: i64i64_v
12211213
12221214
blk0: (exec_ctx:i64, module_ctx:i64)
1223-
v2:i64 = Load module_ctx, 0x8
1224-
v3:i32 = Load v2, 0x8
1225-
v4:i64 = Load module_ctx, 0x10
1226-
v5:i64 = Load v4, 0x8
1227-
v6:i64 = Load module_ctx, 0x18
1228-
v7:f32 = Load v6, 0x8
1229-
v8:i64 = Load module_ctx, 0x20
1230-
v9:f64 = Load v8, 0x8
1215+
v2:i32 = Load module_ctx, 0x8
1216+
v3:i64 = Load module_ctx, 0x18
1217+
v4:f32 = Load module_ctx, 0x28
1218+
v5:f64 = Load module_ctx, 0x38
12311219
Store module_ctx, exec_ctx, 0x8
12321220
Call f1:sig1, exec_ctx, module_ctx
1233-
v10:i64 = Load module_ctx, 0x8
1234-
v11:i32 = Load v10, 0x8
1235-
v12:i64 = Load module_ctx, 0x10
1236-
v13:i64 = Load v12, 0x8
1237-
v14:i64 = Load module_ctx, 0x18
1238-
v15:f32 = Load v14, 0x8
1239-
v16:i64 = Load module_ctx, 0x20
1240-
v17:f64 = Load v16, 0x8
1241-
Jump blk_ret, v3, v5, v7, v9, v11, v13, v15, v17
1221+
v6:i32 = Load module_ctx, 0x8
1222+
v7:i64 = Load module_ctx, 0x18
1223+
v8:f32 = Load module_ctx, 0x28
1224+
v9:f64 = Load module_ctx, 0x38
1225+
Jump blk_ret, v2, v3, v4, v5, v6, v7, v8, v9
12421226
`,
12431227
expAfterOpt: `
12441228
signatures:
12451229
sig1: i64i64_v
12461230
12471231
blk0: (exec_ctx:i64, module_ctx:i64)
1248-
v2:i64 = Load module_ctx, 0x8
1249-
v3:i32 = Load v2, 0x8
1250-
v4:i64 = Load module_ctx, 0x10
1251-
v5:i64 = Load v4, 0x8
1252-
v6:i64 = Load module_ctx, 0x18
1253-
v7:f32 = Load v6, 0x8
1254-
v8:i64 = Load module_ctx, 0x20
1255-
v9:f64 = Load v8, 0x8
1232+
v2:i32 = Load module_ctx, 0x8
1233+
v3:i64 = Load module_ctx, 0x18
1234+
v4:f32 = Load module_ctx, 0x28
1235+
v5:f64 = Load module_ctx, 0x38
12561236
Store module_ctx, exec_ctx, 0x8
12571237
Call f1:sig1, exec_ctx, module_ctx
1258-
v10:i64 = Load module_ctx, 0x8
1259-
v11:i32 = Load v10, 0x8
1260-
v12:i64 = Load module_ctx, 0x10
1261-
v13:i64 = Load v12, 0x8
1262-
v14:i64 = Load module_ctx, 0x18
1263-
v15:f32 = Load v14, 0x8
1264-
v16:i64 = Load module_ctx, 0x20
1265-
v17:f64 = Load v16, 0x8
1266-
Jump blk_ret, v3, v5, v7, v9, v11, v13, v15, v17
1238+
v6:i32 = Load module_ctx, 0x8
1239+
v7:i64 = Load module_ctx, 0x18
1240+
v8:f32 = Load module_ctx, 0x28
1241+
v9:f64 = Load module_ctx, 0x38
1242+
Jump blk_ret, v2, v3, v4, v5, v6, v7, v8, v9
12671243
`,
12681244
},
12691245
{

internal/engine/wazevo/frontend/lower.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,21 +3459,25 @@ func (c *Compiler) reloadMemoryBaseLen() {
34593459
c.clearSafeBounds()
34603460
}
34613461

3462-
// globalInstanceValueOffset is the offsetOf .Value field of wasm.GlobalInstance.
3463-
const globalInstanceValueOffset = 8
3464-
34653462
func (c *Compiler) setWasmGlobalValue(index wasm.Index, v ssa.Value) {
34663463
variable := c.globalVariables[index]
3467-
instanceOffset := c.offset.GlobalInstanceOffset(index)
3464+
opaqueOffset := c.offset.GlobalInstanceOffset(index)
34683465

34693466
builder := c.ssaBuilder
3470-
loadGlobalInstPtr := builder.AllocateInstruction()
3471-
loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(instanceOffset), ssa.TypeI64)
3472-
builder.InsertInstruction(loadGlobalInstPtr)
3467+
if index < c.m.ImportGlobalCount {
3468+
loadGlobalInstPtr := builder.AllocateInstruction()
3469+
loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), ssa.TypeI64)
3470+
builder.InsertInstruction(loadGlobalInstPtr)
34733471

3474-
store := builder.AllocateInstruction()
3475-
store.AsStore(ssa.OpcodeStore, v, loadGlobalInstPtr.Return(), uint32(globalInstanceValueOffset))
3476-
builder.InsertInstruction(store)
3472+
store := builder.AllocateInstruction()
3473+
store.AsStore(ssa.OpcodeStore, v, loadGlobalInstPtr.Return(), uint32(0))
3474+
builder.InsertInstruction(store)
3475+
3476+
} else {
3477+
store := builder.AllocateInstruction()
3478+
store.AsStore(ssa.OpcodeStore, v, c.moduleCtxPtrValue, uint32(opaqueOffset))
3479+
builder.InsertInstruction(store)
3480+
}
34773481

34783482
// The value has changed to `v`, so we record it.
34793483
builder.DefineVariableInCurrentBB(variable, v)
@@ -3482,7 +3486,7 @@ func (c *Compiler) setWasmGlobalValue(index wasm.Index, v ssa.Value) {
34823486
func (c *Compiler) getWasmGlobalValue(index wasm.Index, forceLoad bool) ssa.Value {
34833487
variable := c.globalVariables[index]
34843488
typ := c.globalVariablesTypes[index]
3485-
instanceOffset := c.offset.GlobalInstanceOffset(index)
3489+
opaqueOffset := c.offset.GlobalInstanceOffset(index)
34863490

34873491
builder := c.ssaBuilder
34883492
if !forceLoad {
@@ -3491,16 +3495,21 @@ func (c *Compiler) getWasmGlobalValue(index wasm.Index, forceLoad bool) ssa.Valu
34913495
}
34923496
}
34933497

3494-
loadGlobalInstPtr := builder.AllocateInstruction()
3495-
loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(instanceOffset), ssa.TypeI64)
3496-
builder.InsertInstruction(loadGlobalInstPtr)
3498+
var load *ssa.Instruction
3499+
if index < c.m.ImportGlobalCount {
3500+
loadGlobalInstPtr := builder.AllocateInstruction()
3501+
loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), ssa.TypeI64)
3502+
builder.InsertInstruction(loadGlobalInstPtr)
3503+
load = builder.AllocateInstruction().
3504+
AsLoad(loadGlobalInstPtr.Return(), uint32(0), typ)
3505+
} else {
3506+
load = builder.AllocateInstruction().
3507+
AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), typ)
3508+
}
34973509

3498-
load := builder.AllocateInstruction()
3499-
load.AsLoad(loadGlobalInstPtr.Return(), uint32(globalInstanceValueOffset), typ)
3500-
builder.InsertInstruction(load)
3501-
ret := load.Return()
3502-
builder.DefineVariableInCurrentBB(variable, ret)
3503-
return ret
3510+
v := load.Insert(builder).Return()
3511+
builder.DefineVariableInCurrentBB(variable, v)
3512+
return v
35043513
}
35053514

35063515
const (

internal/engine/wazevo/frontend/lower_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
)
1010

1111
func Test_Offsets(t *testing.T) {
12-
var globalInstance wasm.GlobalInstance
13-
require.Equal(t, int(unsafe.Offsetof(globalInstance.Val)), globalInstanceValueOffset)
1412
var memInstance wasm.MemoryInstance
1513
require.Equal(t, int(unsafe.Offsetof(memInstance.Buffer)), memoryInstanceBufOffset)
1614
var tableInstance wasm.TableInstance

0 commit comments

Comments
 (0)