"""r4d_rows.py ROWS_CPP -- the kKernels rows of libr4d/r4d_rows.cpp as TSV: op, kernel, dtypes, domain. Read as source because a build without HIP has no libr4d.so to ask. A row is { "kernel", "op", "family", "computes", "shape", "dtypes", RAD_DOMAIN_..., ... }; adjacent string literals are joined as the C compiler joins them, so a dtypes string split over lines stays whole. """ import re import sys src = open(sys.argv[1], encoding="utf-8").read() start = src.index("static const RadKernelInfo kKernels[] = {") body = src[start:] # Comments out, so prose in them cannot look like a row. body = re.sub(r"/\*.*?\*/", " ", body, flags=re.S) body = re.sub(r"//[^\n]*", " ", body) body = body.replace("\\\n", "\n") # macro line continuations lit = r'"(?:[^"\\]|\\.)*"' # Between literals a row may splice in a macro: R4D_XSTR(X), a stringised #PARAM, or a name such as # KVDOC. Each is kept as , so a template row's kernel reads attn_window_h_gqa_. mac = r"R4D_XSTR\(\s*\w+\s*\)|#\w+|\b(?!RAD_DOMAIN_)[A-Z][A-Z0-9_]*\b" row_re = re.compile(r"\{\s*((?:(?:" + lit + "|" + mac + r")\s*,?\s*)+?)(RAD_DOMAIN_[A-Z]+)") for m in row_re.finditer(body): if not m.group(1).lstrip().startswith('"'): continue # Split the run into arguments at commas; join adjacent literals and macros within one. args, cur = [], [] for tok in re.finditer(lit + "|" + mac + r"|,", m.group(1)): t = tok.group(0) if t == ",": args.append("".join(cur)) cur = [] elif t.startswith('"'): cur.append(re.sub(r"\\(.)", r"\1", t[1:-1])) else: name = re.sub(r"^R4D_XSTR\(\s*|\s*\)$|^#", "", t) cur.append(f"<{name}>") if cur: args.append("".join(cur)) if len(args) < 6: continue kern, op, dtypes = args[0], args[1], " ".join(args[5].split()) print(f"{op}\t{kern}\t{dtypes}\t{m.group(2)[len('RAD_DOMAIN_'):].lower()}")