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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 25 06:48:41 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(
----------------
david-arm wrote:

I don't think this is right. To me it looks pure accident that it seems to generate the correct code simply because the bitwidth of the element type is 1 in all the existing tests. According to the LangRef:

```
The '``llvm.experimental.cttz.elts``' intrinsic counts the number of trailing
zero elements of a vector.
```

i.e. it's a count of elements, not a count of bits.

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


More information about the llvm-commits mailing list