Skip to content

Commit ff16022

Browse files
authored
[SPIR-V] Stop re-writing SPIR-V type map (#3856)
There is a map used during forward translation. It links LLVM type and SPIR-V type, that is being generated, LLVM type here is used as a key. Due to a bug in type mapping (information is below), it's possible that for a single pointer type in LLVM, the translator will create 2 physically-indentical SPIR-V pointer types and with previous implementation of mapType function the map would be changed during the second type insertion. This patch prevents it, forcing to reuse the type that was created first, though we would still have an unused duplication of this type in SPIR-V module. Please consider following LLVM IR sample: %struct._ZTS4Args.Args = type { %struct._ZTS6Object.Object addrspace(4)* } %struct._ZTS6Object.Object = type { i32 (%struct._ZTS6Object.Object addrspace(4)*, i32)* } Here we have 2 LLVM type declarations. The translator will recursively go through structure members and then through element types of the pointer types, creating the appropriate SPIR-V types and mapping them on LLVM types. Due to a recursive nature of this algorithm, here we will have '%struct._ZTS6Object.Object addrspace(4)*' be processed twice with 2 SPIR-V type declaration instruction being created. Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent 6741147 commit ff16022

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
17931793
}
17941794

17951795
SPIRVType *LLVMToSPIRVBase::mapType(Type *T, SPIRVType *BT) {
1796-
TypeMap[T] = BT;
1796+
auto EmplaceStatus = TypeMap.try_emplace(T, BT);
1797+
(void)EmplaceStatus;
1798+
// TODO: Uncomment the assertion, once the type mapping issue is resolved
1799+
// assert(EmplaceStatus.second && "The type was already added to the map");
17971800
SPIRVDBG(dbgs() << "[mapType] " << *T << " => "; spvdbgs() << *BT << '\n');
17981801
return BT;
17991802
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc --spirv-ext=+all -spirv-text -o %t
3+
; RUN: FileCheck < %t %s
4+
5+
; CHECK: Name [[#NAME:]] "struct._ZTS6Object.Object"
6+
; CHECK-COUNT-1: TypeStruct [[#NAME]]
7+
; TODO add check count one and remove unused, when the type mapping bug is fixed
8+
; CHECK: TypePointer [[#UNUSED:]] {{.*}} [[#NAME]]
9+
; CHECK: TypePointer [[#PTRTY:]] {{.*}} [[#NAME]]
10+
; CHECK: FunctionParameter [[#PTRTY]]
11+
; CHECK-NOT: FunctionParameter [[#UNUSED]]
12+
13+
; ModuleID = 'sycl_test.bc'
14+
source_filename = "sycl_test.cpp"
15+
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"
16+
target triple = "spir64-unknown-unknown-sycldevice"
17+
18+
%struct._ZTS4Args.Args = type { %struct._ZTS6Object.Object addrspace(4)* }
19+
%struct._ZTS6Object.Object = type { i32 (%struct._ZTS6Object.Object addrspace(4)*, i32)* }
20+
21+
; Function Attrs: convergent norecurse nounwind mustprogress
22+
define dso_local spir_func i32 @_Z9somefunc0P4Args(%struct._ZTS4Args.Args addrspace(4)* %a, %struct._ZTS6Object.Object addrspace(4)* %b) {
23+
entry:
24+
ret i32 0
25+
}
26+
27+
!opencl.spir.version = !{!0}
28+
!spirv.Source = !{!1}
29+
!0 = !{i32 1, i32 2}
30+
!1 = !{i32 4, i32 100000}

0 commit comments

Comments
 (0)