[clang] [CIR] Add handling for base subobject alignment (PR #244018)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 18 14:03:06 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Andy Kaylor (andykaylor)
<details>
<summary>Changes</summary>
While working on a previous update to CIR's record alignment handling, I came across a corner case where we weren't recording user-specified alignment for a base subobject and also didn't have enough information to reconstruct the alignment during calling convention lowering. This change fixes that problem.
Assisted-by: Cursor / Grok 4.7
---
Full diff: https://github.com/llvm/llvm-project/pull/244018.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp (+20-6)
- (added) clang/test/CIR/CodeGen/call-conv-lowering-x86_64-base-subobject.cpp (+34)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index 88e5f5e42162e..6b9b231e17be3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -953,12 +953,26 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) {
uint64_t unadjustedAlignInBytes =
astLayout.getUnadjustedAlignment().getQuantity();
bool isTransparentUnion = rd->hasAttr<TransparentUnionAttr>();
-
- cgm.addRecordLayout(ty->getName(),
- cir::RecordLayoutAttr::get(
- mlirCtx, apk, hasTrivialDestructor,
- recordAlignInBytes, unadjustedAlignInBytes,
- isTransparentUnion, rd->hasFlexibleArrayMember()));
+ bool hasFlexibleArrayMember = rd->hasFlexibleArrayMember();
+
+ auto addLayout = [&](mlir::StringAttr name, uint64_t align,
+ uint64_t unadjusted) {
+ cgm.addRecordLayout(name, cir::RecordLayoutAttr::get(
+ mlirCtx, apk, hasTrivialDestructor, align,
+ unadjusted, isTransparentUnion,
+ hasFlexibleArrayMember));
+ };
+ addLayout(ty->getName(), recordAlignInBytes, unadjustedAlignInBytes);
+
+ // The base subobject is a separate named type. Record the non-virtual
+ // alignment for it, and keep the unadjusted alignment within that
+ // alignment. The AST has no non-virtual unadjusted alignment, so use the
+ // record's unadjusted alignment, capped at the non-virtual alignment.
+ if (baseTy && baseTy != *ty) {
+ uint64_t baseAlign = astLayout.getNonVirtualAlignment().getQuantity();
+ uint64_t baseUnadjusted = std::min(unadjustedAlignInBytes, baseAlign);
+ addLayout(baseTy.getName(), baseAlign, baseUnadjusted);
+ }
}
auto rl = std::make_unique<CIRGenRecordLayout>(
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-base-subobject.cpp b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-base-subobject.cpp
new file mode 100644
index 0000000000000..5d328420cbb43
--- /dev/null
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-base-subobject.cpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
+
+// Base is trivial but not standard-layout, so its base subobject is a
+// distinct packed CIR record: five bytes, which is not a multiple of the
+// int member's ABI alignment. alignas(8) gives that subobject language
+// alignment 8. The packed field places it at offset 4, which is not a
+// multiple of 8, so the x86-64 ABI passes Holder in memory.
+
+struct alignas(8) Base {
+ int i;
+private:
+ char c;
+};
+
+struct Holder {
+ char prefix[4];
+ [[no_unique_address]] Base b __attribute__((packed));
+};
+
+void take(Holder h) {}
+
+// CIR-DAG: !rec_Base2Ebase = !cir.struct<"Base.base" packed {data !s32i, data !s8i}>
+// CIR-DAG: !rec_Holder = !cir.struct<"Holder" {data !cir.array<!s8i x 4>, data !rec_Base2Ebase, pad !cir.array<!u8i x 3>}>
+// CIR-DAG: Base.base = #cir.record_layout<arg_passing_kind = can_pass_in_regs, has_trivial_dtor = true, record_align = 8, unadjusted_align = 4>
+
+// CIR-LABEL: cir.func {{.*}} @_Z4take6Holder(
+// CIR-SAME: !cir.ptr<!rec_Holder> {llvm.align = 8 : i64, llvm.byval = !rec_Holder, llvm.noundef}
+
+// LLVM-LABEL: define dso_local void @_Z4take6Holder(ptr noundef byval(%struct.Holder) align 8 %{{.*}})
``````````
</details>
https://github.com/llvm/llvm-project/pull/244018
More information about the cfe-commits
mailing list