Skip to content

Commit 1e6080a

Browse files
authored
compiler: uses slice instead of map for refToBinaryOffset (#2195)
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
1 parent 621f62d commit 1e6080a

File tree

6 files changed

+11
-13
lines changed

6 files changed

+11
-13
lines changed

internal/engine/wazevo/backend/isa/amd64/machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ func (m *machine) Encode(ctx context.Context) (err error) {
20592059
}
20602060

20612061
// ResolveRelocations implements backend.Machine.
2062-
func (m *machine) ResolveRelocations(refToBinaryOffset map[ssa.FuncRef]int, binary []byte, relocations []backend.RelocationInfo, _ []int) {
2062+
func (m *machine) ResolveRelocations(refToBinaryOffset []int, binary []byte, relocations []backend.RelocationInfo, _ []int) {
20632063
for _, r := range relocations {
20642064
offset := r.Offset
20652065
calleeFnOffset := refToBinaryOffset[r.FuncRef]

internal/engine/wazevo/backend/isa/arm64/machine_relocation.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sort"
88

99
"github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
10-
"github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
1110
)
1211

1312
const (
@@ -42,7 +41,7 @@ func (m *machine) CallTrampolineIslandInfo(numFunctions int) (interval, size int
4241

4342
// ResolveRelocations implements backend.Machine ResolveRelocations.
4443
func (m *machine) ResolveRelocations(
45-
refToBinaryOffset map[ssa.FuncRef]int,
44+
refToBinaryOffset []int,
4645
executable []byte,
4746
relocations []backend.RelocationInfo,
4847
callTrampolineIslandOffsets []int,
@@ -72,11 +71,11 @@ func (m *machine) ResolveRelocations(
7271
// encodeCallTrampolineIsland encodes a trampoline island for the given functions.
7372
// Each island consists of a trampoline instruction sequence for each function.
7473
// Each trampoline instruction sequence consists of 4 instructions + 32-bit immediate.
75-
func encodeCallTrampolineIsland(refToBinaryOffset map[ssa.FuncRef]int, islandOffset int, executable []byte) {
74+
func encodeCallTrampolineIsland(refToBinaryOffset []int, islandOffset int, executable []byte) {
7675
for i := 0; i < len(refToBinaryOffset); i++ {
7776
trampolineOffset := islandOffset + trampolineCallSize*i
7877

79-
fnOffset := refToBinaryOffset[ssa.FuncRef(i)]
78+
fnOffset := refToBinaryOffset[i]
8079
diff := fnOffset - (trampolineOffset + 16)
8180
if diff > math.MaxInt32 || diff < math.MinInt32 {
8281
// This case even amd64 can't handle. 4GB is too big.

internal/engine/wazevo/backend/isa/arm64/machine_relocation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Test_maxNumFunctions(t *testing.T) {
1919
func Test_encodeCallTrampolineIsland(t *testing.T) {
2020
executable := make([]byte, 16*1000)
2121
islandOffset := 160
22-
refToBinaryOffset := map[ssa.FuncRef]int{0: 0, 1: 16, 2: 1600, 3: 16000}
22+
refToBinaryOffset := []int{0: 0, 1: 16, 2: 1600, 3: 16000}
2323
encodeCallTrampolineIsland(refToBinaryOffset, islandOffset, executable)
2424
for i := 0; i < len(refToBinaryOffset); i++ {
2525
offset := islandOffset + trampolineCallSize*i

internal/engine/wazevo/backend/machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ type (
5959
PostRegAlloc()
6060

6161
// ResolveRelocations resolves the relocations after emitting machine code.
62-
// * refToBinaryOffset: the map from the function reference to the executable offset.
62+
// * refToBinaryOffset: the map from the function reference (ssa.FuncRef) to the executable offset.
6363
// * executable: the binary to resolve the relocations.
6464
// * relocations: the relocations to resolve.
6565
// * callTrampolineIslandOffsets: the offsets of the trampoline islands in the executable.
6666
ResolveRelocations(
67-
refToBinaryOffset map[ssa.FuncRef]int,
67+
refToBinaryOffset []int,
6868
executable []byte,
6969
relocations []RelocationInfo,
7070
callTrampolineIslandOffsets []int,

internal/engine/wazevo/backend/machine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (m mockMachine) CompileGoFunctionTrampoline(wazevoapi.ExitCode, *ssa.Signat
5656
func (m mockMachine) Encode(context.Context) (err error) { return }
5757

5858
// ResolveRelocations implements Machine.ResolveRelocations.
59-
func (m mockMachine) ResolveRelocations(map[ssa.FuncRef]int, []byte, []RelocationInfo, []int) {}
59+
func (m mockMachine) ResolveRelocations([]int, []byte, []RelocationInfo, []int) {}
6060

6161
// PostRegAlloc implements Machine.SetupPrologue.
6262
func (m mockMachine) PostRegAlloc() {}

internal/engine/wazevo/engine.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@ func (e *engine) compileModule(ctx context.Context, module *wasm.Module, listene
188188
executables: &executables{},
189189
}
190190

191-
// rels is a list of relocations to be resolved. This is reused for each compilation to avoid allocation.
192-
rels := make([]backend.RelocationInfo, 0)
193-
refToBinaryOffset := map[ssa.FuncRef]int{}
194-
195191
if module.IsHostModule {
196192
return e.compileHostModule(ctx, module, listeners)
197193
}
@@ -201,6 +197,9 @@ func (e *engine) compileModule(ctx context.Context, module *wasm.Module, listene
201197
return cm, nil
202198
}
203199

200+
rels := make([]backend.RelocationInfo, 0)
201+
refToBinaryOffset := make([]int, importedFns+localFns)
202+
204203
if wazevoapi.DeterministicCompilationVerifierEnabled {
205204
// The compilation must be deterministic regardless of the order of functions being compiled.
206205
wazevoapi.DeterministicCompilationVerifierRandomizeIndexes(ctx)

0 commit comments

Comments
 (0)