Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -1799,6 +1799,29 @@ D->getType().isConstant(Context) && isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) GV->setSection(".cp.rodata"); + + // The ARM/AArch64 ABI expects structs with bitfields to respect the proper + // container alignment, hence we have to enfore this in the IR so as to + // work around clang combining bitfields into one large type. + llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); + if ( Arch == llvm::Triple::arm + || Arch == llvm::Triple::armeb + || Arch == llvm::Triple::thumb + || Arch == llvm::Triple::thumbeb + || Arch == llvm::Triple::aarch64 + || Arch == llvm::Triple::aarch64_be) { + if (const auto *RT = D->getType()->getAs()) { + const RecordDecl *RD = RT->getDecl(); + + for (auto I = RD->field_begin(), End = RD->field_end(); I != End; ++I) { + if ((*I)->isBitField()) { + const ASTRecordLayout &Info = getContext().getASTRecordLayout(RD); + GV->setAlignment(Info.getAlignment().getQuantity()); + break; + } + } + } + } } if (AddrSpace != Ty->getAddressSpace())