[llvm] [InstCombine] Fold bitcast into vp.load (PR #192173)
Min-Yih Hsu via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 16 10:18:53 PDT 2026
================
@@ -3971,6 +3971,48 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
break;
}
+ case Intrinsic::vp_load: {
+ auto *VPI = cast<VPIntrinsic>(II);
+ // Fold away bit casts of the loaded value by loading the desired type,
+ // if the mask is all-ones.
+ Value *Mask = VPI->getMaskParam();
+ Value *EVL = VPI->getVectorLengthParam();
+ if (!isa<Constant>(Mask) || !cast<Constant>(Mask)->isAllOnesValue() ||
+ !II->hasOneUse())
+ break;
+
+ const DataLayout &DL = II->getDataLayout();
+ auto *Cast = dyn_cast<CastInst>(II->user_back());
+ if (!Cast || !Cast->isNoopCast(DL) || !isa<VectorType>(Cast->getDestTy()))
+ break;
+ VectorType *OrigVecTy = cast<VectorType>(II->getType());
+ Align OrigAlign =
+ DL.getValueOrABITypeAlignment(VPI->getPointerAlignment(), OrigVecTy);
+ ElementCount OrigVecCnt = OrigVecTy->getElementCount();
+ VectorType *NewVecTy = cast<VectorType>(Cast->getDestTy());
+ ElementCount NewVecCnt = NewVecTy->getElementCount();
+
+ // Right now we only support cases where
+ // the NewVec is longer, because for cases where
+ // it's shorter, we have to be sure that EVL can be
+ // exactly divided, otherwise it might page fault / yield
----------------
mshockwave wrote:
it really depends on how you divide the EVL: if we take the floor, then yes, we'll only get incorrect result. But if we take the ceil (i.e. rounding up), `nxv4i16 w/ EVL = 5` becomes `nxv2i32 w/ EVL=3`: we originally only read 2 bytes x 5 = 10 bytes, now we're reading 4 bytes x 3 = 12 bytes. So we might page fault.
https://github.com/llvm/llvm-project/pull/192173
More information about the llvm-commits
mailing list