#!/usr/bin/env python3 """Offline tests for sm86_assets.py (no nvcc, no GPU): the retarget rules and the installed set's consistency.""" import json import sys import unittest from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parent)) import sm86_assets as S # noqa: E402 SAMPLE = b""".version 9.0 .target sm_120 .address_size 64 .visible .entry k( \t.param .u64 a ) { \t.shared .align 4 .b8 partials[128]; \tgriddepcontrol.wait; \tmin.relu.s32 \t%r210, %r56, %r55; \tmov.u64 %rd7, partials; \tcvta.to.shared.u64 %rd7, %rd7; \tmov.u64 %rd8, other; \tcvta.to.shared.u64 %rd8, %rd8; \tret; } """ class RetargetRules(unittest.TestCase): def test_all_rules_and_nothing_else(self): out, applied = S.retarget(SAMPLE) self.assertEqual({a["rule"]: a["count"] for a in applied}, {"target": 1, "griddep": 1, "shared_cvta": 1, "min_relu": 1}) self.assertIn(b".target sm_86\n", out) self.assertNotIn(b"sm_120", out) self.assertNotIn(b"griddepcontrol", out) self.assertNotIn(b"min.relu", out) self.assertIn(b"\tmin.s32 %r210, %r56, %r55;\n\tmax.s32 %r210, %r210, 0;", out) # the cvta after a mov of a declared .shared symbol is gone, the one after an undeclared symbol stays self.assertNotIn(b"cvta.to.shared.u64 %rd7", out) self.assertIn(b"cvta.to.shared.u64 %rd8, %rd8;", out) def test_alias_in_min_relu_is_refused(self): bad = SAMPLE.replace(b"%r210, %r56, %r55", b"%r56, %r56, %r55") with self.assertRaises(ValueError): S.retarget(bad) def test_needs_exactly_one_target(self): with self.assertRaises(ValueError): S.retarget(b".version 9.0\n.address_size 64\n") with self.assertRaises(ValueError): S.retarget(b".target sm_120\n.target sm_120\n") def test_untouched_input_is_plain_target_swap(self): plain = b".version 8.7\n.target sm_120\n.address_size 64\n.entry x()\n{\nret;\n}\n" out, applied = S.retarget(plain) self.assertEqual(out, plain.replace(b"sm_120", b"sm_86")) self.assertEqual([a["rule"] for a in applied], ["target"]) class InstalledSet(unittest.TestCase): def test_check_passes_on_the_checked_in_set(self): man = S.ROOT / S.OUT_REL / S.MANIFEST_NAME if not man.is_file(): self.skipTest("no companion set installed") self.assertEqual(S.cmd_check(None), 0) def test_masking(self): a = {"_ZN6strata7kernels61_GLOBAL__N__a78fc565_28_x": 3} b = {"_ZN6strata7kernels61_GLOBAL__N__fb68b611_28_x": 3} self.assertEqual(S.mask_entries(a), S.mask_entries(b)) if __name__ == "__main__": unittest.main()