Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_bw_hazard.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_bw_hazard.c` 374 lines · 18206 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_bw_hazard.c: static scoreboard hazard checker over encoded sm_121 kernel bytes (omega-mv#554). See the header. 3 * 4 * The decoder, the walk and the fixed point are first-party Omega: om/codegen/bw_hazard.om, lowered by om/units.sh 5 * into src/cudapp_om_bw_hazard.c (export omega_hz_chk) and src/cudapp_om_bw_hazard_dec.c (omega_hz_dec). This file 6 * owns the allocation, the report layout and the name strings. The C below the CUDAPP_MIGRATION_PENDING_C_HAZARD 7 * switch (default OFF; always on freestanding) is the differential oracle tests/regalloc_om/run.sh builds. 8 * 9 * Every form decoded below is a form src/omega_blackwell_codegen.c (encode_single_insn) or the post-encode patch 10 * omega_qmma_patch_code writes. The operand positions are the encoder's: Rd = word 0 bits 16..23, 11 * Ra = word 0 bits 24..31, Rb = word 1 bits 0..7, Rc = word 2 bits 0..7. tests/bw_hazard_test.c checks the decoder 12 * against the encoder for every IR op and against nvdisasm on every kernel. */ 13 #include "omega_bw_hazard.h" 14 15 #include 16 #include 17 18 const char *omega_bw_hazard_kind(uint8_t kind) { 19 return kind == OMEGA_BW_HZ_RAW ? "RAW" : kind == OMEGA_BW_HZ_WAR ? "WAR" : kind == OMEGA_BW_HZ_WAW ? "WAW" : "?"; 20 } 21 22 #if defined(OMEGA_GPU_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_HAZARD) 23 #define BW_HZ_C_PRIMARY 1 24 #endif 25 26 #ifndef BW_HZ_C_PRIMARY 27 #include "cudapp_native.h" 28 29 /* om/codegen/bw_hazard.om name ids 1..37; 0 = no name (an unknown opcode) */ 30 static const char *const g_bw_hz_names[38] = { 31 NULL, "NOP", "BAR", "MEMBAR", "CCTL", "EXIT", "BRA", "BSSY", "BSYNC", "MOV", "S2R", "LDC", "LDCU", "IMAD", 32 "IMAD.WIDE", "IMAD.HI", "ISETP", "IADD3", "HMMA", "QMMA", "SHF", "LOP3", "FADD", "FMUL", "FFMA", "FSETP", "FSEL", 33 "FMNMX", "F2FP", "I2FP", "F2I", "MUFU", "SHFL", "LDG", "STG", "ATOMG", "LDS", "STS"}; 34 35 static uint8_t hz_operand(OmegaBwOperand *o, uint64_t packed) { 36 o->reg = (uint8_t)packed; 37 o->n = (uint8_t)(packed >> 8); 38 o->uniform = (uint8_t)(packed >> 16 & 1u); 39 return 0; 40 } 41 42 /* The Omega record is written even on a refusal, with the fields the C decoder had set when it refused. */ 43 static void hz_unpack(const uint64_t *rec, OmegaBwInsn *d) { 44 memset(d, 0, sizeof *d); 45 d->op = (uint16_t)rec[0]; 46 d->guard = (uint8_t)rec[1]; 47 d->stall = (uint8_t)rec[2]; 48 d->yield = (uint8_t)rec[3]; 49 d->wbar = (uint8_t)rec[4]; 50 d->rbar = (uint8_t)rec[5]; 51 d->wait = (uint8_t)rec[6]; 52 d->var = (uint8_t)rec[7]; 53 d->late = (uint8_t)rec[8]; 54 d->nr = (uint8_t)rec[9]; 55 d->nw = (uint8_t)rec[10]; 56 for (unsigned j = 0; j < d->nr && j < 5; ++j) hz_operand(&d->rd[j], rec[11 + j]); 57 if (d->nw) hz_operand(&d->wr[0], rec[16]); 58 d->flow = (uint8_t)rec[17]; 59 d->target = (int32_t)(int64_t)rec[18]; 60 d->name = rec[19] < 38 ? g_bw_hz_names[rec[19]] : NULL; 61 } 62 63 int omega_bw_decode(const uint8_t b[16], OmegaBwInsn *d) { 64 if (!b || !d) return -1; 65 uint64_t rec[24] = {0}, result = UINT64_MAX; 66 if (omega_hz_dec(&result, b, 16, rec, 24) != 0) { 67 memset(d, 0, sizeof *d); 68 return -1; 69 } 70 hz_unpack(rec, d); 71 return result == 0 ? 0 : -1; 72 } 73 74 /* The portable unit takes fixed shapes: code in a zero-padded 32768-byte view (2048 instructions, twice the encoder's 75 * 1024-instruction buffer) and the wire m as 2560 + 1563 * 2048 = `3203584` words (om/codegen/bw_hazard.om header). 76 * Longer code is refused (-1), a documented divergence from the C oracle, which has no length cap. A walk longer 77 * than the unit's bound (999999 pops) traps; the report is then the refusal state (insns, bad_pc -1) and -1. */ 78 #define BW_HZ_CODE 32768u 79 #define BW_HZ_WORDS 3203584u 80 int omega_bw_hazard_check(const uint8_t *code, size_t len, OmegaBwHazardReport *r) { 81 if (!r) return -1; 82 memset(r, 0, sizeof *r); 83 r->bad_pc = -1; 84 if (!code || len == 0 || len % 16u) return -1; 85 r->insns = len / 16u; 86 if (len > BW_HZ_CODE) return -1; 87 uint8_t *c = calloc(BW_HZ_CODE, 1); 88 uint64_t *m = malloc(BW_HZ_WORDS * sizeof *m); 89 if (!c || !m) { 90 free(c); 91 free(m); 92 return -1; 93 } 94 memcpy(c, code, len); 95 uint64_t result = UINT64_MAX; 96 uint32_t st = omega_hz_chk(&result, c, BW_HZ_CODE, len, m, BW_HZ_WORDS); 97 free(c); 98 if (st != 0) { 99 free(m); 100 return -1; 101 } 102 r->insns = (size_t)m[0]; 103 r->reachable = (size_t)m[1]; 104 r->count = (size_t)m[2]; 105 r->bad_pc = (int64_t)m[3]; 106 for (size_t i = 0; i < r->count && i < OMEGA_BW_HAZARD_MAX; ++i) { 107 const uint64_t *f = &m[8 + 3 * i]; 108 r->f[i].pc = (uint32_t)f[0]; 109 r->f[i].src_pc = (uint32_t)f[1]; 110 r->f[i].kind = (uint8_t)f[2]; 111 r->f[i].reg = (uint8_t)(f[2] >> 8); 112 r->f[i].uniform = (uint8_t)(f[2] >> 16 & 1u); 113 r->f[i].release = (uint8_t)(f[2] >> 24); 114 } 115 free(m); 116 return result == 0 ? 0 : -1; 117 } 118 119 #else /* BW_HZ_C_PRIMARY: the C oracle */ 120 121 static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } 122 123 /* registers of a memory access of size field (word 2 bits 9..11): 0..4 one register, 5 a pair (.64), 6 a quad (.128) */ 124 static int size_regs(uint32_t w2) { 125 unsigned s = (w2 >> 9) & 7u; 126 return s <= 4 ? 1 : s == 5 ? 2 : s == 6 ? 4 : -1; 127 } 128 129 static void R(OmegaBwInsn *d, unsigned reg, int n, int uniform) { 130 if (reg == OMEGA_BW_RZ || n <= 0 || d->nr >= 5) return; 131 d->rd[d->nr].reg = (uint8_t)reg; d->rd[d->nr].n = (uint8_t)n; d->rd[d->nr].uniform = (uint8_t)uniform; d->nr++; 132 } 133 static void W(OmegaBwInsn *d, unsigned reg, int n, int uniform) { 134 if (reg == OMEGA_BW_RZ || n <= 0 || d->nw >= 1) return; 135 d->wr[0].reg = (uint8_t)reg; d->wr[0].n = (uint8_t)n; d->wr[0].uniform = (uint8_t)uniform; d->nw = 1; 136 } 137 138 int omega_bw_decode(const uint8_t b[16], OmegaBwInsn *d) { 139 if (!b || !d) return -1; 140 uint32_t w0 = rd32(b), w1 = rd32(b + 4), w2 = rd32(b + 8), w3 = rd32(b + 12); 141 memset(d, 0, sizeof *d); 142 d->op = (uint16_t)(w0 & 0xfffu); 143 d->guard = (uint8_t)((w0 >> 12) & 0xfu); 144 d->stall = (uint8_t)((w3 >> 9) & 0xfu); 145 d->yield = (uint8_t)((w3 >> 13) & 1u); 146 d->wbar = (uint8_t)((w3 >> 14) & 7u); 147 d->rbar = (uint8_t)((w3 >> 17) & 7u); 148 d->wait = (uint8_t)((w3 >> 20) & 0x3fu); 149 unsigned D = (w0 >> 16) & 0xffu, A = w0 >> 24, B = w1 & 0xffu, C = w2 & 0xffu; 150 int n; 151 switch (d->op) { 152 case 0x918: d->name = "NOP"; break; 153 case 0xb1d: d->name = "BAR"; break; 154 case 0x992: d->name = "MEMBAR"; break; 155 case 0x98f: d->name = "CCTL"; R(d, A, 1, 0); d->late = 1; break; 156 case 0x94d: d->name = "EXIT"; d->flow = OMEGA_BW_FLOW_EXIT; break; 157 case 0x947: { 158 d->name = "BRA"; d->flow = OMEGA_BW_FLOW_BRA; 159 int32_t rel = (int32_t)((uint32_t)((int32_t)w1 >> 2) << 6) | (int32_t)((w0 >> 18) & 0x3fu); 160 d->target = rel + 1; 161 break; 162 } 163 case 0x945: d->name = "BSSY"; break; 164 case 0x941: d->name = "BSYNC"; break; 165 case 0x202: d->name = "MOV"; W(d, D, 1, 0); R(d, B, 1, 0); break; 166 case 0x802: d->name = "MOV"; W(d, D, 1, 0); break; 167 case 0x919: d->name = "S2R"; W(d, D, 1, 0); d->var = 1; break; 168 case 0xb82: 169 d->name = "LDC"; if ((n = size_regs(w2)) < 0) return -1; 170 W(d, D, n, 0); R(d, A, 1, 0); d->var = 1; d->late = 1; break; 171 case 0x7ac: 172 d->name = "LDCU"; if ((n = size_regs(w2)) < 0) return -1; 173 W(d, D, n, 1); R(d, A, 1, 1); d->var = 1; d->late = 1; break; 174 case 0x224: d->name = "IMAD"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 175 case 0x824: d->name = "IMAD"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, C, 1, 0); break; 176 case 0xc24: d->name = "IMAD"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 1); R(d, C, 1, 0); break; 177 case 0x825: d->name = "IMAD.WIDE"; W(d, D, 2, 0); R(d, A, 1, 0); R(d, C, 2, 0); break; 178 case 0x227: d->name = "IMAD.HI"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 179 case 0x20c: d->name = "ISETP"; R(d, A, 1, 0); R(d, B, 1, 0); break; 180 case 0x80c: d->name = "ISETP"; R(d, A, 1, 0); break; 181 case 0xc0c: d->name = "ISETP"; R(d, A, 1, 0); R(d, B, 1, 1); break; 182 case 0x210: d->name = "IADD3"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 183 case 0x23c: d->name = "HMMA"; W(d, D, 4, 0); R(d, A, 4, 0); R(d, B, 2, 0); R(d, C, 4, 0); break; 184 case 0x27a: d->name = "QMMA"; W(d, D, 4, 0); R(d, A, 4, 0); R(d, B, 2, 0); R(d, C, 4, 0); break; 185 case 0x819: d->name = "SHF"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, C, 1, 0); break; 186 case 0x219: d->name = "SHF"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 187 case 0x812: d->name = "LOP3"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, C, 1, 0); break; 188 case 0x212: d->name = "LOP3"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 189 case 0x221: d->name = "FADD"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); break; 190 case 0x220: d->name = "FMUL"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); break; 191 case 0x223: d->name = "FFMA"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); R(d, C, 1, 0); break; 192 case 0x20b: d->name = "FSETP"; R(d, A, 1, 0); R(d, B, 1, 0); break; 193 case 0x208: d->name = "FSEL"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); break; 194 case 0x209: d->name = "FMNMX"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); break; 195 case 0x23e: d->name = "F2FP"; W(d, D, 1, 0); R(d, A, 1, 0); R(d, B, 1, 0); break; 196 case 0x245: d->name = "I2FP"; W(d, D, 1, 0); R(d, B, 1, 0); break; 197 case 0x305: d->name = "F2I"; W(d, D, 1, 0); R(d, B, 1, 0); d->var = 1; break; 198 case 0x308: d->name = "MUFU"; W(d, D, 1, 0); R(d, B, 1, 0); d->var = 1; break; 199 case 0xf89: d->name = "SHFL"; W(d, D, 1, 0); R(d, A, 1, 0); d->var = 1; break; 200 case 0x981: /* LDG Rd, desc[URb][Ra.64+imm]: word 2 bit 8 = 64-bit address, bit 27 = descriptor */ 201 d->name = "LDG"; if ((n = size_regs(w2)) < 0) return -1; 202 W(d, D, n, 0); R(d, A, (w2 & 0x100u) ? 2 : 1, 0); if (w2 & 0x08000000u) R(d, B, 1, 1); 203 d->var = 1; d->late = 1; break; 204 case 0x986: /* STG desc[URc][Ra.64+imm], Rb */ 205 d->name = "STG"; if ((n = size_regs(w2)) < 0) return -1; 206 R(d, A, (w2 & 0x100u) ? 2 : 1, 0); R(d, B, n, 0); if (w2 & 0x08000000u) R(d, C, 1, 1); 207 d->late = 1; break; 208 case 0x9a8: /* ATOMG desc[URc][Ra.64], Rb -> Rd: word 2 bits 9..11 = type (0 U32, 1 S32, 2 64, 3 S64; nvdisasm SM121) */ 209 n = ((w2 >> 9) & 7u) == 2u || ((w2 >> 9) & 7u) == 3u ? 2 : 1; 210 d->name = "ATOMG"; W(d, D, n, 0); R(d, A, (w2 & 0x100u) ? 2 : 1, 0); R(d, B, n, 0); 211 if (w2 & 0x08000000u) R(d, C, 1, 1); 212 d->var = 1; d->late = 1; break; 213 case 0x984: /* LDS Rd, [Ra+URb+imm]: word 2 bit 27 = uniform offset */ 214 d->name = "LDS"; if ((n = size_regs(w2)) < 0) return -1; 215 W(d, D, n, 0); R(d, A, 1, 0); if (w2 & 0x08000000u) R(d, B, 1, 1); 216 d->var = 1; d->late = 1; break; 217 case 0x388: case 0x988: /* STS [Ra+URc], Rb */ 218 d->name = "STS"; if ((n = size_regs(w2)) < 0) return -1; 219 R(d, A, 1, 0); R(d, B, n, 0); if (w2 & 0x08000000u) R(d, C, 1, 1); 220 d->late = 1; break; 221 default: 222 return -1; 223 } 224 return 0; 225 } 226 227 /* ---- the walk --------------------------------------------------------------------------------------------------- 228 * State per register: two 64-bit sets of "release masks". An outstanding op is entered as the bit (1 << m), m the 229 * 6-bit set of barriers whose wait releases it (m = 0: the op declared none, nothing releases it). A wait mask W 230 * releases every entry m with m & W != 0, so it clears kill(W). Union over paths is OR. */ 231 #define NREG 512 /* 256 general + 256 uniform */ 232 233 typedef struct { 234 uint64_t w[NREG]; /* pending writes (RAW / WAW) */ 235 uint64_t r[NREG]; /* pending late reads (WAR) */ 236 uint32_t wpc[NREG]; /* last producer / queued reader seen (a hint for the report) */ 237 uint32_t rpc[NREG]; 238 } State; 239 240 static uint64_t kill_mask(unsigned wait) { 241 uint64_t k = 0; 242 for (unsigned m = 1; m < 64; m++) 243 if (m & wait) k |= 1ull << m; 244 return k; 245 } 246 247 static void note(OmegaBwHazardReport *r, uint32_t pc, uint32_t src, uint8_t kind, unsigned idx, uint64_t pending) { 248 unsigned rel = 0; 249 for (unsigned m = 0; m < 64; m++) 250 if (pending >> m & 1u) { rel = m; break; } /* the smallest mask: 0 when an op declared no barrier */ 251 for (size_t i = 0; i < r->count && i < OMEGA_BW_HAZARD_MAX; i++) 252 if (r->f[i].pc == pc && r->f[i].kind == kind && r->f[i].reg == (uint8_t)idx && r->f[i].uniform == (idx >= 256)) 253 return; 254 if (r->count < OMEGA_BW_HAZARD_MAX) { 255 OmegaBwHazard *h = &r->f[r->count]; 256 h->pc = pc; h->src_pc = src; h->kind = kind; h->reg = (uint8_t)idx; h->uniform = (uint8_t)(idx >= 256); h->release = (uint8_t)rel; 257 } 258 r->count++; 259 } 260 261 /* one instruction: IN state s -> OUT state s (in place). rep != NULL records the findings. */ 262 static void step(const OmegaBwInsn *d, uint32_t pc, State *s, OmegaBwHazardReport *rep) { 263 if (d->wait) { 264 uint64_t k = ~kill_mask(d->wait); 265 for (unsigned i = 0; i < NREG; i++) { s->w[i] &= k; s->r[i] &= k; } 266 } 267 /* reads: RAW against pending writes; the first reader is reported and the entry dropped (no cascade) */ 268 for (unsigned j = 0; j < d->nr; j++) 269 for (unsigned q = 0; q < d->rd[j].n; q++) { 270 unsigned i = (d->rd[j].uniform ? 256u : 0u) + ((d->rd[j].reg + q) & 0xffu); 271 if ((i & 0xffu) == OMEGA_BW_RZ) continue; 272 if (s->w[i]) { if (rep) note(rep, pc, s->wpc[i], OMEGA_BW_HZ_RAW, i, s->w[i]); s->w[i] = 0; } 273 } 274 /* writes: WAR against queued late readers, WAW against pending writes */ 275 for (unsigned j = 0; j < d->nw; j++) 276 for (unsigned q = 0; q < d->wr[j].n; q++) { 277 unsigned i = (d->wr[j].uniform ? 256u : 0u) + ((d->wr[j].reg + q) & 0xffu); 278 if ((i & 0xffu) == OMEGA_BW_RZ) continue; 279 if (s->r[i]) { if (rep) note(rep, pc, s->rpc[i], OMEGA_BW_HZ_WAR, i, s->r[i]); s->r[i] = 0; } 280 if (s->w[i]) { if (rep) note(rep, pc, s->wpc[i], OMEGA_BW_HZ_WAW, i, s->w[i]); s->w[i] = 0; } 281 int has_w = d->wbar != OMEGA_BW_NOBAR; 282 if (has_w || d->var) { s->w[i] |= 1ull << (has_w ? (1u << d->wbar) : 0u); s->wpc[i] = pc; } 283 } 284 /* late source reads: held until a wait on the read barrier or on the write barrier (completion) */ 285 if (d->late || d->rbar != OMEGA_BW_NOBAR) { 286 unsigned m = (d->rbar != OMEGA_BW_NOBAR ? 1u << d->rbar : 0u) | (d->wbar != OMEGA_BW_NOBAR ? 1u << d->wbar : 0u); 287 for (unsigned j = 0; j < d->nr; j++) 288 for (unsigned q = 0; q < d->rd[j].n; q++) { 289 unsigned i = (d->rd[j].uniform ? 256u : 0u) + ((d->rd[j].reg + q) & 0xffu); 290 if ((i & 0xffu) == OMEGA_BW_RZ) continue; 291 s->r[i] |= 1ull << m; s->rpc[i] = pc; 292 } 293 } 294 } 295 296 /* OR src into dst; 1 if dst changed */ 297 static int join(State *dst, const State *src) { 298 int ch = 0; 299 for (unsigned i = 0; i < NREG; i++) { 300 uint64_t w = dst->w[i] | src->w[i], r = dst->r[i] | src->r[i]; 301 if (w != dst->w[i]) { dst->w[i] = w; dst->wpc[i] = src->wpc[i]; ch = 1; } 302 if (r != dst->r[i]) { dst->r[i] = r; dst->rpc[i] = src->rpc[i]; ch = 1; } 303 } 304 return ch; 305 } 306 307 int omega_bw_hazard_check(const uint8_t *code, size_t len, OmegaBwHazardReport *r) { 308 if (!r) return -1; 309 memset(r, 0, sizeof *r); 310 r->bad_pc = -1; 311 if (!code || len == 0 || len % 16u) return -1; 312 size_t n = len / 16u; 313 r->insns = n; 314 OmegaBwInsn *ins = calloc(n, sizeof *ins); 315 State *in = calloc(n, sizeof *in); 316 uint8_t *seen = calloc(n, 1), *queued = calloc(n, 1); 317 uint32_t *work = calloc(n, sizeof *work); 318 State *cur = malloc(sizeof *cur); 319 int rc = -1; 320 if (!ins || !in || !seen || !queued || !work || !cur) goto out; 321 322 /* decode what is reachable from 0 (padding after the final EXIT is never decoded) */ 323 { 324 size_t top = 0; 325 work[top++] = 0; queued[0] = 1; 326 while (top) { 327 uint32_t pc = work[--top]; 328 if (omega_bw_decode(code + (size_t)pc * 16u, &ins[pc]) != 0) { r->bad_pc = pc; goto out; } 329 seen[pc] = 1; 330 const OmegaBwInsn *d = &ins[pc]; 331 int64_t succ[2]; int ns = 0; 332 if (d->flow == OMEGA_BW_FLOW_NEXT || d->guard != 7u) succ[ns++] = (int64_t)pc + 1; 333 if (d->flow == OMEGA_BW_FLOW_BRA) succ[ns++] = (int64_t)pc + d->target; 334 for (int k = 0; k < ns; k++) { 335 if (succ[k] < 0 || succ[k] >= (int64_t)n) { r->bad_pc = pc; goto out; } 336 if (!queued[succ[k]]) { queued[succ[k]] = 1; work[top++] = (uint32_t)succ[k]; } 337 } 338 } 339 } 340 for (size_t i = 0; i < n; i++) r->reachable += seen[i]; 341 342 /* fixed point: IN states only grow (OR), the lattice is finite. Every reachable instruction is stepped at least 343 * once (seen is reused as "not yet stepped"), later only when its IN state grew. */ 344 memset(queued, 0, n); 345 { 346 size_t top = 0; 347 work[top++] = 0; queued[0] = 1; 348 while (top) { 349 uint32_t pc = work[--top]; 350 queued[pc] = 0; seen[pc] = 2; 351 *cur = in[pc]; 352 step(&ins[pc], pc, cur, NULL); 353 const OmegaBwInsn *d = &ins[pc]; 354 int64_t succ[2]; int ns = 0; 355 if (d->flow == OMEGA_BW_FLOW_NEXT || d->guard != 7u) succ[ns++] = (int64_t)pc + 1; 356 if (d->flow == OMEGA_BW_FLOW_BRA) succ[ns++] = (int64_t)pc + d->target; 357 for (int k = 0; k < ns; k++) { 358 uint32_t t = (uint32_t)succ[k]; 359 if ((join(&in[t], cur) || seen[t] == 1) && !queued[t]) { queued[t] = 1; work[top++] = t; } 360 } 361 } 362 } 363 /* record: one pass over the reachable instructions with their fixed-point IN states */ 364 for (size_t pc = 0; pc < n; pc++) { 365 if (!seen[pc]) continue; 366 *cur = in[pc]; 367 step(&ins[pc], (uint32_t)pc, cur, r); 368 } 369 rc = 0; 370 out: 371 free(ins); free(in); free(seen); free(queued); free(work); free(cur); 372 return rc; 373 } 374 #endif /* BW_HZ_C_PRIMARY */