diff --git a/CHANGELOG.md b/CHANGELOG.md index fc1c5e6..dad58fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ remains unsupported. - Added non-blocking Metal compute submission through `submit_words` and `wait`, with completion-aware resource retirement and a sixteen-dispatch in-flight limit. Synchronous calls keep their existing behavior. The benchmark reports the host submission split and compares synchronous and pipelined batches. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. +### Fixed +- Made the native semantic-review timeout in `scripts/test-gpu.sh` configurable with `XENORA_SEMANTIC_TIMEOUT` and raised its default from 120 to 300 seconds, so cold Apple Silicon runs do not fail while the expanded Metal known-answer suite is still progressing. + ### Added - Added a documented independent acceptance rule for promoting AMD RDNA2 desktop GPU (gfx1030) forms from local CPU and Metal results to `fixture verified`, including clean-commit evidence, reviewer independence, raw-log location, review-log summary, failure handling and status regeneration (#302). - Added the VOP3A (`_e64`) encodings of eleven gfx1030 VOP2 integer forms (`v_mul_{i32_i24,hi_i32_i24,u32_u24,hi_u32_u24}`, `v_{min,max}_{i32,u32}`, `v_xnor_b32`, `v_sub_nc_u32`, `v_subrev_nc_u32`) and of the sixteen Wave32 integer compares (explicit SGPR mask destination, `s_cbranch_vccz` through `vcc_lo`, a translation error for reads of runtime-only SGPR results) as semantic-registry encoding variants, with VGPR sources only. New fixtures `vop3-integer` and `vop3-compare` with IR and MSL goldens, and an independent guide-derived oracle (`tests/validation/tests/vop3_{integer,compare}.rs`) that agrees on both reference interpreters and on native Metal. Part of #215. diff --git a/README.md b/README.md index 55d685f..82da167 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,7 @@ These require a Metal-capable Apple Silicon Mac. scripts/test-gpu.sh ``` -The native test suite enables Metal API and shader validation and tests the supported compute, rendering and texture paths. +The native test suite enables Metal API and shader validation and tests the supported compute, rendering and texture paths. The full semantic-review batch defaults to a 300-second timeout; set `XENORA_SEMANTIC_TIMEOUT` to raise or lower it for a slower or faster host. ### Verify guest execution diff --git a/crates/loader/src/dbt.rs b/crates/loader/src/dbt.rs index 4aba211..8a9fa93 100644 --- a/crates/loader/src/dbt.rs +++ b/crates/loader/src/dbt.rs @@ -2267,4 +2267,207 @@ mod tests { ExitReason::MemoryFault(MemoryError::UnmappedAddress(FS_BASE + 8)) ); } + + /// Intel SDM Volume 1, section 3.4.3.1: for `SUB` and `CMP`, `CF` is the + /// borrow out of the most significant bit. `JB` is `CF = 1`, `JAE` is + /// `CF = 0`, `JBE` is `CF = 1 or ZF = 1` and `JA` is `CF = 0 and ZF = 0`. + /// + /// ARM64 reports the inverse: `SUBS` sets `C` when the subtraction does + /// *not* borrow, and its `HI`/`LS` conditions are defined on that same + /// inverted carry. These four x86 conditions therefore cannot be taken + /// from an ARM64 condition code without translating the borrow first, + /// which makes them the ones to pin at the boundaries. + /// One unsigned condition: the low nibble pair as encoded in the `0x70` + /// block, and the rule that decides it from CF and ZF. + type UnsignedCondition = (u8, fn(bool, bool) -> bool); + + const UNSIGNED_CONDITIONS: [UnsignedCondition; 4] = [ + (0x72, |cf, _zf| cf), // JB, JC, JNAE + (0x73, |cf, _zf| !cf), // JAE, JNB, JNC + (0x76, |cf, zf| cf || zf), // JBE, JNA + (0x77, |cf, zf| !cf && !zf), // JA, JNBE + ]; + + /// Build ` eax, ebx` (or `rax, rbx`) followed by the short or near form + /// of `condition`. The not-taken path writes 1 to `ECX` and halts, the + /// taken path writes 2. A near branch targets offset 140, which a `disp8` + /// cannot reach, so the two encodings are exercised separately. + fn unsigned_branch_program(op: [u8; 2], condition: u8, wide: bool, near: bool) -> Vec { + let mut code = Vec::new(); + if wide { + code.push(0x48); // REX.W + } + code.extend_from_slice(&op); // cmp/sub eax, ebx + let jcc = code.len(); + if near { + // The near form is `0F 8x`, where x is the low nibble of the + // short form's opcode. + code.extend_from_slice(&[0x0F, 0x80 | (condition & 0x0F), 0, 0, 0, 0]); + } else { + code.extend_from_slice(&[condition, 0]); + } + code.extend_from_slice(&[0xB9, 1, 0, 0, 0]); // mov ecx, 1 + code.push(0xF4); // hlt + let target = if near { 140 } else { code.len() }; + while code.len() < target { + code.push(0x90); // nop + } + code.extend_from_slice(&[0xB9, 2, 0, 0, 0]); // mov ecx, 2 + code.push(0xF4); // hlt + let next = jcc + if near { 6 } else { 2 }; + let displacement = target as i64 - next as i64; + if near { + code[jcc + 2..jcc + 6].copy_from_slice(&(displacement as i32).to_le_bytes()); + } else { + code[jcc + 1] = displacement as i8 as u8; + } + code + } + + /// Run one branch program and return `(ecx, cf, zf)`. + fn run_unsigned_branch(code: &[u8], left: u64, right: u64) -> (u64, bool, bool) { + let (mut engine, mut cpu, mut memory) = fixture(code); + cpu.set(Register::Rax, left); + cpu.set(Register::Rbx, right); + let result = engine.run(&mut cpu, &mut memory, 32); + assert_eq!(result.exit_reason, ExitReason::Halt); + // Either the two-instruction taken path or the two-instruction + // not-taken path runs after the compare and the branch. + assert_eq!(result.instructions_executed, 4); + // The compare, the branch and the halting move all run as translated + // ARM64, not through the shared interpreter. + assert_eq!(engine.metrics.interpreter_fallbacks, 0); + (cpu.get(Register::Rcx), cpu.flags.cf, cpu.flags.zf) + } + + /// The four unsigned conditions, at both ends of the 32-bit and 64-bit + /// ranges, in the short and the near encoding. Expected flags and + /// outcomes come from the SDM rules above, not from the lowering. + #[test] + fn unsigned_branch_conditions_follow_the_borrow_at_every_boundary() { + const CMP: [u8; 2] = [0x39, 0xD8]; + let cases: &[(bool, u64, u64)] = &[ + // 32-bit: zero, one, equal operands, both ends of the range, and + // the pair where the sign bit flips but the unsigned order holds. + (false, 0, 0), + (false, 0, 1), + (false, 1, 0), + (false, 0xFFFF_FFFF, 0xFFFF_FFFF), + (false, 0xFFFF_FFFF, 0), + (false, 0, 0xFFFF_FFFF), + (false, 0x7FFF_FFFF, 0x8000_0000), + (false, 0x8000_0000, 0x7FFF_FFFF), + // The same boundaries at 64 bits, plus a subtraction that clears + // the sign bit without borrowing. + (true, 0, 0), + (true, 0, 1), + (true, 1, 0), + (true, u64::MAX, u64::MAX), + (true, u64::MAX, 0), + (true, 0, u64::MAX), + (true, 0x7FFF_FFFF_FFFF_FFFF, 0x8000_0000_0000_0000), + (true, 0x8000_0000_0000_0000, 0x7FFF_FFFF_FFFF_FFFF), + (true, 0x8000_0000_0000_0000, 1), + ]; + + for &(wide, left, right) in cases { + let mask = if wide { u64::MAX } else { u64::from(u32::MAX) }; + let (left, right) = (left & mask, right & mask); + // SDM: CF is the borrow out of the most significant bit, ZF is a + // zero result. Both follow from the operands alone. + let (cf, zf) = (left < right, left == right); + for &(condition, holds) in &UNSIGNED_CONDITIONS { + let taken = holds(cf, zf); + for &near in &[false, true] { + let code = unsigned_branch_program(CMP, condition, wide, near); + let (ecx, got_cf, got_zf) = run_unsigned_branch(&code, left, right); + let case = format!( + "{condition:#04x} {:#x} {:#x} {}", + left, + right, + if near { "near" } else { "short" } + ); + assert_eq!(got_cf, cf, "{case}: CF"); + assert_eq!(got_zf, zf, "{case}: ZF"); + assert_eq!(ecx, if taken { 2 } else { 1 }, "{case}: branch outcome"); + } + } + } + } + + /// `SUB` is the other producer of the carry these conditions read, and the + /// one where the ARM64 carry is inverted: `0 - 1` borrows, while + /// `0x8000_0000_0000_0000 - 1` does not borrow even though it clears the + /// sign bit. + #[test] + fn subtract_borrow_into_and_out_of_the_high_bit_feeds_the_unsigned_branches() { + const SUB: [u8; 2] = [0x29, 0xD8]; + let cases: &[(bool, u64, u64)] = &[ + (false, 0, 1), + (false, 0x8000_0000, 1), + (true, 0, 1), + (true, 0x8000_0000_0000_0000, 1), + ]; + + for &(wide, left, right) in cases { + let mask = if wide { u64::MAX } else { u64::from(u32::MAX) }; + let (left, right) = (left & mask, right & mask); + let (cf, zf) = (left < right, left == right); + for &(condition, holds) in &UNSIGNED_CONDITIONS { + let taken = holds(cf, zf); + for &near in &[false, true] { + let code = unsigned_branch_program(SUB, condition, wide, near); + let (ecx, got_cf, got_zf) = run_unsigned_branch(&code, left, right); + let case = format!( + "{condition:#04x} {:#x} {:#x} {}", + left, + right, + if near { "near" } else { "short" } + ); + assert_eq!(got_cf, cf, "{case}: CF"); + assert_eq!(got_zf, zf, "{case}: ZF"); + assert_eq!(ecx, if taken { 2 } else { 1 }, "{case}: branch outcome"); + } + } + } + } + + /// The carry these conditions read is produced in one translated block and + /// consumed in the next. A lowering that left it in the host's condition + /// flags would pass the single-block cases above and fail here. + #[test] + fn unsigned_branch_reads_flags_stored_by_a_previous_block() { + const CMP: [u8; 2] = [0x39, 0xD8]; + let cases: &[(bool, u64, u64)] = &[ + (false, 0, 0), // CF = 0, ZF = 1 + (false, 0, 1), // CF = 1, ZF = 0 + (false, 0xFFFF_FFFF, 0), // CF = 0, ZF = 0 + (true, 0x8000_0000_0000_0000, 1), // CF = 0 although the sign bit clears + ]; + + for &(wide, left, right) in cases { + let (cf, zf) = (left < right, left == right); + for &(condition, holds) in &UNSIGNED_CONDITIONS { + let taken = holds(cf, zf); + let code = unsigned_branch_program(CMP, condition, wide, false); + let (mut engine, mut cpu, mut memory) = fixture(&code); + cpu.set(Register::Rax, left); + cpu.set(Register::Rbx, right); + + // One instruction per run: the compare is translated as a + // one-instruction block, so the branch is compiled separately + // and can only read the flags the first block stored. + let first = engine.run(&mut cpu, &mut memory, 1); + assert_eq!(first.exit_reason, ExitReason::StepLimitExceeded); + assert_eq!(cpu.rip, ENTRY + if wide { 3 } else { 2 }); + assert_eq!((cpu.flags.cf, cpu.flags.zf), (cf, zf)); + + let second = engine.run(&mut cpu, &mut memory, 8); + assert_eq!(second.exit_reason, ExitReason::Halt); + assert_eq!(second.instructions_executed, 3); + assert_eq!(cpu.get(Register::Rcx), if taken { 2 } else { 1 }); + assert_eq!(engine.metrics.interpreter_fallbacks, 0); + } + } + } } diff --git a/scripts/test-gpu.sh b/scripts/test-gpu.sh index d7be36b..27a4f27 100755 --- a/scripts/test-gpu.sh +++ b/scripts/test-gpu.sh @@ -8,6 +8,10 @@ export MTL_SHADER_VALIDATION=1 export MTL_SHADER_VALIDATION_REPORT_TO_STDERR=1 export MTL_SHADER_VALIDATION_ABORT_ON_FAULT=1 export CARGO_BUILD_JOBS=${CARGO_BUILD_JOBS:-4} +# The full semantic-review batch is much larger than the individual GPU tests. +# Cold Apple Silicon runs can exceed two minutes; keep the default bounded but +# allow slower hosts to raise it without editing the script. +export XENORA_SEMANTIC_TIMEOUT=${XENORA_SEMANTIC_TIMEOUT:-300s} cargo test --locked -p xenora-metal --features gpu-tests --test native_compute --no-run timeout 30s cargo test --locked -p xenora-metal --features gpu-tests --test native_compute -- \ @@ -58,4 +62,4 @@ timeout 30s cargo test --locked -p xenora-ffi --features gpu-tests --test audio_ # Native Metal known answers of the independent semantic review, including SOP2 scalar ALU # and VOPC compares. cargo test --locked -p xenora-semantic-review --no-run -timeout 120s cargo test --locked -p xenora-semantic-review -- --ignored --nocapture --test-threads=1 +timeout "$XENORA_SEMANTIC_TIMEOUT" cargo test --locked -p xenora-semantic-review -- --ignored --nocapture --test-threads=1