Index: lib/Target/AMDGPU/SIInsertWaitcnts.cpp =================================================================== --- lib/Target/AMDGPU/SIInsertWaitcnts.cpp +++ lib/Target/AMDGPU/SIInsertWaitcnts.cpp @@ -1558,7 +1558,8 @@ MachineBasicBlock *SIInsertWaitcnts::loopBottom(const MachineLoop *Loop) { MachineBasicBlock *Bottom = Loop->getHeader(); for (MachineBasicBlock *MBB : Loop->blocks()) - if (MBB->getNumber() > Bottom->getNumber()) + if (MBB->getNumber() > Bottom->getNumber() && + MBB->isSuccessor(Loop->getHeader())) Bottom = MBB; return Bottom; } Index: test/CodeGen/AMDGPU/waitcnt-looptest.ll =================================================================== --- test/CodeGen/AMDGPU/waitcnt-looptest.ll +++ test/CodeGen/AMDGPU/waitcnt-looptest.ll @@ -144,3 +144,74 @@ attributes #0 = { "target-cpu"="fiji" "target-features"="-flat-for-global" } attributes #1 = { nounwind readnone speculatable } + +; Check that the waitcnt insertion algorithm correctly propagates wait counts +; from bottom loop to the loop header. + +; GCN-LABEL: {{^}}testLoopBottom +; GCN: BB1_2: +; GCN: s_waitcnt vmcnt(0) + +define amdgpu_kernel void @testLoopBottom([0 x i8] addrspace(4)* inreg noalias dereferenceable(18446744073709551615), [0 x i8] addrspace(4)* inreg noalias dereferenceable(18446744073709551615), i32 inreg, i32 inreg, i32, i32, i32, i32) { +main_body: + %8 = getelementptr [0 x i8], [0 x i8] addrspace(4)* %1, i64 0, i64 48 + %9 = bitcast i8 addrspace(4)* %8 to <4 x i32> addrspace(4)*, !amdgpu.uniform !0 + %10 = load <4 x i32>, <4 x i32> addrspace(4)* %9, align 16, !invariant.load !0 + %11 = call float @llvm.amdgcn.buffer.load.f32(<4 x i32> %10, i32 0, i32 0, i1 false, i1 false) + %12 = bitcast [0 x i8] addrspace(4)* %0 to <4 x i32> addrspace(4)*, !amdgpu.uniform !0 + %13 = load <4 x i32>, <4 x i32> addrspace(4)* %12, align 16, !invariant.load !0 + %14 = call float @llvm.SI.load.const.v4i32(<4 x i32> %13, i32 64) + %15 = icmp eq i32 %2, 3 + %16 = select i1 %15, i32 1065353216, i32 -1082130432 + br label %17 + +;