Skip to content

Commit d671e8d

Browse files
LU-JOHNsys-ce-bb
authored andcommitted
Test behavior of common global variables (#2306)
Add tests to ensure that: A common global without addrspace is reported as an error A common addrspace global is not transformed to a locally allocated variable Updated error message from "can not" to "cannot". Otherwise this is a NFC. Signed-off-by: Lu, John <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@8c357de
1 parent a130112 commit d671e8d

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
20052005
StorageClass = SPIRSPIRVAddrSpaceMap::map(AddressSpace);
20062006
if (StorageClass == StorageClassFunction) {
20072007
std::stringstream SS;
2008-
SS << "Global variable can not have Function storage class. "
2008+
SS << "Global variable cannot have Function storage class. "
20092009
<< "Consider setting a proper address space.\n "
20102010
<< "Original LLVM value:\n"
20112011
<< toString(GV);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; Ensure "common global" with no addrspace is reported as an error
2+
; since the SPIR-V spec states:
3+
;
4+
; global variable declarations must always have an address space
5+
; specified and that address space cannot be `0`
6+
7+
; RUN: llvm-as %s -o %t.bc
8+
; RUN: not llvm-spirv %t.bc 2>&1 \
9+
; RUN: | FileCheck %s --check-prefix=CHECK-ERROR
10+
11+
; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction:
12+
; CHECK-ERROR-NEXT: Global variable cannot have Function storage class. Consider setting a proper address space.
13+
; CHECK-ERROR-NEXT: Original LLVM value:
14+
; CHECK-ERROR-NEXT: @CG = common global i32 0, align 4
15+
16+
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"
17+
target triple = "spir64-unknown-unknown"
18+
19+
@CG = common global i32 0, align 4
20+
21+
define i32 @f() #0 {
22+
%1 = load i32, i32* @CG, align 4
23+
ret i32 %1
24+
}

llvm-spirv/test/GlobalVarNoAddrspace.ll renamed to llvm-spirv/test/negative/GlobalVarNoAddrspace.ll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
; Ensure "global" with no addrspace is reported as an error
2+
; since the SPIR-V spec states:
3+
;
4+
; global variable declarations must always have an address space
5+
; specified and that address space cannot be `0`
6+
17
; RUN: llvm-as %s -o %t.bc
28
; RUN: not llvm-spirv %t.bc 2>&1 \
39
; RUN: | FileCheck %s --check-prefix=CHECK-ERROR
410

511
; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction:
6-
; CHECK-ERROR-NEXT: Global variable can not have Function storage class. Consider setting a proper address space.
12+
; CHECK-ERROR-NEXT: Global variable cannot have Function storage class. Consider setting a proper address space.
13+
; CHECK-ERROR-NEXT: Original LLVM value:
14+
; CHECK-ERROR-NEXT: @G = global i1 true
715

816
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"
917
target triple = "spir64-unknown-unknown"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; Ensure that a "common global" is not converted to a locally allocated
2+
; variable when translated to SPIR-V and back to LLVM.
3+
4+
; RUN: llvm-as %s -o %t.bc
5+
; RUN: llvm-spirv %t.bc -o %t.spv
6+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
8+
9+
; CHECK-LLVM-NOT: alloca
10+
; CHECK-LLVM: @CAG = common addrspace(1) global i32 0, align 4
11+
; CHECK-LLVM-NOT: alloca
12+
13+
target triple = "spir64-unknown-unknown"
14+
15+
@CAG = common addrspace(1) global i32 0, align 4
16+
17+
define i32 @f() #0 {
18+
%1 = load i32, i32 addrspace(1) * @CAG, align 4
19+
ret i32 %1
20+
}

0 commit comments

Comments
 (0)