[llvm] [SROA] Use tree-structure merge to remove alloca (PR #152793)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 11 05:58:12 PDT 2025
================
@@ -2667,6 +2668,90 @@ static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V,
return V;
}
+/// This function takes two vector values and combines them into a single vector
+/// by concatenating their elements. The function handles:
+///
+/// 1. Element type mismatch: If either vector's element type differs from
+/// NewAIEltType, the function bitcasts the vector to use NewAIEltType while
+/// preserving the total bit width (adjusting the number of elements
+/// accordingly).
+///
+/// 2. Size mismatch: After transforming the vectors to have the desired element
+/// type, if the two vectors have different numbers of elements, the smaller
+/// vector is extended with poison values to match the size of the larger
+/// vector before concatenation.
+///
+/// 3. Concatenation: The vectors are merged using a shuffle operation that
+/// places all elements of V0 first, followed by all elements of V1.
+///
+/// \param V0 The first vector to merge (must be a vector type)
+/// \param V1 The second vector to merge (must be a vector type)
+/// \param DL The data layout for size calculations
+/// \param NewAIEltTy The desired element type for the result vector
+/// \param Builder IRBuilder for creating new instructions
+/// \return A new vector containing all elements from V0 followed by all
+/// elements from V1
+static Value *mergeTwoVectors(Value *V0, Value *V1, const DataLayout &DL,
+ Type *NewAIEltTy, IRBuilder<> &Builder) {
+ // V0 and V1 are vectors
+ // Create a new vector type with combined elements
+ // Use ShuffleVector to concatenate the vectors
+ auto *VecType0 = cast<FixedVectorType>(V0->getType());
+ auto *VecType1 = cast<FixedVectorType>(V1->getType());
+
+ // If V0/V1 element types are different from NewAllocaElementType,
+ // we need to introduce bitcasts before merging them
+ auto BitcastIfNeeded = [&](Value *&V, FixedVectorType *&VecType,
+ const char *DebugName) {
+ Type *EltType = VecType->getElementType();
+ if (EltType != NewAIEltTy) {
+ // Calculate new number of elements to maintain same bit width
+ unsigned TotalBits =
+ VecType->getNumElements() * DL.getTypeSizeInBits(EltType);
+ unsigned NewNumElts = TotalBits / DL.getTypeSizeInBits(NewAIEltTy);
+
+ auto *NewVecType = FixedVectorType::get(NewAIEltTy, NewNumElts);
+ V = Builder.CreateBitCast(V, NewVecType);
+ VecType = NewVecType;
+ LLVM_DEBUG(dbgs() << " bitcast " << DebugName << ": " << *V << "\n");
+ }
+ };
+
+ BitcastIfNeeded(V0, VecType0, "V0");
+ BitcastIfNeeded(V1, VecType1, "V1");
+
+ unsigned NumElts0 = VecType0->getNumElements();
----------------
agorenstein-nvidia wrote:
Could some or all of what follows from here be replaced by a call to `concatenateTwoVectors`?
https://github.com/llvm/llvm-project/pull/152793
More information about the llvm-commits
mailing list