Skip to content

Commit ae2218e

Browse files
jcranmer-intelPavel V Chupin
authored andcommitted
Remove more uses of getPointerElementType (part of issue #1444).
This is the easiest tranche of changes: where the pointer element type is usually retrieved by looking a little further afield for the element type, or via scavenging from sret/byval parameters. Original commit: KhronosGroup/SPIRV-LLVM-Translator@33012ca
1 parent b14272f commit ae2218e

File tree

8 files changed

+58
-58
lines changed

8 files changed

+58
-58
lines changed

llvm-spirv/lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ static size_t getOCLCpp11AtomicMaxNumOps(StringRef Name) {
7070
.Default(0);
7171
}
7272

73+
static Type *getBlockStructType(Value *Parameter) {
74+
// In principle, this information should be passed to us from Clang via
75+
// an elementtype attribute. However, said attribute requires that the
76+
// function call be an intrinsic, which it is not. Instead, we rely on being
77+
// able to trace this to the declaration of a variable: OpenCL C specification
78+
// section 6.12.5 should guarantee that we can do this.
79+
Value *UnderlyingObject = Parameter->stripPointerCasts();
80+
Type *ParamType = nullptr;
81+
if (auto *GV = dyn_cast<GlobalValue>(UnderlyingObject))
82+
ParamType = GV->getValueType();
83+
else if (auto *Alloca = dyn_cast<AllocaInst>(UnderlyingObject))
84+
ParamType = Alloca->getAllocatedType();
85+
else
86+
llvm_unreachable("Blocks in OpenCL C must be traceable to allocation site");
87+
return ParamType;
88+
}
89+
7390
class OCLToSPIRVBase : public InstVisitor<OCLToSPIRVBase> {
7491
public:
7592
OCLToSPIRVBase() : M(nullptr), Ctx(nullptr), CLVer(0) {}
@@ -674,11 +691,9 @@ CallInst *OCLToSPIRVBase::visitCallAtomicCmpXchg(CallInst *CI) {
674691
M, CI,
675692
[&](CallInst *CI, std::vector<Value *> &Args, Type *&RetTy) {
676693
Expected = Args[1]; // temporary save second argument.
677-
Args[1] = new LoadInst(Args[1]->getType()->getPointerElementType(),
678-
Args[1], "exp", false, CI);
679694
RetTy = Args[2]->getType();
680-
assert(Args[0]->getType()->getPointerElementType()->isIntegerTy() &&
681-
Args[1]->getType()->isIntegerTy() &&
695+
Args[1] = new LoadInst(RetTy, Args[1], "exp", false, CI);
696+
assert(Args[1]->getType()->isIntegerTy() &&
682697
Args[2]->getType()->isIntegerTy() &&
683698
"In SPIR-V 1.0 arguments of OpAtomicCompareExchange must be "
684699
"an integer type scalars");
@@ -1584,9 +1599,7 @@ void OCLToSPIRVBase::visitCallEnqueueKernel(CallInst *CI,
15841599
// Param Size: Size of block literal structure
15851600
// Param Aligment: Aligment of block literal structure
15861601
// TODO: these numbers should be obtained from block literal structure
1587-
Type *ParamType = getUnderlyingObject(BlockLiteral)->getType();
1588-
if (PointerType *PT = dyn_cast<PointerType>(ParamType))
1589-
ParamType = PT->getPointerElementType();
1602+
Type *ParamType = getBlockStructType(BlockLiteral);
15901603
Args.push_back(getInt32(M, DL.getTypeStoreSize(ParamType)));
15911604
Args.push_back(getInt32(M, DL.getPrefTypeAlignment(ParamType)));
15921605

@@ -1636,10 +1649,7 @@ void OCLToSPIRVBase::visitCallKernelQuery(CallInst *CI,
16361649
M, CI,
16371650
[=](CallInst *CI, std::vector<Value *> &Args) {
16381651
Value *Param = *Args.rbegin();
1639-
Type *ParamType = getUnderlyingObject(Param)->getType();
1640-
if (PointerType *PT = dyn_cast<PointerType>(ParamType)) {
1641-
ParamType = PT->getPointerElementType();
1642-
}
1652+
Type *ParamType = getBlockStructType(Param);
16431653
// Last arg corresponds to SPIRV Param operand.
16441654
// Insert Invoke in front of Param.
16451655
// Add Param Size and Param Align at the end.

llvm-spirv/lib/SPIRV/OCLUtil.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,22 +1252,18 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
12521252
else
12531253
addUnsignedArg(1);
12541254
} else if (NameRef.startswith("intel_sub_group_block_write")) {
1255-
// distinguish write to image and other data types as position
1256-
// of uint argument is different though name is the same.
1257-
auto *Arg0Ty = getArgTy(0);
1258-
if (Arg0Ty->isPointerTy() &&
1259-
Arg0Ty->getPointerElementType()->isIntegerTy()) {
1255+
// distinguish write to image and other data types based on number of
1256+
// arguments--images have one more argument.
1257+
if (F->getFunctionType()->getNumParams() == 2) {
12601258
addUnsignedArg(0);
12611259
addUnsignedArg(1);
12621260
} else {
12631261
addUnsignedArg(2);
12641262
}
12651263
} else if (NameRef.startswith("intel_sub_group_block_read")) {
1266-
// distinguish read from image and other data types as position
1267-
// of uint argument is different though name is the same.
1268-
auto *Arg0Ty = getArgTy(0);
1269-
if (Arg0Ty->isPointerTy() &&
1270-
Arg0Ty->getPointerElementType()->isIntegerTy()) {
1264+
// distinguish read from image and other data types based on number of
1265+
// arguments--images have one more argument.
1266+
if (F->getFunctionType()->getNumParams() == 1) {
12711267
setArgAttr(0, SPIR::ATTR_CONST);
12721268
addUnsignedArg(0);
12731269
}

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,9 +3389,10 @@ void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) {
33893389
if (!AnnotStr.empty()) {
33903390
auto *GS = Builder.CreateGlobalStringPtr(AnnotStr);
33913391

3392-
auto GEP = Builder.CreateConstInBoundsGEP2_32(AllocatedTy, AL, 0, I);
3392+
auto *GEP = cast<GetElementPtrInst>(
3393+
Builder.CreateConstInBoundsGEP2_32(AllocatedTy, AL, 0, I));
33933394

3394-
Type *IntTy = GEP->getType()->getPointerElementType()->isIntegerTy()
3395+
Type *IntTy = GEP->getResultElementType()->isIntegerTy()
33953396
? GEP->getType()
33963397
: Int8PtrTyPrivate;
33973398

llvm-spirv/lib/SPIRV/SPIRVRegularizeLLVM.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,41 +344,38 @@ void SPIRVRegularizeLLVMBase::lowerUMulWithOverflow(
344344

345345
void SPIRVRegularizeLLVMBase::expandVEDWithSYCLTypeSRetArg(Function *F) {
346346
auto Attrs = F->getAttributes();
347+
StructType *SRetTy = cast<StructType>(Attrs.getParamStructRetType(0));
347348
Attrs = Attrs.removeParamAttribute(F->getContext(), 0, Attribute::StructRet);
348349
std::string Name = F->getName().str();
349350
CallInst *OldCall = nullptr;
350351
mutateFunction(
351352
F,
352353
[=, &OldCall](CallInst *CI, std::vector<Value *> &Args, Type *&RetTy) {
353354
Args.erase(Args.begin());
354-
auto *SRetPtrTy = cast<PointerType>(CI->getOperand(0)->getType());
355-
auto *ET = SRetPtrTy->getPointerElementType();
356-
RetTy = cast<StructType>(ET)->getElementType(0);
355+
RetTy = SRetTy->getElementType(0);
357356
OldCall = CI;
358357
return Name;
359358
},
360359
[=, &OldCall](CallInst *NewCI) {
361360
IRBuilder<> Builder(OldCall);
362-
auto *SRetPtrTy = cast<PointerType>(OldCall->getOperand(0)->getType());
363-
auto *ET = SRetPtrTy->getPointerElementType();
364-
Value *Target = Builder.CreateStructGEP(ET, OldCall->getOperand(0), 0);
361+
Value *Target =
362+
Builder.CreateStructGEP(SRetTy, OldCall->getOperand(0), 0);
365363
return Builder.CreateStore(NewCI, Target);
366364
},
367365
nullptr, &Attrs, true);
368366
}
369367

370368
void SPIRVRegularizeLLVMBase::expandVIDWithSYCLTypeByValComp(Function *F) {
371369
auto Attrs = F->getAttributes();
370+
auto *CompPtrTy = cast<StructType>(Attrs.getParamByValType(1));
372371
Attrs = Attrs.removeParamAttribute(F->getContext(), 1, Attribute::ByVal);
373372
std::string Name = F->getName().str();
374373
mutateFunction(
375374
F,
376375
[=](CallInst *CI, std::vector<Value *> &Args) {
377-
auto *CompPtrTy = cast<PointerType>(CI->getOperand(1)->getType());
378-
auto *ET = CompPtrTy->getPointerElementType();
379-
Type *HalfTy = cast<StructType>(ET)->getElementType(0);
376+
Type *HalfTy = CompPtrTy->getElementType(0);
380377
IRBuilder<> Builder(CI);
381-
auto *Target = Builder.CreateStructGEP(ET, CI->getOperand(1), 0);
378+
auto *Target = Builder.CreateStructGEP(CompPtrTy, CI->getOperand(1), 0);
382379
Args[1] = Builder.CreateLoad(HalfTy, Target);
383380
return Name;
384381
},
@@ -392,9 +389,8 @@ void SPIRVRegularizeLLVMBase::expandSYCLTypeUsing(Module *M) {
392389
for (auto &F : *M) {
393390
if (F.getName().startswith("_Z28__spirv_VectorExtractDynamic") &&
394391
F.hasStructRetAttr()) {
395-
auto *SRetPtrTy = cast<PointerType>(F.getArg(0)->getType());
396-
if (isSYCLHalfType(SRetPtrTy->getPointerElementType()) ||
397-
isSYCLBfloat16Type(SRetPtrTy->getPointerElementType()))
392+
auto *SRetTy = F.getParamStructRetType(0);
393+
if (isSYCLHalfType(SRetTy) || isSYCLBfloat16Type(SRetTy))
398394
ToExpandVEDWithSYCLTypeSRetArg.push_back(&F);
399395
else
400396
llvm_unreachable("The return type of the VectorExtractDynamic "
@@ -403,8 +399,7 @@ void SPIRVRegularizeLLVMBase::expandSYCLTypeUsing(Module *M) {
403399
}
404400
if (F.getName().startswith("_Z27__spirv_VectorInsertDynamic") &&
405401
F.getArg(1)->getType()->isPointerTy()) {
406-
auto *CompPtrTy = cast<PointerType>(F.getArg(1)->getType());
407-
auto *ET = CompPtrTy->getPointerElementType();
402+
auto *ET = F.getParamByValType(1);
408403
if (isSYCLHalfType(ET) || isSYCLBfloat16Type(ET))
409404
ToExpandVIDWithSYCLTypeByValComp.push_back(&F);
410405
else

llvm-spirv/lib/SPIRV/SPIRVToOCL12.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ Instruction *SPIRVToOCL12Base::visitCallSPIRVAtomicLoad(CallInst *CI) {
151151
Args.resize(1);
152152
// There is no atomic_load in OpenCL 1.2 spec.
153153
// Emit this builtin via call of atomic_add(*p, 0).
154-
Type *ptrElemTy = Args[0]->getType()->getPointerElementType();
155-
Args.push_back(Constant::getNullValue(ptrElemTy));
156-
return mapAtomicName(OpAtomicIAdd, ptrElemTy);
154+
Type *PtrElemTy = CI->getType();
155+
Args.push_back(Constant::getNullValue(PtrElemTy));
156+
return mapAtomicName(OpAtomicIAdd, PtrElemTy);
157157
},
158158
&Attrs);
159159
}
@@ -165,9 +165,9 @@ Instruction *SPIRVToOCL12Base::visitCallSPIRVAtomicStore(CallInst *CI) {
165165
[=](CallInst *, std::vector<Value *> &Args, Type *&RetTy) {
166166
std::swap(Args[1], Args[3]);
167167
Args.resize(2);
168-
// The type of the value pointed to by Pointer (1st argument)
169-
// must be the same as Result Type.
170-
RetTy = Args[0]->getType()->getPointerElementType();
168+
// The type of the value pointed to by Pointer (1st argument), or the
169+
// value being exchanged (2nd argument) must be the same as Result Type.
170+
RetTy = Args[1]->getType();
171171
return mapAtomicName(OpAtomicExchange, RetTy);
172172
},
173173
[=](CallInst *CI) -> Instruction * { return CI; }, &Attrs);

llvm-spirv/lib/SPIRV/SPIRVToOCL20.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,7 @@ Instruction *SPIRVToOCL20Base::visitCallSPIRVAtomicIncDec(CallInst *CI, Op OC) {
183183
// = 1.
184184
auto Name = OCLSPIRVBuiltinMap::rmap(
185185
OC == OpAtomicIIncrement ? OpAtomicIAdd : OpAtomicISub);
186-
auto Ptr = findFirstPtr(Args);
187-
Type *ValueTy =
188-
cast<PointerType>(Args[Ptr]->getType())->getPointerElementType();
186+
Type *ValueTy = CI->getType();
189187
assert(ValueTy->isIntegerTy());
190188
Args.insert(Args.begin() + 1, llvm::ConstantInt::get(ValueTy, 1));
191189
return Name;
@@ -241,6 +239,8 @@ Instruction *SPIRVToOCL20Base::visitCallSPIRVAtomicCmpExchg(CallInst *CI) {
241239
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
242240
Instruction *PInsertBefore = CI;
243241

242+
Type *MemTy = CI->getType();
243+
244244
return mutateCallInstOCL(
245245
M, CI,
246246
[=](CallInst *, std::vector<Value *> &Args, Type *&RetTy) {
@@ -250,13 +250,12 @@ Instruction *SPIRVToOCL20Base::visitCallSPIRVAtomicCmpExchg(CallInst *CI) {
250250
// OCL built-ins returns boolean value and stores a new/original
251251
// value by pointer passed as 2nd argument (aka expected) while SPIR-V
252252
// instructions returns this new/original value as a resulting value.
253-
AllocaInst *PExpected = new AllocaInst(CI->getType(), 0, "expected",
253+
AllocaInst *PExpected = new AllocaInst(MemTy, 0, "expected",
254254
&(*PInsertBefore->getParent()
255255
->getParent()
256256
->getEntryBlock()
257257
.getFirstInsertionPt()));
258-
PExpected->setAlignment(
259-
Align(CI->getType()->getScalarSizeInBits() / 8));
258+
PExpected->setAlignment(Align(MemTy->getScalarSizeInBits() / 8));
260259
new StoreInst(Args[1], PExpected, PInsertBefore);
261260
unsigned AddrSpc = SPIRAS_Generic;
262261
Type *PtrTyAS = PointerType::getWithSamePointeeType(
@@ -276,9 +275,8 @@ Instruction *SPIRVToOCL20Base::visitCallSPIRVAtomicCmpExchg(CallInst *CI) {
276275
// returning it has to be loaded from the memory where 'expected'
277276
// value is stored. This memory must contain the needed value after a
278277
// call to OCL built-in is completed.
279-
return new LoadInst(
280-
CI->getArgOperand(1)->getType()->getPointerElementType(),
281-
CI->getArgOperand(1), "original", PInsertBefore);
278+
return new LoadInst(MemTy, CI->getArgOperand(1), "original",
279+
PInsertBefore);
282280
},
283281
&Attrs);
284282
}

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ bool lowerBuiltinVariableToCall(GlobalVariable *GV,
18611861

18621862
Value *Ptr = LD->getPointerOperand();
18631863

1864-
if (isa<FixedVectorType>(Ptr->getType()->getPointerElementType())) {
1864+
if (isa<FixedVectorType>(LD->getType())) {
18651865
LD->replaceAllUsesWith(Vectors.back());
18661866
} else {
18671867
auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4680,7 +4680,7 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
46804680
// the original return type.
46814681
if (CI->hasStructRetAttr()) {
46824682
assert(ResTy->isVoidTy() && "Return type is not void");
4683-
ResTy = cast<PointerType>(OpItr->getType())->getPointerElementType();
4683+
ResTy = CI->getParamStructRetType(0);
46844684
OpItr++;
46854685
}
46864686

@@ -4771,7 +4771,7 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
47714771
// the original return type.
47724772
if (CI->hasStructRetAttr()) {
47734773
assert(ResTy->isVoidTy() && "Return type is not void");
4774-
ResTy = cast<PointerType>(OpItr->getType())->getPointerElementType();
4774+
ResTy = CI->getParamStructRetType(0);
47754775
OpItr++;
47764776
}
47774777

@@ -4848,7 +4848,7 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
48484848
// the original return type.
48494849
if (CI->hasStructRetAttr()) {
48504850
assert(ResTy->isVoidTy() && "Return type is not void");
4851-
ResTy = cast<PointerType>(OpItr->getType())->getPointerElementType();
4851+
ResTy = CI->getParamStructRetType(0);
48524852
OpItr++;
48534853
}
48544854

@@ -4953,7 +4953,7 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
49534953
if (!RetTy->isVoidTy()) {
49544954
SPRetTy = transType(RetTy);
49554955
} else if (Args.size() > 0 && F->arg_begin()->hasStructRetAttr()) {
4956-
SPRetTy = transType(F->arg_begin()->getType()->getPointerElementType());
4956+
SPRetTy = transType(F->getParamStructRetType(0));
49574957
Args.erase(Args.begin());
49584958
}
49594959
auto *SPI = SPIRVInstTemplateBase::create(OC);

0 commit comments

Comments
 (0)