Skip to content

Commit 7171801

Browse files
[Backport to llvm_release_180] Fix globals' type deduction in TypeScavenger (KhronosGroup#3613)
Backport of PR KhronosGroup#2431 into `llvm_release_180`. All commits applied cleanly. Authored-by: Maksim Shelegov <maksim.shelegov@intel.com>
1 parent d53bd97 commit 7171801

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

lib/SPIRV/SPIRVTypeScavenger.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,12 @@ void SPIRVTypeScavenger::typeGlobalValue(GlobalValue &GV, Constant *Init) {
618618
auto It = DeducedTypes.find(C);
619619
if (It != DeducedTypes.end())
620620
return It->second;
621+
} else if (auto *GEP = dyn_cast<GEPOperator>(C)) {
622+
auto *ResultTy =
623+
TypedPointerType::get(GEP->getResultElementType(),
624+
GEP->getType()->getPointerAddressSpace());
625+
DeducedTypes[C] = ResultTy;
626+
return ResultTy;
621627
}
622628

623629
return getUnknownTyped(C->getType());

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,15 @@ SPIRVValue *LLVMToSPIRVBase::transConstant(Value *V) {
14441444
}
14451445

14461446
if (auto *ConstUE = dyn_cast<ConstantExpr>(V)) {
1447+
if (auto *GEP = dyn_cast<GEPOperator>(ConstUE)) {
1448+
std::vector<SPIRVValue *> Indices;
1449+
for (unsigned I = 0, E = GEP->getNumIndices(); I != E; ++I)
1450+
Indices.push_back(transValue(GEP->getOperand(I + 1), nullptr));
1451+
auto *TransPointerOperand = transValue(GEP->getPointerOperand(), nullptr);
1452+
SPIRVType *TranslatedTy = transScavengedType(GEP);
1453+
return BM->addPtrAccessChainInst(TranslatedTy, TransPointerOperand,
1454+
Indices, nullptr, GEP->isInBounds());
1455+
}
14471456
auto *Inst = ConstUE->getAsInstruction();
14481457
SPIRVDBG(dbgs() << "ConstantExpr: " << *ConstUE << '\n';
14491458
dbgs() << "Instruction: " << *Inst << '\n';)

test/layout.ll

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,27 @@
4343
; CHECK-NOT: {{[0-9]*}} Variable
4444
; CHECK-NOT: {{[0-9]*}} Function
4545

46-
; CHECK: {{[0-9]*}} TypeInt [[TypeInt:[0-9]+]] 32
47-
; CHECK: {{[0-9]*}} TypeInt [[TypeI8:[0-9]+]] 8
48-
; CHECK: {{[0-9]*}} Constant [[TypeInt]] [[Two:[0-9]+]] 2
49-
; CHECK: {{[0-9]*}} TypePointer [[TPointer:[0-9]+]]
50-
; CHECK: {{[0-9]*}} TypePointer [[SConstOpType:[0-9]+]]
46+
; CHECK: {{[0-9]*}} TypeInt [[TypeInt32:[0-9]+]] 32
47+
; CHECK: {{[0-9]*}} TypeInt [[TypeInt8:[0-9]+]] 8
48+
; CHECK: {{[0-9]*}} Constant [[TypeInt32]] [[Int32Two:[0-9]+]] 2
49+
; CHECK: {{[0-9]*}} TypeArray [[TypeArrayInt32:[0-9]+]] [[TypeInt32]] [[Int32Two]]
50+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr5ArrayInt32:[0-9]+]] 5 [[TypeArrayInt32]]
51+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr5Int32:[0-9]+]] 5 [[TypeInt32]]
52+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr5Ptr5Int32:[0-9]+]] 5 [[TypePtr5Int32]]
5153
; CHECK: {{[0-9]*}} TypeFloat [[TypeFloat:[0-9]+]]
52-
; CHECK: {{[0-9]*}} TypeArray [[TypeArray:[0-9]+]] [[TypeFloat]] [[Two]]
53-
; CHECK: {{[0-9]*}} TypeVector [[TypeVectorInt3:[0-9]+]] [[TypeInt]] 3
54-
; CHECK: {{[0-9]*}} TypePointer [[TPointerI8:[0-9]+]] 8 [[TypeI8]]
55-
; CHECK: {{[0-9]*}} TypeStruct [[BID:[0-9]+]] {{[0-9]+}} [[TPointerI8]]
54+
; CHECK: {{[0-9]*}} TypeArray [[TypeArrayFloat:[0-9]+]] [[TypeFloat]] [[Int32Two]]
55+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr8ArrayFloat:[0-9]+]] 0 [[TypeArrayFloat]]
56+
; CHECK: {{[0-9]*}} TypeVector [[TypeVectorInt32:[0-9]+]] [[TypeInt32]] 3
57+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr8VectorInt32:[0-9]+]] 0 [[TypeVectorInt32]]
58+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr0Int8:[0-9]+]] 8 [[TypeInt8]]
59+
; CHECK: {{[0-9]*}} TypeStruct [[BID:[0-9]+]] {{[0-9]+}} [[TypePtr0Int8]]
5660
; CHECK: {{[0-9]*}} TypeStruct [[CID:[0-9]+]] {{[0-9]+}} [[BID]]
5761
; CHECK: {{[0-9]*}} TypeStruct [[AID:[0-9]+]] {{[0-9]+}} [[CID]]
5862
; CHECK: {{[0-9]*}} TypeVoid [[Void:[0-9]+]]
59-
; CHECK: {{[0-9]*}} TypeFunction [[TypeBar1:[0-9]+]] [[Void]] [[SConstOpType]]
60-
; CHECK: {{[0-9]*}} Variable [[TPointer]] [[Var:[0-9]+]]
61-
; CHECK: {{[0-9]*}} SpecConstantOp [[SConstOpType]] [[SConstOp:[0-9]+]] 70 [[Var]]
63+
; CHECK: {{[0-9]*}} TypePointer [[TypePtr5Int8:[0-9]+]] 5 [[TypeInt8]]
64+
; CHECK: {{[0-9]*}} TypeFunction [[TypeBar1:[0-9]+]] [[Void]] [[TypePtr5Int8]]
65+
; CHECK: {{[0-9]*}} Variable [[TypePtr5ArrayInt32]] [[Var:[0-9]+]]
66+
; CHECK: {{[0-9]*}} SpecConstantOp [[TypePtr5Int32]] [[SConstOp:[0-9]+]] 70 [[Var]]
6267
; CHECK: {{[0-9]*}} Variable {{[0-9]+}} {{[0-9]+}} 5 [[SConstOp]]
6368

6469
; CHECK-NOT: {{[0-9]*}} Capability

test/type-scavenger/globals.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,37 @@ target triple = "spir-unknown-unknown"
99
; CHECK-DAG: TypeInt [[I8:[0-9]+]] 8 0
1010
; CHECK-DAG: TypeInt [[I16:[0-9]+]] 16 0
1111
; CHECK-DAG: TypeInt [[I32:[0-9]+]] 32 0
12+
; CHECK-DAG: TypeInt [[I64:[0-9]+]] 64 0
1213
; CHECK-DAG: TypePointer [[I8PTR:[0-9]+]] 5 [[I8]]
1314
; CHECK-DAG: TypePointer [[I16PTR:[0-9]+]] 5 [[I16]]
1415
; CHECK-DAG: TypePointer [[I16PTRPTR:[0-9]+]] 5 [[I16PTR]]
1516
; CHECK-DAG: TypePointer [[I32PTR:[0-9]+]] 5 [[I32]]
17+
; CHECK-DAG: TypePointer [[I32PTRPTR:[0-9]+]] 5 [[I32PTR]]
1618
; CHECK-DAG: TypeArray [[I8PTRx2:[0-9]+]] [[I8PTR]]
1719
; CHECK-DAG: TypePointer [[I8PTRx2PTR:[0-9]+]] 5 [[I8PTRx2]]
20+
; CHECK-DAG: TypeArray [[I32x4:[0-9]+]] [[I32]]
21+
; CHECK-DAG: TypeArray [[I32x3x4:[0-9]+]] [[I32x4]]
22+
; CHECK-DAG: TypeArray [[I32x2x3x4:[0-9]+]] [[I32x3x4]]
23+
; CHECK-DAG: TypePointer [[I32x2x3x4PTR:[0-9]+]] 5 [[I32x2x3x4]]
1824
; CHECK: Variable [[I16PTR]] [[A:[0-9]+]] 5
1925
; CHECK: Variable [[I32PTR]] [[B:[0-9]+]] 5
2026
; CHECK: Variable [[I16PTRPTR]] [[C:[0-9]+]] 5 [[A]]
2127
; CHECK: SpecConstantOp [[I8PTR]] [[AI8:[0-9]+]] 124 [[A]]
2228
; CHECK: SpecConstantOp [[I8PTR]] [[BI8:[0-9]+]] 124 [[B]]
2329
; CHECK: ConstantComposite [[I8PTRx2]] [[DINIT:[0-9]+]] [[AI8]] [[BI8]]
2430
; CHECK: Variable [[I8PTRx2PTR]] [[D:[0-9]+]] 5 [[DINIT]]
31+
; CHECK: ConstantComposite [[I32x2x3x4]] [[FINIT:[0-9]+]]
32+
; CHECK: Variable [[I32x2x3x4PTR]] [[F:[0-9]+]] 5 [[FINIT]]
33+
; CHECK: SpecConstantOp [[I32PTR]] [[GINIT:[0-9]+]] 70 [[F]]
34+
; CHECK: Variable [[I32PTRPTR]] [[G:[0-9]+]] 5 [[GINIT]]
2535

2636
@a = addrspace(1) global i16 0
2737
@b = external addrspace(1) global i32
2838
@c = addrspace(1) global ptr addrspace(1) @a
2939
@d = addrspace(1) global [2 x ptr addrspace(1)] [ptr addrspace(1) @a, ptr addrspace(1) @b]
3040
;@e = global [1 x ptr] [ptr @foo]
41+
@f = addrspace(1) global [2 x [3 x [4 x i32]]] [[3 x [4 x i32]] [[4 x i32] [i32 1, i32 2, i32 3, i32 4], [4 x i32] [i32 1, i32 2, i32 3, i32 4], [4 x i32] [i32 1, i32 2, i32 3, i32 4]], [3 x [4 x i32]] [[4 x i32] [i32 1, i32 2, i32 3, i32 4], [4 x i32] [i32 1, i32 2, i32 3, i32 4], [4 x i32] [i32 1, i32 2, i32 3, i32 4]]]
42+
@g = addrspace(1) global ptr addrspace(1) getelementptr inbounds ([2 x [3 x [4 x i32]]], ptr addrspace(1) @f, i64 0, i64 1, i64 2, i64 3)
3143

3244
; Function Attrs: nounwind
3345
define spir_kernel void @foo() {

0 commit comments

Comments
 (0)