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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 07:15:35 PDT 2026


================
@@ -1355,6 +1355,34 @@ 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();
+      }
+
+      // We rely on the fact that different VPlans are created for the
+      // fixed-vector and scalable-vector cases.
+      ElementCount MaxVF =
+          *max_element(Plan.vectorFactors(), ElementCount::isKnownLT) *
+          Multiplier;
+
+      Type *I1Ty = IntegerType::getInt1Ty(Plan.getContext());
+      if (auto *C = dyn_cast_if_present<Constant>(Folder.FoldIntrinsic(
+              Intrinsic::get_active_lane_mask, Ops,
+              VectorType::get(I1Ty, MaxVF), {}, Plan.getIRFunction())))
+        // We cannot handle vector constants that are not all-true or all-false,
+        // because they would not be collapsable to a scalar constant, that
+        // would be necessary for live-in simplification. We do not produce ALMs
+        // foldable to false at the moment.
----------------
fhahn wrote:

>  but only in cases where the mask ends up dead,

Yes they get removed later, would still be good to keep the test (could print after the combine) so at least we add coverage for the path (and show that it cannot be turned into assert)

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


More information about the llvm-commits mailing list