[llvm] [ConstantFolding] Add folding for [de]interleave2, insert and extract (PR #141301)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 9 12:48:04 PDT 2025


================
@@ -3911,6 +3993,26 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
       return nullptr;
     return ConstantStruct::get(StTy, SinResult, CosResult);
   }
+  case Intrinsic::vector_deinterleave2: {
+    auto *Vec = dyn_cast<Constant>(Operands[0]);
+    if (!Vec)
+      return nullptr;
+
+    unsigned NumElements =
+        cast<VectorType>(Vec->getType())->getElementCount().getKnownMinValue() /
+        2;
+    SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
+    for (unsigned I = 0; I < NumElements; ++I) {
+      Constant *Elt0 = Vec->getAggregateElement(2 * I);
+      Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
+      if (!Elt0 || !Elt1)
+        return nullptr;
+      Res0[I] = Elt0;
+      Res1[I] = Elt1;
+    }
+    return ConstantStruct::get(StTy, ConstantVector::get(Res0),
----------------
topperc wrote:

Isn't `ConstantVector::get` going to create a fixed vector type? How does this work for scalable vectors?

```
Constant *ConstantVector::get(ArrayRef<Constant*> V) {                           
  if (Constant *C = getImpl(V))                                                  
    return C;                                                                    
  auto *Ty = FixedVectorType::get(V.front()->getType(), V.size());               
  return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);             
} 
```

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


More information about the llvm-commits mailing list