← Back to Pull requests

[llubi] Implements arithmetic and reduction VP intrinsic #246938

Open
nofe1248 opened this pull request with 1521 lines diff across 13 files
View on GitHub
nofe1248 nofe1248 commented on Nov 29, 2026 06:47 UTC
Author

This PR implements arithmetic and reduction vector predication intrinsic.

Diff: 1521 lines (+1521 / -0) across 13 files Reviewer: dtcxzyw Review duration: 2935m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.61) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.61 · 1521 lines diff across 13 files
0.61
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Real PR Conversation Timeline
10 items
dtcxzyw dtcxzyw commented on code on Nov 29, 2026 16:58 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {

Move the logic into the caller.

dtcxzyw dtcxzyw commented on code on Nov 29, 2026 17:12 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {

This doesn't follow the logical-and semantics in https://github.com/llvm/llvm-project/pull/241182.

dtcxzyw dtcxzyw commented on code on Nov 29, 2026 17:16 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);

vp.udiv/vp.sdiv/vp.urem/vp.srem are also included here even though they're not speculatable. They can still cause UB if division by zero or overflow occurs on any lane even when EVL > number of elts.

See https://github.com/llvm/llvm-project/pull/240774.

dtcxzyw dtcxzyw commented on code on Nov 29, 2026 17:22 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);
+      if (hasProgramExited())
+        break;
+      if (Length && Mask == BooleanKind::True)
+        Res[I] = std::move(Lane);
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPReduction(CallBase &CB, ArrayRef<AnyValue> Args) {
+    const auto &Vec = Args[1].asAggregate();
+    const auto Length = getVPVectorLength(Args[3], Vec.size());
+    if (!Length)
+      return AnyValue::poison();
+
+    Type *RetTy = CB.getType();
+    FastMathFlags FMF = CB.getFastMathFlagsOrNone();
+    std::vector<AnyValue> Active;
+    for (size_t I = 0; I != *Length; ++I) {
+      switch (getMaskLane(Args[2], I)) {
+      case BooleanKind::False:
+        break;
+      case BooleanKind::True:
+        Active.push_back(Vec[I]);
+        break;
+      case BooleanKind::Poison:
+        return AnyValue::poison();
+      }
+    }
+    if (Active.empty())
+      return RetTy->isFloatingPointTy()
+                 ? handleFMFFlags(Args[0], FMF, /*IsInput*/ true)

                 ? handleFMFFlags(Args[0], FMF, /*IsInput=*/true)

dtcxzyw dtcxzyw commented on code on Nov 29, 2026 17:31 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);
+      if (hasProgramExited())
+        break;
+      if (Length && Mask == BooleanKind::True)
+        Res[I] = std::move(Lane);
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPReduction(CallBase &CB, ArrayRef<AnyValue> Args) {
+    const auto &Vec = Args[1].asAggregate();
+    const auto Length = getVPVectorLength(Args[3], Vec.size());
+    if (!Length)
+      return AnyValue::poison();
+
+    Type *RetTy = CB.getType();
+    FastMathFlags FMF = CB.getFastMathFlagsOrNone();
+    std::vector<AnyValue> Active;
+    for (size_t I = 0; I != *Length; ++I) {
+      switch (getMaskLane(Args[2], I)) {
+      case BooleanKind::False:
+        break;
+      case BooleanKind::True:
+        Active.push_back(Vec[I]);
+        break;
+      case BooleanKind::Poison:
+        return AnyValue::poison();
+      }
+    }
+    if (Active.empty())
+      return RetTy->isFloatingPointTy()
+                 ? handleFMFFlags(Args[0], FMF, /*IsInput*/ true)
+                 : Args[0];
+
+    Intrinsic::ID IID =
+        *VPIntrinsic::getFunctionalIntrinsicIDForVP(CB.getIntrinsicID());
+    if (IID == Intrinsic::vector_reduce_fadd ||
+        IID == Intrinsic::vector_reduce_fmul) {
+      AnyValue ReduceArgs[] = {Args[0], AnyValue(std::move(Active))};
+      return callVectorReduction(RetTy, FMF, IID, ReduceArgs);
+    }
+    if (RetTy->isIntegerTy()) {
+      Active.insert(Active.begin(), Args[0]);
+      AnyValue Vector(std::move(Active));
+      return callVectorReduction(RetTy, FMF, IID, {Vector});
+    }
+
+    // Reduce the vector before combining with start, as in the LangRef
+    // expansion. This also preserves the existing sNaN reduction handling.
+    AnyValue Vector(std::move(Active));
+    AnyValue Reduced = callVectorReduction(RetTy, FMF, IID, {Vector});

I think this doesn't work when all lanes are disabled.

npmania npmania commented on code on Nov 29, 2026 20:47 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);
+      if (hasProgramExited())
+        break;
+      if (Length && Mask == BooleanKind::True)
+        Res[I] = std::move(Lane);
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPReduction(CallBase &CB, ArrayRef<AnyValue> Args) {
+    const auto &Vec = Args[1].asAggregate();
+    const auto Length = getVPVectorLength(Args[3], Vec.size());
+    if (!Length)
+      return AnyValue::poison();
+
+    Type *RetTy = CB.getType();
+    FastMathFlags FMF = CB.getFastMathFlagsOrNone();
+    std::vector<AnyValue> Active;
+    for (size_t I = 0; I != *Length; ++I) {
+      switch (getMaskLane(Args[2], I)) {
+      case BooleanKind::False:
+        break;
+      case BooleanKind::True:
+        Active.push_back(Vec[I]);
+        break;
+      case BooleanKind::Poison:
+        return AnyValue::poison();
+      }
+    }
+    if (Active.empty())
+      return RetTy->isFloatingPointTy()
+                 ? handleFMFFlags(Args[0], FMF, /*IsInput*/ true)

For FP min/max reductions, this can flip the sign of zero when nsz is set. According to current LangRef, nsz must not flip same-sign zeros for FP min/max, so nsz should be cleared before calling handleFMFFlags.

npmania npmania commented on code on Nov 29, 2026 20:48 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);
+      if (hasProgramExited())
+        break;
+      if (Length && Mask == BooleanKind::True)
+        Res[I] = std::move(Lane);
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPReduction(CallBase &CB, ArrayRef<AnyValue> Args) {
+    const auto &Vec = Args[1].asAggregate();
+    const auto Length = getVPVectorLength(Args[3], Vec.size());
+    if (!Length)
+      return AnyValue::poison();
+
+    Type *RetTy = CB.getType();
+    FastMathFlags FMF = CB.getFastMathFlagsOrNone();
+    std::vector<AnyValue> Active;
+    for (size_t I = 0; I != *Length; ++I) {
+      switch (getMaskLane(Args[2], I)) {
+      case BooleanKind::False:
+        break;
+      case BooleanKind::True:
+        Active.push_back(Vec[I]);
+        break;
+      case BooleanKind::Poison:
+        return AnyValue::poison();
+      }
+    }
+    if (Active.empty())
+      return RetTy->isFloatingPointTy()
+                 ? handleFMFFlags(Args[0], FMF, /*IsInput*/ true)
+                 : Args[0];
+
+    Intrinsic::ID IID =
+        *VPIntrinsic::getFunctionalIntrinsicIDForVP(CB.getIntrinsicID());
+    if (IID == Intrinsic::vector_reduce_fadd ||
+        IID == Intrinsic::vector_reduce_fmul) {
+      AnyValue ReduceArgs[] = {Args[0], AnyValue(std::move(Active))};
+      return callVectorReduction(RetTy, FMF, IID, ReduceArgs);
+    }
+    if (RetTy->isIntegerTy()) {
+      Active.insert(Active.begin(), Args[0]);
+      AnyValue Vector(std::move(Active));
+      return callVectorReduction(RetTy, FMF, IID, {Vector});
+    }
+
+    // Reduce the vector before combining with start, as in the LangRef
+    // expansion. This also preserves the existing sNaN reduction handling.
+    AnyValue Vector(std::move(Active));
+    AnyValue Reduced = callVectorReduction(RetTy, FMF, IID, {Vector});
+    return visitFPBinOpWithResult(
+        RetTy, FMF, Reduced, Args[0],
+        [&](const APFloat &LHS, const APFloat &RHS) -> APFloat {
+          return applyFPMinMax(IID, LHS, RHS, FMF.noSignedZeros());
+        });

Ditto. I think we should pass a copy of FMF with nsz cleared to visitFPBinOpWithResult while keeping FMF.noSignedZeros() for applyFPMinMax here.

npmania npmania commented on code on Nov 29, 2026 21:05 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);

I think we should not skip Mask == BooleanKind::False when EVL is poison, since %EVLmask is all-poison in that case, so M = select %EVLmask, %mask, false is poison even on lanes where %mask is false.

lukel97 lukel97 commented on code on Nov 30, 2026 00:08 UTC
Code Review
llvm/lib/UBAwareInterpreter/Interpreter.cpp:1
@@ -1424,11 +1424,117 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
     return IsZeroPoison ? AnyValue::poison() : AnyValue(APInt(RetBW, Length));
   }
 
+  static std::optional<size_t> getVPVectorLength(const AnyValue &EVL,
+                                                 size_t NumLanes) {
+    if (EVL.isPoison() || EVL.asInteger().ugt(NumLanes))
+      return std::nullopt;
+    return EVL.asInteger().getZExtValue();
+  }
+
+  AnyValue callVPArithmetic(CallBase &CB, ArrayRef<AnyValue> Args) {
+    Intrinsic::ID IID = CB.getIntrinsicID();
+    Type *RetTy = CB.getType();
+    const auto &Vec = Args[0].asAggregate();
+    const AnyValue &EVL = Args[3];
+    const auto Length = getVPVectorLength(EVL, Vec.size());
+    if (!Length && (!EVL.isPoison() || IID == Intrinsic::vp_cttz_elts))
+      return AnyValue::getPoisonValue(Ctx, RetTy);
+
+    if (IID == Intrinsic::vp_cttz_elts) {
+      auto MaskedVec = Vec;
+      const AnyValue Zero(
+          APInt(CB.getArgOperand(0)->getType()->getScalarSizeInBits(), 0));
+      for (size_t I = 0; I != *Length; ++I) {
+        switch (getMaskLane(Args[2], I)) {
+        case BooleanKind::False:
+          MaskedVec[I] = Zero;
+          break;
+        case BooleanKind::True:
+          break;
+        case BooleanKind::Poison:
+          MaskedVec[I] = AnyValue::poison();
+          break;
+        }
+      }
+      return countTrailingZeroElements(RetTy, MaskedVec,
+                                       Args[1].asInteger().isOne(), *Length);
+    }
+
+    unsigned Opcode = *VPIntrinsic::getFunctionalOpcodeForVP(IID);
+    const auto &RHS = Args[1].asAggregate();
+    std::vector<AnyValue> Res(Vec.size(), AnyValue::poison());
+    const size_t Limit = Length.value_or(
+        std::min<size_t>(Vec.size(), std::numeric_limits<uint32_t>::max()));
+    for (size_t I = 0; I != Limit; ++I) {
+      BooleanKind Mask = getMaskLane(Args[2], I);
+      if (Mask == BooleanKind::False)
+        continue;
+      AnyValue Lane = computeDivRem(Opcode, Vec[I], RHS[I]);
+      if (hasProgramExited())
+        break;
+      if (Length && Mask == BooleanKind::True)
+        Res[I] = std::move(Lane);
+    }
+    return std::move(Res);
+  }
+
+  AnyValue callVPReduction(CallBase &CB, ArrayRef<AnyValue> Args) {
+    const auto &Vec = Args[1].asAggregate();
+    const auto Length = getVPVectorLength(Args[3], Vec.size());
+    if (!Length)
+      return AnyValue::poison();
+
+    Type *RetTy = CB.getType();
+    FastMathFlags FMF = CB.getFastMathFlagsOrNone();
+    std::vector<AnyValue> Active;
+    for (size_t I = 0; I != *Length; ++I) {
+      switch (getMaskLane(Args[2], I)) {
+      case BooleanKind::False:
+        break;
+      case BooleanKind::True:
+        Active.push_back(Vec[I]);
+        break;
+      case BooleanKind::Poison:
+        return AnyValue::poison();
+      }

I'm not sure if a poison mask lane means a poison result, since we recently updated the langref to clarify that the effective mask is M = select <W x i1> %EVLmask, <W x i1> %mask, <W x i1> splat (i1 false). I.e. it's a logical and that blocks poison propagation. So I think it's only poison if the poison mask lane is < EVL.

Reviewers
Assignees
nofe1248 nofe1248
Labels
risk: low confidence: 90.6% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 60.6% (MEDIUM) · Higher = larger blast radius
60.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.6% · Lower = rushed or shallow review
90.6%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.7% · Safe to merge
5.7%