[clang] [AMDGPU] Pack uniform float structs into vector types (PR #195417)

Matt Arsenault via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 10:40:54 PDT 2026


================
@@ -78,6 +78,68 @@ bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
   return Members * NumRegs <= MaxNumRegsForArgsRet;
 }
 
+/// Check if struct contains only identical float types that can be packed
+/// into a vector (e.g., {half, half} -> <2 x half>, {float, float} -> <2 x
+/// float>). Returns the vector type if packable, nullptr otherwise.
+static llvm::Type *
+getPackableHomogeneousFloatVectorType(const RecordDecl *RD,
+                                      const ASTContext &Context,
+                                      llvm::LLVMContext &VMContext) {
+  QualType FirstFloatTy;
+  unsigned Count = 0;
+
+  for (const FieldDecl *Field : RD->fields()) {
+    // No bitfields in float vector packing
+    if (Field->isBitField())
+      return nullptr;
+
+    QualType FieldTy = Field->getType();
+
+    // Must be a floating-point type
+    if (!FieldTy->isFloatingType())
+      return nullptr;
+
+    // All fields must be the same type
+    if (FirstFloatTy.isNull()) {
+      FirstFloatTy = FieldTy;
+    } else if (!Context.hasSameType(FirstFloatTy, FieldTy)) {
+      return nullptr; // Mixed float types like {half, float}
+    }
+
+    Count++;
+  }
+
+  // Only pack 2 or 4 elements (common vector sizes)
+  if (Count != 2 && Count != 4)
+    return nullptr;
+
+  // Convert QualType to LLVM Type
+  llvm::Type *EltTy = nullptr;
+  const BuiltinType *BT = FirstFloatTy->getAs<BuiltinType>();
+  if (!BT)
+    return nullptr;
+
+  switch (BT->getKind()) {
----------------
arsenm wrote:

This seems like something you shouldn't need to reinvent 

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


More information about the cfe-commits mailing list