From 5981b75fd8d7d0dbbfb445f720c9a4e4a9edb992 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Thu, 14 Dec 2023 18:16:21 +0800 Subject: [PATCH] Generate better json for other projects --- check.py | 2 +- check_instr.py | 2 +- main.py | 87 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/check.py b/check.py index 81e14308..0aad8d90 100644 --- a/check.py +++ b/check.py @@ -3,6 +3,7 @@ # Update site directory before running: `poetry run mkdocs build` for ext in ["lsx", "lasx"]: + def parse_fn(line, skip_last): before_paren = line.split("(")[0].split(" ") between_parens = line.split("(")[1].split(")")[0] @@ -19,7 +20,6 @@ def parse_fn(line, skip_last): return tuple(result) - # gcc intrinsics gcc_intrinsics = set() for line in open(f"gcc_{ext}intrin.h", "r"): diff --git a/check_instr.py b/check_instr.py index 9fc7bade..3869e181 100644 --- a/check_instr.py +++ b/check_instr.py @@ -44,4 +44,4 @@ args = " ".join(inst.split(";")[0].split(" ")[1:]) if args != fmt: - print("Mismatch inst=", inst, ",args=", args, ",fmt=", fmt) \ No newline at end of file + print("Mismatch inst=", inst, ",args=", args, ",fmt=", fmt) diff --git a/main.py b/main.py index aa46ab64..2baab4c9 100644 --- a/main.py +++ b/main.py @@ -12,16 +12,13 @@ # read latency & throughput cpus = ["3A6000", "3C5000"] -measure = { - "3A6000": {}, - "3C5000": {} -} +measure = {"3A6000": {}, "3C5000": {}} for cpu in cpus: - with open(f'code/measure-{cpu}.csv', newline='') as csvfile: + with open(f"code/measure-{cpu}.csv", newline="") as csvfile: reader = csv.DictReader(csvfile) for row in reader: latency = [] - for part in row['latency'].split('/'): + for part in row["latency"].split("/"): if part == "": lat = "N/A" else: @@ -31,21 +28,22 @@ latency.append(lat) latency = sorted(list(set(latency))) - throughput_cpi = float(row['throughput(cpi)']) + throughput_cpi = float(row["throughput(cpi)"]) if abs(throughput_cpi - round(throughput_cpi)) < 0.03: throughput_cpi = round(throughput_cpi) # TODO: handle small cpi better by 1/ipc - throughput_ipc = float(row['throughput(ipc)']) + throughput_ipc = float(row["throughput(ipc)"]) if abs(throughput_ipc - round(throughput_ipc)) < 0.03: throughput_ipc = round(throughput_ipc) - measure[cpu][row['name']] = { - 'latency': ", ".join(map(str, latency)), - 'throughput(cpi)': throughput_cpi, - 'throughput(ipc)': throughput_ipc, + measure[cpu][row["name"]] = { + "latency": ", ".join(map(str, latency)), + "throughput(cpi)": throughput_cpi, + "throughput(ipc)": throughput_ipc, } + # depends on implementation of env.macro() def my_macro(env): def wrap(fn): @@ -54,16 +52,21 @@ def vfn(*args): cur_simd = "lsx" cur_vlen = 128 return fn(*args) + env.macros[f"{fn.__name__}"] = vfn + def xvfn(*args): global cur_simd, cur_vlen cur_simd = "lasx" cur_vlen = 256 return fn(*args) + env.macros[f"x{fn.__name__}"] = xvfn return fn + return wrap + def define_env(env): widths = { "b": 8, @@ -128,8 +131,8 @@ def instruction(intrinsic, instr, desc): for cpu in cpus: if instr_name in measure[cpu]: show_cpus.append(cpu) - latencies.append(measure[cpu][instr_name]['latency']) - throughputs.append(measure[cpu][instr_name]['throughput(cpi)']) + latencies.append(measure[cpu][instr_name]["latency"]) + throughputs.append(measure[cpu][instr_name]["throughput(cpi)"]) if len(show_cpus) > 0: latency_throughput = f""" @@ -139,7 +142,9 @@ def instruction(intrinsic, instr, desc): |-----|---------|------------------| """ for i in range(len(show_cpus)): - latency_throughput += f"| {show_cpus[i]} | {latencies[i]} | {throughputs[i]} |\n" + latency_throughput += ( + f"| {show_cpus[i]} | {latencies[i]} | {throughputs[i]} |\n" + ) else: latency_throughput = "" @@ -590,7 +595,7 @@ def xvinsve0(name): @env.macro def vext2xv(name, name2): global cur_simd - cur_simd = "lsx" # avoid replacing vext2xv to xvext2xv + cur_simd = "lsx" # avoid replacing vext2xv to xvext2xv width = widths[name] width2 = widths[name2] signedness = signednesses[name] @@ -1800,7 +1805,7 @@ def vldi(): ) @env.macro - def all_intrinsics(): + def all_intrinsics(render=True): result = [] for file in glob.glob("docs/*/*.md"): for line in open(file, "r"): @@ -1824,17 +1829,36 @@ def all_intrinsics(): extension = "LSX" else: extension = "LASX" - result.append( - { - "name": intrinsic, - "content": markdown.markdown( - body, - extensions=["fenced_code", "codehilite", "tables"], - ), - "group": title, - "extension": extension - } - ) + + if render: + result.append( + { + "name": intrinsic, + "content": markdown.markdown( + body, + extensions=[ + "fenced_code", + "codehilite", + "tables", + ], + ), + "group": title, + "extension": extension, + } + ) + else: + # do not render + result.append( + { + "name": intrinsic.split("(")[0] + .strip() + .split(" ")[-1], + "full_name": intrinsic, + "content": body.strip(), + "group": title, + "extension": extension, + } + ) break return json.dumps(sorted(result, key=lambda e: e["name"])) @@ -1877,7 +1901,8 @@ def latency_throughput_table(): result += "" return result -if __name__ == '__main__': + +if __name__ == "__main__": # Fake an env class Env: def __init__(self): @@ -1888,5 +1913,5 @@ def macro(self, fn): env = Env() define_env(env) - result = env.macros["all_intrinsics"]() - json.dump(json.loads(result), open("intrinsics.json", 'w'), indent=True) + result = env.macros["all_intrinsics"](render=False) + json.dump(json.loads(result), open("intrinsics.json", "w"), indent=True)