From 8b247b0f1068fcca07b0752a46ffea160f83b46d Mon Sep 17 00:00:00 2001 From: Thierry Date: Tue, 24 Nov 2026 11:27:14 -0300 Subject: [PATCH] Recompiler: 16-bit compares read their low halves whatever op_sel says (#1293) On RDNA2, v_cmp_* / v_cmpx_* over f16, u16 and i16 take both sources from the low 16 bits and ignore op_sel. This was measured on gfx1036 in Marvisak/VIctory#833, with v_add_nc_u16 under the same bits honouring them as the control. vopc selected the high half for each set bit, so op_sel:[1,0] on 0x4000_3C00 < 0x3C00_3E00 compared 2.0 < 1.5 instead of 1.0 < 1.5. v_cmp_class_f16 was not measured and keeps op_sel. gpu_cmp_16_ignores_op_sel runs one f16, u16 and i16 compare each, with op_sel bits set, whose high halves give the opposite answer. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 4 +++- crates/xcl/src/gpu/recomp/tests.rs | 31 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..fc2530a0bf 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3598,7 +3598,9 @@ impl Em { return Err("VOPC"); } let (mut ua, mut ub) = (u[0].clone(), u[1].clone()); - let half = |i: u64, s: &str| if m.opsel >> i & 1 == 1 { format!("(({s}) >> 16)") } else { format!("(({s}) & 0xFFFFu)") }; + // RDNA2's 16-bit compares read both sources from the low halves whatever op_sel says + // (measured on gfx1036, #1293); class was not measured and keeps op_sel + let half = |i: u64, s: &str| if op == "class" && m.opsel >> i & 1 == 1 { format!("(({s}) >> 16)") } else { format!("(({s}) & 0xFFFFu)") }; let ty = match ty { "f16" => { // compare in f32: exact for every f16 value (class keeps the f16 view) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 5a8d735396..3ea86adbbe 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7168,6 +7168,37 @@ fn gpu_cmp_class_f16_preserves_snan() { } } +/// 16-bit compares take both sources from the low halves: RDNA2 ignores op_sel on them (#1293). +#[test] +fn gpu_cmp_16_ignores_op_sel() { + for wave64 in gpu_waves() { + let p = prog(&[ + // low halves 1.0 < 1.5 (the high ones, 2.0 < 1.0, would be false) + ("V_CMP_LT_F16", "ENC_VOP3", &[("VDST", 0), ("SRC0", V + 0), ("SRC1", V + 1), ("OP_SEL", 0b01)], None), + ("V_CMP_LT_F16", "ENC_VOP3", &[("VDST", 2), ("SRC0", V + 0), ("SRC1", V + 1), ("OP_SEL", 0b10)], None), + // low halves 5 > 2 (high: 1 > 9) + ("V_CMP_GT_U16", "ENC_VOP3", &[("VDST", 4), ("SRC0", V + 2), ("SRC1", V + 3), ("OP_SEL", 0b11)], None), + // low halves -1 < 0 (high: 32767 < -32768) + ("V_CMP_LT_I16", "ENC_VOP3", &[("VDST", 6), ("SRC0", V + 4), ("SRC1", V + 5), ("OP_SEL", 0b01)], None), + ]); + let mut st = KernelState::default(); + for r in st.vgpr.iter_mut() { + r[0] = 0x4000_3C00; // f16 2.0 : 1.0 + r[1] = 0x3C00_3E00; // f16 1.0 : 1.5 + r[2] = 0x0001_0005; + r[3] = 0x0009_0002; + r[4] = 0x7FFF_FFFF; + r[5] = 0x8000_0000; + } + let (r, lanes) = run_wave(&p, &st, wave64); + let all = (u32::MAX, if wave64 { u32::MAX } else { 0 }); + assert_eq!((r.sgpr[0], r.sgpr[1]), all, "wave{lanes} v_cmp_lt_f16 op_sel:[1,0]"); + assert_eq!((r.sgpr[2], r.sgpr[3]), all, "wave{lanes} v_cmp_lt_f16 op_sel:[0,1]"); + assert_eq!((r.sgpr[4], r.sgpr[5]), all, "wave{lanes} v_cmp_gt_u16 op_sel:[1,1]"); + assert_eq!((r.sgpr[6], r.sgpr[7]), all, "wave{lanes} v_cmp_lt_i16 op_sel:[1,0]"); + } +} + #[test] fn wave32_src_vccz_execz_and_saveexec_b32_hlsl() { let w32_code = prog(&[