Debian Codegraph @

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

raw
1 package amd64
2
3 import (
4 "runtime"
5 "strings"
6 "testing"
7
8 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
9 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
10 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
11 "github.com/tetratelabs/wazero/internal/testing/require"
12 )
13
14 func TestMachine_lowerToAddressMode(t *testing.T) {
15 _, _, m := newSetupWithMockContext()
16 defer func() {
17 runtime.KeepAlive(m)
18 }()
19 newAmodeImmReg := m.newAmodeImmReg
20 newAmodeRegRegShift := m.newAmodeRegRegShift
21
22 nextVReg := regalloc.VReg(100).SetRegType(regalloc.RegTypeInt)
23 for _, tc := range []struct {
24 name string
25 in func(*mockCompiler, ssa.Builder, *machine) (ptr ssa.Value, offset uint32)
26 insts []string
27 am *amode
28 }{
29 {
30 name: "iadd const, const; offset != 0",
31 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
32 iconst1 := b.AllocateInstruction().AsIconst32(1).Insert(b)
33 iconst2 := b.AllocateInstruction().AsIconst32(2).Insert(b)
34 iadd := b.AllocateInstruction().AsIadd(iconst1.Return(), iconst2.Return()).Insert(b)
35 ptr = iadd.Return()
36 offset = 3
37 ctx.definitions[iconst1.Return()] = backend.SSAValueDefinition{Instr: iconst1}
38 ctx.definitions[iconst2.Return()] = backend.SSAValueDefinition{Instr: iconst2}
39 ctx.definitions[ptr] = backend.SSAValueDefinition{Instr: iadd}
40 return
41 },
42 insts: []string{
43 "movabsq $6, %r100?",
44 },
45 am: newAmodeImmReg(0, nextVReg),
46 },
47 {
48 name: "iadd const, param; offset != 0",
49 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
50 iconst1 := b.AllocateInstruction().AsIconst32(1).Insert(b)
51 p := b.CurrentBlock().AddParam(b, ssa.TypeI64)
52 iadd := b.AllocateInstruction().AsIadd(iconst1.Return(), p).Insert(b)
53 ptr = iadd.Return()
54 offset = 3
55 ctx.definitions[iconst1.Return()] = backend.SSAValueDefinition{Instr: iconst1}
56 ctx.vRegMap[p] = raxVReg
57 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
58 ctx.definitions[ptr] = backend.SSAValueDefinition{Instr: iadd}
59 return
60 },
61 am: newAmodeImmReg(1+3, raxVReg),
62 },
63 {
64 name: "iadd param, param; offset != 0",
65 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
66 p1 := b.CurrentBlock().AddParam(b, ssa.TypeI64)
67 p2 := b.CurrentBlock().AddParam(b, ssa.TypeI64)
68 iadd := b.AllocateInstruction().AsIadd(p1, p2).Insert(b)
69 ptr = iadd.Return()
70 offset = 3
71 ctx.vRegMap[p1] = raxVReg
72 ctx.definitions[p1] = backend.SSAValueDefinition{V: p1}
73 ctx.vRegMap[p2] = rcxVReg
74 ctx.definitions[p2] = backend.SSAValueDefinition{V: p2}
75 ctx.definitions[ptr] = backend.SSAValueDefinition{Instr: iadd}
76 return
77 },
78 am: newAmodeRegRegShift(3, raxVReg, rcxVReg, 0),
79 },
80 {
81 name: "huge offset",
82 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
83 ptr = b.CurrentBlock().AddParam(b, ssa.TypeI64)
84 offset = 1 << 31
85 ctx.vRegMap[ptr] = raxVReg
86 ctx.definitions[ptr] = backend.SSAValueDefinition{V: ptr}
87 return
88 },
89 insts: []string{
90 "movabsq $2147483648, %r100?",
91 },
92 am: newAmodeRegRegShift(0, nextVReg, raxVReg, 0),
93 },
94
95 // The other iadd cases are covered by TestMachine_lowerAddendsToAmode.
96 {
97 name: "uextend const32",
98 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
99 iconst32 := b.AllocateInstruction().AsIconst32(123).Insert(b)
100 uextend := b.AllocateInstruction().AsUExtend(iconst32.Return(), 32, 64).Insert(b)
101 ctx.definitions[iconst32.Return()] = backend.SSAValueDefinition{Instr: iconst32}
102 ctx.definitions[uextend.Return()] = backend.SSAValueDefinition{Instr: uextend}
103 return uextend.Return(), 0
104 },
105 insts: []string{
106 "movabsq $123, %r100?",
107 },
108 am: newAmodeImmReg(0, nextVReg),
109 },
110 {
111 name: "Ishl param64, const",
112 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
113 p := b.CurrentBlock().AddParam(b, ssa.TypeI64)
114 iconst64 := b.AllocateInstruction().AsIconst64(2).Insert(b)
115 ishl := b.AllocateInstruction().AsIshl(p, iconst64.Return()).Insert(b)
116 ctx.vRegMap[p] = raxVReg
117 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
118 ctx.definitions[iconst64.Return()] = backend.SSAValueDefinition{Instr: iconst64}
119 ctx.definitions[ishl.Return()] = backend.SSAValueDefinition{Instr: ishl}
120 return ishl.Return(), 1 << 30
121 },
122 insts: []string{
123 "xor %r100?, %r100?",
124 },
125 am: newAmodeRegRegShift(1<<30, nextVReg, raxVReg, 2),
126 },
127 {
128 name: "add Iconst, (Ishl param64, const)",
129 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, offset uint32) {
130 p1 := b.CurrentBlock().AddParam(b, ssa.TypeI64)
131 p2 := b.CurrentBlock().AddParam(b, ssa.TypeI64)
132 const2 := b.AllocateInstruction().AsIconst64(2).Insert(b)
133 ishl := b.AllocateInstruction().AsIshl(p1, const2.Return()).Insert(b)
134 iadd := b.AllocateInstruction().AsIadd(p2, ishl.Return()).Insert(b)
135 ctx.vRegMap[p1] = raxVReg
136 ctx.definitions[p1] = backend.SSAValueDefinition{V: p1}
137 ctx.vRegMap[p2] = rcxVReg
138 ctx.definitions[p2] = backend.SSAValueDefinition{V: p2}
139 ctx.definitions[const2.Return()] = backend.SSAValueDefinition{Instr: const2}
140 ctx.definitions[ishl.Return()] = backend.SSAValueDefinition{Instr: ishl}
141 ctx.definitions[iadd.Return()] = backend.SSAValueDefinition{Instr: iadd}
142 return iadd.Return(), 1 << 30
143 },
144 am: newAmodeRegRegShift(1<<30, rcxVReg, raxVReg, 2),
145 },
146 } {
147 t.Run(tc.name, func(t *testing.T) {
148 ctx, b, m := newSetupWithMockContext()
149 ctx.vRegCounter = int(nextVReg.ID()) - 1
150 ptr, offset := tc.in(ctx, b, m)
151 actual := m.lowerToAddressMode(ptr, offset)
152 require.Equal(t, strings.Join(tc.insts, "\n"), formatEmittedInstructionsInCurrentBlock(m))
153 require.Equal(t, tc.am, actual, actual.String())
154 })
155 }
156 }
157
158 func TestMachine_lowerAddendFromInstr(t *testing.T) {
159 for _, tc := range []struct {
160 name string
161 in func(*mockCompiler, ssa.Builder, *machine) *ssa.Instruction
162 exp addend
163 }{
164 {
165 name: "iconst64",
166 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
167 return b.AllocateInstruction().AsIconst64(123 << 32).Insert(b)
168 },
169 exp: addend{regalloc.VRegInvalid, 123 << 32, 0},
170 },
171 {
172 name: "iconst32",
173 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
174 return b.AllocateInstruction().AsIconst32(123).Insert(b)
175 },
176 exp: addend{regalloc.VRegInvalid, 123, 0},
177 },
178 {
179 name: "uextend const32",
180 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
181 iconst32 := b.AllocateInstruction().AsIconst32(123).Insert(b)
182 ctx.definitions[iconst32.Return()] = backend.SSAValueDefinition{Instr: iconst32}
183 return b.AllocateInstruction().AsUExtend(iconst32.Return(), 32, 64).Insert(b)
184 },
185 exp: addend{regalloc.VRegInvalid, 123, 0},
186 },
187 {
188 name: "uextend const64",
189 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
190 p := b.CurrentBlock().AddParam(b, ssa.TypeI32)
191 ctx.vRegMap[p] = raxVReg
192 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
193 return b.AllocateInstruction().AsUExtend(p, 32, 64).Insert(b)
194 },
195 exp: addend{raxVReg, 0, 0},
196 },
197 {
198 name: "uextend param i32",
199 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
200 p := b.CurrentBlock().AddParam(b, ssa.TypeI32)
201 ctx.vRegMap[p] = raxVReg
202 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
203 return b.AllocateInstruction().AsUExtend(p, 32, 64).Insert(b)
204 },
205 exp: addend{raxVReg, 0, 0},
206 },
207 {
208 name: "sextend const32",
209 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
210 iconst32 := b.AllocateInstruction().AsIconst32(123).Insert(b)
211 ctx.definitions[iconst32.Return()] = backend.SSAValueDefinition{Instr: iconst32}
212 return b.AllocateInstruction().AsSExtend(iconst32.Return(), 32, 64).Insert(b)
213 },
214 exp: addend{regalloc.VRegInvalid, 123, 0},
215 },
216 {
217 name: "sextend const64",
218 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
219 p := b.CurrentBlock().AddParam(b, ssa.TypeI32)
220 ctx.vRegMap[p] = raxVReg
221 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
222 return b.AllocateInstruction().AsSExtend(p, 32, 64).Insert(b)
223 },
224 exp: addend{raxVReg, 0, 0},
225 },
226 {
227 name: "sextend param i32",
228 in: func(ctx *mockCompiler, b ssa.Builder, m *machine) *ssa.Instruction {
229 p := b.CurrentBlock().AddParam(b, ssa.TypeI32)
230 ctx.vRegMap[p] = raxVReg
231 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
232 return b.AllocateInstruction().AsSExtend(p, 32, 64).Insert(b)
233 },
234 exp: addend{raxVReg, 0, 0},
235 },
236 } {
237 t.Run(tc.name, func(t *testing.T) {
238 ctx, b, m := newSetupWithMockContext()
239 a := m.lowerAddendFromInstr(tc.in(ctx, b, m))
240 require.Equal(t, tc.exp, a)
241 })
242 }
243 }
244
245 func TestMachine_lowerAddendsToAmode(t *testing.T) {
246 _, _, m := newSetupWithMockContext()
247 defer func() {
248 runtime.KeepAlive(m)
249 }()
250 newAmodeImmReg := m.newAmodeImmReg
251 newAmodeRegRegShift := m.newAmodeRegRegShift
252
253 x1, x2 := raxVReg, rcxVReg
254
255 nextVReg, nextNextVReg := regalloc.VReg(100).SetRegType(regalloc.RegTypeInt), regalloc.VReg(101).SetRegType(regalloc.RegTypeInt)
256 _ = nextNextVReg
257 for _, tc := range []struct {
258 name string
259 x, y addend
260 offset uint32
261 exp *amode
262 insts []string
263 }{
264 {
265 name: "only offset",
266 x: addend{r: regalloc.VRegInvalid}, y: addend{r: regalloc.VRegInvalid},
267 offset: 4095,
268 insts: []string{"movabsq $4095, %r100?"},
269 exp: newAmodeImmReg(0, nextVReg),
270 },
271 {
272 name: "only offset, offx, offy",
273 x: addend{r: regalloc.VRegInvalid, off: 1}, y: addend{r: regalloc.VRegInvalid, off: 2},
274 offset: 4095,
275 insts: []string{"movabsq $4098, %r100?"},
276 exp: newAmodeImmReg(0, nextVReg),
277 },
278 {
279 name: "only offset, offx, offy; not fitting",
280 x: addend{r: regalloc.VRegInvalid, off: 1 << 30}, y: addend{r: regalloc.VRegInvalid, off: 2 << 30},
281 offset: 4095,
282 insts: []string{"movabsq $3221229567, %r100?"},
283 exp: newAmodeImmReg(0, nextVReg),
284 },
285 {
286 name: "one a64 with imm32",
287 x: addend{r: x1}, y: addend{r: regalloc.VRegInvalid},
288 offset: 4095,
289 exp: newAmodeImmReg(4095, x1),
290 },
291 {
292 name: "one a64 with imm32",
293 x: addend{r: x1}, y: addend{r: regalloc.VRegInvalid},
294 offset: 1 << 16,
295 exp: newAmodeImmReg(1<<16, x1),
296 },
297 {
298 name: "two a64 with imm32",
299 x: addend{r: x1}, y: addend{r: x2},
300 offset: 1 << 16,
301 exp: newAmodeRegRegShift(1<<16, x1, x2, 0),
302 },
303 {
304 name: "two a64 with offset fitting",
305 x: addend{r: x1}, y: addend{r: x2},
306 offset: 1 << 30,
307 exp: newAmodeRegRegShift(1<<30, x1, x2, 0),
308 },
309 {
310 name: "rx with offset not fitting",
311 x: addend{r: x1}, y: addend{r: regalloc.VRegInvalid, off: 1 << 30},
312 offset: 1 << 30,
313 insts: []string{
314 "movabsq $2147483648, %r100?",
315 },
316 exp: newAmodeRegRegShift(0, x1, nextVReg, 0),
317 },
318 {
319 name: "ry with offset not fitting",
320 x: addend{r: regalloc.VRegInvalid, off: 1 << 30}, y: addend{r: x1},
321 offset: 1 << 30,
322 insts: []string{
323 "movabsq $2147483648, %r100?",
324 },
325 exp: newAmodeRegRegShift(0, nextVReg, x1, 0),
326 },
327 {
328 name: "rx with shift, ry with shift, offset != 0",
329 x: addend{r: x1, shift: 2}, y: addend{r: x2, shift: 3},
330 offset: 1 << 30,
331 insts: []string{
332 "shlq $2, %rax",
333 },
334 exp: newAmodeRegRegShift(1<<30, x1, x2, 3),
335 },
336 {
337 name: "rx, ry with shift, offset != 0",
338 x: addend{r: x1}, y: addend{r: x2, shift: 3},
339 offset: 1 << 30,
340 exp: newAmodeRegRegShift(1<<30, x1, x2, 3),
341 },
342 {
343 name: "rx with shift, ry, offset != 0",
344 x: addend{r: x1, shift: 3}, y: addend{r: x2},
345 offset: 1 << 30,
346 exp: newAmodeRegRegShift(1<<30, x2, x1, 3),
347 },
348 {
349 name: "rx with shift, ry invalid, offset != 0",
350 x: addend{r: x1, shift: 3}, y: addend{r: regalloc.VRegInvalid},
351 offset: 1 << 30,
352 insts: []string{
353 "xor %r100?, %r100?",
354 },
355 exp: newAmodeRegRegShift(1<<30, nextVReg, x1, 3),
356 },
357 {
358 name: "rx invalid, rx with shift, offset != 0",
359 x: addend{r: regalloc.VRegInvalid}, y: addend{r: x1, shift: 3},
360 offset: 1 << 30,
361 insts: []string{
362 "xor %r100?, %r100?",
363 },
364 exp: newAmodeRegRegShift(1<<30, nextVReg, x1, 3),
365 },
366 {
367 name: "huge offset",
368 x: addend{r: regalloc.VRegInvalid}, y: addend{r: x1, shift: 3},
369 offset: 1 << 31,
370 insts: []string{
371 "movabsq $2147483648, %r100?",
372 },
373 exp: newAmodeRegRegShift(0, nextVReg, x1, 3),
374 },
375 {
376 name: "huge offset",
377 x: addend{r: regalloc.VRegInvalid, off: 1 << 31}, y: addend{r: x1, shift: 3},
378 offset: 0,
379 insts: []string{
380 "movabsq $2147483648, %r100?",
381 },
382 exp: newAmodeRegRegShift(0, nextVReg, x1, 3),
383 },
384 } {
385 t.Run(tc.name, func(t *testing.T) {
386 ctx, _, m := newSetupWithMockContext()
387 ctx.vRegCounter = int(nextVReg.ID()) - 1
388 actual := m.lowerAddendsToAmode(tc.x, tc.y, tc.offset)
389 require.Equal(t, strings.Join(tc.insts, "\n"), formatEmittedInstructionsInCurrentBlock(m))
390 require.Equal(t, tc.exp, actual, actual.String())
391 })
392 }
393 }