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 / emit-tables.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Rebuilds the gfx942 tables from one instruction list. |
| 4 | # check.sh does not run this file. It assembles the .s with llvm-mc and packs the .isa |
| 5 | # with hsaco.om, and those two byte strings have to match. |
| 6 | |
| 7 | import os |
| 8 | import struct |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | ROOT = os.path.dirname(os.path.abspath(__file__)) |
| 13 | |
| 14 | |
| 15 | def u32(x): |
| 16 | return struct.pack("<I", x & 0xFFFFFFFF) |
| 17 | |
| 18 | |
| 19 | def flat(offset, sc0, nt, op, sc1, vaddr, vdata, saddr, vdst, sve=0, seg=2, acc=0): |
| 20 | w0 = ( |
| 21 | (offset & 0x1FFF) |
| 22 | | ((sve & 1) << 13) |
| 23 | | ((seg & 3) << 14) |
| 24 | | ((sc0 & 1) << 16) |
| 25 | | ((nt & 1) << 17) |
| 26 | | ((op & 0x7F) << 18) |
| 27 | | ((sc1 & 1) << 25) |
| 28 | | (0x37 << 26) |
| 29 | ) |
| 30 | w1 = ( |
| 31 | (vaddr & 0xFF) |
| 32 | | ((vdata & 0xFF) << 8) |
| 33 | | ((saddr & 0x7F) << 16) |
| 34 | | ((acc & 1) << 23) |
| 35 | | ((vdst & 0xFF) << 24) |
| 36 | ) |
| 37 | return u32(w0) + u32(w1) |
| 38 | |
| 39 | |
| 40 | def mubuf(op, offen, sc0, sc1, nt, vaddr, vdata, srsrc, soffset, offset=0, idxen=0): |
| 41 | w0 = ( |
| 42 | (offset & 0xFFF) |
| 43 | | ((offen & 1) << 12) |
| 44 | | ((idxen & 1) << 13) |
| 45 | | ((sc0 & 1) << 14) |
| 46 | | ((sc1 & 1) << 15) |
| 47 | | ((nt & 1) << 17) |
| 48 | | ((op & 0x7F) << 18) |
| 49 | | (0x38 << 26) |
| 50 | ) |
| 51 | w1 = ( |
| 52 | (vaddr & 0xFF) |
| 53 | | ((vdata & 0xFF) << 8) |
| 54 | | ((srsrc & 0x1F) << 16) |
| 55 | | ((soffset & 0xFF) << 24) |
| 56 | ) |
| 57 | return u32(w0) + u32(w1) |
| 58 | |
| 59 | |
| 60 | def sop2(op, a, b, dst): |
| 61 | w = (a & 0xFF) | ((b & 0xFF) << 8) | ((dst & 0x7F) << 16) | ((op & 0x7F) << 23) | (2 << 30) |
| 62 | return u32(w) |
| 63 | |
| 64 | |
| 65 | def sop1(op, src, dst): |
| 66 | w = (src & 0xFF) | ((op & 0xFF) << 8) | ((dst & 0x7F) << 16) | (0x17D << 23) |
| 67 | return u32(w) |
| 68 | |
| 69 | |
| 70 | def sopc(op, a, b): |
| 71 | w = (a & 0xFF) | ((b & 0xFF) << 8) | ((op & 0x7F) << 16) | (0x17E << 23) |
| 72 | return u32(w) |
| 73 | |
| 74 | |
| 75 | def sopp(op, simm): |
| 76 | w = (simm & 0xFFFF) | ((op & 0x7F) << 16) | (0x17F << 23) |
| 77 | return u32(w) |
| 78 | |
| 79 | |
| 80 | def smem(sdata, op, offset, sbase=0): |
| 81 | w0 = (sbase & 0x3F) | ((sdata & 0x7F) << 6) | (1 << 17) | ((op & 0xFF) << 18) | (0x30 << 26) |
| 82 | w1 = offset & 0x1FFFFF |
| 83 | return u32(w0) + u32(w1) |
| 84 | |
| 85 | |
| 86 | def vop2(src0, vsrc1, vdst, op): |
| 87 | w = (src0 & 0x1FF) | ((vsrc1 & 0xFF) << 9) | ((vdst & 0xFF) << 17) | ((op & 0x3F) << 25) |
| 88 | return u32(w) |
| 89 | |
| 90 | |
| 91 | def vop1(src, op, vdst): |
| 92 | w = (src & 0x1FF) | ((op & 0xFF) << 9) | ((vdst & 0xFF) << 17) | (0x3F << 25) |
| 93 | return u32(w) |
| 94 | |
| 95 | |
| 96 | def vop3(vdst, op, src0, src1, src2): |
| 97 | w0 = (vdst & 0xFF) | ((op & 0x3FF) << 16) | (0x34 << 26) |
| 98 | w1 = (src0 & 0x1FF) | ((src1 & 0x1FF) << 9) | ((src2 & 0x1FF) << 18) |
| 99 | return u32(w0) + u32(w1) |
| 100 | |
| 101 | |
| 102 | def dpp(vsrc1, vdst, op, src, ctrl): |
| 103 | w0 = 250 | ((vsrc1 & 0xFF) << 9) | ((vdst & 0xFF) << 17) | ((op & 0x3F) << 25) |
| 104 | w1 = (src & 0xFF) | ((ctrl & 0x1FF) << 8) | (0xF << 24) | (0xF << 28) |
| 105 | return u32(w0) + u32(w1) |
| 106 | |
| 107 | |
| 108 | class Prog: |
| 109 | def __init__(self): |
| 110 | self.ops = [] |
| 111 | self.labels = {} |
| 112 | |
| 113 | def add(self, blob, asm, kind="i", extra=None): |
| 114 | self.ops.append({"blob": blob, "asm": asm, "kind": kind, "extra": extra, "at": None}) |
| 115 | |
| 116 | def label(self, name): |
| 117 | self.ops.append({"blob": b"", "asm": name + ":", "kind": "label", "extra": name, "at": None}) |
| 118 | |
| 119 | def branch(self, op, name, mnemonic): |
| 120 | self.ops.append({"blob": None, "asm": mnemonic + " " + name, "kind": "br", "extra": (op, name), "at": None}) |
| 121 | |
| 122 | def layout(self): |
| 123 | at = 0 |
| 124 | for op in self.ops: |
| 125 | op["at"] = at |
| 126 | if op["kind"] == "label": |
| 127 | self.labels[op["extra"]] = at |
| 128 | elif op["kind"] == "br": |
| 129 | at += 4 |
| 130 | else: |
| 131 | at += len(op["blob"]) |
| 132 | raw = b"" |
| 133 | isa = [] |
| 134 | asm = [] |
| 135 | ids = {} |
| 136 | nlab = 0 |
| 137 | for op in self.ops: |
| 138 | if op["kind"] == "label": |
| 139 | nlab += 1 |
| 140 | ids[op["extra"]] = nlab |
| 141 | for op in self.ops: |
| 142 | if op["kind"] == "label": |
| 143 | isa.append("T %d" % ids[op["extra"]]) |
| 144 | asm.append(op["asm"]) |
| 145 | continue |
| 146 | if op["kind"] == "br": |
| 147 | bop, name = op["extra"] |
| 148 | nxt = op["at"] + 4 |
| 149 | d = (self.labels[name] - nxt) // 4 |
| 150 | if d < 0: |
| 151 | simm = d + 65536 |
| 152 | else: |
| 153 | simm = d |
| 154 | if simm < 0 or simm > 65535: |
| 155 | raise SystemExit("branch %s out of range" % name) |
| 156 | raw += sopp(bop, simm) |
| 157 | isa.append("I 4 @%d %d" % (ids[name], bop)) |
| 158 | asm.append(op["asm"]) |
| 159 | continue |
| 160 | raw += op["blob"] |
| 161 | isa.append(op["extra"]) |
| 162 | asm.append(op["asm"]) |
| 163 | return raw, isa, asm |
| 164 | |
| 165 | |
| 166 | # SOP2 opcodes are the 7-bit field in CDNA3 ISA ยง12.1 (S_AND_B32 is 12, not 6). |
| 167 | SOP2_ADD = 0 |
| 168 | SOP2_ADDC = 4 |
| 169 | SOP2_AND = 12 |
| 170 | SOP2_LSHL = 28 |
| 171 | SOP2_LSHR = 30 |
| 172 | SOP2_MUL = 36 |
| 173 | SOP2_MULHI = 44 |
| 174 | |
| 175 | |
| 176 | def sop2e(p, op, a, b, dst, text): |
| 177 | p.add(sop2(op, a, b, dst), text, "isa", "I 2 %d %d %d %d" % (a, b, dst, op)) |
| 178 | |
| 179 | |
| 180 | def sop1e(p, op, src, dst, text, lit=None): |
| 181 | blob = sop1(op, src, dst) |
| 182 | line = "I 3 %d %d %d" % (src, op, dst) |
| 183 | if lit is not None: |
| 184 | blob += u32(lit) |
| 185 | line += "\nL %d" % lit |
| 186 | p.add(blob, text, "isa", line) |
| 187 | |
| 188 | |
| 189 | def sopce(p, op, a, b, text): |
| 190 | p.add(sopc(op, a, b), text, "isa", "I 11 %d %d %d" % (a, b, op)) |
| 191 | |
| 192 | |
| 193 | def vope(p, src0, vsrc1, vdst, op, text, lit=None): |
| 194 | blob = vop2(src0, vsrc1, vdst, op) |
| 195 | line = "I 5 %d %d %d %d" % (src0, vsrc1, vdst, op) |
| 196 | if lit is not None: |
| 197 | blob += u32(lit) |
| 198 | line += "\nL %d" % lit |
| 199 | p.add(blob, text, "isa", line) |
| 200 | |
| 201 | |
| 202 | def v1e(p, src, op, vdst, text): |
| 203 | p.add(vop1(src, op, vdst), text, "isa", "I 8 %d %d %d" % (src, op, vdst)) |
| 204 | |
| 205 | |
| 206 | def v3e(p, vdst, op, s0, s1, s2, text): |
| 207 | p.add(vop3(vdst, op, s0, s1, s2), text, "isa", "I 9 %d %d %d %d %d" % (vdst, op, s0, s1, s2)) |
| 208 | |
| 209 | |
| 210 | def dppe(p, vsrc1, vdst, op, src, ctrl, text): |
| 211 | p.add(dpp(vsrc1, vdst, op, src, ctrl), text, "isa", "I 10 250 %d %d %d %d %d 15 15" % (vsrc1, vdst, op, src, ctrl)) |
| 212 | |
| 213 | |
| 214 | def flate(p, offset, sc0, nt, op, sc1, vaddr, vdata, saddr, vdst, text): |
| 215 | p.add( |
| 216 | flat(offset, sc0, nt, op, sc1, vaddr, vdata, saddr, vdst), |
| 217 | text, |
| 218 | "isa", |
| 219 | "I 7 %d 0 2 %d %d %d %d %d %d %d 0 %d" % (offset, sc0, nt, op, sc1, vaddr, vdata, saddr, vdst), |
| 220 | ) |
| 221 | |
| 222 | |
| 223 | def mube(p, op, offen, sc0, sc1, nt, vaddr, vdata, srsrc, soffset, text): |
| 224 | p.add( |
| 225 | mubuf(op, offen, sc0, sc1, nt, vaddr, vdata, srsrc, soffset), |
| 226 | text, |
| 227 | "isa", |
| 228 | "I 12 0 %d 0 %d %d %d %d %d %d %d %d" % (offen, sc0, sc1, nt, op, vaddr, vdata, srsrc, soffset), |
| 229 | ) |
| 230 | |
| 231 | |
| 232 | def smeme(p, sdata, op, offset, text): |
| 233 | p.add(smem(sdata, op, offset), text, "isa", "I 1 0 %d 0 1 %d %d" % (sdata, op, offset)) |
| 234 | |
| 235 | |
| 236 | INLINE0 = 128 |
| 237 | |
| 238 | |
| 239 | def issue_tile(p, k, w, x): |
| 240 | sop2e(p, SOP2_MUL, 20, 18, 22, "s_mul_i32 s22, s20, s18") |
| 241 | sop2e(p, SOP2_MULHI, 20, 18, 23, "s_mul_hi_u32 s23, s20, s18") |
| 242 | sop2e(p, SOP2_ADD, 22, k, 22, "s_add_u32 s22, s22, s%d" % k) |
| 243 | sop2e(p, SOP2_ADDC, 23, INLINE0, 23, "s_addc_u32 s23, s23, 0") |
| 244 | sop2e(p, SOP2_ADD, 4, 22, 22, "s_add_u32 s22, s4, s22") |
| 245 | sop2e(p, SOP2_ADDC, 5, 23, 23, "s_addc_u32 s23, s5, s23") |
| 246 | flate(p, 0, 0, 1, 23, 0, 2, 0, 22, w, "global_load_dwordx4 v[%d:%d], v2, s[22:23] nt" % (w, w + 3)) |
| 247 | sop2e(p, SOP2_LSHL, k, 130, 24, "s_lshl_b32 s24, s%d, 2" % k) |
| 248 | sop2e(p, SOP2_LSHR, k, 158, 25, "s_lshr_b32 s25, s%d, 30" % k) |
| 249 | sop2e(p, SOP2_ADD, 6, 24, 24, "s_add_u32 s24, s6, s24") |
| 250 | sop2e(p, SOP2_ADDC, 7, 25, 25, "s_addc_u32 s25, s7, s25") |
| 251 | for i, off in enumerate((0, 16, 32, 48)): |
| 252 | vd = x + 4 * i |
| 253 | flate( |
| 254 | p, off, 0, 0, 23, 0, 3, 0, 24, vd, |
| 255 | "global_load_dwordx4 v[%d:%d], v3, s[24:25]%s" % (vd, vd + 3, "" if off == 0 else " offset:%d" % off), |
| 256 | ) |
| 257 | |
| 258 | |
| 259 | def fma4(p, w, a0, a1, a2, a3): |
| 260 | v1e(p, 256 + w, 86, 6, "v_cvt_pk_f32_fp8_e32 v[6:7], v%d" % w) |
| 261 | v3e(p, 4, 459, 262, 256 + a0, 260, "v_fma_f32 v4, v6, v%d, v4" % a0) |
| 262 | v3e(p, 4, 459, 263, 256 + a1, 260, "v_fma_f32 v4, v7, v%d, v4" % a1) |
| 263 | vope(p, 144, w, 1, 16, "v_lshrrev_b32_e32 v1, 16, v%d" % w) |
| 264 | v1e(p, 257, 86, 6, "v_cvt_pk_f32_fp8_e32 v[6:7], v1") |
| 265 | v3e(p, 4, 459, 262, 256 + a2, 260, "v_fma_f32 v4, v6, v%d, v4" % a2) |
| 266 | v3e(p, 4, 459, 263, 256 + a3, 260, "v_fma_f32 v4, v7, v%d, v4" % a3) |
| 267 | |
| 268 | |
| 269 | def compute_tile(p, w, x): |
| 270 | for i in range(4): |
| 271 | fma4(p, w + i, x + 4 * i, x + 4 * i + 1, x + 4 * i + 2, x + 4 * i + 3) |
| 272 | |
| 273 | |
| 274 | def wait(p, simm, text): |
| 275 | p.add(sopp(12, simm), text, "isa", "I 4 %d 12" % simm) |
| 276 | |
| 277 | |
| 278 | def build_kernel(): |
| 279 | p = Prog() |
| 280 | smeme(p, 4, 2, 0, "s_load_dwordx4 s[4:7], s[0:1], 0x0") |
| 281 | smeme(p, 8, 2, 16, "s_load_dwordx4 s[8:11], s[0:1], 0x10") |
| 282 | smeme(p, 12, 2, 32, "s_load_dwordx4 s[12:15], s[0:1], 0x20") |
| 283 | smeme(p, 16, 2, 48, "s_load_dwordx4 s[16:19], s[0:1], 0x30") |
| 284 | wait(p, 0xC07F, "s_waitcnt lgkmcnt(0)") |
| 285 | vope(p, 255, 0, 0, 0x13, "v_and_b32_e32 v0, 0x3ff, v0", 0x3FF) |
| 286 | sop2e(p, SOP2_AND, 19, 129, 22, "s_and_b32 s22, s19, 1") |
| 287 | sopce(p, 6, 22, INLINE0, "s_cmp_eq_u32 s22, 0") |
| 288 | p.branch(5, "linear", "s_cbranch_scc1") |
| 289 | sop2e(p, SOP2_AND, 2, 135, 22, "s_and_b32 s22, s2, 7") |
| 290 | sop2e(p, SOP2_LSHR, 2, 131, 23, "s_lshr_b32 s23, s2, 3") |
| 291 | sop2e(p, SOP2_ADD, 17, 135, 24, "s_add_u32 s24, s17, 7") |
| 292 | sop2e(p, SOP2_LSHR, 24, 131, 24, "s_lshr_b32 s24, s24, 3") |
| 293 | sop2e(p, SOP2_MUL, 22, 24, 20, "s_mul_i32 s20, s22, s24") |
| 294 | sop2e(p, SOP2_ADD, 20, 23, 20, "s_add_u32 s20, s20, s23") |
| 295 | p.branch(2, "haverow", "s_branch") |
| 296 | p.label("linear") |
| 297 | sop1e(p, 0, 2, 20, "s_mov_b32 s20, s2") |
| 298 | p.label("haverow") |
| 299 | sopce(p, 9, 20, 17, "s_cmp_ge_u32 s20, s17") |
| 300 | p.branch(5, "done", "s_cbranch_scc1") |
| 301 | v1e(p, INLINE0, 1, 4, "v_mov_b32_e32 v4, 0") |
| 302 | sop1e(p, 0, INLINE0, 21, "s_mov_b32 s21, 0") |
| 303 | sop1e(p, 0, 255, 31, "s_mov_b32 s31, 0x400", 1024) |
| 304 | vope(p, 132, 0, 2, 0x12, "v_lshlrev_b32_e32 v2, 4, v0") |
| 305 | vope(p, 134, 0, 3, 0x12, "v_lshlrev_b32_e32 v3, 6, v0") |
| 306 | p.label("pairs") |
| 307 | sop2e(p, SOP2_ADD, 31, 31, 22, "s_add_u32 s22, s31, s31") |
| 308 | sop2e(p, SOP2_ADD, 21, 22, 22, "s_add_u32 s22, s21, s22") |
| 309 | sopce(p, 8, 22, 18, "s_cmp_gt_u32 s22, s18") |
| 310 | p.branch(5, "single", "s_cbranch_scc1") |
| 311 | issue_tile(p, 21, 8, 16) |
| 312 | sop2e(p, SOP2_ADD, 21, 31, 30, "s_add_u32 s30, s21, s31") |
| 313 | issue_tile(p, 30, 32, 36) |
| 314 | wait(p, 0x0F75, "s_waitcnt vmcnt(5)") |
| 315 | compute_tile(p, 8, 16) |
| 316 | wait(p, 0x0F70, "s_waitcnt vmcnt(0)") |
| 317 | compute_tile(p, 32, 36) |
| 318 | sop2e(p, SOP2_ADD, 21, 31, 21, "s_add_u32 s21, s21, s31") |
| 319 | sop2e(p, SOP2_ADD, 21, 31, 21, "s_add_u32 s21, s21, s31") |
| 320 | p.branch(2, "pairs", "s_branch") |
| 321 | p.label("single") |
| 322 | sop2e(p, SOP2_ADD, 21, 31, 22, "s_add_u32 s22, s21, s31") |
| 323 | sopce(p, 8, 22, 18, "s_cmp_gt_u32 s22, s18") |
| 324 | p.branch(5, "tail", "s_cbranch_scc1") |
| 325 | issue_tile(p, 21, 8, 16) |
| 326 | wait(p, 0x0F70, "s_waitcnt vmcnt(0)") |
| 327 | compute_tile(p, 8, 16) |
| 328 | sop2e(p, SOP2_ADD, 21, 31, 21, "s_add_u32 s21, s21, s31") |
| 329 | p.label("tail") |
| 330 | sop1e(p, 0, INLINE0, 28, "s_mov_b32 s28, 0") |
| 331 | p.label("tloop") |
| 332 | sopce(p, 6, 28, 144, "s_cmp_eq_u32 s28, 16") |
| 333 | p.branch(5, "reduce", "s_cbranch_scc1") |
| 334 | vope(p, 21, 2, 1, 0x34, "v_add_u32_e32 v1, s21, v2") |
| 335 | vope(p, 28, 1, 1, 0x34, "v_add_u32_e32 v1, s28, v1") |
| 336 | v3e(p, 106, 201, 257, 18, 0, "v_cmp_lt_u32_e64 vcc, v1, s18") |
| 337 | sop1e(p, 0x20, 106, 26, "s_and_saveexec_b64 s[26:27], vcc") |
| 338 | sop2e(p, SOP2_MUL, 20, 18, 22, "s_mul_i32 s22, s20, s18") |
| 339 | sop2e(p, SOP2_MULHI, 20, 18, 23, "s_mul_hi_u32 s23, s20, s18") |
| 340 | sop2e(p, SOP2_ADD, 4, 22, 22, "s_add_u32 s22, s4, s22") |
| 341 | sop2e(p, SOP2_ADDC, 5, 23, 23, "s_addc_u32 s23, s5, s23") |
| 342 | vope(p, 22, 1, 8, 0x19, "v_add_co_u32_e32 v8, vcc, s22, v1") |
| 343 | v1e(p, INLINE0, 1, 12, "v_mov_b32_e32 v12, 0") |
| 344 | v1e(p, 23, 1, 13, "v_mov_b32_e32 v13, s23") |
| 345 | vope(p, 269, 12, 9, 0x1C, "v_addc_co_u32_e32 v9, vcc, v13, v12, vcc") |
| 346 | flate(p, 0, 0, 1, 16, 0, 8, 0, 127, 10, "global_load_ubyte v10, v[8:9], off nt") |
| 347 | vope(p, 130, 1, 8, 0x12, "v_lshlrev_b32_e32 v8, 2, v1") |
| 348 | vope(p, 158, 1, 9, 0x10, "v_lshrrev_b32_e32 v9, 30, v1") |
| 349 | vope(p, 6, 8, 8, 0x19, "v_add_co_u32_e32 v8, vcc, s6, v8") |
| 350 | v1e(p, 7, 1, 13, "v_mov_b32_e32 v13, s7") |
| 351 | v1e(p, INLINE0, 1, 12, "v_mov_b32_e32 v12, 0") |
| 352 | vope(p, 269, 9, 9, 0x1C, "v_addc_co_u32_e32 v9, vcc, v13, v9, vcc") |
| 353 | flate(p, 0, 0, 0, 20, 0, 8, 0, 127, 11, "global_load_dword v11, v[8:9], off") |
| 354 | wait(p, 0x0F70, "s_waitcnt vmcnt(0)") |
| 355 | v1e(p, 266, 86, 6, "v_cvt_pk_f32_fp8_e32 v[6:7], v10") |
| 356 | v3e(p, 4, 459, 262, 267, 260, "v_fma_f32 v4, v6, v11, v4") |
| 357 | sop1e(p, 1, 26, 126, "s_mov_b64 exec, s[26:27]") |
| 358 | sop2e(p, SOP2_ADD, 28, 129, 28, "s_add_u32 s28, s28, 1") |
| 359 | p.branch(2, "tloop", "s_branch") |
| 360 | p.label("reduce") |
| 361 | for ctrl, n in ((0x118, 8), (0x114, 4), (0x112, 2), (0x111, 1)): |
| 362 | dppe(p, 4, 4, 1, 4, ctrl, "v_add_f32_dpp v4, v4, v4 row_shr:%d row_mask:0xf bank_mask:0xf" % n) |
| 363 | for lane, reg in ((15, 143), (31, 159), (47, 175), (63, 191)): |
| 364 | v3e(p, 28 + lane // 16, 649, 260, reg, 0, "v_readlane_b32 s%d, v4, %d" % (28 + lane // 16, lane)) |
| 365 | v1e(p, 28, 1, 5, "v_mov_b32_e32 v5, s28") |
| 366 | v1e(p, 29, 1, 6, "v_mov_b32_e32 v6, s29") |
| 367 | vope(p, 261, 6, 5, 1, "v_add_f32_e32 v5, v5, v6") |
| 368 | v1e(p, 30, 1, 6, "v_mov_b32_e32 v6, s30") |
| 369 | v1e(p, 31, 1, 7, "v_mov_b32_e32 v7, s31") |
| 370 | vope(p, 262, 7, 6, 1, "v_add_f32_e32 v6, v6, v7") |
| 371 | vope(p, 261, 6, 4, 1, "v_add_f32_e32 v4, v5, v6") |
| 372 | sop2e(p, SOP2_LSHL, 20, 130, 22, "s_lshl_b32 s22, s20, 2") |
| 373 | sop2e(p, SOP2_ADD, 8, 22, 22, "s_add_u32 s22, s8, s22") |
| 374 | sop2e(p, SOP2_ADDC, 9, INLINE0, 23, "s_addc_u32 s23, s9, 0") |
| 375 | v1e(p, 22, 1, 8, "v_mov_b32_e32 v8, s22") |
| 376 | v1e(p, 23, 1, 9, "v_mov_b32_e32 v9, s23") |
| 377 | flate(p, 0, 0, 0, 20, 0, 8, 0, 127, 5, "global_load_dword v5, v[8:9], off") |
| 378 | wait(p, 0x0F70, "s_waitcnt vmcnt(0)") |
| 379 | vope(p, 260, 5, 4, 5, "v_mul_f32_e32 v4, v4, v5") |
| 380 | sop2e(p, SOP2_AND, 19, 130, 22, "s_and_b32 s22, s19, 2") |
| 381 | sopce(p, 6, 22, INLINE0, "s_cmp_eq_u32 s22, 0") |
| 382 | p.branch(5, "nofuse", "s_cbranch_scc1") |
| 383 | sop2e(p, SOP2_LSHL, 20, 130, 22, "s_lshl_b32 s22, s20, 2") |
| 384 | sop2e(p, SOP2_ADD, 12, 22, 22, "s_add_u32 s22, s12, s22") |
| 385 | sop2e(p, SOP2_ADDC, 13, INLINE0, 23, "s_addc_u32 s23, s13, 0") |
| 386 | v1e(p, 22, 1, 8, "v_mov_b32_e32 v8, s22") |
| 387 | v1e(p, 23, 1, 9, "v_mov_b32_e32 v9, s23") |
| 388 | flate(p, 0, 0, 0, 20, 0, 8, 0, 127, 5, "global_load_dword v5, v[8:9], off") |
| 389 | sop2e(p, SOP2_LSHL, 20, 130, 22, "s_lshl_b32 s22, s20, 2") |
| 390 | sop2e(p, SOP2_ADD, 14, 22, 22, "s_add_u32 s22, s14, s22") |
| 391 | sop2e(p, SOP2_ADDC, 15, INLINE0, 23, "s_addc_u32 s23, s15, 0") |
| 392 | v1e(p, 22, 1, 8, "v_mov_b32_e32 v8, s22") |
| 393 | v1e(p, 23, 1, 9, "v_mov_b32_e32 v9, s23") |
| 394 | flate(p, 0, 0, 0, 20, 0, 8, 0, 127, 6, "global_load_dword v6, v[8:9], off") |
| 395 | wait(p, 0x0F70, "s_waitcnt vmcnt(0)") |
| 396 | vope(p, 260, 5, 4, 1, "v_add_f32_e32 v4, v4, v5") |
| 397 | vope(p, 16, 4, 4, 5, "v_mul_f32_e32 v4, s16, v4") |
| 398 | vope(p, 260, 6, 4, 5, "v_mul_f32_e32 v4, v4, v6") |
| 399 | p.label("nofuse") |
| 400 | sop2e(p, SOP2_AND, 19, 132, 22, "s_and_b32 s22, s19, 4") |
| 401 | sopce(p, 6, 22, INLINE0, "s_cmp_eq_u32 s22, 0") |
| 402 | p.branch(5, "store32", "s_cbranch_scc1") |
| 403 | vope(p, 144, 4, 5, 16, "v_lshrrev_b32_e32 v5, 16, v4") |
| 404 | vope(p, 129, 5, 6, 0x13, "v_and_b32_e32 v6, 1, v5") |
| 405 | vope(p, 255, 6, 6, 0x34, "v_add_u32_e32 v6, 0x7fff, v6", 0x7FFF) |
| 406 | vope(p, 260, 6, 6, 0x34, "v_add_u32_e32 v6, v4, v6") |
| 407 | vope(p, 144, 6, 5, 16, "v_lshrrev_b32_e32 v5, 16, v6") |
| 408 | v3e(p, 106, 202, 256, INLINE0, 0, "v_cmp_eq_u32_e64 vcc, v0, 0") |
| 409 | sop1e(p, 0x20, 106, 26, "s_and_saveexec_b64 s[26:27], vcc") |
| 410 | sop2e(p, SOP2_LSHL, 20, 129, 22, "s_lshl_b32 s22, s20, 1") |
| 411 | sop2e(p, SOP2_ADD, 10, 22, 22, "s_add_u32 s22, s10, s22") |
| 412 | sop2e(p, SOP2_ADDC, 11, INLINE0, 23, "s_addc_u32 s23, s11, 0") |
| 413 | v1e(p, 22, 1, 8, "v_mov_b32_e32 v8, s22") |
| 414 | v1e(p, 23, 1, 9, "v_mov_b32_e32 v9, s23") |
| 415 | flate(p, 0, 0, 0, 26, 0, 8, 5, 127, 0, "global_store_short v[8:9], v5, off") |
| 416 | sop1e(p, 1, 26, 126, "s_mov_b64 exec, s[26:27]") |
| 417 | p.branch(2, "done", "s_branch") |
| 418 | p.label("store32") |
| 419 | v3e(p, 106, 202, 256, INLINE0, 0, "v_cmp_eq_u32_e64 vcc, v0, 0") |
| 420 | sop1e(p, 0x20, 106, 26, "s_and_saveexec_b64 s[26:27], vcc") |
| 421 | sop2e(p, SOP2_LSHL, 20, 130, 22, "s_lshl_b32 s22, s20, 2") |
| 422 | sop2e(p, SOP2_ADD, 10, 22, 22, "s_add_u32 s22, s10, s22") |
| 423 | sop2e(p, SOP2_ADDC, 11, INLINE0, 23, "s_addc_u32 s23, s11, 0") |
| 424 | v1e(p, 22, 1, 8, "v_mov_b32_e32 v8, s22") |
| 425 | v1e(p, 23, 1, 9, "v_mov_b32_e32 v9, s23") |
| 426 | flate(p, 0, 0, 0, 28, 0, 8, 4, 127, 0, "global_store_dword v[8:9], v4, off") |
| 427 | sop1e(p, 1, 26, 126, "s_mov_b64 exec, s[26:27]") |
| 428 | p.label("done") |
| 429 | p.add(sopp(1, 0), "s_endpgm", "isa", "I 4 0 1") |
| 430 | return p |
| 431 | |
| 432 | |
| 433 | def build_probe(): |
| 434 | p = Prog() |
| 435 | combos = [(0, 0, 0), (1, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 1), (1, 1, 0), (0, 1, 1), (1, 1, 1)] |
| 436 | for sc0, nt, sc1 in combos: |
| 437 | mods = " ".join(x for x, b in (("sc0", sc0), ("nt", nt), ("sc1", sc1)) if b) |
| 438 | flate( |
| 439 | p, 0, sc0, nt, 23, sc1, 0, 0, 127, 0, |
| 440 | "global_load_dwordx4 v[0:3], v[0:1], off" + ((" " + mods) if mods else ""), |
| 441 | ) |
| 442 | flate(p, 0, 0, 0, 23, 0, 0, 0, 0, 0, "global_load_dwordx4 v[0:3], v0, s[0:1]") |
| 443 | flate(p, 0, 0, 1, 23, 0, 0, 0, 0, 0, "global_load_dwordx4 v[0:3], v0, s[0:1] nt") |
| 444 | flate(p, 16, 0, 0, 23, 0, 1, 0, 2, 4, "global_load_dwordx4 v[4:7], v1, s[2:3] offset:16") |
| 445 | flate(p, 16, 0, 1, 23, 0, 1, 0, 2, 4, "global_load_dwordx4 v[4:7], v1, s[2:3] offset:16 nt") |
| 446 | for sc0, nt, sc1 in combos: |
| 447 | mods = " ".join(x for x, b in (("sc0", sc0), ("nt", nt), ("sc1", sc1)) if b) |
| 448 | mube( |
| 449 | p, 23, 1, sc0, sc1, nt, 0, 0, 0, 0x80, |
| 450 | "buffer_load_dwordx4 v[0:3], v0, s[0:3], 0 offen" + ((" " + mods) if mods else ""), |
| 451 | ) |
| 452 | for sc0, sc1 in ((0, 0), (1, 0), (0, 1), (1, 1)): |
| 453 | mods = " ".join(x for x, b in (("sc0", sc0), ("sc1", sc1)) if b) |
| 454 | mube(p, 40, 0, sc0, sc1, 0, 0, 0, 0, 0, "buffer_wbl2" + ((" " + mods) if mods else "")) |
| 455 | mube(p, 41, 0, sc0, sc1, 0, 0, 0, 0, 0, "buffer_inv" + ((" " + mods) if mods else "")) |
| 456 | for sc0, nt, sc1 in ((1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)): |
| 457 | mods = " ".join(x for x, b in (("sc0", sc0), ("nt", nt), ("sc1", sc1)) if b) |
| 458 | flate(p, 0, sc0, nt, 66, sc1, 0, 2, 127, 0, "global_atomic_add v0, v[0:1], v2, off " + mods) |
| 459 | for nt, sc1 in ((0, 0), (0, 1), (1, 0), (1, 1)): |
| 460 | mods = " ".join(x for x, b in (("nt", nt), ("sc1", sc1)) if b) |
| 461 | text = "global_atomic_add v4, v2, s[6:7]" + ((" " + mods) if mods else "") |
| 462 | flate(p, 0, 0, nt, 66, sc1, 4, 2, 6, 0, text) |
| 463 | p.add(sopp(1, 0), "s_endpgm", "isa", "I 4 0 1") |
| 464 | return p |
| 465 | |
| 466 | |
| 467 | HEAD_KERNEL = """# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 468 | # fp8gemv_stream. Packed by gpu/amd/gfx942/hsaco.om. Bytes must match fp8gemv_stream.s |
| 469 | # assembled by llvm-mc-18 -triple=amdgcn -mcpu=gfx942. |
| 470 | # One wave, one row. Lane L owns columns k+L*16 .. +15. Weight loads are |
| 471 | # global_load_dwordx4 nt=1. Activation loads set no sc/nt bit. A pair of 1024-column |
| 472 | # tiles is issued before either tile's FMAs. Tail columns use the same lane map. |
| 473 | # DPP row_shr 8,4,2,1 with the assembler default row_mask and bank_mask, bound_ctrl clear. |
| 474 | # Then (lane15+lane31)+(lane47+lane63). y = scale[row]*acc, and if flags bit1 is set |
| 475 | # y = (y+residual[row])*inv_rms*gamma[row]. Bit0 remaps the workgroup id, bit2 stores bf16. |
| 476 | K fp8gemv_stream 64 8 0 0 32 52 0 64 64 12 0xaf0106 0x84 8 |
| 477 | A W 0 8 0 |
| 478 | A x 8 8 0 |
| 479 | A scale 16 8 0 |
| 480 | A y 24 8 0 |
| 481 | A residual 32 8 0 |
| 482 | A gamma 40 8 0 |
| 483 | A inv_rms 48 4 1 |
| 484 | A M 52 4 1 |
| 485 | A K 56 4 1 |
| 486 | A flags 60 4 1 |
| 487 | """ |
| 488 | |
| 489 | FMTS = """ |
| 490 | F 1 2 |
| 491 | B 1 0 26 6 0x30 |
| 492 | D 1 0 0 0 6 |
| 493 | D 1 1 0 6 7 |
| 494 | D 1 2 0 16 1 |
| 495 | D 1 3 0 17 1 |
| 496 | D 1 4 0 18 8 |
| 497 | D 1 5 1 0 21 |
| 498 | F 2 1 |
| 499 | B 2 0 30 2 2 |
| 500 | D 2 0 0 0 8 |
| 501 | D 2 1 0 8 8 |
| 502 | D 2 2 0 16 7 |
| 503 | D 2 3 0 23 7 |
| 504 | F 3 1 |
| 505 | B 3 0 23 9 0x17d |
| 506 | D 3 0 0 0 8 |
| 507 | D 3 1 0 8 8 |
| 508 | D 3 2 0 16 7 |
| 509 | F 4 1 |
| 510 | B 4 0 23 9 0x17f |
| 511 | D 4 0 0 0 16 |
| 512 | D 4 1 0 16 7 |
| 513 | F 5 1 |
| 514 | B 5 0 31 1 0 |
| 515 | D 5 0 0 0 9 |
| 516 | D 5 1 0 9 8 |
| 517 | D 5 2 0 17 8 |
| 518 | D 5 3 0 25 6 |
| 519 | F 7 2 |
| 520 | B 7 0 26 6 0x37 |
| 521 | D 7 0 0 0 13 |
| 522 | D 7 1 0 13 1 |
| 523 | D 7 2 0 14 2 |
| 524 | D 7 3 0 16 1 |
| 525 | D 7 4 0 17 1 |
| 526 | D 7 5 0 18 7 |
| 527 | D 7 6 0 25 1 |
| 528 | D 7 7 1 0 8 |
| 529 | D 7 8 1 8 8 |
| 530 | D 7 9 1 16 7 |
| 531 | D 7 10 1 23 1 |
| 532 | D 7 11 1 24 8 |
| 533 | F 8 1 |
| 534 | B 8 0 25 7 0x3f |
| 535 | D 8 0 0 0 9 |
| 536 | D 8 1 0 9 8 |
| 537 | D 8 2 0 17 8 |
| 538 | F 9 2 |
| 539 | B 9 0 26 6 0x34 |
| 540 | D 9 0 0 0 8 |
| 541 | D 9 1 0 16 10 |
| 542 | D 9 2 1 0 9 |
| 543 | D 9 3 1 9 9 |
| 544 | D 9 4 1 18 9 |
| 545 | F 10 2 |
| 546 | B 10 0 31 1 0 |
| 547 | D 10 0 0 0 9 |
| 548 | D 10 1 0 9 8 |
| 549 | D 10 2 0 17 8 |
| 550 | D 10 3 0 25 6 |
| 551 | D 10 4 1 0 8 |
| 552 | D 10 5 1 8 9 |
| 553 | D 10 6 1 24 4 |
| 554 | D 10 7 1 28 4 |
| 555 | F 11 1 |
| 556 | B 11 0 23 9 0x17e |
| 557 | D 11 0 0 0 8 |
| 558 | D 11 1 0 8 8 |
| 559 | D 11 2 0 16 7 |
| 560 | F 12 2 |
| 561 | B 12 0 26 6 0x38 |
| 562 | D 12 0 0 0 12 |
| 563 | D 12 1 0 12 1 |
| 564 | D 12 2 0 13 1 |
| 565 | D 12 3 0 14 1 |
| 566 | D 12 4 0 15 1 |
| 567 | D 12 5 0 17 1 |
| 568 | D 12 6 0 18 7 |
| 569 | D 12 7 1 0 8 |
| 570 | D 12 8 1 8 8 |
| 571 | D 12 9 1 16 5 |
| 572 | D 12 10 1 24 8 |
| 573 | """ |
| 574 | |
| 575 | HEAD_PROBE = """# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 576 | # Every cache-policy encoding the writer emits for this kernel family. |
| 577 | # Field positions are the ones that reproduce llvm-mc-18 -mcpu=gfx942. |
| 578 | K encprobe 8 8 0 0 16 8 0 64 64 2 0xaf0081 0x84 8 |
| 579 | A p 0 8 0 |
| 580 | """ |
| 581 | |
| 582 | |
| 583 | def write_pair(stem, header, prog, asm_header): |
| 584 | raw, isa, asm = prog.layout() |
| 585 | isa_path = os.path.join(ROOT, stem + ".isa") |
| 586 | s_path = os.path.join(ROOT, stem + ".s") |
| 587 | with open(isa_path, "w") as f: |
| 588 | f.write(header) |
| 589 | f.write(FMTS) |
| 590 | f.write("\n".join(isa)) |
| 591 | f.write("\n") |
| 592 | with open(s_path, "w") as f: |
| 593 | f.write(asm_header) |
| 594 | f.write(".text\n") |
| 595 | f.write("\n".join(asm)) |
| 596 | f.write("\n") |
| 597 | return raw |
| 598 | |
| 599 | |
| 600 | def main(): |
| 601 | k = write_pair( |
| 602 | "fp8gemv_stream", |
| 603 | HEAD_KERNEL, |
| 604 | build_kernel(), |
| 605 | "// fp8gemv_stream, gfx942. Same words as fp8gemv_stream.isa.\n", |
| 606 | ) |
| 607 | e = write_pair( |
| 608 | "encprobe", |
| 609 | HEAD_PROBE, |
| 610 | build_probe(), |
| 611 | "// encprobe, gfx942. Same words as encprobe.isa.\n", |
| 612 | ) |
| 613 | print("kernel", len(k), "probe", len(e)) |
| 614 | |
| 615 | |
| 616 | if __name__ == "__main__": |
| 617 | main() |