Skip to content

Commit 35fa317

Browse files
maarquitos14jsji
authored andcommitted
Prevent duplicate array type insertion into TypeMap. (#3186)
Check if an array type exists in TypeMap before inserting it to prevent duplications. Original commit: KhronosGroup/SPIRV-LLVM-Translator@2a5a4e71d79d074
1 parent 189f512 commit 35fa317

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,13 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
465465
ConstantInt::get(getSizetType(), ArraySize, false), nullptr)));
466466
mapType(T, TransType);
467467
if (ElTy->isPointerTy()) {
468-
mapType(
468+
Type *ArrTy =
469469
ArrayType::get(TypedPointerType::get(Type::getInt8Ty(*Ctx),
470470
ElTy->getPointerAddressSpace()),
471-
ArraySize),
472-
TransType);
471+
ArraySize);
472+
LLVMToSPIRVTypeMap::iterator Loc = TypeMap.find(ArrTy);
473+
if (Loc == TypeMap.end())
474+
mapType(ArrTy, TransType);
473475
}
474476
return TransType;
475477
}

llvm-spirv/test/duplicate-types.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; Check that we don't end up with duplicated array types in TypeMap.
2+
; No FileCheck needed, we only want to check the absence of errors.
3+
; RUN: llvm-as %s -o %t.bc
4+
; RUN: llvm-spirv %t.bc -o %t.spv
5+
; RUN: spirv-val %t.spv
6+
7+
; ModuleID = 'duplicate-array-types'
8+
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"
9+
target triple = "spir64-unknown-unknown"
10+
11+
%duplicate = type { [2 x ptr addrspace(4)] }
12+
13+
; Function Attrs: mustprogress norecurse nounwind
14+
define spir_kernel void @foo() {
15+
entry:
16+
alloca [2 x ptr addrspace(4)], align 8
17+
alloca %duplicate, align 8
18+
ret void
19+
}

0 commit comments

Comments
 (0)