[clang] [llvm] [SROA] Canonicalize homogeneous structs into fixed vectors (PR #165159)
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 24 07:22:21 PDT 2026
================
@@ -5130,6 +5130,162 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
return true;
}
+/// Try to canonicalize a homogeneous, tightly-packed struct to a vector type.
+///
+/// For structs where all elements have the same type and are tightly packed
+/// (no padding), we can represent them as a fixed vector which enables better
+/// optimization (e.g., vector selects instead of memcpy).
+///
+/// \param STy The struct type to try to canonicalize.
+/// \param DL The DataLayout for size/alignment queries.
+/// \returns The equivalent vector type, or nullptr if not applicable.
+static FixedVectorType *tryCanonicalizeStructToVector(StructType *STy,
+ const DataLayout &DL) {
+ unsigned NumElts = STy->getNumElements();
+ if (NumElts != 2 && NumElts != 4)
+ return nullptr;
+
+ // All elements must be the same type.
+ Type *EltTy = STy->getElementType(0);
+ for (unsigned I = 1; I < NumElts; ++I)
+ if (STy->getElementType(I) != EltTy)
+ return nullptr;
+
+ // Element type must be valid for vectors.
+ if (!VectorType::isValidElementType(EltTy))
+ return nullptr;
+
+ // Only allow integer types >= 8 bits or floating point.
+ if (auto *IT = dyn_cast<IntegerType>(EltTy)) {
+ if (IT->getBitWidth() < 8)
+ return nullptr;
+ } else if (!EltTy->isFloatingPointTy()) {
+ return nullptr;
+ }
+
+ // Element size must be fixed and non-zero.
+ TypeSize EltTS = DL.getTypeAllocSize(EltTy);
+ if (!EltTS.isFixed())
+ return nullptr;
+ uint64_t EltSize = EltTS.getFixedValue();
+ if (EltSize < 1)
+ return nullptr;
+
+ const StructLayout *SL = DL.getStructLayout(STy);
+ uint64_t StructSize = SL->getSizeInBytes();
+ if (StructSize == 0)
----------------
yxsamliu wrote:
removed the separate zero-size guard and now just rely on the existing element checks together with the packed-size check below
https://github.com/llvm/llvm-project/pull/165159
More information about the cfe-commits
mailing list