Skip to content

Commit da0dce7

Browse files
VyacheslavLevytskyyjsji
authored andcommitted
ensure that PHI node has an incoming value per each predecessor instance even if the input SPIR-V module is invalid as reported by spirv-val (#2852)
Original commit: KhronosGroup/SPIRV-LLVM-Translator@69f65ef9257f3db
1 parent 2f75ef0 commit da0dce7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,7 +3166,10 @@ static void validatePhiPredecessors(Function *F) {
31663166
for (PHINode &Phi : BB.phis()) {
31673167
SmallVector<Value *> Vs;
31683168
SmallVector<BasicBlock *> Bs;
3169+
SmallPtrSet<BasicBlock *, 8> UsedB;
31693170
for (auto [V, B] : zip(Phi.incoming_values(), Phi.blocks())) {
3171+
if (!UsedB.insert(B).second)
3172+
continue;
31703173
unsigned N = PredsCnt[B];
31713174
Vs.insert(Vs.end(), N, V);
31723175
Bs.insert(Bs.end(), N, B);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; The goal of the test case is to ensure that PHI node has an incoming value per each predecessor
2+
; instance even if the input SPIR-V module is invalid as reported by spirv-val.
3+
4+
; We don't run spirv-val on this module therefore.
5+
6+
; REQUIRES: spirv-as
7+
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
8+
; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s
9+
10+
OpCapability Kernel
11+
OpCapability Addresses
12+
OpCapability Int64
13+
OpCapability Linkage
14+
%1 = OpExtInstImport "OpenCL.std"
15+
OpMemoryModel Physical64 OpenCL
16+
OpSource OpenCL_CPP 100000
17+
OpName %foo "foo"
18+
OpName %get "get"
19+
OpDecorate %foo LinkageAttributes "foo" Export
20+
OpDecorate %get LinkageAttributes "get" Import
21+
%uint = OpTypeInt 32 0
22+
%3 = OpTypeFunction %uint
23+
%ulong = OpTypeInt 64 0
24+
%uint_2 = OpConstant %uint 2
25+
%uint_4 = OpConstant %uint 4
26+
%get = OpFunction %uint None %3
27+
OpFunctionEnd
28+
%foo = OpFunction %uint None %3
29+
%11 = OpLabel
30+
%9 = OpFunctionCall %uint %get
31+
OpSwitch %9 %12 10 %13 4 %13 0 %13 42 %13
32+
%12 = OpLabel
33+
OpBranch %13
34+
%13 = OpLabel
35+
%10 = OpPhi %uint %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_2 %12
36+
%14 = OpPhi %uint %uint_2 %12 %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_4 %11
37+
OpReturnValue %14
38+
OpFunctionEnd
39+
40+
; CHECK: phi i32 [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 2, %2 ]
41+
; CHECK: phi i32 [ 2, %2 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ]

0 commit comments

Comments
 (0)