Skip to content

Commit

Permalink
[NFC] Test behavior of common global variables (#2306)
Browse files Browse the repository at this point in the history
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]>
LU-JOHN authored Jan 24, 2024
1 parent 918036c commit 8c357de
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
@@ -1989,7 +1989,7 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
StorageClass = SPIRSPIRVAddrSpaceMap::map(AddressSpace);
if (StorageClass == StorageClassFunction) {
std::stringstream SS;
SS << "Global variable can not have Function storage class. "
SS << "Global variable cannot have Function storage class. "
<< "Consider setting a proper address space.\n "
<< "Original LLVM value:\n"
<< toString(GV);
24 changes: 24 additions & 0 deletions test/negative/CommonGlobalVarNoAddrspace.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Ensure "common global" with no addrspace is reported as an error
; since the SPIR-V spec states:
;
; global variable declarations must always have an address space
; specified and that address space cannot be `0`

; RUN: llvm-as %s -o %t.bc
; RUN: not llvm-spirv %t.bc 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-ERROR

; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction:
; CHECK-ERROR-NEXT: Global variable cannot have Function storage class. Consider setting a proper address space.
; CHECK-ERROR-NEXT: Original LLVM value:
; CHECK-ERROR-NEXT: @CG = common global i32 0, align 4

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"
target triple = "spir64-unknown-unknown"

@CG = common global i32 0, align 4

define i32 @f() #0 {
%1 = load i32, i32* @CG, align 4
ret i32 %1
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
; Ensure "global" with no addrspace is reported as an error
; since the SPIR-V spec states:
;
; global variable declarations must always have an address space
; specified and that address space cannot be `0`

; RUN: llvm-as %s -o %t.bc
; RUN: not llvm-spirv %t.bc 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-ERROR

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

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"
target triple = "spir64-unknown-unknown"
20 changes: 20 additions & 0 deletions test/transcoding/CommonAddrspaceGlobalVar.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; Ensure that a "common global" is not converted to a locally allocated
; variable when translated to SPIR-V and back to LLVM.

; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -o %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

; CHECK-LLVM-NOT: alloca
; CHECK-LLVM: @CAG = common addrspace(1) global i32 0, align 4
; CHECK-LLVM-NOT: alloca

target triple = "spir64-unknown-unknown"

@CAG = common addrspace(1) global i32 0, align 4

define i32 @f() #0 {
%1 = load i32, i32 addrspace(1) * @CAG, align 4
ret i32 %1
}

0 comments on commit 8c357de

Please sign in to comment.