Debian Codegraph @

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

raw
1 package amd64
2
3 import (
4 "fmt"
5 "runtime"
6 "strings"
7 "testing"
8
9 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
10 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
11 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
12 "github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
13 "github.com/tetratelabs/wazero/internal/platform"
14 "github.com/tetratelabs/wazero/internal/testing/require"
15 )
16
17 func Test_asImm32(t *testing.T) {
18 v, ok := asImm32(0xffffffff, true)
19 require.True(t, ok)
20 require.Equal(t, uint32(0xffffffff), v)
21
22 v, ok = asImm32(0xffffffff, false)
23 require.False(t, ok)
24 require.Equal(t, uint32(0), v)
25
26 v, ok = asImm32(0x80000000, true)
27 require.True(t, ok)
28 require.Equal(t, uint32(0x80000000), v)
29
30 v, ok = asImm32(0x80000000, false)
31 require.False(t, ok)
32 require.Equal(t, uint32(0), v)
33
34 v, ok = asImm32(0xffffffff<<1, true)
35 require.False(t, ok)
36 require.Equal(t, uint32(0), v)
37 }
38
39 func TestMachine_getOperand_Reg(t *testing.T) {
40 for _, tc := range []struct {
41 name string
42 setup func(*mockCompiler, ssa.Builder, *machine) backend.SSAValueDefinition
43 exp operand
44 instructions []string
45 }{
46 {
47 name: "block param",
48 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
49 ctx.vRegMap[1234] = raxVReg
50 return backend.SSAValueDefinition{V: 1234, Instr: nil}
51 },
52 exp: newOperandReg(raxVReg),
53 },
54
55 {
56 name: "const instr",
57 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
58 instr := builder.AllocateInstruction()
59 instr.AsIconst32(0xf00000f)
60 builder.InsertInstruction(instr)
61 ctx.vRegCounter = 99
62 return backend.SSAValueDefinition{Instr: instr}
63 },
64 exp: newOperandReg(regalloc.VReg(100).SetRegType(regalloc.RegTypeInt)),
65 instructions: []string{"movl $251658255, %r100d?"},
66 },
67 {
68 name: "non const instr (single-return)",
69 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
70 c := builder.AllocateInstruction()
71 sig := &ssa.Signature{Results: []ssa.Type{ssa.TypeI64}}
72 builder.DeclareSignature(sig)
73 c.AsCall(ssa.FuncRef(0), sig, ssa.ValuesNil)
74 builder.InsertInstruction(c)
75 r := c.Return()
76 ctx.vRegMap[r] = regalloc.VReg(50)
77 return backend.SSAValueDefinition{V: r}
78 },
79 exp: newOperandReg(regalloc.VReg(50)),
80 },
81 {
82 name: "non const instr (multi-return)",
83 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
84 c := builder.AllocateInstruction()
85 sig := &ssa.Signature{Results: []ssa.Type{ssa.TypeI64, ssa.TypeF64, ssa.TypeF64}}
86 builder.DeclareSignature(sig)
87 c.AsCall(ssa.FuncRef(0), sig, ssa.ValuesNil)
88 builder.InsertInstruction(c)
89 _, rs := c.Returns()
90 ctx.vRegMap[rs[1]] = regalloc.VReg(50)
91 return backend.SSAValueDefinition{V: rs[1]}
92 },
93 exp: newOperandReg(regalloc.VReg(50)),
94 },
95 } {
96 t.Run(tc.name, func(t *testing.T) {
97 ctx, b, m := newSetupWithMockContext()
98 def := tc.setup(ctx, b, m)
99 actual := m.getOperand_Reg(def)
100 require.Equal(t, tc.exp, actual)
101 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
102 })
103 }
104 }
105
106 func TestMachine_getOperand_Imm32_Reg(t *testing.T) {
107 for _, tc := range []struct {
108 name string
109 setup func(*mockCompiler, ssa.Builder, *machine) backend.SSAValueDefinition
110 exp operand
111 instructions []string
112 }{
113 {
114 name: "block param falls back to getOperand_Reg",
115 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
116 ctx.vRegMap[1234] = raxVReg
117 return backend.SSAValueDefinition{V: 1234, Instr: nil}
118 },
119 exp: newOperandReg(raxVReg),
120 },
121 {
122 name: "const imm 32",
123 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
124 instr := builder.AllocateInstruction()
125 instr.AsIconst32(0xf00000f)
126 builder.InsertInstruction(instr)
127 ctx.vRegCounter = 99
128 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
129 return backend.SSAValueDefinition{Instr: instr}
130 },
131 exp: newOperandImm32(0xf00000f),
132 },
133 } {
134 t.Run(tc.name, func(t *testing.T) {
135 ctx, b, m := newSetupWithMockContext()
136 def := tc.setup(ctx, b, m)
137 actual := m.getOperand_Imm32_Reg(def)
138 require.Equal(t, tc.exp, actual)
139 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
140 })
141 }
142 }
143
144 func Test_machine_getOperand_Mem_Imm32_Reg(t *testing.T) {
145 _, _, m := newSetupWithMockContext()
146 defer func() {
147 runtime.KeepAlive(m)
148 }()
149 newAmodeImmReg := m.newAmodeImmReg
150
151 for _, tc := range []struct {
152 name string
153 setup func(*mockCompiler, ssa.Builder, *machine) backend.SSAValueDefinition
154 exp operand
155 instructions []string
156 }{
157 {
158 name: "block param falls back to getOperand_Imm32_Reg",
159 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
160 ctx.vRegMap[1234] = raxVReg
161 return backend.SSAValueDefinition{V: 1234, Instr: nil}
162 },
163 exp: newOperandReg(raxVReg),
164 },
165 {
166 name: "amode with block param",
167 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
168 blk := builder.CurrentBlock()
169 ptr := blk.AddParam(builder, ssa.TypeI64)
170 ctx.vRegMap[ptr] = raxVReg
171 ctx.definitions[ptr] = backend.SSAValueDefinition{V: ptr}
172 instr := builder.AllocateInstruction()
173 instr.AsLoad(ptr, 123, ssa.TypeI64).Insert(builder)
174 return backend.SSAValueDefinition{Instr: instr}
175 },
176 exp: newOperandMem(newAmodeImmReg(123, raxVReg)),
177 },
178 {
179 name: "amode with iconst",
180 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
181 iconst := builder.AllocateInstruction().AsIconst64(456).Insert(builder)
182 instr := builder.AllocateInstruction()
183 instr.AsLoad(iconst.Return(), 123, ssa.TypeI64).Insert(builder)
184 ctx.definitions[iconst.Return()] = backend.SSAValueDefinition{Instr: iconst}
185 return backend.SSAValueDefinition{Instr: instr}
186 },
187 instructions: []string{
188 "movabsq $579, %r1?", // r1 := 123+456
189 },
190 exp: newOperandMem(newAmodeImmReg(0, regalloc.VReg(1).SetRegType(regalloc.RegTypeInt))),
191 },
192 {
193 name: "amode with iconst and extend",
194 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
195 iconst := builder.AllocateInstruction().AsIconst32(0xffffff).Insert(builder)
196 uextend := builder.AllocateInstruction().AsUExtend(iconst.Return(), 32, 64).Insert(builder)
197
198 instr := builder.AllocateInstruction()
199 instr.AsLoad(uextend.Return(), 123, ssa.TypeI64).Insert(builder)
200
201 ctx.definitions[uextend.Return()] = backend.SSAValueDefinition{Instr: uextend}
202 ctx.definitions[iconst.Return()] = backend.SSAValueDefinition{Instr: iconst}
203
204 return backend.SSAValueDefinition{Instr: instr}
205 },
206 instructions: []string{
207 fmt.Sprintf("movabsq $%d, %%r1?", 0xffffff+123),
208 },
209 exp: newOperandMem(newAmodeImmReg(0, regalloc.VReg(1).SetRegType(regalloc.RegTypeInt))),
210 },
211 {
212 name: "amode with iconst and extend",
213 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
214 iconst := builder.AllocateInstruction().AsIconst32(456).Insert(builder)
215 uextend := builder.AllocateInstruction().AsUExtend(iconst.Return(), 32, 64).Insert(builder)
216
217 instr := builder.AllocateInstruction()
218 instr.AsLoad(uextend.Return(), 123, ssa.TypeI64).Insert(builder)
219
220 ctx.definitions[uextend.Return()] = backend.SSAValueDefinition{Instr: uextend}
221 ctx.definitions[iconst.Return()] = backend.SSAValueDefinition{Instr: iconst}
222
223 return backend.SSAValueDefinition{Instr: instr}
224 },
225 instructions: []string{
226 fmt.Sprintf("movabsq $%d, %%r1?", 456+123),
227 },
228 exp: newOperandMem(newAmodeImmReg(0, regalloc.VReg(1).SetRegType(regalloc.RegTypeInt))),
229 },
230 {
231 name: "amode with iconst and add",
232 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
233 p := builder.CurrentBlock().AddParam(builder, ssa.TypeI64)
234 iconst := builder.AllocateInstruction().AsIconst64(456).Insert(builder)
235 iadd := builder.AllocateInstruction().AsIadd(iconst.Return(), p).Insert(builder)
236
237 instr := builder.AllocateInstruction()
238 instr.AsLoad(iadd.Return(), 789, ssa.TypeI64).Insert(builder)
239
240 ctx.vRegMap[p] = raxVReg
241 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
242 ctx.definitions[iconst.Return()] = backend.SSAValueDefinition{Instr: iconst}
243 ctx.definitions[iadd.Return()] = backend.SSAValueDefinition{Instr: iadd}
244
245 return backend.SSAValueDefinition{Instr: instr}
246 },
247 exp: newOperandMem(newAmodeImmReg(456+789, raxVReg)),
248 },
249 {
250 name: "amode with iconst, block param and add",
251 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) backend.SSAValueDefinition {
252 iconst1 := builder.AllocateInstruction().AsIconst64(456).Insert(builder)
253 iconst2 := builder.AllocateInstruction().AsIconst64(123).Insert(builder)
254 iadd := builder.AllocateInstruction().AsIadd(iconst1.Return(), iconst2.Return()).Insert(builder)
255
256 instr := builder.AllocateInstruction()
257 instr.AsLoad(iadd.Return(), 789, ssa.TypeI64).Insert(builder)
258
259 ctx.definitions[iconst1.Return()] = backend.SSAValueDefinition{Instr: iconst1}
260 ctx.definitions[iconst2.Return()] = backend.SSAValueDefinition{Instr: iconst2}
261 ctx.definitions[iadd.Return()] = backend.SSAValueDefinition{Instr: iadd}
262
263 return backend.SSAValueDefinition{Instr: instr}
264 },
265 instructions: []string{
266 fmt.Sprintf("movabsq $%d, %%r1?", 123+456+789),
267 },
268 exp: newOperandMem(newAmodeImmReg(0, regalloc.VReg(1).SetRegType(regalloc.RegTypeInt))),
269 },
270 } {
271 t.Run(tc.name, func(t *testing.T) {
272 ctx, b, m := newSetupWithMockContext()
273 def := tc.setup(ctx, b, m)
274 actual := m.getOperand_Mem_Imm32_Reg(def)
275 if tc.exp.kind == operandKindMem {
276 require.Equal(t, tc.exp.addressMode(), actual.addressMode())
277 } else {
278 require.Equal(t, tc.exp, actual)
279 }
280 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
281 })
282 }
283 }
284
285 func TestMachine_lowerExitWithCode(t *testing.T) {
286 _, _, m := newSetupWithMockContext()
287 m.nextLabel = 1
288 m.lowerExitWithCode(r15VReg, wazevoapi.ExitCodeUnreachable)
289 m.insert(m.allocateInstr().asUD2())
290 m.FlushPendingInstructions()
291 m.rootInstr = m.perBlockHead
292 require.Equal(t, `
293 mov.q %rsp, 56(%r15)
294 mov.q %rbp, 1152(%r15)
295 movl $3, %ebp
296 mov.l %rbp, (%r15)
297 L1:
298 lea L1, %rbp
299 mov.q %rbp, 48(%r15)
300 exit_sequence %r15
301 L2:
302 ud2
303 `, m.Format())
304 }
305
306 func Test_machine_lowerClz(t *testing.T) {
307 for _, tc := range []struct {
308 name string
309 setup func(*mockCompiler, ssa.Builder, *machine) backend.SSAValueDefinition
310 cpuFlags platform.CpuFeatureFlags
311 typ ssa.Type
312 exp string
313 }{
314 {
315 name: "no extra flags (64)",
316 cpuFlags: 0,
317 typ: ssa.TypeI64,
318 exp: `
319 testq %rax, %rax
320 jnz L1
321 movabsq $64, %r1?
322 jmp L2
323 L1:
324 bsrq %rax, %r1?
325 xor $63, %r1?
326 L2:
327 movq %r1?, %rcx
328 `,
329 },
330 {
331 name: "ABM (64)",
332 cpuFlags: platform.CpuFeatureAmd64ABM,
333 typ: ssa.TypeI64,
334 exp: `
335 lzcntq %rax, %rcx
336 `,
337 },
338 {
339 name: "no extra flags (32)",
340 cpuFlags: 0,
341 typ: ssa.TypeI32,
342 exp: `
343 testl %eax, %eax
344 jnz L1
345 movl $32, %r1d?
346 jmp L2
347 L1:
348 bsrl %eax, %r1d?
349 xor $31, %r1d?
350 L2:
351 movq %r1?, %rcx
352 `,
353 },
354 {
355 name: "ABM (32)",
356 cpuFlags: platform.CpuFeatureAmd64ABM,
357 typ: ssa.TypeI32,
358 exp: `
359 lzcntl %eax, %ecx
360 `,
361 },
362 } {
363 t.Run(tc.name, func(t *testing.T) {
364 ctx, b, m := newSetupWithMockContext()
365 m.nextLabel = 1
366 p := b.CurrentBlock().AddParam(b, tc.typ)
367 m.cpuFeatures = tc.cpuFlags
368
369 ctx.vRegMap[p] = raxVReg
370 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
371 ctx.vRegMap[0] = rcxVReg
372 instr := &ssa.Instruction{}
373 instr.AsClz(p)
374 m.lowerClz(instr)
375 m.FlushPendingInstructions()
376 m.rootInstr = m.perBlockHead
377 require.Equal(t, tc.exp, m.Format())
378 })
379 }
380 }
381
382 func TestMachine_lowerCtz(t *testing.T) {
383 for _, tc := range []struct {
384 name string
385 setup func(*mockCompiler, ssa.Builder, *machine) backend.SSAValueDefinition
386 cpuFlags platform.CpuFeatureFlags
387 typ ssa.Type
388 exp string
389 }{
390 {
391 name: "no extra flags (64)",
392 cpuFlags: 0,
393 typ: ssa.TypeI64,
394 exp: `
395 testq %rax, %rax
396 jnz L1
397 movabsq $64, %r1?
398 jmp L2
399 L1:
400 bsfq %rax, %r1?
401 L2:
402 movq %r1?, %rcx
403 `,
404 },
405 {
406 name: "ABM (64)",
407 cpuFlags: platform.CpuFeatureAmd64BMI1,
408 typ: ssa.TypeI64,
409 exp: `
410 tzcntq %rax, %rcx
411 `,
412 },
413 {
414 name: "no extra flags (32)",
415 cpuFlags: 0,
416 typ: ssa.TypeI32,
417 exp: `
418 testl %eax, %eax
419 jnz L1
420 movl $32, %r1d?
421 jmp L2
422 L1:
423 bsfl %eax, %r1d?
424 L2:
425 movq %r1?, %rcx
426 `,
427 },
428 {
429 name: "ABM (32)",
430 cpuFlags: platform.CpuFeatureAmd64BMI1,
431 typ: ssa.TypeI32,
432 exp: `
433 tzcntl %eax, %ecx
434 `,
435 },
436 } {
437 t.Run(tc.name, func(t *testing.T) {
438 ctx, b, m := newSetupWithMockContext()
439 m.nextLabel = 1
440 p := b.CurrentBlock().AddParam(b, tc.typ)
441 m.cpuFeatures = tc.cpuFlags
442
443 ctx.vRegMap[p] = raxVReg
444 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
445 ctx.vRegMap[0] = rcxVReg
446 instr := &ssa.Instruction{}
447 instr.AsCtz(p)
448 m.lowerCtz(instr)
449 m.FlushPendingInstructions()
450 m.rootInstr = m.perBlockHead
451 require.Equal(t, tc.exp, m.Format())
452 })
453 }
454 }