Debian Codegraph @

rpython/jit/backend/riscv/test/test_codebuilder.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 from hypothesis import assume, given, settings, strategies
4 from rpython.jit.backend.riscv import codebuilder
5 from rpython.jit.backend.riscv import registers as r
6 from rpython.jit.backend.riscv import rounding_modes
7 from rpython.jit.backend.riscv.arch import XLEN
8 from rpython.jit.backend.riscv.instructions import (
9 AMO_ACQUIRE, AMO_RELEASE, FMO_INPUT, FMO_OUTPUT, FMO_READ, FMO_WRITE,
10 all_instructions)
11 from rpython.jit.backend.riscv.test.external_assembler import assemble
12 import py
13
14
15 class CodeBuilder(codebuilder.AbstractRISCVBuilder):
16 def __init__(self):
17 self.buffer = []
18 self._int_const_pool = {}
19 self._float_const_pool = {}
20
21 def writechar(self, char):
22 self.buffer.append(char)
23
24 def overwrite(self, index, char):
25 self.buffer[index] = char
26
27 def hexdump(self):
28 return ''.join(self.buffer)
29
30 def append_pending_int_constant(self, load_inst_pos, reg, const_value):
31 self._int_const_pool[load_inst_pos] = (reg, const_value)
32
33 def append_pending_float_constant(self, load_inst_pos, reg, const_value):
34 self._float_const_pool[load_inst_pos] = (reg, const_value)
35
36 def _get_relative_pos_for_load_imm(self, break_basic_block=False):
37 return len(self.buffer)
38
39 emit_pending_constants = codebuilder._emit_pending_constants
40
41
42 class TestCodeBuilder(object):
43 pass
44
45
46 def _get_op_name(mnemonic):
47 return mnemonic.replace('_', '.')
48
49 def _get_fence_memory_order_str(mo_bits):
50 res = ''
51 if mo_bits & FMO_INPUT:
52 res += 'i'
53 if mo_bits & FMO_OUTPUT:
54 res += 'o'
55 if mo_bits & FMO_READ:
56 res += 'r'
57 if mo_bits & FMO_WRITE:
58 res += 'w'
59 return res
60
61 def _get_amo_suffix(aqrl):
62 res = ''
63 if aqrl & AMO_ACQUIRE:
64 res += 'aq'
65 if aqrl & AMO_RELEASE:
66 res += 'rl'
67 return '.' + res if res else ''
68
69 def _get_op_test_strategy(op_spec):
70 if op_spec == 'R' or op_spec == 'RB':
71 return strategies.sampled_from(r.registers_except_zero)
72 elif op_spec == 'F':
73 return strategies.sampled_from(r.fp_registers)
74 elif op_spec == 'I12':
75 return strategies.integers(min_value=-2**11, max_value=2**11 - 1)
76 elif op_spec == 'I13':
77 return strategies.integers(min_value=-2**11, max_value=2**11 - 1) \
78 .map(lambda x: x * 2).filter(lambda x: x != 2)
79 elif op_spec == 'U20':
80 return strategies.integers(min_value=0, max_value=2**20 - 1)
81 elif op_spec == 'I21':
82 return strategies.integers(min_value=-2**19, max_value=2**19 - 1) \
83 .map(lambda x: x * 2).filter(lambda x: x != 2)
84 elif op_spec == 'SH5':
85 return strategies.integers(min_value=0, max_value=2**5 - 1)
86 elif op_spec == 'SH6':
87 return strategies.integers(min_value=0, max_value=2**6 - 1)
88 elif op_spec == 'RM':
89 return strategies.sampled_from(rounding_modes.all_rounding_modes)
90 elif op_spec == 'FMO':
91 return strategies.integers(min_value=1, max_value=15)
92 elif op_spec == 'AMO':
93 return strategies.integers(min_value=0, max_value=3)
94 elif op_spec == 'AMOLR':
95 return strategies.sampled_from([0, AMO_ACQUIRE,
96 AMO_ACQUIRE | AMO_RELEASE])
97 elif op_spec == 'AMOSC':
98 return strategies.sampled_from([0, AMO_RELEASE,
99 AMO_ACQUIRE | AMO_RELEASE])
100 assert False, 'unhandled op spec: ' + op_spec
101
102 def _gen_r_type_instr_test(mnemonic, instr_type, op_spec):
103 op_spec = op_spec.split(':')
104 assert len(op_spec) == 3
105
106 @settings(max_examples=20)
107 @given(rd=_get_op_test_strategy(op_spec[0]),
108 rs1=_get_op_test_strategy(op_spec[1]),
109 rs2=_get_op_test_strategy(op_spec[2]))
110 def test(self, rd, rs1, rs2):
111 cb = CodeBuilder()
112 getattr(cb, mnemonic)(rd, rs1, rs2)
113 test_out = cb.hexdump()
114
115 op_name = _get_op_name(mnemonic)
116 asm = '%s %s, %s, %s' % (op_name, rd, rs1, rs2)
117 ref_out = assemble(asm)
118
119 assert ref_out == test_out
120
121 return test
122
123 def _gen_i_type_instr_test(mnemonic, instr_type, op_spec):
124 op_spec = op_spec.split(':')
125 assert len(op_spec) == 3
126
127 @settings(max_examples=20)
128 @given(rd=_get_op_test_strategy(op_spec[0]),
129 rs1=_get_op_test_strategy(op_spec[1]),
130 imm=_get_op_test_strategy(op_spec[2]))
131 def test(self, rd, rs1, imm):
132 cb = CodeBuilder()
133 getattr(cb, mnemonic)(rd, rs1, imm)
134 test_out = cb.hexdump()
135
136 op_name = _get_op_name(mnemonic)
137 if op_spec[1] == 'RB':
138 asm = '%s %s, %d(%s)' % (op_name, rd, imm, rs1)
139 else:
140 asm = '%s %s, %s, %d' % (op_name, rd, rs1, imm)
141 ref_out = assemble(asm)
142
143 assert ref_out == test_out
144
145 return test
146
147 def _gen_s_type_instr_test(mnemonic, instr_type, op_spec):
148 op_spec = op_spec.split(':')
149 assert len(op_spec) == 3
150 assert op_spec[1] == 'RB'
151
152 @settings(max_examples=20)
153 @given(rs2=_get_op_test_strategy(op_spec[0]),
154 rs1=_get_op_test_strategy(op_spec[1]),
155 imm=_get_op_test_strategy(op_spec[2]))
156 def test(self, rs2, rs1, imm):
157 cb = CodeBuilder()
158 getattr(cb, mnemonic)(rs2, rs1, imm)
159 test_out = cb.hexdump()
160
161 op_name = _get_op_name(mnemonic)
162 asm = '%s %s, %d(%s)' % (op_name, rs2, imm, rs1)
163 ref_out = assemble(asm)
164
165 assert ref_out == test_out
166
167 return test
168
169 def _gen_b_type_instr_test(mnemonic, instr_type, op_spec):
170 op_spec = op_spec.split(':')
171 assert len(op_spec) == 3
172
173 @settings(max_examples=20)
174 @given(rs1=_get_op_test_strategy(op_spec[0]),
175 rs2=_get_op_test_strategy(op_spec[1]),
176 imm=_get_op_test_strategy(op_spec[2]))
177 def test(self, rs1, rs2, imm):
178 cb = CodeBuilder()
179 getattr(cb, mnemonic)(rs1, rs2, imm)
180 test_out = cb.hexdump()
181
182 op_name = _get_op_name(mnemonic)
183 if imm <= 0:
184 asm = '''
185 .L0:
186 .fill %d
187 %s %s, %s, .L0
188 ''' % (-imm, op_name, rs1, rs2)
189 ref_out = assemble(asm)
190 assert len(ref_out) == -imm + 4
191 assert ref_out[0:-imm] == '\x00' * -imm
192 ref_out = ref_out[-imm:]
193 else:
194 asm = '''
195 %s %s, %s, .L0
196 .fill %d
197 .L0:
198 ''' % (op_name, rs1, rs2, imm - 4)
199 ref_out = assemble(asm)
200 assert len(ref_out) == imm
201 assert ref_out[4:] == '\x00' * (imm - 4)
202 ref_out = ref_out[0:4]
203
204 assert ref_out == test_out
205
206 return test
207
208 def _gen_u_type_instr_test(mnemonic, instr_type, op_spec):
209 op_spec = op_spec.split(':')
210 assert len(op_spec) == 2
211
212 @settings(max_examples=20)
213 @given(rd=_get_op_test_strategy(op_spec[0]),
214 imm=_get_op_test_strategy(op_spec[1]))
215 def test(self, rd, imm):
216 cb = CodeBuilder()
217 getattr(cb, mnemonic)(rd, imm)
218 test_out = cb.hexdump()
219
220 op_name = _get_op_name(mnemonic)
221 asm = '%s %s, %d' % (op_name, rd, imm)
222 ref_out = assemble(asm)
223
224 assert ref_out == test_out
225
226 return test
227
228 def _gen_j_type_instr_test(mnemonic, instr_type, op_spec):
229 op_spec = op_spec.split(':')
230 assert len(op_spec) == 2
231
232 @settings(max_examples=20)
233 @given(rd=_get_op_test_strategy(op_spec[0]),
234 imm=_get_op_test_strategy(op_spec[1]))
235 def test(self, rd, imm):
236 cb = CodeBuilder()
237 getattr(cb, mnemonic)(rd, imm)
238 test_out = cb.hexdump()
239
240 op_name = _get_op_name(mnemonic)
241 if imm <= 0:
242 asm = '''
243 .L0:
244 .fill %d
245 %s %s, .L0
246 ''' % (-imm, op_name, rd)
247 ref_out = assemble(asm)
248 assert len(ref_out) == -imm + 4
249 assert ref_out[0:-imm] == '\x00' * -imm
250 ref_out = ref_out[-imm:]
251 else:
252 asm = '''
253 %s %s, .L0
254 .fill %d
255 .L0:
256 ''' % (op_name, rd, imm - 4)
257 ref_out = assemble(asm)
258 assert len(ref_out) == imm
259 assert ref_out[4:] == '\x00' * (imm - 4)
260 ref_out = ref_out[0:4]
261
262 assert ref_out == test_out
263
264 return test
265
266 def _gen_r4_rm_type_instr_test(mnemonic, instr_type, op_spec):
267 op_spec = op_spec.split(':')
268 assert len(op_spec) == 5
269
270 @settings(max_examples=20)
271 @given(rd=_get_op_test_strategy(op_spec[0]),
272 rs1=_get_op_test_strategy(op_spec[1]),
273 rs2=_get_op_test_strategy(op_spec[2]),
274 rs3=_get_op_test_strategy(op_spec[3]),
275 rm=_get_op_test_strategy(op_spec[4]))
276 def test(self, rd, rs1, rs2, rs3, rm):
277 cb = CodeBuilder()
278 getattr(cb, mnemonic)(rd, rs1, rs2, rs3, rm)
279 test_out = cb.hexdump()
280
281 op_name = _get_op_name(mnemonic)
282 asm = '%s %s, %s, %s, %s, %s' % (op_name, rd, rs1, rs2, rs3, rm)
283 ref_out = assemble(asm)
284
285 assert ref_out == test_out
286
287 return test
288
289 def _gen_r_rm_type_instr_test(mnemonic, instr_type, op_spec):
290 op_spec = op_spec.split(':')
291 assert len(op_spec) == 4
292
293 @settings(max_examples=20)
294 @given(rd=_get_op_test_strategy(op_spec[0]),
295 rs1=_get_op_test_strategy(op_spec[1]),
296 rs2=_get_op_test_strategy(op_spec[2]),
297 rm=_get_op_test_strategy(op_spec[3]))
298 def test(self, rd, rs1, rs2, rm):
299 cb = CodeBuilder()
300 getattr(cb, mnemonic)(rd, rs1, rs2, rm)
301 test_out = cb.hexdump()
302
303 op_name = _get_op_name(mnemonic)
304 asm = '%s %s, %s, %s, %s' % (op_name, rd, rs1, rs2, rm)
305 ref_out = assemble(asm)
306
307 assert ref_out == test_out
308
309 return test
310
311 def _gen_i12_type_instr_test(mnemonic, instr_type, op_spec):
312 op_spec = op_spec.split(':')
313 assert len(op_spec) == 2
314
315 @settings(max_examples=20)
316 @given(rd=_get_op_test_strategy(op_spec[0]),
317 rs1=_get_op_test_strategy(op_spec[1]))
318 def test(self, rd, rs1):
319 cb = CodeBuilder()
320 getattr(cb, mnemonic)(rd, rs1)
321 test_out = cb.hexdump()
322
323 op_name = _get_op_name(mnemonic)
324 asm = '%s %s, %s' % (op_name, rd, rs1)
325 ref_out = assemble(asm)
326
327 assert ref_out == test_out
328
329 return test
330
331 def _gen_i12_rm_type_instr_test(mnemonic, instr_type, op_spec):
332 op_spec = op_spec.split(':')
333 assert len(op_spec) == 3
334
335 @settings(max_examples=20)
336 @given(rd=_get_op_test_strategy(op_spec[0]),
337 rs1=_get_op_test_strategy(op_spec[1]),
338 rm=_get_op_test_strategy(op_spec[2]))
339 def test(self, rd, rs1, rm):
340 cb = CodeBuilder()
341 getattr(cb, mnemonic)(rd, rs1, rm)
342 test_out = cb.hexdump()
343
344 op_name = _get_op_name(mnemonic)
345 asm = '%s %s, %s, %s' % (op_name, rd, rs1, rm)
346 ref_out = assemble(asm)
347
348 assert ref_out == test_out
349
350 return test
351
352 def _gen_a_type_instr_test(mnemonic, instr_type, op_spec):
353 assert op_spec == ''
354
355 def test(self):
356 cb = CodeBuilder()
357 getattr(cb, mnemonic)()
358 test_out = cb.hexdump()
359
360 op_name = _get_op_name(mnemonic)
361 asm = '%s' % (op_name)
362 ref_out = assemble(asm)
363
364 assert ref_out == test_out
365
366 return test
367
368 def _gen_f_type_instr_test(mnemonic, instr_type, op_spec):
369 op_spec = op_spec.split(':')
370 assert len(op_spec) == 2
371
372 @settings(max_examples=20)
373 @given(pred_order=_get_op_test_strategy(op_spec[0]),
374 succ_order=_get_op_test_strategy(op_spec[1]))
375 def test(self, pred_order, succ_order):
376 cb = CodeBuilder()
377 getattr(cb, mnemonic)(pred_order, succ_order)
378 test_out = cb.hexdump()
379
380 op_name = _get_op_name(mnemonic)
381 pred_order_str = _get_fence_memory_order_str(pred_order)
382 succ_order_str = _get_fence_memory_order_str(succ_order)
383 asm = '%s %s, %s' % (op_name, pred_order_str, succ_order_str)
384 ref_out = assemble(asm)
385
386 assert ref_out == test_out
387
388 return test
389
390 def _gen_amo2_type_instr_test(mnemonic, instr_type, op_spec):
391 op_spec = op_spec.split(':')
392 assert len(op_spec) == 3
393
394 @settings(max_examples=20)
395 @given(rd=_get_op_test_strategy(op_spec[0]),
396 rs1=_get_op_test_strategy(op_spec[1]),
397 aqrl=_get_op_test_strategy(op_spec[2]))
398 def test(self, rd, rs1, aqrl):
399 cb = CodeBuilder()
400 getattr(cb, mnemonic)(rd, rs1, aqrl)
401 test_out = cb.hexdump()
402
403 op_name = _get_op_name(mnemonic) + _get_amo_suffix(aqrl)
404 asm = '%s %s, (%s)' % (op_name, rd, rs1)
405 ref_out = assemble(asm)
406
407 assert ref_out == test_out
408
409 return test
410
411 def _gen_amo3_type_instr_test(mnemonic, instr_type, op_spec):
412 op_spec = op_spec.split(':')
413 assert len(op_spec) == 4
414
415 @settings(max_examples=20)
416 @given(rd=_get_op_test_strategy(op_spec[0]),
417 rs2=_get_op_test_strategy(op_spec[1]),
418 rs1=_get_op_test_strategy(op_spec[2]),
419 aqrl=_get_op_test_strategy(op_spec[3]))
420 def test(self, rd, rs2, rs1, aqrl):
421 cb = CodeBuilder()
422 getattr(cb, mnemonic)(rd, rs2, rs1, aqrl)
423 test_out = cb.hexdump()
424
425 op_name = _get_op_name(mnemonic) + _get_amo_suffix(aqrl)
426 # Note: AMO instruction assembly puts rs2 in parentheses and at the
427 # end.
428 asm = '%s %s, %s, (%s)' % (op_name, rd, rs2, rs1)
429 ref_out = assemble(asm)
430
431 assert ref_out == test_out
432
433 return test
434
435 _INSTR_TYPE_DICT = {
436 'R': _gen_r_type_instr_test,
437 'I': _gen_i_type_instr_test,
438 'S': _gen_s_type_instr_test,
439 'B': _gen_b_type_instr_test,
440 'U': _gen_u_type_instr_test,
441 'J': _gen_j_type_instr_test,
442 'I_SHAMT5': _gen_i_type_instr_test,
443 'I_SHAMT6': _gen_i_type_instr_test,
444 'R4_RM': _gen_r4_rm_type_instr_test,
445 'R_RM': _gen_r_rm_type_instr_test,
446 'I12': _gen_i12_type_instr_test,
447 'I12_RM': _gen_i12_rm_type_instr_test,
448 'A': _gen_a_type_instr_test,
449 'F': _gen_f_type_instr_test,
450 'AMO2': _gen_amo2_type_instr_test,
451 'AMO3': _gen_amo3_type_instr_test,
452 }
453
454 def _gen_instr_test(mnemonic, instr_type, op_spec):
455 return _INSTR_TYPE_DICT[instr_type](mnemonic, instr_type, op_spec)
456
457 def _gen_all_instr_tests(cls):
458 for mnemonic, instr_type, op_spec, fields in all_instructions:
459 if instr_type not in _INSTR_TYPE_DICT:
460 continue
461 setattr(cls, 'test_' + mnemonic,
462 _gen_instr_test(mnemonic, instr_type, op_spec))
463
464 _gen_all_instr_tests(TestCodeBuilder)
465
466
467 class TestLoadImm(object):
468 def test_load_int_imm_single_addi(self):
469 rd = r.x5
470 imm = -42
471
472 cb = CodeBuilder()
473 cb.load_int_imm(rd.value, imm)
474 test_out = cb.hexdump()
475
476 cb = CodeBuilder()
477 cb.ADDI(rd.value, r.zero.value, imm)
478 ref_out = cb.hexdump()
479
480 assert ref_out == test_out
481
482 def test_load_int_imm_single_lui(self):
483 rd = r.x5
484 imm = -8192
485
486 cb = CodeBuilder()
487 cb.load_int_imm(rd.value, imm)
488 test_out = cb.hexdump()
489
490 cb = CodeBuilder()
491 cb.LUI(rd.value, 0xffffe)
492 ref_out = cb.hexdump()
493
494 assert ref_out == test_out
495
496 def test_load_int_imm_lui_addiw(self):
497 rd = r.x5
498 imm = -8191
499
500 cb = CodeBuilder()
501 cb.load_int_imm(rd.value, imm)
502 test_out = cb.hexdump()
503
504 cb = CodeBuilder()
505 cb.LUI(rd.value, 0xffffe)
506 cb.ADDIW(rd.value, rd.value, 0x001)
507 ref_out = cb.hexdump()
508
509 assert ref_out == test_out
510
511 def test_load_int_imm_lui_addiw_i32_max(self):
512 rd = r.x5
513 imm = 0x7fffffff
514
515 cb = CodeBuilder()
516 cb.load_int_imm(rd.value, imm)
517 test_out = cb.hexdump()
518
519 cb = CodeBuilder()
520 cb.LUI(rd.value, 0x80000)
521 cb.ADDIW(rd.value, rd.value, -1)
522 ref_out = cb.hexdump()
523
524 assert ref_out == test_out
525
526 def test_load_int_imm_lui_addiw_i32_min(self):
527 rd = r.x5
528 imm = -0x80000000
529
530 cb = CodeBuilder()
531 cb.load_int_imm(rd.value, imm)
532 test_out = cb.hexdump()
533
534 cb = CodeBuilder()
535 cb.LUI(rd.value, 0x80000)
536 ref_out = cb.hexdump()
537
538 assert ref_out == test_out
539
540 def test_load_int_imm_lui_addiw_i32_next_min(self):
541 rd = r.x5
542 imm = -0x7fffffff
543
544 cb = CodeBuilder()
545 cb.load_int_imm(rd.value, imm)
546 test_out = cb.hexdump()
547
548 cb = CodeBuilder()
549 cb.LUI(rd.value, 0x80000)
550 cb.ADDIW(rd.value, rd.value, 1)
551 ref_out = cb.hexdump()
552
553 assert ref_out == test_out
554
555 def test_load_int_imm_large_imm(self):
556 if XLEN != 8:
557 py.test.skip('only 64-bit need constant pool, skip 32-bit')
558
559 rd = r.x10
560 imm = 0x1122334455667788
561
562 cb = CodeBuilder()
563 cb.load_int_imm(rd.value, imm)
564 cb.RET()
565 cb.emit_pending_constants()
566 test_out = cb.hexdump()
567
568 cb = CodeBuilder()
569 cb.AUIPC(rd.value, 0)
570 cb.load_int(rd.value, rd.value, 16)
571 cb.RET()
572 cb.write32(0) # Automatically inserted alignment
573 cb.write64(imm)
574 ref_out = cb.hexdump()
575
576 assert ref_out == test_out
577
578 def test_load_float_imm(self):
579 rd = r.f10
580 imm = 0x3ff0000000000000
581
582 cb = CodeBuilder()
583 cb.load_float_imm(rd.value, imm)
584 cb.RET()
585 cb.emit_pending_constants()
586 test_out = cb.hexdump()
587
588 cb = CodeBuilder()
589 cb.AUIPC(r.x31.value, 0)
590 cb.load_float(rd.value, r.x31.value, 16)
591 cb.RET()
592 cb.write32(0) # Automatically inserted alignment
593 cb.write64(imm)
594 ref_out = cb.hexdump()
595
596 assert ref_out == test_out