From c0418dd76226bc192b4acf37d38293f81f1ced71 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:54:38 -0500 Subject: [PATCH] Kernels: pass a fixed compilation-unit id (-cuid) to the HIP device compile device_command in clean/runner/build.py now adds -cuid=wg_ to every HIP device compile (the id is the output file's base name, with characters outside [A-Za-z0-9_] replaced by "_", so it has no path in it). Without -cuid, clang derives the id itself; the id sets the __hip_cuid_... symbol name in the code object, and with a fixed id that name does not depend on the build folder. The gfx1201 and gfx1200 builds of one kernel get the same id. Measured (HIP SDK 7.2, BUILDING.md 3.3 command, code objects only: --extra-archs gfx1200 --no-model-k --no-runtime): - built in two different folders, all 54 code objects are byte-identical; - against release 0.6.0, which was built without -cuid, no file is byte-identical. All 54 have identical .text and .note sections and the same section list. .rodata is identical in 52; in post_w64__orig.co (both targets) it moves by 64 bytes, and its 93 kernel descriptors differ only in their code-entry offset, by that amount. Not measured: DXIL or kernels.json, model K code objects. The code objects were not run. BUILDING.md, "Comparing kernels with a release": builds made with -cuid compare file by file; for 0.6.0 compare the .text and .note sections, not .rodata or the whole file. The module docstring of build.py shows the flag. A CPU unit test, clean/tests/test_build_cuid.py, checks that the command carries -cuid=wg_, the same for any output folder, with no path in it. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- BUILDING.md | 14 ++++++++-- clean/runner/build.py | 10 ++++--- clean/tests/test_build_cuid.py | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 clean/tests/test_build_cuid.py diff --git a/BUILDING.md b/BUILDING.md index 117f1d40..2808d42e 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -176,8 +176,18 @@ repository (the synthetic converter library stand-ins `WGC_SYNTH_BINS`, a model ### Comparing kernels with a release The kernels are deterministic for a given DXC / HIP SDK version: `out\rr-rt\kernels.json` (name, arguments, SHA-256 of -each DXIL), `out\runtime\dx12\manifest.json` (SHA-256 of each DXIL) and the code objects `out\runtime\*.co` can be -compared file by file with another build of the same source. +each DXIL) and `out\runtime\dx12\manifest.json` (SHA-256 of each DXIL) can be compared with another build of the same +source: for `kernels.json`, compare each kernel's `nargs` and `sha256`. + +The code objects `out\runtime\*.co` and `out\runtime\gfx1200\*.co` of builds made with `-cuid` (the compilation-unit +id that `clean\runner\build.py` now fixes) can be compared file by file. In a check of two build folders, all 54 +compiled code objects (gfx1201 and gfx1200) were byte-identical. + +Release 0.6.0 was built without `-cuid`, so none of its code objects is byte-identical to a new build: the +`__hip_cuid_...` symbol differs. Compare the `.text` and `.note` sections instead (extract them with an ELF tool, for +example `llvm-objcopy --dump-section`); they were identical in all 54, as was the section list. The `.rodata` section +was identical in 52. In `post_w64__orig.co` (both targets) it moves by 64 bytes, and its 93 kernel descriptors differ +only in their code-entry offset, by that amount; so do not use `.rodata` as the comparison for 0.6.0. ## 7. GPU steps (optional; not needed to build) diff --git a/clean/runner/build.py b/clean/runner/build.py index 5a498154..9590238e 100644 --- a/clean/runner/build.py +++ b/clean/runner/build.py @@ -11,13 +11,14 @@ 8; the FidelityFX front end loads it through wgGetUpscalerBackend), built with the HIP SDK's clang against the static Microsoft C runtime (/MT). The converter map is copied next to it (the runtime reads it to check model.wgcm). -Each kernel source is compiled with the command given in spec 90: - clang -x hip --offload-arch=gfx1201 --cuda-device-only -nogpuinc -O3 --no-gpu-bundle-output +Each kernel source is compiled with the command given in spec 90, plus a fixed compilation-unit id (device_command): + clang -x hip --offload-arch=gfx1201 --cuda-device-only -nogpuinc -O3 --no-gpu-bundle-output -cuid=wg_ --rocm-path= --rocm-device-lib-path=/amdgcn/bitcode -o .co """ import argparse import json import os +import re import shutil import subprocess import sys @@ -96,8 +97,11 @@ def find_clang(rocm): def device_command(clang, rocm, src, out, device_libs=True, report=False, extra=(), soft_fp8=False, defines=None): + # a fixed compilation-unit id from the output file's name, so the __hip_cuid_... symbol of a code object is the same + # in every build folder (without -cuid, clang derives the id itself) + cuid = "wg_" + re.sub(r"[^A-Za-z0-9_]", "_", os.path.splitext(os.path.basename(out))[0]) cmd = [clang, "-x", "hip", f"--offload-arch={ARCH}", "--cuda-device-only", "-nogpuinc", "-O3", - "--no-gpu-bundle-output"] + "--no-gpu-bundle-output", f"-cuid={cuid}"] if device_libs: cmd += [f"--rocm-path={rocm}", f"--rocm-device-lib-path={os.path.join(rocm, 'amdgcn', 'bitcode')}"] else: diff --git a/clean/tests/test_build_cuid.py b/clean/tests/test_build_cuid.py new file mode 100644 index 00000000..5814ccf8 --- /dev/null +++ b/clean/tests/test_build_cuid.py @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: MIT +"""The HIP device compile command carries a fixed compilation-unit id; nothing is executed, the paths are dummies.""" +import os +import unittest +from clean.runner import build + +CLANG = "dummy-clang" +ROCM = os.path.join("dummy-rocm", "7.2") +SRC = "src.hip" + + +def cuid_flags(cmd): + return [a for a in cmd if a.startswith("-cuid")] + + +class DeviceCommandCuid(unittest.TestCase): + def test_cuid_flag_is_in_the_command(self): + cmd = build.device_command(CLANG, ROCM, SRC, os.path.join("out", "post_w64.co")) + self.assertEqual(cuid_flags(cmd), ["-cuid=wg_post_w64"]) + + def test_cuid_does_not_depend_on_the_output_folder(self): + a = build.device_command(CLANG, ROCM, SRC, os.path.join("one", "folder", "post_w64.co")) + b = build.device_command(CLANG, ROCM, SRC, os.path.join("another", "place", "wg", "post_w64.co")) + self.assertEqual(cuid_flags(a), cuid_flags(b)) + self.assertEqual(len(cuid_flags(a)), 1) + + def test_cuid_has_no_path_in_it(self): + flag = cuid_flags(build.device_command(CLANG, ROCM, SRC, os.path.join("a b", "c-d", "post.w64__orig.co")))[0] + value = flag.split("=", 1)[1] + self.assertTrue(value.startswith("wg_")) + self.assertEqual(value, "wg_post_w64__orig") + self.assertNotIn(os.sep, value) + self.assertNotIn("/", value) + self.assertNotIn(" ", value) + + def test_cuid_follows_the_output_name(self): + a = cuid_flags(build.device_command(CLANG, ROCM, SRC, "swin.co")) + b = cuid_flags(build.device_command(CLANG, ROCM, SRC, "post.co")) + self.assertEqual(a, ["-cuid=wg_swin"]) + self.assertNotEqual(a, b) + + def test_cuid_is_also_in_the_compile_check_command(self): + cmd = build.device_command(CLANG, ROCM, SRC, "swin.co", device_libs=False) + self.assertEqual(cuid_flags(cmd), ["-cuid=wg_swin"]) + self.assertIn("-nogpulib", cmd) + + +if __name__ == "__main__": + unittest.main()