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

Paul Walker via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 22 03:09:20 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;
+      SeenActive |= CI->isOne();
+      Result[I] = ConstantInt::getBool(Ty, !SeenActive);
----------------
paulwalker-arm wrote:

Up to you but I wondered if it's more efficient to do something like:
```
Constant *ResElt = ConstantInt::getBool(true);
Constant *Inactive = ConstantInt::getBool(False);
for each elt 
  ...
  if (CI->isOne())
    ResElt = Inactive;
  Result[I] = ResElt;
```

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


More information about the llvm-branch-commits mailing list