[llvm] [SCCP] Handle llvm.experimental.get.vector.length calls (PR #169527)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 26 19:58:03 PST 2025


================
@@ -2098,6 +2098,38 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
       return (void)mergeInValue(ValueState[II], II,
                                 ValueLatticeElement::getRange(Result));
     }
+    if (II->getIntrinsicID() == Intrinsic::experimental_get_vector_length) {
+      Value *CountArg = II->getArgOperand(0);
+      Value *VF = II->getArgOperand(1);
+      bool Scalable = cast<ConstantInt>(II->getArgOperand(2))->isOne();
+
+      // Computation happens in the larger type.
+      unsigned BitWidth = std::max(CountArg->getType()->getScalarSizeInBits(),
+                                   VF->getType()->getScalarSizeInBits());
+
+      ConstantRange Count = getValueState(CountArg)
+                                .asConstantRange(CountArg->getType(), false)
+                                .zextOrTrunc(BitWidth);
+      ConstantRange MaxLanes = getValueState(VF)
+                                   .asConstantRange(VF->getType(), false)
+                                   .zextOrTrunc(BitWidth);
+      if (Scalable)
+        MaxLanes =
+            MaxLanes.multiply(getVScaleRange(II->getFunction(), BitWidth));
+
+      // The result is always less than both Count and MaxLanes.
+      ConstantRange Result(
+          APInt::getZero(BitWidth),
+          APIntOps::umin(Count.getUpper(), MaxLanes.getUpper()));
+
+      // If Count <= MaxLanes, getvectorlength(Count, MaxLanes) = Count
+      if (Count.icmp(CmpInst::ICMP_ULE, MaxLanes))
+        Result = Count;
+
+      Result = Result.zextOrTrunc(II->getType()->getScalarSizeInBits());
----------------
lukel97 wrote:

I think in this case the result is poison from the definition in the LangRef:

> If the result value does not fit in the result type, then the result is a [poison value](https://llvm.org/docs/LangRef.html#poisonvalues).

MaxLanes is 0x1FFFFFFFC and Count is 0x100000000, and because Count <= MaxLanes the result is Count. But 0x100000000 is larger than 32 bits so it returns poison, so I think the transform should be valid

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


More information about the llvm-commits mailing list