"""The per-basic-block descriptor lookup cache of tools/rdna2spirv.py (standard library only: no GPU, LLVM or SPIRV-Tools). python -I tests/test_desc_cache.py Every image instruction needs the bindless index of its T# (8 SGPRs) and S# (4 SGPRs): a call of %desc_lookup_fn. A second instruction on the same descriptors in the same basic block reuses the first one's result as long as no SGPR of the descriptor was written in between. These tests translate small pixel shaders (the .s text is made here, with encodings of the right length so that branch targets work out) and count the calls in the SPIR-V. """ import gc import os import shutil import struct import subprocess import sys import tempfile import warnings ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.dont_write_bytecode = True sys.path.insert(0, os.path.join(ROOT, "tools")) import rdna2spirv # noqa: E402 LOOKUP_FN = "%desc_lookup_fn" # the probe every image instruction calls def lookup_calls(text): """How many descriptor lookups the module calls. The probe's result type depends on the table entry layout (`--descriptor-table`: a plain module gets the index only, a tagged one index and kind).""" return sum(1 for line in text.splitlines() if "OpFunctionCall" in line and LOOKUP_FN in line) STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("