[clang] [CIR] Fix access to bitfields inside a union (PR #154398)

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 19 12:49:55 PDT 2025


================
@@ -372,9 +372,11 @@ Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
                                                  unsigned index) {
   mlir::Location loc = getLoc(field->getLocation());
   cir::PointerType fieldPtr = cir::PointerType::get(fieldType);
+  auto rec = cast<cir::RecordType>(base.getAddress().getElementType());
+  if (index == 0 && rec.isUnion())
----------------
Andres-Salamanca wrote:

Yes, in the incubator we handled it that way, but in this discussion we decided to remove that:
https://github.com/llvm/llvm-project/pull/145971#discussion_r2170040068
As for *“Is index ever non-zero for unions?”*, the answer is no we always get zero for bitfields in unions, because of here:
https://github.com/llvm/llvm-project/blob/9627944cba4f3a2b33c64bcfc9ea654d56d008a3/clang/lib/CIR/CodeGen/CIRGenExpr.cpp#L394-L395
https://github.com/llvm/llvm-project/blob/961b052e98bf547be0d2f655f276e209d2b68099/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp#L707-L709
One thing to note is that when loading a non-bitfield, it does receive an index different from 0:
https://godbolt.org/z/vMTc74Go1

That index comes from here:
https://github.com/llvm/llvm-project/blob/9627944cba4f3a2b33c64bcfc9ea654d56d008a3/clang/lib/CIR/CodeGen/CIRGenExpr.cpp#L436-L438

But in the end, we’re still using index 0 when calculating the offset:
https://github.com/llvm/llvm-project/blob/9627944cba4f3a2b33c64bcfc9ea654d56d008a3/clang/lib/CIR/CodeGen/CIRGenExpr.cpp#L59-L62


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


More information about the cfe-commits mailing list