Skip to content

Commit 3e7326a

Browse files
LU-JOHNsys-ce-bb
authored andcommitted
Set WordCount for SPIRVVariable correctly (#2437)
SPIRVVariable should have a WordCount of 4 + 1 (if optional Initializer is added). Set WordCount to 4 if Initializer is not added. Signed-off-by: Lu, John <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@274e918b508c993
1 parent 5089875 commit 3e7326a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ class SPIRVVariable : public SPIRVInstruction {
443443
const std::string &TheName,
444444
SPIRVStorageClassKind TheStorageClass, SPIRVBasicBlock *TheBB,
445445
SPIRVModule *TheM)
446-
: SPIRVInstruction(TheInitializer ? 5 : 4, OpVariable, TheType, TheId,
447-
TheBB, TheM),
446+
: SPIRVInstruction(TheInitializer && !TheInitializer->isUndef() ? 5 : 4,
447+
OpVariable, TheType, TheId, TheBB, TheM),
448448
StorageClass(TheStorageClass) {
449449
if (TheInitializer && !TheInitializer->isUndef())
450450
Initializer.push_back(TheInitializer->getId());

llvm-spirv/test/var_undef.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; Ensure that encoding of variable with undef initializer
2+
; has correct wordcount
3+
4+
; RUN: llvm-as %s -o %t.bc
5+
; RUN: llvm-spirv %t.bc -o %t.spv
6+
; RUN: llvm-spirv -to-text %t.spv -o %t.spt
7+
; RUN: FileCheck < %t.spt %s --check-prefix CHECK-SPIRV
8+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
9+
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
10+
; RUN: FileCheck < %t.rev.ll %s --check-prefix CHECK-LLVM
11+
12+
; CHECK-SPIRV:Name [[BAR_VAR:[0-9]+]] "bar"
13+
;; bar variable does not have optional initializer
14+
;; word count must be 4
15+
; CHECK-SPIRV:4 Variable [[#]] [[BAR_VAR]]
16+
17+
; CHECK-LLVM:@bar = internal addrspace(3) global %range undef, align 8
18+
19+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
20+
target triple = "spir64-unknown-unknown"
21+
22+
%anon = type { %range }
23+
%range = type { %array }
24+
%array = type { [2 x i64] }
25+
26+
@foo = internal addrspace(3) global %anon undef, align 8
27+
28+
@bar = internal unnamed_addr addrspace(3) global %range undef, align 8

0 commit comments

Comments
 (0)