[llvm] [llubi] Initial support for vector predication intrinsics (PR #212093)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 00:09:34 PDT 2026


================
@@ -814,6 +925,170 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return Mask.asAggregate()[I].asBoolean();
   }
 
+  std::optional<uint64_t> getVPVectorLength(VPIntrinsic &VPI,
+                                            ArrayRef<AnyValue> Args) {
+    const auto EVLPos =
+        VPIntrinsic::getVectorLengthParamPos(VPI.getIntrinsicID());
+    assert(EVLPos && "VP intrinsic without an explicit vector length");
+
+    const AnyValue &EVL = Args[*EVLPos];
+    if (EVL.isPoison()) {
+      reportImmediateUB() << "VP intrinsic with poison explicit vector length.";
+      return std::nullopt;
+    }
+
+    const uint64_t MaxEVL = Ctx.getEVL(VPI.getStaticVectorLength());
+    if (EVL.asInteger().ugt(MaxEVL)) {
+      reportImmediateUB() << "VP intrinsic explicit vector length exceeds "
+                             "the runtime vector length. EVL: "
+                          << EVL.asInteger() << ", Vector length: " << MaxEVL
+                          << ".";
+      return std::nullopt;
+    }
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue getUndefinedScalar(Type *Ty) {
+    AnyValue Res = AnyValue::getPoisonValue(Ctx, Ty);
+    Ctx.freeze(Res, Ty);
+    return Res;
+  }
+
+  AnyValue applyVPFastMathFlags(VPIntrinsic &VPI, AnyValue Val) {
+    const FastMathFlags FMF = VPI.getFastMathFlagsOrNone();
+    if (!FMF.any())
+      return Val;
+    return handleFMFFlags(std::move(Val), FMF, /*IsInput=*/true);
+  }
+
+  AnyValue callVPSelectIntrinsic(VPIntrinsic &VPI, ArrayRef<AnyValue> Args) {
+    const auto EVL = getVPVectorLength(VPI, Args);
+    if (!EVL)
+      return AnyValue::getPoisonValue(Ctx, VPI.getType());
+
+    const auto &Cond = Args[0].asAggregate();
+    const auto &TrueVal = Args[1].asAggregate();
+    const auto &FalseVal = Args[2].asAggregate();
+    Type *ElemTy = cast<VectorType>(VPI.getType())->getElementType();
+
+    std::vector<AnyValue> Res;
+    Res.reserve(Cond.size());
+    for (size_t I = 0, E = Cond.size(); I != E; ++I) {
+      if (I >= *EVL) {
+        Res.push_back(getUndefinedScalar(ElemTy));
+        continue;
+      }
+
+      switch (Cond[I].asBoolean()) {
+      case BooleanKind::True:
+        Res.push_back(applyVPFastMathFlags(VPI, TrueVal[I]));
+        break;
+      case BooleanKind::False:
+        Res.push_back(applyVPFastMathFlags(VPI, FalseVal[I]));
+        break;
+      case BooleanKind::Poison:
+        Res.push_back(AnyValue::poison());
+        break;
+      }
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPMergeIntrinsic(VPIntrinsic &VPI, ArrayRef<AnyValue> Args) {
+    const auto Pivot = getVPVectorLength(VPI, Args);
+    if (!Pivot)
+      return AnyValue::getPoisonValue(Ctx, VPI.getType());
+
+    const auto &Cond = Args[0].asAggregate();
+    const auto &TrueVal = Args[1].asAggregate();
+    const auto &FalseVal = Args[2].asAggregate();
+
+    std::vector<AnyValue> Res;
+    Res.reserve(Cond.size());
+    for (size_t I = 0, E = Cond.size(); I != E; ++I) {
+      if (I >= *Pivot) {
+        Res.push_back(applyVPFastMathFlags(VPI, FalseVal[I]));
+        continue;
+      }
+
+      switch (Cond[I].asBoolean()) {
+      case BooleanKind::True:
+        Res.push_back(applyVPFastMathFlags(VPI, TrueVal[I]));
+        break;
+      case BooleanKind::False:
+        Res.push_back(applyVPFastMathFlags(VPI, FalseVal[I]));
+        break;
+      case BooleanKind::Poison:
+        Res.push_back(AnyValue::poison());
+        break;
+      }
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPIntegerBinaryIntrinsic(VPIntrinsic &VPI,
+                                        ArrayRef<AnyValue> Args) {
+    const auto EVL = getVPVectorLength(VPI, Args);
+    if (!EVL)
+      return AnyValue::getPoisonValue(Ctx, VPI.getType());
+
+    const auto MaskPos = VPIntrinsic::getMaskParamPos(VPI.getIntrinsicID());
+    assert(MaskPos && "VP integer binary intrinsic without a mask");
+    const auto &LHS = Args[0].asAggregate();
+    const auto &RHS = Args[1].asAggregate();
+    const AnyValue &Mask = Args[*MaskPos];
+    const unsigned Opcode = *VPI.getFunctionalOpcode();
+
+    std::vector<AnyValue> Res;
+    Res.reserve(LHS.size());
+    for (size_t I = 0, E = LHS.size(); I != E; ++I) {
+      // Check EVL first: a poison mask lane past EVL is disabled by the EVL
+      // mask and must not affect the result.
+      if (I >= *EVL) {
+        Res.push_back(AnyValue::poison());
----------------
dtcxzyw wrote:

Use `std::vector::resize` instead.


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


More information about the llvm-commits mailing list