Skip to content

Commit e44bdbb

Browse files
[Backport to 16] Fix globals' type deduction in TypeScavenger (KhronosGroup#3243)
This is a follow up to KhronosGroup#3240. It backports one more commit that fixes type dedcuction in type scavenger, and also adds a small fix for the previous PR that resulted in double use of type scavenger. --------- Co-authored-by: Maksim Shelegov <maksim.shelegov@intel.com>
1 parent b09a6e8 commit e44bdbb

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

lib/SPIRV/SPIRVTypeScavenger.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,12 @@ void SPIRVTypeScavenger::typeGlobalValue(GlobalValue &GV, Constant *Init) {
584584
auto It = DeducedTypes.find(C);
585585
if (It != DeducedTypes.end())
586586
return It->second;
587+
} else if (auto *GEP = dyn_cast<GEPOperator>(C)) {
588+
auto *ResultTy =
589+
TypedPointerType::get(GEP->getResultElementType(),
590+
GEP->getType()->getPointerAddressSpace());
591+
DeducedTypes[C] = ResultTy;
592+
return ResultTy;
587593
}
588594

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

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,17 @@ SPIRVValue *LLVMToSPIRVBase::transConstant(Value *V) {
14141414
return BM->addCompositeConstant(ExpectedType, BV);
14151415
}
14161416

1417-
if (auto ConstUE = dyn_cast<ConstantExpr>(V)) {
1418-
auto Inst = ConstUE->getAsInstruction();
1417+
if (auto *ConstUE = dyn_cast<ConstantExpr>(V)) {
1418+
if (auto *GEP = dyn_cast<GEPOperator>(ConstUE)) {
1419+
std::vector<SPIRVValue *> Indices;
1420+
for (unsigned I = 0, E = GEP->getNumIndices(); I != E; ++I)
1421+
Indices.push_back(transValue(GEP->getOperand(I + 1), nullptr));
1422+
auto *TransPointerOperand = transValue(GEP->getPointerOperand(), nullptr);
1423+
SPIRVType *TranslatedTy = transScavengedType(GEP);
1424+
return BM->addPtrAccessChainInst(TranslatedTy, TransPointerOperand,
1425+
Indices, nullptr, GEP->isInBounds());
1426+
}
1427+
auto *Inst = ConstUE->getAsInstruction();
14191428
SPIRVDBG(dbgs() << "ConstantExpr: " << *ConstUE << '\n';
14201429
dbgs() << "Instruction: " << *Inst << '\n';)
14211430
auto BI = transValue(Inst, nullptr, false);
@@ -5126,9 +5135,6 @@ bool LLVMToSPIRVBase::translate() {
51265135
if (isEmptyLLVMModule(M))
51275136
BM->addCapability(CapabilityLinkage);
51285137

5129-
// Use the type scavenger to recover pointer element types.
5130-
Scavenger = std::make_unique<SPIRVTypeScavenger>(*M);
5131-
51325138
if (!lowerBuiltinCallsToVariables(M))
51335139
return false;
51345140

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)