diff --git a/scripts/audit_shader4_regs.py b/scripts/audit_shader4_regs.py index 50fc5500ec..1d1980045d 100644 --- a/scripts/audit_shader4_regs.py +++ b/scripts/audit_shader4_regs.py @@ -215,10 +215,13 @@ def expand_reg(a): is_dual_dst = op.startswith("v_") and ( "_co_" in op or op.startswith(("v_mad_u64_u32", "v_mad_i64_i32", "v_div_scale_f32", "v_div_scale_f64")) ) + # The assembler's short form v_add_co_ci_u32 vD, src0, src1 (also sub/subrev) has VCC as its carry-out and + # carry-in and names neither: args[1] is src0, read, not a carry-out destination. + is_short_co_ci = op.endswith("_co_ci_u32") and len(args) == 3 # Carry-out / scale-out forms write the VGPR and scalar mask first, even when they also take a carry-in source. if is_lds_load: dsts, srcs = [], args - elif not is_no_dest and is_dual_dst and len(args) >= 2: + elif not is_no_dest and is_dual_dst and len(args) >= 2 and not is_short_co_ci: dsts = args[:2] srcs = args[2:] elif is_no_dest: diff --git a/scripts/vs_input_survey.py b/scripts/vs_input_survey.py index 0c2326309a..dcdead7833 100644 --- a/scripts/vs_input_survey.py +++ b/scripts/vs_input_survey.py @@ -84,7 +84,10 @@ def read_before_write(insns): elif op == "v_swap_b32" and len(args) >= 2: dsts, srcs = args[:2], args[:2] elif (op.startswith("v_") and ("_co_" in op or op in DUAL_DST_VALU) - and len(args) >= 2 and args[1]["k"] in ("sgpr", "special", "slist")): + and len(args) >= 2 and args[1]["k"] in ("sgpr", "special", "slist") + # the short form v_add_co_ci_u32 vD, src0, src1 (also sub/subrev): carry out and in are an + # implicit VCC, and args[1] is src0, a read + and not (op.endswith("_co_ci_u32") and len(args) == 3)): dsts, srcs = args[:2], args[2:] # vdst + scalar carry/scale-out dst else: dst_read = ( diff --git a/tests/test_audit_shader4_regs.py b/tests/test_audit_shader4_regs.py index eee337d7c9..4822b92184 100644 --- a/tests/test_audit_shader4_regs.py +++ b/tests/test_audit_shader4_regs.py @@ -168,6 +168,25 @@ def test_vector_carry_in_and_out_are_distinct_in_the_audit(self) -> None: self.assertEqual(report["rbw_vgpr_counts"], {"v2": 3, "v4": 3}) self.assertEqual(report["s3_rbw_op_patterns"], {}) + def test_short_form_carry_in_reads_s3_in_the_audit(self) -> None: + # The short form v_add_co_ci_u32 vD, src0, src1 has VCC as its carry-out and carry-in: s3 is src0, read + # before any write, so it is reported, and the read does not count as a write of s3. + with tempfile.TemporaryDirectory() as disasm: + for op in ("v_add_co_ci_u32", "v_sub_co_ci_u32", "v_subrev_co_ci_u32"): + path = os.path.join(disasm, f"cs_{op}_short.s") + with open(path, "w", encoding="utf-8") as f: + f.write(f"\t{op}_e32 v1, s3, v2\n" + "\ts_mov_b32 s9, s3\n\ts_endpgm\n") + + report = audit_mod.audit(disasm)["cs"] + self.assertEqual(report["count"], 3) + self.assertEqual(report["rbw_sgpr_counts"], {"s3": 3}) + self.assertEqual(report["rbw_vgpr_counts"], {"v2": 3}) + # s3 is never written, so the s_mov_b32 after it reads s3 before a write too. + self.assertEqual(report["s3_rbw_op_patterns"], { + "v_add_co_ci_u32 D, s3, vN": 1, "v_sub_co_ci_u32 D, s3, vN": 1, "v_subrev_co_ci_u32 D, s3, vN": 1, + "s_mov_b32 D, s3": 3}) + def test_limit_below_one_is_rejected_before_output(self): # --limit 0 used to audit nothing and still print a full zero report with exit 0. with tempfile.TemporaryDirectory() as tmp: diff --git a/tests/test_vs_input_survey_rbw.py b/tests/test_vs_input_survey_rbw.py index eb9ad00b88..2ca139e4bb 100644 --- a/tests/test_vs_input_survey_rbw.py +++ b/tests/test_vs_input_survey_rbw.py @@ -35,6 +35,19 @@ def test_carry_in_carry_out_writes_both_vdst_and_sdst(self): self.assertEqual(read_s, {4, 5}) self.assertEqual(read_v, {0, 2}) + def test_short_form_carry_in_reads_its_scalar_src0(self): + # The assembler's short form v_add_co_ci_u32 vD, src0, src1 has an implicit VCC carry-out and carry-in: + # s3 (src0) is read before any write, not a carry-out destination, so the later read of s3 does not hide it. + for op in ("v_add_co_ci_u32", "v_sub_co_ci_u32", "v_subrev_co_ci_u32"): + with self.subTest(op=op): + insns = parse([ + f"\t{op}_e32 v1, s3, v2", + "\ts_mov_b32 s9, s3", + ]) + read_s, read_v = V.read_before_write(insns) + self.assertEqual(read_s, {3}) + self.assertEqual(read_v, {2}) + def test_dual_dest_valu_ops_write_scalar_carry_or_scale(self): insns = parse([ "\tv_mad_u64_u32 v[0:1], s[2:3], v4, v5, v[6:7]",