Skip to content

[AMDGPU] Track async events for vm_vsrc simplifications - #211333

Closed
jrbyrnes wants to merge 5 commits into
mainfrom
users/jebyrnes/VsrcTrackAsync
Closed

jrbyrnes wants to merge 5 commits into
mainfrom
users/jebyrnes/VsrcTrackAsync

Conversation

@jrbyrnes

@jrbyrnes jrbyrnes commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

We need to resolve hazard between global_load_async <- vgpr <- lds using wait vm_vsrc. In cases where we omit this, we are seeing correctness issues.

In SIInsertWaitcnts, when simplifying the vm_vsrc, we check to see if an existing s_wait proves that the vm_vsrc is redundant. It does this by comparing the covered events of the existing s_wait with the outstanding events at this position. If the existing s_wait covers all relevant outstanding events, then the wait vm_vsrc is redundant.

However, we previously did not include the async_cnt as a relevant event to check. Thus we may have decided our s_wait covers all relevant events, even if there are outstanding global_load_async.

For example, in the new test, simplifyVmVsrc thinks there are no outstanding events whatsoever. The presence of the wait dscnt triggers the other part of conditional UpdateWait.get(AMDGPU::VM_VSRC) >= CheckCount so we simplify the wait vm_vsrc. By also tracking async_cnt, we now have accurate understanding of outstanding events.

Partially fixes LCOMPILER-2480

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-amdgpu

Author: Jeffrey Byrnes (jrbyrnes)

Changes

We need to resolve hazard between global_load_async &lt;- vgpr &lt;- lds using wait vm_vsrc. In cases where we omit this, we are seeing correctness issues.

In SIInsertWaitcnts, when simplifying the vm_vsrc, we check to see if an existing s_wait proves that the vm_vsrc is redundant. It does this by comparing the covered events of the existing s_wait with the outstanding events at this position. If the existing s_wait covers all relevant outstanding events, then the wait vm_vsrc is redundant.

However, we previously did not include the async_cnt as a relevant event to check. Thus we may have decided our s_wait covers all relevant events, even if there are outstanding global_load_async.

For example, in the new test, simplifyVmVsrc thinks there are no outstanding events whatsoever. The presence of the wait dscnt triggers the other part of conditional UpdateWait.get(AMDGPU::VM_VSRC) &gt;= CheckCount so we simplify the wait vm_vsrc. By also tracking async_cnt, we now have accurate understanding of outstanding events.


Full diff: https://github.com/llvm/llvm-project/pull/211333.diff

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp (+2-2)
  • (added) llvm/test/CodeGen/AMDGPU/vsrc-simplify-asynccnt.mir (+31)
diff --git a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
index 7fa33458fa325..8b8e3ddd0978a 100644
--- a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
@@ -1314,8 +1314,8 @@ void WaitcntBrackets::simplifyVmVsrc(const AMDGPU::Waitcnt &CheckWait,
   // have read its VGPR sources, but only if there are no other outstanding VMEM
   // operations that use a different counter (like SAMPLE_CNT).
   static constexpr AMDGPU::InstCounterType VmemCounters[] = {
-      AMDGPU::LOAD_CNT, AMDGPU::STORE_CNT, AMDGPU::SAMPLE_CNT, AMDGPU::BVH_CNT,
-      AMDGPU::DS_CNT};
+      AMDGPU::LOAD_CNT, AMDGPU::STORE_CNT, AMDGPU::SAMPLE_CNT,
+      AMDGPU::BVH_CNT,  AMDGPU::DS_CNT,    AMDGPU::ASYNC_CNT};
   HWEvents VmemEvents = llvm::accumulate(
       VmemCounters, HWEvents(), [&](HWEvents Acc, AMDGPU::InstCounterType T) {
         return Acc | Context->getWaitEvents(T);
diff --git a/llvm/test/CodeGen/AMDGPU/vsrc-simplify-asynccnt.mir b/llvm/test/CodeGen/AMDGPU/vsrc-simplify-asynccnt.mir
new file mode 100644
index 0000000000000..d73f120611c3f
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/vsrc-simplify-asynccnt.mir
@@ -0,0 +1,31 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1250 --amdgpu-expert-scheduling-mode -run-pass=si-insert-waitcnts -o - %s | FileCheck %s
+
+---
+name:            test_vm_vsrc
+machineFunctionInfo:
+  isEntryFunction: true
+body:             |
+  bb.0:
+    ; CHECK-LABEL: name: test_vm_vsrc
+    ; CHECK: GLOBAL_WB 0, implicit $exec
+    ; CHECK-NEXT: V_NOP_e32 implicit $exec
+    ; CHECK-NEXT: S_SETREG_IMM32_B32 2, 2074, implicit-def $mode, implicit $mode
+    ; CHECK-NEXT: S_SETREG_IMM32_B32 1, 1601, implicit-def $mode, implicit $mode
+    ; CHECK-NEXT: $vgpr5 = IMPLICIT_DEF
+    ; CHECK-NEXT: $vgpr2_vgpr3 = IMPLICIT_DEF
+    ; CHECK-NEXT: $vgpr4 = IMPLICIT_DEF
+    ; CHECK-NEXT: GLOBAL_LOAD_ASYNC_TO_LDS_B128 killed $vgpr5, $vgpr2_vgpr3, 0, 0, implicit-def dead $asynccnt, implicit $exec, implicit $asynccnt
+    ; CHECK-NEXT: S_WAIT_DSCNT 0
+    ; CHECK-NEXT: S_WAITCNT_DEPCTR .VmVsrc_0
+    ; CHECK-NEXT: renamable $vgpr4_vgpr5_vgpr6_vgpr7 = DS_LOAD_TR16_B128 killed renamable $vgpr4, 0, 0, implicit $exec
+    ; CHECK-NEXT: S_ENDPGM 0
+    S_SETREG_IMM32_B32 1, 1601, implicit-def $mode, implicit $mode
+    $vgpr5 = IMPLICIT_DEF
+    $vgpr2_vgpr3 = IMPLICIT_DEF
+    $vgpr4 = IMPLICIT_DEF
+    GLOBAL_LOAD_ASYNC_TO_LDS_B128 killed $vgpr5, $vgpr2_vgpr3, 0, 0, implicit-def dead $asynccnt, implicit $exec, implicit $asynccnt
+    S_WAIT_DSCNT 0
+    renamable $vgpr4_vgpr5_vgpr6_vgpr7 = DS_LOAD_TR16_B128 killed renamable $vgpr4, 0, 0, implicit $exec
+    S_ENDPGM 0
+...

AMDGPU::LOAD_CNT, AMDGPU::STORE_CNT, AMDGPU::SAMPLE_CNT, AMDGPU::BVH_CNT,
AMDGPU::DS_CNT};
AMDGPU::LOAD_CNT, AMDGPU::STORE_CNT, AMDGPU::SAMPLE_CNT,
AMDGPU::BVH_CNT, AMDGPU::DS_CNT, AMDGPU::ASYNC_CNT};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because my memory is having a moment, this runs meaningfully after the asyncmark/asyncwait intrinsics get lowered to real counter usages, yes?

Also, while we're at it, is there a TENSORCNT hazard? That instruction's SGPRs-only, but I thought I might as well ask.

@jrbyrnes jrbyrnes Jul 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because my memory is having a moment, this runs meaningfully after the asyncmark/asyncwait intrinsics get lowered to real counter usages, yes?

No I think not - these are consolidated into ASYNC_CNT when processing WAIT_ASYNCMARK , and recorded as scores when processing the ASYNCMARK. So, it looks possible that we have outstanding ASYNCMARK not encoded into the ASYNC_CNT.

Presumably we would want to treat the outstanding ASYNCMARK as real memory state, and thus this simplification should honor it, right?

Also, while we're at it, is there a TENSORCNT hazard? That instruction's SGPRs-only, but I thought I might as well ask.

VALU can write SGPR via instructions like readlane, which can then be consumed by TDM / other memory instruction. But this would be tracked by va_sdst and not vm_vsrc.

The other concern (aside from VALU) would be LDS, which can't write to SGPR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably we would want to treat the outstanding ASYNCMARK as real memory state, and thus this simplification should honor it, right?

The latest just disallows wait based simplifications in the presence of outstanding asyncmarks. The optimization relies on being able to do meaningful comparisons between the wait for the vm_vsrc and the wait for various memory types. Since the outstanding asyncmark are not contained in this structure, we lose the meaningful comparison. This is a conservative approach, and likely can be cleaned up a bit.

jrbyrnes added 5 commits July 26, 2026 12:46
Change-Id: Ia835c90c2087d3e7453917a1f62c0d2d7f27d531
Change-Id: Ib8f6c7f73b440b90c80da191ba52980c1f216122
Change-Id: I2d274814df14d5844e05e801facea39d0aa14b4b
Change-Id: Ib55e86ba1c13fc54a64bc8b424dfc50d9c899062
Change-Id: Ia0e7df7e06f00f1f329d3fdc2809a2e737ed494a
@jrbyrnes

Copy link
Copy Markdown
Contributor Author

Superseded by #212481

@jrbyrnes jrbyrnes closed this Jul 28, 2026
@jayfoad

jayfoad commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Superseded by #212481

Thanks/sorry -- I had not seen this PR.

; CHECK-NEXT: renamable $vgpr6_vgpr7_vgpr8_vgpr9 = DS_LOAD_TR16_B128 killed renamable $vgpr4, 0, 0, implicit $exec
; CHECK-NEXT: ASYNCMARK
; CHECK-NEXT: S_WAIT_DSCNT 0
; CHECK-NEXT: S_WAITCNT_DEPCTR .VmVsrc_0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the presence of the ASYNCMARK means we would need to wait on VM_VSRC here?

@github-actions
github-actions Bot deleted the users/jebyrnes/VsrcTrackAsync branch July 30, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants