[llvm] [VPlan] Constant-fold ActiveLaneMask intrinsics (PR #208852)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 22 01:20:09 PDT 2026
================
@@ -1179,6 +1179,43 @@ VPIRValue *vputils::tryToFoldLiveIns(VPSingleDefRecipe &R,
case Instruction::ExtractElement:
assert(!Ops[0]->getType()->isVectorTy() && "Live-ins should be scalar");
return Ops[0];
+ case VPInstruction::ActiveLaneMask:
+ case VPInstruction::WideActiveLaneMask: {
+ uint64_t Multiplier = 1;
+ if (Opcode == VPInstruction::WideActiveLaneMask) {
+ // Optimizing WideALM can only happen after the Plan is unrolled.
+ if (!Plan.isUnrolled())
+ return nullptr;
+ Multiplier = cast<ConstantInt>(Ops[2])->getZExtValue();
+ Ops.pop_back();
+ }
+ Type *I1Ty = IntegerType::getInt1Ty(Plan.getContext());
+ ElementCount MaxVF =
+ max_element(Plan.vectorFactors(), ElementCount::isKnownLT)
+ ->multiplyCoefficientBy(Multiplier);
+
+ // We rely on the fact that there is one VPlan for fixed-vectors and
+ // another for scalable-vectors: we can only proceed with the scalable
+ // VPlan if a vscale_range function attribute was found.
+ std::optional<unsigned> MaxVScale = getMaxVScale(Plan.getIRFunction());
+ if (MaxVF.isScalable() && !MaxVScale)
+ return nullptr;
+ bool Overflow;
+ unsigned MaxVFScaled =
+ SaturatingMultiply(MaxVF.getKnownMinValue(),
+ (MaxVF.isScalable() ? *MaxVScale : 1), &Overflow);
+ if (Overflow)
+ return nullptr;
+ if (auto *C = dyn_cast_if_present<Constant>(
+ Folder.FoldIntrinsic(Intrinsic::get_active_lane_mask, Ops,
+ FixedVectorType::get(I1Ty, MaxVFScaled))))
----------------
david-arm wrote:
I agree with @paulwalker-arm here that passing this indirectly via `SimplifyQuery` seems like the proper way and what SimplifyQuery was intended for, rather than making interfaces more complicated. It's a nice thing to be able to fold these intrinsics in the loop vectoriser, but we're only folding vector pre-header code here which won't have any bearing on the cost model. Like @paulwalker-arm said this code will be optimised immediately afterwards by instcombine.
https://github.com/llvm/llvm-project/pull/208852
More information about the llvm-commits
mailing list