llvm.org GIT mirror llvm / a16ecd4
[DivergenceAnalysis] Treat PHI with incoming undef as constant Summary: If a PHI has an incoming undef, we can pretend that it is equal to one non-undef, non-self incoming value. This is particularly relevant in combination with the StructurizeCFG pass, which introduces PHI nodes with undefs. Previously, this lead to branch conditions that were uniform before StructurizeCFG to become non-uniform afterwards, which confused the SIAnnotateControlFlow pass. This fixes a crash when Mesa radeonsi compiles a shader from dEQP-GLES3.functional.shaders.switch.switch_in_for_loop_dynamic_vertex Reviewers: arsenm, tstellarAMD, jingyue Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D19013 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266347 91177308-0d34-0410-b5e6-96231b3b80d8 Nicolai Haehnle 3 years ago
5 changed file(s) with 93 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
26912691 /// hasConstantValue - If the specified PHI node always merges together the
26922692 /// same value, return the value, otherwise return null.
26932693 Value *hasConstantValue() const;
2694
2695 /// hasConstantOrUndefValue - Whether the specified PHI node always merges
2696 /// together the same value, assuming undefs are equal to a unique
2697 /// non-undef value.
2698 bool hasConstantOrUndefValue() const;
26942699
26952700 /// Methods for support type inquiry through isa, cast, and dyn_cast:
26962701 static inline bool classof(const Instruction *I) {
145145 for (auto I = IPostDom->begin(); isa(I); ++I) {
146146 // A PHINode is uniform if it returns the same value no matter which path is
147147 // taken.
148 if (!cast(I)->hasConstantValue() && DV.insert(&*I).second)
148 if (!cast(I)->hasConstantOrUndefValue() && DV.insert(&*I).second)
149149 Worklist.push_back(&*I);
150150 }
151151
151151 if (ConstantValue == this)
152152 return UndefValue::get(getType());
153153 return ConstantValue;
154 }
155
156 /// hasConstantOrUndefValue - Whether the specified PHI node always merges
157 /// together the same value, assuming that undefs result in the same value as
158 /// non-undefs.
159 /// Unlike \ref hasConstantValue, this does not return a value because the
160 /// unique non-undef incoming value need not dominate the PHI node.
161 bool PHINode::hasConstantOrUndefValue() const {
162 Value *ConstantValue = nullptr;
163 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
164 Value *Incoming = getIncomingValue(i);
165 if (Incoming != this && !isa(Incoming)) {
166 if (ConstantValue && ConstantValue != Incoming)
167 return false;
168 ConstantValue = Incoming;
169 }
170 }
171 return true;
154172 }
155173
156174 //===----------------------------------------------------------------------===//
0 ; RUN: opt -mtriple=amdgcn-- -analyze -divergence %s | FileCheck %s
1
2 ; CHECK-LABEL: 'test1':
3 ; CHECK-NEXT: DIVERGENT: i32 %bound
4 ; CHECK-NEXT: DIVERGENT: %break = icmp sge i32 %counter, %bound
5 ; CHECK-NEXT: DIVERGENT: br i1 %break, label %footer, label %body
6 ; CHECK-NEXT: DIVERGENT: br i1 %break, label %end, label %header
7 ; Note: %counter is not divergent!
8 define amdgpu_ps void @test1(i32 %bound) {
9 entry:
10 br label %header
11
12 header:
13 %counter = phi i32 [ 0, %entry ], [ %counter.footer, %footer ]
14 %break = icmp sge i32 %counter, %bound
15 br i1 %break, label %footer, label %body
16
17 body:
18 %counter.next = add i32 %counter, 1
19 br label %footer
20
21 footer:
22 %counter.footer = phi i32 [ %counter.next, %body ], [ undef, %header ]
23 br i1 %break, label %end, label %header
24
25 end:
26 ret void
27 }
0 ; RUN: llc -mtriple=amdgcn-- < %s | FileCheck %s
1
2 ; The branch instruction in LOOP49 has a uniform condition, but PHI instructions
3 ; introduced by the structurizecfg pass previously caused a false divergence
4 ; which ended up in an assertion (or incorrect code) because
5 ; SIAnnotateControlFlow and structurizecfg had different ideas about which
6 ; branches are uniform.
7 ;
8 ; CHECK-LABEL: {{^}}main:
9 ; CHECK: ; %LOOP49
10 ; CHECK: v_cmp_ne_i32_e32 vcc,
11 ; CHECK: s_cbranch_vccnz
12 ; CHECK: ; %ENDIF53
13 define amdgpu_vs float @main(i32 %in) {
14 main_body:
15 %cmp = mul i32 %in, 2
16 br label %LOOP
17
18 LOOP: ; preds = %ENDLOOP48, %main_body
19 %counter = phi i32 [ 0, %main_body ], [ %counter.next, %ENDLOOP48 ]
20 %v.LOOP = phi i32 [ 0, %main_body ], [ %v.ENDLOOP48, %ENDLOOP48 ]
21 %tmp7 = icmp slt i32 %cmp, %counter
22 br i1 %tmp7, label %IF, label %LOOP49
23
24 IF: ; preds = %LOOP
25 %r = bitcast i32 %v.LOOP to float
26 ret float %r
27
28 LOOP49: ; preds = %LOOP
29 %tmp8 = icmp ne i32 %counter, 0
30 br i1 %tmp8, label %ENDLOOP48, label %ENDIF53
31
32 ENDLOOP48: ; preds = %ENDIF53, %LOOP49
33 %v.ENDLOOP48 = phi i32 [ %v.LOOP, %LOOP49 ], [ %v.ENDIF53, %ENDIF53 ]
34 %counter.next = add i32 %counter, 1
35 br label %LOOP
36
37 ENDIF53: ; preds = %LOOP49
38 %v.ENDIF53 = add i32 %v.LOOP, %counter
39 br label %ENDLOOP48
40 }