[llvm-branch-commits] [llvm] [ConstantFolding] Add constant folding for mask.beforefirst (PR #234472)

Luke Lau via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 22 04:05:27 PDT 2026


================
@@ -4551,6 +4552,20 @@ static Constant *ConstantFoldFixedVectorCall(
     }
     return nullptr;
   }
+  case Intrinsic::mask_beforefirst: {
+    bool SeenActive = false;
+    for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
+      Constant *Elt = Operands[0]->getAggregateElement(I);
+      if (isa_and_nonnull<PoisonValue>(Elt))
+        return PoisonValue::get(FVTy);
+      auto *CI = dyn_cast_or_null<ConstantInt>(Elt);
+      if (!CI)
+        return nullptr;
----------------
lukel97 wrote:

I think my reply in https://github.com/llvm/llvm-project/pull/234472#discussion_r4347469961 answers this partly, but to add to it. We need to freeze the exit condition in the loop anyway. We call vector.reduce.or (to see if we've early exited) and cttz.elts (for getting the exiting lane), so reusing the freeze costs us nothing. 

I'd also be quite wary of having short circuiting semantics as it would diverge from the other reduction intrinsics which all propagate poison on any poison lane. It also makes expansion less efficient since we can't use said reduction intrinsics. 



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


More information about the llvm-branch-commits mailing list