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