[llvm] [VPlan] Constant-fold ActiveLaneMask intrinsics (PR #208852)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 2 09:04:34 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))))
----------------
fhahn wrote:
Thinking a bit more again, I am wondering if we could avoid creating the scaled type here alltogether? For very large VScale's this would use a huge amount of memory.
It looks like `simplifyBinaryIntrinsic` already has the logic needed, but it does not trigger, becuase it does not take `CtxF` as argument (and we do not have a context instruction).
Could we update it + pass the context function to the folder? That would simplify things here a bit, re-use more existing logic and avoid constructing huge types for massive vscales.
https://github.com/llvm/llvm-project/pull/208852
More information about the llvm-commits
mailing list