Skip to content

Commit 5bb0f8e

Browse files
ViacheslavRbigcbot
authored andcommitted
Reduce usage of pointer element types (24).
This change replaces calls to getNonOpaquePtrEltTy with element type information got through other means. This change is part of the effort to support opaque pointers in newer LLVM versions.
1 parent 8338901 commit 5bb0f8e

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

IGC/AdaptorOCL/ResolveConstExprCalls.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SPDX-License-Identifier: MIT
1414

1515
#include "llvmWrapper/IR/Attributes.h"
1616
#include "llvmWrapper/IR/Type.h"
17+
#include "llvmWrapper/IR/Function.h"
1718

1819
#include <llvm/Pass.h>
1920
#include <llvm/IR/Module.h>
@@ -124,12 +125,12 @@ bool transformConstExprCastCall(CallInst& Call) {
124125
// sized type and the sized type has to have the same size as the old type.
125126
if (ParamTy != ActTy && IGCLLVM::hasParamAttr(CallerPAL, i, llvm::Attribute::ByVal)) {
126127
PointerType* ParamPTy = dyn_cast<PointerType>(ParamTy);
127-
if (!ParamPTy || !IGCLLVM::getNonOpaquePtrEltTy(ParamPTy)->isSized())
128+
if (!ParamPTy || !IGCLLVM::getArg(*Callee, i)->getParamByValType()->isSized())
128129
return false;
129130

130131
Type* CurElTy = Call.getParamByValType(i);
131132
if (DL.getTypeAllocSize(CurElTy) !=
132-
DL.getTypeAllocSize(IGCLLVM::getNonOpaquePtrEltTy(ParamPTy)))
133+
DL.getTypeAllocSize(IGCLLVM::getArg(*Callee, i)->getParamByValType()))
133134
return false;
134135
}
135136
}
@@ -177,7 +178,7 @@ bool transformConstExprCastCall(CallInst& Call) {
177178
// Add any parameter attributes.
178179
if (IGCLLVM::hasParamAttr(CallerPAL, i, llvm::Attribute::ByVal)) {
179180
IGCLLVM::AttrBuilder AB(FT->getContext(), IGCLLVM::getParamAttrs(CallerPAL, i));
180-
AB.addByValAttr(IGCLLVM::getNonOpaquePtrEltTy(NewArg->getType()));
181+
AB.addByValAttr(IGCLLVM::getArg(*Callee, i)->getParamByValType());
181182
ArgAttrs.push_back(AttributeSet::get(Ctx, AB));
182183
}
183184
else

IGC/AdaptorOCL/SPIRV/SPIRVReader.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,9 +2700,7 @@ SPIRVToLLVM::postProcessFunctionsReturnStruct(Function *F) {
27002700
Args.insert(Args.begin(), Alloca);
27012701
auto NewCI = CallInst::Create(NewF, Args, "", CI);
27022702
NewCI->setCallingConv(CI->getCallingConv());
2703-
auto Load = new LoadInst(
2704-
IGCLLVM::getNonOpaquePtrEltTy(Alloca->getType()),
2705-
Alloca,"",CI);
2703+
auto Load = new LoadInst(Alloca->getAllocatedType(), Alloca, "", CI);
27062704
CI->replaceAllUsesWith(Load);
27072705
CI->eraseFromParent();
27082706
}
@@ -3483,7 +3481,9 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
34833481
for (unsigned I = 0, E = CS->getNumOperands(); I != E; I++)
34843482
{
34853483
auto *op = CS->getOperand(I);
3486-
auto *pGEP = IRB.CreateConstInBoundsGEP2_32(IGCLLVM::getNonOpaquePtrEltTy(pointer->getType()), pointer, 0, I);
3484+
Type* elTy = isa<AllocaInst>(pointer) ? cast<AllocaInst>(pointer)->getAllocatedType()
3485+
: cast<GetElementPtrInst>(pointer)->getResultElementType();
3486+
auto* pGEP = IRB.CreateConstInBoundsGEP2_32(elTy, pointer, 0, I);
34873487
if (auto *InnerCS = dyn_cast<ConstantStruct>(op))
34883488
LowerConstantStructStore(InnerCS, pGEP);
34893489
else
@@ -3520,7 +3520,7 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
35203520
IGC_ASSERT_MESSAGE(BB, "Invalid BB");
35213521
auto val = transValue(BL->getSrc(), F, BB);
35223522
LoadInst* LI = new LoadInst(
3523-
IGCLLVM::getNonOpaquePtrEltTy(val->getType()),
3523+
transType(BL->getType()),
35243524
val,
35253525
BV->getName(),
35263526
BL->hasDecorate(DecorationVolatile) || BL->SPIRVMemoryAccess::isVolatile() != 0,
@@ -3555,11 +3555,13 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
35553555
Type* Int8Ty = Type::getInt8Ty(Dst->getContext());
35563556
llvm::Value* Src = ConstantInt::get(Int8Ty, 0);
35573557
llvm::Value* newDst = Dst;
3558-
if (!IGCLLVM::getNonOpaquePtrEltTy(Dst->getType())->isIntegerTy(8)) {
3559-
Type* Int8PointerTy = Type::getInt8PtrTy(Dst->getContext(),
3560-
Dst->getType()->getPointerAddressSpace());
3561-
newDst = llvm::BitCastInst::CreatePointerCast(Dst,
3562-
Int8PointerTy, "", BB);
3558+
if (!IGCLLVM::isOpaquePointerTy(Dst->getType())) {
3559+
if (!IGCLLVM::getNonOpaquePtrEltTy(Dst->getType())->isIntegerTy(8)) { // Legacy code: getNonOpaquePtrEltTy
3560+
Type* Int8PointerTy = Type::getInt8PtrTy(Dst->getContext(),
3561+
Dst->getType()->getPointerAddressSpace());
3562+
newDst = llvm::BitCastInst::CreatePointerCast(Dst,
3563+
Int8PointerTy, "", BB);
3564+
}
35633565
}
35643566
CI = Builder.CreateMemSet(newDst, Src, Size, IGCLLVM::getCorrectAlign(Align), IsVolatile);
35653567
}
@@ -3640,7 +3642,7 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
36403642
case OpInBoundsPtrAccessChain: {
36413643
auto AC = static_cast<SPIRVAccessChainBase *>(BV);
36423644
auto Base = transValue(AC->getBase(), F, BB);
3643-
Type *BaseTy = IGCLLVM::getNonOpaquePtrEltTy(Base->getType());
3645+
Type* BaseTy = transType(AC->getBase()->getType()->getPointerElementType());
36443646
auto Index = transValue(AC->getIndices(), F, BB);
36453647
if (!AC->hasPtrIndex())
36463648
Index.insert(Index.begin(), getInt32(M, 0));
@@ -3841,7 +3843,7 @@ SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
38413843
auto func = transValue(BC->getCalledValue(), F, BB);
38423844
auto Call = CallInst::Create(
38433845
llvm::cast<llvm::FunctionType>(
3844-
IGCLLVM::getNonOpaquePtrEltTy(func->getType())),
3846+
transType(BC->getCalledValue()->getType()->getPointerElementType())),
38453847
func,
38463848
transValue(BC->getArgumentValues(), F, BB),
38473849
BC->getName(),
@@ -4243,8 +4245,10 @@ SPIRVToLLVM::transFunction(SPIRVFunction *BF) {
42434245
#if LLVM_VERSION_MAJOR >= 12
42444246
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind);
42454247
Type *AttrTy = nullptr;
4246-
if (LLVMKind == Attribute::AttrKind::ByVal || LLVMKind == Attribute::AttrKind::StructRet)
4247-
AttrTy = IGCLLVM::getNonOpaquePtrEltTy(I->getType());
4248+
if (LLVMKind == Attribute::AttrKind::ByVal)
4249+
AttrTy = I->getParamByValType();
4250+
else if (LLVMKind == Attribute::AttrKind::StructRet)
4251+
AttrTy = I->getParamStructRetType();
42484252

42494253
// Make sure to use a correct constructor for a typed/typeless attribute
42504254
auto A = AttrTy ? Attribute::get(*Context, LLVMKind, AttrTy)

IGC/AdaptorOCL/preprocess_spvir/HandleSPIRVDecorations/HandleSpirvDecorationMetadata.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,6 @@ void HandleSpirvDecorationMetadata::handleCacheControlINTELForOCL1DBlockPrefetch
529529
Function* F = I.getCalledFunction();
530530
IGC_ASSERT(F);
531531

532-
Type* pointeeTy = IGCLLVM::getNonOpaquePtrEltTy(I.getArgOperand(0)->getType());
533-
IGC_ASSERT(pointeeTy->isIntegerTy());
534-
535532
StringRef numElementsFromName = Matches[3] != "" ? Matches[3] : "1";
536533
uint32_t numElementsToPrefetch = std::stoi(numElementsFromName.str());
537534
IGC_ASSERT(numElementsToPrefetch == 1 ||
@@ -540,10 +537,17 @@ void HandleSpirvDecorationMetadata::handleCacheControlINTELForOCL1DBlockPrefetch
540537
numElementsToPrefetch == 8 ||
541538
numElementsToPrefetch == 16);
542539

543-
uint32_t typeSizeInBytes = pointeeTy->getIntegerBitWidth() / 8;
544-
545-
Value* numBytesArg =
546-
(ConstantInt::get(Type::getInt32Ty(I.getContext()), (typeSizeInBytes * numElementsToPrefetch)));
540+
uint32_t typeSizeInBytes = 0;
541+
if (Matches[2].equals("uc"))
542+
typeSizeInBytes = 1;
543+
else if (Matches[2].equals("us"))
544+
typeSizeInBytes = 2;
545+
else if (Matches[2].equals("ui"))
546+
typeSizeInBytes = 4;
547+
else if (Matches[2].equals("ul"))
548+
typeSizeInBytes = 8;
549+
else
550+
IGC_ASSERT(0 && "Unsupported type prefetch!");
547551

548552
std::string typeName;
549553
switch (typeSizeInBytes)
@@ -565,6 +569,9 @@ void HandleSpirvDecorationMetadata::handleCacheControlINTELForOCL1DBlockPrefetch
565569
break;
566570
}
567571

572+
Value* numBytesArg =
573+
(ConstantInt::get(Type::getInt32Ty(I.getContext()), (typeSizeInBytes * numElementsToPrefetch)));
574+
568575
SmallVector<Value*, 3> args(I.args());
569576
args.push_back(numBytesArg);
570577
args.push_back(ConstantInt::get(Type::getInt32Ty(I.getContext()), cacheControl.value));

0 commit comments

Comments
 (0)