interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / amd / gfx942 / fp8gemv / check.sh

123 lines · 3969 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# Byte-exact lock for the gfx942 encodings, then the CPU numeric reference.
4# llvm-mc-18 -triple=amdgcn -mcpu=gfx942 -show-encoding, the assembled .text,
5# hsaco.om's packing of the .isa, and the committed .golden file must be identical.
6# No GPU is required. A mismatch exits 1.
7set -eu
8HERE=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
9ROOT=$(CDPATH= cd -- "$HERE/../../../.." && pwd)
10OMC=$ROOT/seed/out/omc
11QEMU=/usr/bin/qemu-aarch64-static
12MC=/usr/bin/llvm-mc-18
13OBJCOPY=/usr/bin/llvm-objcopy-18
14SUM=/usr/bin/sha256sum
15
16echo "omc sha256"
17got=$($SUM "$OMC" | awk '{print $1}')
18want=$(awk '{print $1}' "$ROOT/seed/out/omc.sha256")
19echo "$got"
20if [ "$got" != "$want" ]; then
21 echo "omc sha256 does not match seed/out/omc.sha256" >&2
22 exit 1
23fi
24
25work=$(mktemp -d)
26trap 'rm -rf "$work"' EXIT
27
28echo "compile hsaco.om"
29env -i "$QEMU" "$OMC" < "$ROOT/gpu/amd/gfx942/hsaco.om" > "$work/hsaco" 2>"$work/hsaco.err"
30if [ -s "$work/hsaco.err" ]; then
31 cat "$work/hsaco.err" >&2
32 exit 1
33fi
34chmod +x "$work/hsaco"
35
36cmp_one() {
37 stem=$1
38 echo "llvm-mc $stem"
39 "$MC" -triple=amdgcn -mcpu=gfx942 -show-encoding "$HERE/$stem.s" > "$work/$stem.enc"
40 "$MC" -triple=amdgcn -mcpu=gfx942 -filetype=obj -o "$work/$stem.o" "$HERE/$stem.s"
41 "$OBJCOPY" -O binary --only-section=.text "$work/$stem.o" "$work/$stem.mc"
42 echo "hsaco $stem"
43 env -i "$QEMU" "$work/hsaco" "$HERE/$stem.isa" > "$work/$stem.hsaco" 2>"$work/$stem.herr"
44 if [ -s "$work/$stem.herr" ]; then
45 cat "$work/$stem.herr" >&2
46 exit 1
47 fi
48 "$OBJCOPY" -O binary --only-section=.text "$work/$stem.hsaco" "$work/$stem.hs"
49 python3 - "$work/$stem.enc" "$work/$stem.mc" "$work/$stem.hs" "$HERE/$stem.golden" << 'PY'
50import re, sys
51enc, mc, hs, gold = sys.argv[1:]
52text = open(enc).read()
53# A branch prints "A" for the 16-bit displacement. The object file has it resolved.
54words = re.findall(r"encoding: \[([0-9a-fxA, ]+)\]", text)
55slots = []
56for w in words:
57 for tok in w.split(","):
58 tok = tok.strip()
59 slots.append(None if tok == "A" else int(tok, 16))
60obj = open(mc, "rb").read()
61packed = open(hs, "rb").read()
62want = bytes.fromhex("".join(
63 line.split("#", 1)[0].strip()
64 for line in open(gold)
65 if line.split("#", 1)[0].strip()
66))
67show_ok = len(slots) == len(obj) and all(
68 s is None or s == obj[i] for i, s in enumerate(slots)
69)
70wild = sum(1 for s in slots if s is None)
71print(
72 "bytes", len(obj),
73 "show==obj", show_ok,
74 "branch-bytes", wild,
75 "obj==hsaco", obj == packed,
76 "obj==golden", obj == want,
77)
78if not (show_ok and obj == packed == want):
79 sys.exit(1)
80PY
81}
82
83cmp_one encprobe
84cmp_one fp8gemv_stream
85
86echo "descriptor"
87python3 - "$HERE/fp8gemv_stream.isa" << 'PY'
88import sys
89line = ""
90for raw in open(sys.argv[1]):
91 if raw.startswith("K "):
92 line = raw
93 break
94p = line.split()
95# K name size align group private sgprs vgprs agprs maxgroup wave rsrc3 rsrc1 rsrc2 properties
96sgprs, vgprs = int(p[6]), int(p[7])
97rsrc3, rsrc1 = int(p[11], 0), int(p[12], 0)
98vgpr_gran = max(0, (vgprs + 7) // 8 - 1)
99sgpr_used = sgprs + 2
100sgpr_field = 2 * max(0, (sgpr_used + 15) // 16 - 1)
101expect = 0xAF0000 | (sgpr_field << 6) | vgpr_gran
102acc = (vgprs + 3) // 4 - 1
103print("sgprs", sgprs, "vgprs", vgprs, "rsrc1", hex(rsrc1), "expect", hex(expect), "rsrc3", rsrc3, "expect", acc)
104if rsrc1 != expect or rsrc3 != acc:
105 sys.exit(1)
106if "00805edc" not in open(sys.argv[1].rsplit("/", 1)[0] + "/fp8gemv_stream.golden").read():
107 sys.exit(1)
108PY
109
110echo "cpu reference"
111{ printf 'target x86linux\nom2\n'; cat "$ROOT/gpu/amd/gfx942/exec/fma.om" "$HERE/check.om"; } \
112 | env -i "$QEMU" "$OMC" > "$work/check" 2>"$work/check.err"
113if [ -s "$work/check.err" ]; then
114 cat "$work/check.err" >&2
115 exit 1
116fi
117chmod +x "$work/check"
118"$work/check"
119
120echo "plain reference"
121g++ -O2 -std=c++17 -ffp-contract=off -o "$work/plain_ref_test" "$HERE/plain_ref_test.cpp"
122"$work/plain_ref_test"
123echo "check.sh pass"