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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 02:13:50 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))))
----------------
nikic wrote:

> We see this regression: https://llvm-compile-time-tracker.com/compare.php?from=46dabd2f3a822a161b8b12191f8ba32b913a6de0&to=2fff5a18bba0566d5fa3966529f4aef130355673&stat=instructions:u -- it's small, but above noise levels, and we get questionable benefit?

Maybe I'm reading this wrong, but this doesn't seem to have statistically meaningful regressions?

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


More information about the llvm-commits mailing list