[llvm] [VPlan] Handle FirstActiveLane when unrolling. (PR #145394)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 25 07:48:11 PDT 2025


================
@@ -765,9 +765,35 @@ Value *VPInstruction::generate(VPTransformState &State) {
     return Builder.CreateOrReduce(A);
   }
   case VPInstruction::FirstActiveLane: {
-    Value *Mask = State.get(getOperand(0));
-    return Builder.CreateCountTrailingZeroElems(Builder.getInt64Ty(), Mask,
-                                                true, Name);
+    if (getNumOperands() == 1) {
+      Value *Mask = State.get(getOperand(0));
+      return Builder.CreateCountTrailingZeroElems(Builder.getInt64Ty(), Mask,
+                                                  true, Name);
+    }
+    // If there are multiple operands, create a chain of selects to pick the
+    // first operand with an active lane and add the number of lanes of the
+    // preceding operands.
+    Value *RuntimeVF =
+        getRuntimeVF(State.Builder, State.Builder.getInt64Ty(), State.VF);
+    Type *ElemTy = State.TypeAnalysis.inferScalarType(getOperand(0));
+    Value *RuntimeBitwidth = Builder.CreateMul(
----------------
fhahn wrote:

Argh, I looked at the regular cttz definition, which uses the bitwidth.... Currently it's only used for vector predicate operands (so should always be i1), update the remove the multiply by the scalar size

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


More information about the llvm-commits mailing list