Skip to content

Commit

Permalink
ensure that PHI node has an incoming value per each predecessor insta…
Browse files Browse the repository at this point in the history
…nce even if the input SPIR-V module is invalid as reported by spirv-val (KhronosGroup#2852)
  • Loading branch information
VyacheslavLevytskyy authored and MrSidims committed Nov 9, 2024
1 parent a5b635a commit 447eaab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3059,12 +3059,15 @@ static void validatePhiPredecessors(Function *F) {
for (PHINode &Phi : BB.phis()) {
SmallVector<Value *> Vs;
SmallVector<BasicBlock *> Bs;
SmallPtrSet<BasicBlock *, 8> UsedB;
auto Vals = Phi.incoming_values();
auto Blocks = Phi.blocks();
auto *VIt = Vals.begin();
auto *BIt = Blocks.begin();
for (; VIt != Vals.end() && BIt != Blocks.end(); ++VIt, ++BIt) {
const unsigned N = PredsCnt[*BIt];
if (!UsedB.insert(*BIt).second)
continue;
Vs.insert(Vs.end(), N, *VIt);
Bs.insert(Bs.end(), N, *BIt);
}
Expand Down
41 changes: 41 additions & 0 deletions test/phi-multiple-predecessors-invalid-input.spvasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; The goal of the test case is to 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.

; We don't run spirv-val on this module therefore.

; REQUIRES: spirv-as
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s

OpCapability Kernel
OpCapability Addresses
OpCapability Int64
OpCapability Linkage
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpSource OpenCL_CPP 100000
OpName %foo "foo"
OpName %get "get"
OpDecorate %foo LinkageAttributes "foo" Export
OpDecorate %get LinkageAttributes "get" Import
%uint = OpTypeInt 32 0
%3 = OpTypeFunction %uint
%ulong = OpTypeInt 64 0
%uint_2 = OpConstant %uint 2
%uint_4 = OpConstant %uint 4
%get = OpFunction %uint None %3
OpFunctionEnd
%foo = OpFunction %uint None %3
%11 = OpLabel
%9 = OpFunctionCall %uint %get
OpSwitch %9 %12 10 %13 4 %13 0 %13 42 %13
%12 = OpLabel
OpBranch %13
%13 = OpLabel
%10 = OpPhi %uint %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_2 %12
%14 = OpPhi %uint %uint_2 %12 %uint_4 %11 %uint_4 %11 %uint_4 %11 %uint_4 %11
OpReturnValue %14
OpFunctionEnd

; CHECK: phi i32 [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 2, %2 ]
; CHECK: phi i32 [ 2, %2 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ], [ 4, %0 ]

0 comments on commit 447eaab

Please sign in to comment.