Skip to content

Uniformity Analysis: incorrect control-divergence result. #137277

Closed
@jgu222

Description

@jgu222

The uniform analysis fails to recognize PHI node as divergence.

The test (phi_issue.ll) using AMDGPU to get divergence branch. The uniformity
analysis marks all PHI nodes at B6 as uniform, which is incorrect.
Basically, it is the following simple if-then-else (except there are multiple
basic blocks in both then and else brances):

       div-if  ;  divergent branch
      /       \
  x0=1    x1 = 2
     \        /
      \     /
    x = phi  x0, x1 

x is marked as UNIFORM, which is incorrect as div-if is divergent branch


;
; opt -mtriple amdgcn-unknown-amdhsa -passes='print<uniformity>' -disable-output  phi_issue.ll
;
define amdgpu_kernel void @test_ctrl_divergence(i32 %a, i32 %b, i32 %c, i32 %d) {
Entry:
  %tid = call i32 @llvm.amdgcn.workitem.id.x()
  %div.cond = icmp eq i32 %tid, 0
  br i1 %div.cond, label %B3, label %B0 ; divergent branch

B0:
  %a0 = add i32 %a, 1
  br label %B1

B1:
  %b0 = add i32 %b, 2
  br label %B2
  
B2:
  %c0 = add i32 %c, 3
  br label %B6

B3:
  %a1 = add i32 %a, 10
  br label %B4

B4:
  %b1 = add i32 %b, 20
  br label %B5
  
B5:
  %c1 = add i32 %c, 30
  br label %B6

B6:
  %div_a = phi i32 [%a0, %B2], [%a1,  %B5]
  %div_b = phi i32 [%b0, %B2], [%b1,  %B5]
  %div_c = phi i32 [%c0, %B2], [%c1,  %B5]
  br i1 %div.cond, label %B8, label %B7 ; divergent branch

B7:
  %d1 = add i32 %d, 1
  br label %B8

B8:
  %div_d = phi i32 [%d1, %B7], [%d, %B6]
  ret void
}


declare i32 @llvm.amdgcn.workitem.id.x() #0  

attributes #0 = {nounwind readnone }

%opt -mtriple amdgcn-unknown-amdhsa -passes='print<uniformity>' -disable-output  phi_issue.ll
...
LOCK Entry
DEFINITIONS
  DIVERGENT:   %tid = call i32 @llvm.amdgcn.workitem.id.x()
  DIVERGENT:   %div.cond = icmp eq i32 %tid, 0
TERMINATORS
  DIVERGENT:   br i1 %div.cond, label %B3, label %B0
END BLOCK
......
BLOCK B6
DEFINITIONS
               %div_a = phi i32 [ %a0, %B2 ], [ %a1, %B5 ]
               %div_b = phi i32 [ %b0, %B2 ], [ %b1, %B5 ]
               %div_c = phi i32 [ %c0, %B2 ], [ %c1, %B5 ]
TERMINATORS
  DIVERGENT:   br i1 %div.cond, label %B8, label %B7
END BLOCK

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions