[llvm] [VectorCombine] Use isSafeToSpeculativelyExecute to guard VP scalarization (PR #69494)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 18 11:54:39 PDT 2023
================
@@ -825,23 +825,28 @@ bool VectorCombine::scalarizeVPIntrinsic(Instruction &I) {
ElementCount EC = cast<VectorType>(Op0->getType())->getElementCount();
Value *EVL = VPI.getArgOperand(3);
const DataLayout &DL = VPI.getModule()->getDataLayout();
- bool MustHaveNonZeroVL =
- IntrID == Intrinsic::vp_sdiv || IntrID == Intrinsic::vp_udiv ||
- IntrID == Intrinsic::vp_srem || IntrID == Intrinsic::vp_urem;
-
- if (!MustHaveNonZeroVL || isKnownNonZero(EVL, DL, 0, &AC, &VPI, &DT)) {
- Value *ScalarOp0 = getSplatValue(Op0);
- Value *ScalarOp1 = getSplatValue(Op1);
- Value *ScalarVal =
- ScalarIntrID
- ? Builder.CreateIntrinsic(VecTy->getScalarType(), *ScalarIntrID,
- {ScalarOp0, ScalarOp1})
- : Builder.CreateBinOp((Instruction::BinaryOps)(*FunctionalOpcode),
- ScalarOp0, ScalarOp1);
- replaceValue(VPI, *Builder.CreateVectorSplat(EC, ScalarVal));
- return true;
- }
- return false;
+
+ bool SafeToSpeculate;
+ if (ScalarIntrID)
+ SafeToSpeculate = Intrinsic::getAttributes(I.getContext(), *ScalarIntrID)
+ .hasFnAttr(Attribute::AttrKind::Speculatable);
+ else
+ SafeToSpeculate = isSafeToSpeculativelyExecuteWithOpcode(
+ *FunctionalOpcode, &VPI, nullptr, &AC, &DT);
+ if (!SafeToSpeculate && !isKnownNonZero(EVL, DL, 0, &AC, &VPI, &DT))
----------------
nikic wrote:
I realize that this is pre-existing code, but this isKnownNonZero check shouldn't be there at all. This is doubly incorrect, because it does not handle poison values correctly, and because it does not account for INT_MIN / -1 UB (unless that does not exist for the VP intrinsics, in which case it needs to be specified in LangRef).
https://github.com/llvm/llvm-project/pull/69494
More information about the llvm-commits
mailing list