[llvm] [VPlan] Fix packed replication of struct types (PR #160274)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 24 06:23:53 PDT 2025


================
@@ -3267,11 +3267,21 @@ void VPPredInstPHIRecipe::execute(VPTransformState &State) {
   // also do that packing, thereby "hoisting" the insert-element sequence.
   // Otherwise, a phi node for the scalar value is needed.
   if (State.hasVectorValue(getOperand(0))) {
-    Value *VectorValue = State.get(getOperand(0));
-    InsertElementInst *IEI = cast<InsertElementInst>(VectorValue);
-    PHINode *VPhi = State.Builder.CreatePHI(IEI->getType(), 2);
-    VPhi->addIncoming(IEI->getOperand(0), PredicatingBB); // Unmodified vector.
-    VPhi->addIncoming(IEI, PredicatedBB); // New vector with inserted element.
+    auto *VecI = cast<Instruction>(State.get(getOperand(0)));
+    assert(isa<InsertElementInst>(VecI) || isa<InsertValueInst>(VecI));
+
+    // If VectorI is a struct, it will be a sequence like:
+    // %1       = insertvalue %unmodified, %x, 0
+    // %2       = insertvalue %1, %y, 1
+    // %VectorI = insertvalue %2, %z, 2
+    // To get the unmodified vector we need to look through the chain.
+    if (auto *StructTy = dyn_cast<StructType>(VecI->getType()))
+      for (unsigned I = 0; I < StructTy->getNumContainedTypes() - 1; I++)
+        VecI = cast<Instruction>(VecI->getOperand(0));
+
----------------
MacDue wrote:

Add an assert here? (after as `VecI` is already checked before). 
```suggestion
      for (unsigned I = 0; I < StructTy->getNumContainedTypes() - 1; I++) {
        VecI = cast<Instruction>(VecI->getOperand(0));
        assert(isa<InsertValueInst>(VecI));
      }
```


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


More information about the llvm-commits mailing list