[llvm] [VPlan] Constant-fold ActiveLaneMask intrinsics (PR #208852)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 02:09:45 PDT 2026


================
@@ -1115,6 +1115,23 @@ VPIRValue *vputils::tryToFoldLiveIns(VPSingleDefRecipe &R,
     case Instruction::ExtractElement:
       assert(!Ops[0]->getType()->isVectorTy() && "Live-ins should be scalar");
       return Ops[0];
+    case VPInstruction::ActiveLaneMask: {
+      uint64_t Multiplier = cast<ConstantInt>(Ops[2])->getZExtValue();
+      Type *I1Ty = IntegerType::getInt1Ty(Plan.getContext());
+      if (Plan.hasScalableVF())
+        // We do not produce foldable scalable ALMs at the moment.
+        return nullptr;
+
+      // We do not produce ALMs foldable to false at the moment.
+      unsigned MaxVF = Multiplier * max_element(Plan.vectorFactors(),
+                                                ElementCount::isKnownLT)
+                                        ->getFixedValue();
+      if (auto *C = dyn_cast_if_present<Constant>(Folder.FoldIntrinsic(
----------------
david-arm wrote:

There isn't really anything stopping us from applying the same folds for scalable vectors when a `vscale_range` attribute is present on the function. If `FoldIntrinsic` doesn't currently support scalable vectors, then I think this needs fixing.

It's a bit hacky, but we should also be able to change the `max_element` code to use the maximum value of vscale when walking all the VFs in the plan. That way you can create a `MaxVF` that takes the worst outcome for scalable vectors into account. This would fold more cases, such as the new test you've added below.

https://github.com/llvm/llvm-project/pull/208852


More information about the llvm-commits mailing list