Repository navigation
fix(hipblaslt): wait for LDS operands in handwritten gfx950 final loops #12676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
newling
merged 3 commits into
develop
from
users/newling/fix-gfx950-custom-final-loop-wait
Oct 7, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_CustomKernels_final_loop_wait.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Copyright Advanced Micro Devices, Inc., or its affiliates. | ||
| # SPDX-License-Identifier: MIT | ||
| """Check LDS operand readiness on the handwritten gfx950 optimized final path.""" | ||
|
|
||
| from collections import deque | ||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
| from Tensile.CustomKernels import getCustomKernelContents | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
| KERNELS = [ | ||
| f"Custom_Cijk_Alik_Bljk_{dtype}_BH_MT256x256x64_MI16x16x1_UserArgs_shortname{variant}_gfx950" | ||
| for dtype, variant in [("BBS", 0), ("BBS", 1), ("HHS", 0)] | ||
| ] | ||
|
|
||
|
|
||
| def _pending_final_loop_operands(source): | ||
| """Follow the beta=0, alpha=1, GSU=1, full-tile fallthrough path. | ||
|
|
||
| Start at the preceding LDS drain, track the subsequent reads, and check | ||
| their MFMA consumers across the final-loop boundary. These kernels use | ||
| only ds_read_b128 for LGKM operations in this interval. FIFO retirement | ||
| accounts for gfx950's 15 outstanding LGKM operations as well as waits. | ||
| This is a focused dependency check, not a general assembly interpreter. | ||
| """ | ||
| definitions = dict(re.findall(r"^\.set\s+(\w+),\s*([^/\n]+)", source, re.M)) | ||
|
|
||
| def value(expression): | ||
| # Register expressions in these kernels are sums of constants and | ||
| # .set symbols. Fail on unsupported syntax instead of evaluating code. | ||
| terms = expression.strip().split("+") | ||
| result = 0 | ||
| for term in terms: | ||
| term = term.strip() | ||
| if re.fullmatch(r"0x[0-9a-fA-F]+|[0-9]+", term): | ||
| result += int(term, 0) | ||
| else: | ||
| assert re.fullmatch(r"\w+", term), term | ||
| result += value(definitions[term]) | ||
| return result | ||
|
|
||
| def registers(operand): | ||
| bounds = operand.split(":") | ||
| first, last = value(bounds[0]), value(bounds[-1]) | ||
| return set(range(first, last + 1)) | ||
|
|
||
| boundary = source.index("label_toPGR1:") | ||
| start = source.rindex("s_waitcnt lgkmcnt(0)", 0, boundary) | ||
| end = source.index("label_OptNLL_End:", boundary) | ||
| # All branches selecting the optimized path fall through. Its final | ||
| # branch exits to the epilogue after the compute instructions checked here. | ||
| lines = source[start:end].splitlines() | ||
| pending = deque() | ||
| violations = [] | ||
| reads = mfmas = 0 | ||
| for line in lines: | ||
| instruction = line.split("//", 1)[0].strip() | ||
| assert not instruction.startswith(("s_load_", "ds_write_")), instruction | ||
| wait = re.match(r"s_waitcnt\s+lgkmcnt\((\d+)\)", instruction) | ||
| if wait: | ||
| while len(pending) > int(wait[1]): | ||
| pending.popleft() | ||
| elif instruction.startswith("ds_read_"): | ||
| assert instruction.startswith("ds_read_b128 "), instruction | ||
| destination = re.match(r"ds_read_b128\s+v\[([^\]]+)\]", instruction) | ||
| assert destination, instruction | ||
| pending.append(registers(destination[1])) | ||
| reads += 1 | ||
| if len(pending) > 15: | ||
| pending.popleft() | ||
| elif instruction.startswith("v_mfma_"): | ||
| operands = re.findall(r"v\[([^\]]+)\]", instruction) | ||
| assert len(operands) == 2, instruction | ||
| used = registers(operands[0]) | registers(operands[1]) | ||
| outstanding = set().union(*pending) | ||
| if used & outstanding: | ||
| violations.append((sorted(used & outstanding), instruction)) | ||
| mfmas += 1 | ||
| assert reads >= 16 and mfmas >= 128, "The expected compute path was not checked" | ||
| return violations | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("kernel", KERNELS) | ||
| def test_optimized_final_loop_consumes_completed_lds_reads(kernel): | ||
| violations = _pending_final_loop_operands(getCustomKernelContents(kernel)) | ||
| assert not violations, f"{kernel}: pending LDS operands at {violations}" |
90 changes: 90 additions & 0 deletions
90
projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_custom_tn_prefetch_waits.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Copyright Advanced Micro Devices, Inc., or its affiliates. | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """Check DirectToLDS completion before the handwritten gfx950 TN local reads. | ||
|
|
||
| The PGR2 waits count the younger second prefetch. On the one-DepthU path, | ||
| that prefetch is skipped, so the first prefetch needs its own completion | ||
| before the barrier makes its LDS writes visible to the local reads. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
| from Tensile.resources import custom_kernel_text | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
| KERNELS = [ | ||
| f"Custom_Cijk_Alik_Bljk_{dtype}_BH_MT256x256x64_MI16x16x1_UserArgs_shortname{variant}_gfx950" | ||
| for dtype, variant in [("BBS", 0), ("BBS", 1), ("HHS", 0)] | ||
| ] | ||
|
|
||
|
|
||
| def _prefetch_hazards(source, loop_count): | ||
| # No VM wait precedes the PGR2 branch: the first A/B prefetch is pending. | ||
| start = source.index("/* prefetch: global -> local */") | ||
| branch = source.index("s_cmp_eq_u32 s[sgprLoopCounterL], 0x1", start) | ||
| prefix = source[start:branch] | ||
| end = source.index("label_openLoopL:", branch) | ||
| code = [line.split("//", 1)[0].strip() for line in source[branch:end].splitlines()] | ||
| labels = {line[:-1]: i for i, line in enumerate(code) if line.endswith(":")} | ||
| pending, issued, completed, visible = [], set(), set(), set() | ||
| prefetch_counts = {"A": 0, "B": 0} | ||
|
|
||
| def issue(instruction): | ||
| match = re.search(r"s\[sgprSrd([AB]):", instruction) | ||
| assert match and "lds" in instruction | ||
| tensor = match.group(1) | ||
| index = prefetch_counts[tensor] | ||
| event = (tensor, index // 8, index % 8) | ||
| prefetch_counts[tensor] += 1 | ||
| pending.append(event) | ||
| issued.add(event) | ||
|
|
||
| for instruction in prefix.splitlines(): | ||
| if instruction.startswith("buffer_load_"): | ||
| issue(instruction) | ||
| assert not re.search(r"s_waitcnt.*vmcnt", instruction) | ||
| assert prefetch_counts == {"A": 8, "B": 8} | ||
| ip, scc, hazards, reads = 0, False, [], 0 | ||
| while ip < len(code): | ||
| instruction = code[ip] | ||
| if instruction.startswith("s_cmp_eq_u32 s[sgprLoopCounterL], 0x1"): | ||
| scc = loop_count == 1 | ||
| elif instruction.startswith("s_cbranch_scc"): | ||
| operation, target = instruction.split() | ||
| if scc == operation.endswith("1"): | ||
| ip = labels[target] | ||
| continue | ||
| elif instruction.startswith("s_branch "): | ||
| ip = labels[instruction.split()[1]] | ||
| continue | ||
| elif instruction.startswith("buffer_load_"): | ||
| issue(instruction) | ||
| elif instruction.startswith("s_waitcnt "): | ||
| match = re.search(r"vmcnt\((\d+)\)", instruction) | ||
| if match: | ||
| threshold = int(match.group(1)) | ||
| retire = max(0, len(pending) - threshold) | ||
| completed.update(pending[:retire]) | ||
| del pending[:retire] | ||
| elif instruction.startswith("s_barrier"): | ||
| visible.update(completed) | ||
| elif instruction.startswith("ds_read_b128 "): | ||
| tensor = re.search(r"v\[vgprLocalReadAddr([AB])\]", instruction).group(1) | ||
| first_prefetch = {event for event in issued if event[0:2] == (tensor, 0)} | ||
| if not first_prefetch <= visible: | ||
| hazards.append((ip, tensor, sorted(first_prefetch - visible))) | ||
| reads += 1 | ||
| ip += 1 | ||
| assert reads == 16 | ||
| assert prefetch_counts == {"A": 8 * min(loop_count, 2), "B": 8 * min(loop_count, 2)} | ||
| return hazards | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("kernel", KERNELS) | ||
| @pytest.mark.parametrize("loop_count", [1, 2]) | ||
| def test_first_prefetch_is_visible_before_local_reads(kernel, loop_count): | ||
| assert not _prefetch_hazards(custom_kernel_text(kernel), loop_count) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.