[clang] [CIR] Upstream support for record packing and padding (PR #136036)

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 18 10:31:31 PDT 2025


================
@@ -225,17 +235,108 @@ void RecordType::complete(ArrayRef<Type> members, bool packed, bool padded) {
 //===----------------------------------------------------------------------===//
 
 llvm::TypeSize
-RecordType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
-                              ::mlir::DataLayoutEntryListRef params) const {
-  assert(!cir::MissingFeatures::recordTypeLayoutInfo());
-  return llvm::TypeSize::getFixed(8);
+RecordType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                              mlir::DataLayoutEntryListRef params) const {
+  if (!layoutInfo)
+    computeSizeAndAlignment(dataLayout);
+  return llvm::TypeSize::getFixed(
+      mlir::cast<cir::RecordLayoutAttr>(layoutInfo).getSize() * 8);
 }
 
 uint64_t
 RecordType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
                             ::mlir::DataLayoutEntryListRef params) const {
-  assert(!cir::MissingFeatures::recordTypeLayoutInfo());
-  return 4;
+  if (!layoutInfo)
+    computeSizeAndAlignment(dataLayout);
+  return mlir::cast<cir::RecordLayoutAttr>(layoutInfo).getAlignment();
+}
+
+void RecordType::computeSizeAndAlignment(
+    const mlir::DataLayout &dataLayout) const {
+  assert(isComplete() && "Cannot get layout of incomplete records");
+  // Do not recompute.
+  if (layoutInfo)
----------------
bcardosolopes wrote:

>  verified that even with a simple test case (clang/test/CIR/CodeGen/struct.cpp) we recompute the layout multiple times for the same type. This just isn't working the way it was intended.

Thanks for double checking

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


More information about the cfe-commits mailing list