Skip to content

Commit cbb8d74

Browse files
author
Viviane Potocnik
committed
util: add opcode caching, handle unsupported insns, fix scalar formats
1 parent b55bb34 commit cbb8d74

File tree

2 files changed

+88
-23
lines changed

2 files changed

+88
-23
lines changed

util/trace/gen_trace.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -309,35 +309,48 @@
309309

310310
# -------------------- FPU helpers --------------------
311311

312+
_cached_opcodes = None
313+
314+
315+
def load_opcodes():
316+
global _cached_opcodes
317+
opcode_file_name = 'opcodes-flt-occamy_CUSTOM.csv'
318+
opcode_file_path = pathlib.Path(__file__).parent.absolute() / opcode_file_name
319+
320+
_cached_opcodes = {}
321+
with open(opcode_file_path, 'r') as f:
322+
for line in f:
323+
fields = line.strip().split(',')
324+
insn_name = fields[0]
325+
vec_params = fields[1:5]
326+
_cached_opcodes[insn_name] = vec_params
327+
312328

313329
def vec_formatter(insn: str, op_type: str, hex_val: int, fmt: int) -> str:
314-
# file data:
315-
# instruction,source_width,source_vec_len,destination_width,destination_vec_len
316-
opcodes_file_name = 'opcodes-flt-occamy_CUSTOM.csv'
317-
opcodes_file_path = pathlib.Path(__file__).parent.absolute() / opcodes_file_name
330+
global _cached_opcodes
331+
if _cached_opcodes is None:
332+
load_opcodes()
333+
318334
# cut the insn after the first space
319335
insn = insn.split(' ')[0]
320336
# check if operand is a source or a destination
321337
is_rd = (op_type == 'rd')
322-
# check if the insn is in the opcodes file
323-
with open(opcodes_file_path, 'r') as f:
324-
for line in f:
325-
if insn in line:
326-
vec_params = line.strip().split(',')[1:5]
327-
# check if vector support for the insn is implemented
328-
if vec_params != ([''] * 4):
329-
# decode vector
330-
if not is_rd:
331-
width, vec_len = map(int, vec_params[0:2])
332-
else:
333-
width, vec_len = map(int, vec_params[2:4])
334-
# divide the hex value into source_vec_len each of width source_width
335-
vec = reversed([hex_val >> (width * i) & (2**width - 1) for i in range(vec_len)])
336-
# decode the source_vec
337-
return [flt_decode(val, fmt) for val in vec]
338-
else:
339-
# if vector instruction but vector formatting not supported, return hex
340-
return hex(hex_val)
338+
# check if the insn is in the opcodes file else return None
339+
vec_params = _cached_opcodes.get(insn, None)
340+
# check if vector support for the insn is implemented
341+
if vec_params != ([''] * 4) or vec_params[2] != '1' or not None:
342+
# decode vector
343+
if not is_rd:
344+
width, vec_len = map(int, vec_params[0:2])
345+
else:
346+
width, vec_len = map(int, vec_params[2:4])
347+
# divide the hex value into source_vec_len each of width source_width
348+
vec = reversed([hex_val >> (width * i) & (2**width - 1) for i in range(vec_len)])
349+
# decode the source_vec
350+
return [flt_decode(val, fmt) for val in vec]
351+
else:
352+
# if vector instruction but vector formatting not supported, return hex
353+
return hex(hex_val)
341354
# if not vector instruction, default to scalar behaviour
342355
return flt_lit(hex_val, fmt)
343356

util/trace/opcodes-flt-occamy_CUSTOM.csv

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,56 @@
11
instruction,source_width,source_vec_len,destination_width,destination_vec_len
2+
fadd.d,64,1,64,1
3+
fsub.d,64,1,64,1
4+
fmul.d,64,1,64,1
5+
fdiv.d,64,1,64,1
6+
fsgnj.d,64,1,64,1
7+
fsgnjn.d,64,1,64,1
8+
fsgnjx.d,64,1,64,1
9+
fmin.d,64,1,64,1
10+
fmax.d,64,1,64,1
11+
fcvt.s.d,64,1,64,1
12+
fcvt.d.s,64,1,64,1
13+
fsqrt.d,64,1,64,1
14+
fle.d,64,1,64,1
15+
flt.d,64,1,64,1
16+
feq.d,64,1,64,1
17+
fcvt.w.d,,,,
18+
fcvt.wu.d,,,,
19+
fclass.d,,,,
20+
fcvt.d.w,,,,
21+
fcvt.d.wu,,,,
22+
fld,64,1,64,1
23+
fsd,64,1,64,1
24+
fmadd.d,64,1,64,1
25+
fmsub.d,64,1,64,1
26+
fnmsub.d,64,1,64,1
27+
fnmadd.d,64,1,64,1
28+
fadd.s,32,1,32,1
29+
fsub.s,32,1,32,1
30+
fmul.s,32,1,32,1
31+
fdiv.s,32,1,32,1
32+
fsgnj.s,32,1,32,1
33+
fsgnjn.s,32,1,32,1
34+
fsgnjx.s,32,1,32,1
35+
fmin.s,32,1,32,1
36+
fmax.s,32,1,32,1
37+
fsqrt.s,32,1,32,1
38+
fle.s,32,1,32,1
39+
flt.s,32,1,32,1
40+
feq.s,32,1,32,1
41+
fcvt.w.s,32,1,32,1
42+
fcvt.wu.s,32,1,32,1
43+
fmv.x.w,32,1,32,1
44+
fclass.s,32,1,32,1
45+
fcvt.s.w,32,1,32,1
46+
fcvt.s.wu,32,1,32,1
47+
fmv.w.x,32,1,32,1
48+
flw,32,1,32,1
49+
fsw,32,1,32,1
50+
fmadd.s,32,1,32,1
51+
fmsub.s,32,1,32,1
52+
fnmsub.s,32,1,32,1
53+
fnmadd.s,32,1,32,1
254
flh,16,1,16,1
355
fsh,16,1,16,1
456
fadd.s,32,1,32,1

0 commit comments

Comments
 (0)