Skip to content

Guard "[ConstantFold]Remove typed pointer specific folds" with INTEL_SYCL_OPAQUEPOINTER_READY #10415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
if (SrcTy == DestTy)
return V; // no-op cast

#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
// Check to see if we are casting a pointer to an aggregate to a pointer to
// the first element. If so, return the appropriate GEP instruction.
if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
if (PTy->getAddressSpace() == DPTy->getAddressSpace() &&
!PTy->isOpaque() && !DPTy->isOpaque() &&
PTy->getNonOpaquePointerElementType()->isSized()) {
SmallVector<Value*, 8> IdxList;
Value *Zero =
Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
IdxList.push_back(Zero);
Type *ElTy = PTy->getNonOpaquePointerElementType();
while (ElTy && ElTy != DPTy->getNonOpaquePointerElementType()) {
ElTy = GetElementPtrInst::getTypeAtIndex(ElTy, (uint64_t)0);
IdxList.push_back(Zero);
}

if (ElTy == DPTy->getNonOpaquePointerElementType())
// This GEP is inbounds because all indices are zero.
return ConstantExpr::getInBoundsGetElementPtr(
PTy->getNonOpaquePointerElementType(), V, IdxList);
}
#endif //INTEL_SYCL_OPAQUEPOINTER_READY

// Handle casts from one vector constant to another. We know that the src
// and dest type have the same size (otherwise its an illegal cast).
if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Expand Down Expand Up @@ -2010,6 +2035,12 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);

auto IsNoOp = [&]() {
#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
// For non-opaque pointers having multiple indices will change the result
// type of the GEP.
if (!C->getType()->getScalarType()->isOpaquePointerTy() && Idxs.size() != 1)
return false;
#endif //INTEL_SYCL_OPAQUEPOINTER_READY
// Avoid losing inrange information.
if (InRangeIndex)
return false;
Expand Down Expand Up @@ -2058,10 +2089,41 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
}
}

if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
if (auto *GEP = dyn_cast<GEPOperator>(CE))
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
return C;
#ifndef INTEL_SYCL_OPAQUEPOINTER_READY
// Attempt to fold casts to the same type away. For example, folding:
//
// i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
// i64 0, i64 0)
// into:
//
// i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
//
// Don't fold if the cast is changing address spaces.
Constant *Idx0 = cast<Constant>(Idxs[0]);
if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
PointerType *SrcPtrTy =
dyn_cast<PointerType>(CE->getOperand(0)->getType());
PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
if (SrcPtrTy && DstPtrTy && !SrcPtrTy->isOpaque() &&
!DstPtrTy->isOpaque()) {
ArrayType *SrcArrayTy =
dyn_cast<ArrayType>(SrcPtrTy->getNonOpaquePointerElementType());
ArrayType *DstArrayTy =
dyn_cast<ArrayType>(DstPtrTy->getNonOpaquePointerElementType());
if (SrcArrayTy && DstArrayTy
&& SrcArrayTy->getElementType() == DstArrayTy->getElementType()
&& SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
return ConstantExpr::getGetElementPtr(SrcArrayTy,
(Constant *)CE->getOperand(0),
Idxs, InBounds, InRangeIndex);
}
}
#endif //INTEL_SYCL_OPAQUEPOINTER_READY
}

// Check to see if any array indices are not within the corresponding
// notional array or vector bounds. If so, try to determine if they can be
Expand Down