[llvm] r263658 - AMDGPU: Prevent uniform loops from becoming infinite

Nicolai Haehnle via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 16 13:14:33 PDT 2016


Author: nha
Date: Wed Mar 16 15:14:33 2016
New Revision: 263658

URL: http://llvm.org/viewvc/llvm-project?rev=263658&view=rev
Log:
AMDGPU: Prevent uniform loops from becoming infinite

Summary:
Uniform loops where the branch leaving the loop is predicated on VCCNZ
must be skipped if EXEC = 0, otherwise they will be infinite.

Reviewers: tstellarAMD, arsenm

Subscribers: arsenm, llvm-commits

Differential Revision: http://reviews.llvm.org/D18137

Added:
    llvm/trunk/test/CodeGen/AMDGPU/uniform-loop-inside-nonuniform.ll
Modified:
    llvm/trunk/lib/Target/AMDGPU/SILowerControlFlow.cpp

Modified: llvm/trunk/lib/Target/AMDGPU/SILowerControlFlow.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/SILowerControlFlow.cpp?rev=263658&r1=263657&r2=263658&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AMDGPU/SILowerControlFlow.cpp (original)
+++ llvm/trunk/lib/Target/AMDGPU/SILowerControlFlow.cpp Wed Mar 16 15:14:33 2016
@@ -137,6 +137,12 @@ bool SILowerControlFlow::shouldSkip(Mach
          NumInstr < SkipThreshold && I != E; ++I) {
 
       if (I->isBundle() || !I->isBundled())
+        // When a uniform loop is inside non-uniform control flow, the branch
+        // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken
+        // when EXEC = 0. We should skip the loop lest it becomes infinite.
+        if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ)
+          return true;
+
         if (++NumInstr >= SkipThreshold)
           return true;
     }

Added: llvm/trunk/test/CodeGen/AMDGPU/uniform-loop-inside-nonuniform.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AMDGPU/uniform-loop-inside-nonuniform.ll?rev=263658&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/AMDGPU/uniform-loop-inside-nonuniform.ll (added)
+++ llvm/trunk/test/CodeGen/AMDGPU/uniform-loop-inside-nonuniform.ll Wed Mar 16 15:14:33 2016
@@ -0,0 +1,28 @@
+;RUN: llc -march=amdgcn -mcpu=verde < %s | FileCheck %s --check-prefix=CHECK
+
+; Test a simple uniform loop that lives inside non-uniform control flow.
+
+;CHECK-LABEL: {{^}}test1:
+;CHECK: s_cbranch_execz
+;CHECK: %loop_body
+define void @test1(<8 x i32> inreg %rsrc, <2 x i32> %addr.base, i32 %y, i32 %p) #0 {
+main_body:
+  %cc = icmp eq i32 %p, 0
+  br i1 %cc, label %out, label %loop_body
+
+loop_body:
+  %counter = phi i32 [ 0, %main_body ], [ %incr, %loop_body ]
+
+  ; Prevent the loop from being optimized out
+  call void asm sideeffect "", "" ()
+
+  %incr = add i32 %counter, 1
+  %lc = icmp sge i32 %incr, 1000
+  br i1 %lc, label %out, label %loop_body
+
+out:
+  ret void
+}
+
+attributes #0 = { "ShaderType"="0" }
+attributes #1 = { nounwind readonly }




More information about the llvm-commits mailing list