[llvm] [ConstantFolding] Add folding for [de]interleave2, insert and extract (PR #141301)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Fri May 30 07:39:41 PDT 2025
================
@@ -3734,6 +3738,98 @@ static Constant *ConstantFoldFixedVectorCall(
}
return nullptr;
}
+ case Intrinsic::vector_extract: {
+ auto *Vec = dyn_cast<Constant>(Operands[0]);
+ auto *Idx = cast<ConstantInt>(Operands[1]);
+ if (!Vec || !Idx || !isa<FixedVectorType>(Vec->getType()))
+ return nullptr;
+
+ unsigned NumElements = FVTy->getNumElements();
+ unsigned VecNumElements =
+ cast<FixedVectorType>(Vec->getType())->getNumElements();
+ unsigned StartingIndex = Idx->getZExtValue();
+
+ // Extracting entire vector is nop
+ if (NumElements == VecNumElements && StartingIndex == 0)
+ return Vec;
+
+ const unsigned NonPoisonNumElements =
+ std::min(StartingIndex + NumElements, VecNumElements);
+ for (unsigned I = StartingIndex; I < NonPoisonNumElements; ++I) {
+ Constant *Elt = Vec->getAggregateElement(I);
+ if (!Elt)
+ return nullptr;
+ Result[I - StartingIndex] = Elt;
+ }
----------------
lukel97 wrote:
Stylistic nit, you could avoid `NonPoisonNumElements` and the second loop if you handle it in the main loop
```suggestion
for (unsigned I = 0; I < NumElements; ++I) {
// Out of bounds elements are poison
if (StartingIndex + I >= VecNumElements) {
Result[I] = PoisonValue::get(FVTy->getElementType());
continue;
}
Constant *Elt = Vec->getAggregateElement(StartingIndex + I);
if (!Elt)
return nullptr;
Result[I] = Elt;
}
```
https://github.com/llvm/llvm-project/pull/141301
More information about the llvm-commits
mailing list