Skip to content

Commit

Permalink
[Backport to 15] Don't add initializer to local variables with extern…
Browse files Browse the repository at this point in the history
…al linkage. (#2643)

If a variable is external, it will be defined during linking, the translator
should not do it.

Signed-off-by: Marcos Maronas <[email protected]>
  • Loading branch information
maarquitos14 authored Jul 17, 2024
1 parent fe1b798 commit 0303992
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
else if (LinkageTy == GlobalValue::CommonLinkage)
// In LLVM, variables with common linkage type must be initialized to 0.
Initializer = Constant::getNullValue(Ty);
else if (BS == SPIRVStorageClassKind::StorageClassWorkgroup)
else if (BS == SPIRVStorageClassKind::StorageClassWorkgroup &&
LinkageTy != GlobalValue::ExternalLinkage)
Initializer = dyn_cast<Constant>(UndefValue::get(Ty));
else if ((LinkageTy != GlobalValue::ExternalLinkage) &&
(BS == SPIRVStorageClassKind::StorageClassCrossWorkgroup))
Expand Down
10 changes: 10 additions & 0 deletions test/local_var_keeps_external.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; RUN: llvm-as %s -o %t.bc -opaque-pointers
; RUN: llvm-spirv %t.bc -o %t.spv -opaque-pointers
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc -emit-opaque-pointers
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll -opaque-pointers
; RUN: FileCheck < %t.rev.ll %s --check-prefix CHECK-LLVM

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-G1"
target triple = "spir64-unknown-unknown"
@__LocalVar = external addrspace(3) global ptr addrspace(1)
; CHECK-LLVM:@__LocalVar = external addrspace(3) global ptr addrspace(1)

0 comments on commit 0303992

Please sign in to comment.