27#include "llvm/Support/Casting.h"
40struct CIRRecordLowering final {
45 struct MemberInfo final {
47 enum class InfoKind { VFPtr,
Field,
Base, VBase } kind;
50 cir::RecordMemberKind memberKind;
55 MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data,
56 cir::RecordMemberKind memberKind,
58 : offset{offset}, kind{kind}, data{data}, memberKind{memberKind},
60 MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data,
61 cir::RecordMemberKind memberKind,
const CXXRecordDecl *rd)
62 : offset{offset}, kind{kind}, data{data}, memberKind{memberKind},
65 bool operator<(
const MemberInfo &other)
const {
66 return offset < other.offset;
70 static bool ownsBytes(
const MemberInfo &member) {
74 cir::BitFieldDeclAttr getBitFieldDecl(
const FieldDecl *field) {
75 return cir::BitFieldDeclAttr::get(
76 cirGenTypes.convertTypeForMem(field->
getType()),
80 MemberInfo makeAccessUnitInfo(CharUnits offset, mlir::Type storage,
81 llvm::ArrayRef<cir::BitFieldDeclAttr> fields) {
83 cir::BitFieldType::get(&cirGenTypes.getMLIRContext(), storage, fields);
85 const bool holdsNamedField =
86 mlir::cast<cir::BitFieldType>(unitTy).holdsNamedField();
87 return makeStorageInfo(offset, unitTy,
88 holdsNamedField ? cir::RecordMemberKind::BitField
89 : cir::RecordMemberKind::Empty);
95 MemberInfo makeZeroWidthBitFieldInfo(
const FieldDecl *field) {
97 return makeAccessUnitInfo(bitsToCharUnits(getFieldBitOffset(field)),
98 mlir::Type{}, getBitFieldDecl(field));
102 CIRRecordLowering(CIRGenTypes &cirGenTypes,
const RecordDecl *recordDecl,
106 MemberInfo makeStorageInfo(CharUnits offset, mlir::Type data,
107 cir::RecordMemberKind memberKind) {
108 return MemberInfo(offset, MemberInfo::InfoKind::Field, data, memberKind);
112 void setBitFieldInfo(
const FieldDecl *fd, CharUnits startOffset,
113 mlir::Type storageType);
115 void lower(
bool nonVirtualBaseType);
116 void lowerUnion(
bool nonVirtualBaseType);
119 void determinePacked(
bool nvBaseType);
121 void insertPadding();
123 void computeVolatileBitfields();
124 void accumulateBases();
125 void accumulateVPtrs();
126 void accumulateVBases();
127 void accumulateFields(
bool nonVirtualBaseType);
132 mlir::Type getVFPtrType();
135 bool isBigEndian()
const {
return astContext.getTargetInfo().isBigEndian(); }
137 CharUnits bitsToCharUnits(
uint64_t bitOffset) {
138 return astContext.toCharUnitsFromBits(bitOffset);
141 void calculateZeroInit();
143 CharUnits getSize(mlir::Type Ty) {
146 CharUnits getSizeInBits(mlir::Type ty) {
152 CharUnits getMemberAlignment(mlir::Type ty) {
156 bool isZeroInitializable(
const FieldDecl *fd) {
157 return cirGenTypes.isZeroInitializable(fd->
getType());
159 bool isZeroInitializable(
const RecordDecl *rd) {
160 return cirGenTypes.isZeroInitializable(rd);
167 static cir::RecordMemberKind makeMemberKind(
bool holdsData,
168 bool isNamedBitField) {
170 return cir::RecordMemberKind::Empty;
172 : cir::RecordMemberKind::Data;
178 cir::RecordMemberKind getFieldMemberKind(
const FieldDecl *fd) {
179 assert(!fd->
isBitField() &&
"a bit-field is marked with its access unit");
187 cir::RecordMemberKind getBaseMemberKind(
const CXXRecordDecl *baseDecl) {
189 astContext.getCanonicalTagType(baseDecl))
190 ? cir::RecordMemberKind::Empty
191 : cir::RecordMemberKind::Data;
195 mlir::Type getUIntNType(
uint64_t numBits) {
196 unsigned alignedBits = llvm::PowerOf2Ceil(numBits);
197 alignedBits = std::max(8u, alignedBits);
198 return cir::IntType::get(&cirGenTypes.getMLIRContext(), alignedBits,
202 mlir::Type getCharType() {
203 return cir::IntType::get(&cirGenTypes.getMLIRContext(),
204 astContext.getCharWidth(),
208 mlir::Type getByteArrayType(CharUnits numberOfChars) {
209 assert(!numberOfChars.
isZero() &&
"Empty byte arrays aren't allowed.");
210 mlir::Type
type = getCharType();
217 mlir::Type getStorageType(
const CXXRecordDecl *RD) {
218 return cirGenTypes.getCIRGenRecordLayout(RD).getBaseSubobjectCIRType();
223 mlir::Type getBitfieldStorageType(
unsigned numBits) {
224 unsigned alignedBits = llvm::alignTo(numBits, astContext.getCharWidth());
226 return builder.getUIntNTy(alignedBits);
228 mlir::Type
type = getCharType();
229 return cir::ArrayType::get(
type, alignedBits / astContext.getCharWidth());
232 mlir::Type getStorageType(
const FieldDecl *
fieldDecl) {
233 mlir::Type
type = cirGenTypes.convertTypeForMem(
fieldDecl->getType());
235 cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
236 "getStorageType for bitfields");
238 if (hasBoolVectorStorageAlignMismatch(
type))
239 cirGenTypes.getCGModule().errorNYI(
241 "getStorageType for bool vector whose storage integer is aligned "
242 "differently from the vector");
253 bool hasBoolVectorStorageAlignMismatch(mlir::Type
type) {
254 while (
auto arrTy = mlir::dyn_cast<cir::ArrayType>(
type))
255 type = arrTy.getElementType();
256 auto vecTy = mlir::dyn_cast<cir::VectorType>(
type);
257 if (!vecTy || !mlir::isa<cir::BoolType>(vecTy.getElementType()))
260 mlir::IntegerType::get(
type.getContext(), vecTy.getBoolStorageWidth());
261 return dataLayout.layout.getTypeABIAlignment(storageTy) !=
262 dataLayout.layout.getTypeABIAlignment(vecTy);
266 return astRecordLayout.getFieldOffset(
fieldDecl->getFieldIndex());
270 void fillOutputFields();
272 void appendPaddingBytes(CharUnits size) {
275 mlir::Type padTy = getByteArrayType(size);
276 if (recordDecl->isUnion()) {
277 assert(!unionPadding &&
"at most one union tail-padding type");
278 unionPadding = padTy;
280 addField(padTy, cir::RecordMemberKind::Pad);
284 void addField(mlir::Type ty, cir::RecordMemberKind memberKind) {
285 fieldTypes.push_back(ty);
286 fieldKinds.push_back(memberKind);
294 llvm::ArrayRef<mlir::Type> getFieldTypes()
const {
return fieldTypes; }
295 llvm::ArrayRef<cir::RecordMemberKind> getFieldKinds()
const {
299 CIRGenTypes &cirGenTypes;
300 CIRGenBuilderTy &builder;
301 const ASTContext &astContext;
302 const RecordDecl *recordDecl;
303 const CXXRecordDecl *cxxRecordDecl;
304 const ASTRecordLayout &astRecordLayout;
306 std::vector<MemberInfo> members;
307 mlir::Type unionPadding;
308 llvm::DenseMap<const FieldDecl *, CIRGenBitFieldInfo> bitFields;
309 llvm::DenseMap<const FieldDecl *, unsigned> fieldIdxMap;
310 llvm::DenseMap<const CXXRecordDecl *, unsigned> nonVirtualBases;
311 llvm::DenseMap<const CXXRecordDecl *, unsigned> virtualBases;
312 cir::CIRDataLayout dataLayout;
314 LLVM_PREFERRED_TYPE(
bool)
315 unsigned zeroInitializable : 1;
316 LLVM_PREFERRED_TYPE(
bool)
317 unsigned zeroInitializableAsBase : 1;
318 LLVM_PREFERRED_TYPE(
bool)
325 llvm::SmallVector<mlir::Type, 16> fieldTypes;
326 llvm::SmallVector<cir::RecordMemberKind> fieldKinds;
328 CIRRecordLowering(
const CIRRecordLowering &) =
delete;
329 void operator=(
const CIRRecordLowering &) =
delete;
333CIRRecordLowering::CIRRecordLowering(
CIRGenTypes &cirGenTypes,
335 : cirGenTypes{cirGenTypes}, builder{cirGenTypes.getBuilder()},
339 cirGenTypes.getASTContext().getASTRecordLayout(
recordDecl)},
340 dataLayout{cirGenTypes.getCGModule().getModule()},
341 zeroInitializable{
true}, zeroInitializableAsBase{
true}, packed{packed} {}
343void CIRRecordLowering::setBitFieldInfo(
const FieldDecl *fd,
345 mlir::Type storageType) {
349 (
unsigned)(getFieldBitOffset(fd) - astContext.
toBits(startOffset));
351 info.storageSize = getSizeInBits(storageType).getQuantity();
352 info.storageOffset = startOffset;
353 info.storageType = storageType;
365 info.volatileStorageSize = 0;
366 info.volatileOffset = 0;
370void CIRRecordLowering::lower(
bool nonVirtualBaseType) {
372 lowerUnion(nonVirtualBaseType);
373 computeVolatileBitfields();
380 accumulateFields(nonVirtualBaseType);
385 if (members.empty()) {
386 appendPaddingBytes(size);
387 computeVolatileBitfields();
390 if (!nonVirtualBaseType)
394 llvm::stable_sort(members);
401 makeStorageInfo(size, getUIntNType(8), cir::RecordMemberKind::Data));
402 determinePacked(nonVirtualBaseType);
408 computeVolatileBitfields();
411void CIRRecordLowering::fillOutputFields() {
412 for (
const MemberInfo &member : members) {
415 if (
member.kind == MemberInfo::InfoKind::Field) {
417 fieldIdxMap[
member.fieldDecl->getCanonicalDecl()] =
418 fieldTypes.size() - 1;
422 assert(
member.fieldDecl &&
423 "member.data is a nullptr so member.fieldDecl should not be");
427 }
else if (
member.kind == MemberInfo::InfoKind::Base) {
428 nonVirtualBases[
member.cxxRecordDecl] = fieldTypes.size() - 1;
429 }
else if (
member.kind == MemberInfo::InfoKind::VBase) {
430 virtualBases[
member.cxxRecordDecl] = fieldTypes.size() - 1;
452 mlir::Type unitStorage;
453 llvm::SmallVector<cir::BitFieldDeclAttr> unitFields;
454 for (; field != fieldEnd && field->isBitField(); ++field) {
456 if (field->isZeroLengthBitField()) {
457 members.push_back(makeZeroWidthBitFieldInfo(*field));
461 uint64_t bitOffset = getFieldBitOffset(*field);
463 cir::BitFieldDeclAttr
decl = getBitFieldDecl(*field);
466 if (run == fieldEnd || bitOffset >= tail) {
468 startBitOffset = bitOffset;
472 unitIdx = members.size();
474 unitFields.assign(1,
decl);
475 members.push_back(makeAccessUnitInfo(bitsToCharUnits(startBitOffset),
476 unitStorage, unitFields));
478 unitFields.push_back(
decl);
479 members[unitIdx] = makeAccessUnitInfo(bitsToCharUnits(startBitOffset),
480 unitStorage, unitFields);
482 assert(members[unitIdx].offset == bitsToCharUnits(startBitOffset) &&
483 "unitIdx must name the current run's access unit");
486 members.push_back(MemberInfo(bitsToCharUnits(startBitOffset),
487 MemberInfo::InfoKind::Field,
nullptr,
488 cir::RecordMemberKind::Data, *field));
504 CharUnits beginOffset;
513 CharUnits bestEndOffset;
521 bool atAlignedBoundary =
false;
523 if (field != fieldEnd && field->isBitField()) {
524 uint64_t bitOffset = getFieldBitOffset(*field);
525 if (begin == fieldEnd) {
530 assert((bitOffset % charBits) == 0 &&
"Not at start of char");
531 beginOffset = bitsToCharUnits(bitOffset);
532 bitSizeSinceBegin = 0;
533 }
else if ((bitOffset % charBits) != 0) {
541 astContext.
toBits(beginOffset) + bitSizeSinceBegin &&
542 "Concatenating non-contiguous bitfields");
547 if (field->isZeroLengthBitField())
549 atAlignedBoundary =
true;
554 if (begin == fieldEnd)
558 atAlignedBoundary =
true;
564 bool installBest =
false;
565 if (atAlignedBoundary) {
571 CharUnits accessSize = bitsToCharUnits(bitSizeSinceBegin + charBits - 1);
572 if (bestEnd == begin) {
576 bestEndOffset = beginOffset + accessSize;
579 if (!bitSizeSinceBegin)
583 }
else if (accessSize > regSize) {
592 mlir::Type
type = getUIntNType(astContext.
toBits(accessSize));
599 CharUnits
align = getMemberAlignment(
type);
610 if (installBest && bestEnd == field) {
613 if (getSize(
type) == accessSize)
623 CharUnits limitOffset;
624 for (
auto probe = field; probe != fieldEnd; ++probe)
627 assert((getFieldBitOffset(*probe) % charBits) == 0 &&
628 "Next storage is not byte-aligned");
629 limitOffset = bitsToCharUnits(getFieldBitOffset(*probe));
636 CharUnits typeSize = getSize(
type);
637 if (beginOffset + typeSize <= limitOffset) {
640 bestEndOffset = beginOffset + typeSize;
649 .FineGrainedBitfieldAccesses) {
655 bitSizeSinceBegin = astContext.
toBits(limitOffset - beginOffset);
662 assert((field == fieldEnd || !field->isBitField() ||
663 (getFieldBitOffset(*field) % charBits) == 0) &&
664 "Installing but not at an aligned bitfield or limit");
665 CharUnits accessSize = bestEndOffset - beginOffset;
666 if (!accessSize.
isZero()) {
672 assert(getSize(getUIntNType(astContext.
toBits(accessSize))) >
674 "Clipped access need not be clipped");
675 type = getByteArrayType(accessSize);
677 type = getUIntNType(astContext.
toBits(accessSize));
678 assert(getSize(
type) == accessSize &&
679 "Unclipped access must be clipped");
684 llvm::SmallVector<cir::BitFieldDeclAttr> unitFields;
685 for (
auto occupant = begin; occupant != bestEnd; ++occupant)
686 if (!occupant->isZeroLengthBitField())
687 unitFields.push_back(getBitFieldDecl(*occupant));
688 assert(!unitFields.empty() &&
"an access unit holds a bit-field");
689 members.push_back(makeAccessUnitInfo(beginOffset,
type, unitFields));
691 for (; begin != bestEnd; ++begin)
692 if (!begin->isZeroLengthBitField())
693 members.push_back(MemberInfo(beginOffset,
694 MemberInfo::InfoKind::Field,
nullptr,
695 cir::RecordMemberKind::Data, *begin));
701 assert(field != fieldEnd && field->isBitField() &&
702 "Accumulating past end of bitfields");
703 assert(!
barrier &&
"Accumulating across barrier");
704 if (field->isZeroLengthBitField())
705 members.push_back(makeZeroWidthBitFieldInfo(*field));
707 bitSizeSinceBegin += field->getBitWidthValue();
715void CIRRecordLowering::accumulateFields(
bool nonVirtualBaseType) {
718 field != fieldEnd;) {
719 if (field->isBitField()) {
720 field = accumulateBitFields(field, fieldEnd);
721 assert((field == fieldEnd || !field->isBitField()) &&
722 "Failed to accumulate all the bitfields");
724 field->isPotentiallyOverlapping()) {
741 MemberInfo(bitsToCharUnits(getFieldBitOffset(*field)),
742 MemberInfo::InfoKind::Field,
743 getStorageType(field->getType()->getAsCXXRecordDecl()),
744 getFieldMemberKind(*field), *field));
752 members.push_back(MemberInfo(
753 bitsToCharUnits(getFieldBitOffset(*field)),
754 MemberInfo::InfoKind::Field,
755 field->isPotentiallyOverlapping()
756 ? getStorageType(field->getType()->getAsCXXRecordDecl())
757 : getStorageType(*field),
758 getFieldMemberKind(*field), *field));
764void CIRRecordLowering::calculateZeroInit() {
765 for (
const MemberInfo &member : members) {
766 if (
member.kind == MemberInfo::InfoKind::Field) {
767 if (!
member.fieldDecl || isZeroInitializable(
member.fieldDecl))
769 zeroInitializable = zeroInitializableAsBase =
false;
771 }
else if (
member.kind == MemberInfo::InfoKind::Base ||
772 member.kind == MemberInfo::InfoKind::VBase) {
773 if (isZeroInitializable(
member.cxxRecordDecl))
775 zeroInitializable =
false;
776 if (
member.kind == MemberInfo::InfoKind::Base)
777 zeroInitializableAsBase =
false;
782void CIRRecordLowering::determinePacked(
bool nvBaseType) {
791 for (
const MemberInfo &member : members) {
794 if (!ownsBytes(member))
798 if (!
member.offset.isMultipleOf(getMemberAlignment(
member.data)))
800 if (
member.offset < nvSize)
801 nvAlignment = std::max(nvAlignment, getMemberAlignment(
member.data));
802 alignment = std::max(alignment, getMemberAlignment(
member.data));
806 if (!members.back().offset.isMultipleOf(alignment))
815 members.back().data = getUIntNType(astContext.
toBits(alignment));
818void CIRRecordLowering::insertPadding() {
819 std::vector<std::pair<CharUnits, CharUnits>> padding;
821 for (
const MemberInfo &member : members) {
829 if (!ownsBytes(member)) {
830 if (
member.offset > size) {
831 padding.push_back(std::make_pair(size,
member.offset - size));
836 CharUnits offset =
member.offset;
837 assert(offset >= size);
840 : getMemberAlignment(
member.data)))
841 padding.push_back(std::make_pair(size, offset - size));
842 size = offset + getSize(
member.data);
847 for (
const std::pair<CharUnits, CharUnits> &paddingPair : padding)
848 members.push_back(makeStorageInfo(paddingPair.first,
849 getByteArrayType(paddingPair.second),
850 cir::RecordMemberKind::Pad));
851 llvm::stable_sort(members);
854static cir::ArgPassingKind
858 return cir::ArgPassingKind::CanPassInRegs;
860 return cir::ArgPassingKind::CannotPassInRegs;
862 return cir::ArgPassingKind::CanNeverPassInRegs;
864 llvm_unreachable(
"unknown RecordArgPassingKind");
869[[maybe_unused]]
static bool
876std::unique_ptr<CIRGenRecordLayout>
878 CIRRecordLowering lowering(*
this, rd,
false);
879 assert(ty->
isIncomplete() &&
"recomputing record layout?");
880 lowering.lower(
false);
892 if (llvm::isa<CXXRecordDecl>(rd)) {
901 lowering.astRecordLayout.
getSize()) {
902 CIRRecordLowering baseLowering(*
this, rd, lowering.packed);
903 baseLowering.lower(
true);
905 baseTy = builder.getCompleteNamedRecordType(
906 baseLowering.getFieldTypes(), baseLowering.packed, baseIdentifier,
907 baseLowering.getFieldKinds());
916 assert((rd->
isUnion() || lowering.packed == baseLowering.packed) &&
917 "Non-virtual and complete types must agree on packedness");
922 cgm.getDiags().hasErrorOccurred()) &&
923 "base subobject member kinds must reproduce its ABI emptiness");
931 ty->
complete(lowering.getFieldTypes(), lowering.packed, lowering.unionPadding,
932 lowering.getFieldKinds());
939 cgm.getDiags().hasErrorOccurred()) &&
940 "member kinds must reproduce the ABI emptiness of the record");
944 mlir::MLIRContext *mlirCtx = ty->getContext();
945 cir::ArgPassingKind apk =
948 bool hasTrivialDestructor =
true;
949 if (
auto *cxxRD = dyn_cast<CXXRecordDecl>(rd))
950 hasTrivialDestructor = cxxRD->hasTrivialDestructor();
951 const auto &astLayout = astContext.getASTRecordLayout(rd);
952 uint64_t recordAlignInBytes = astLayout.getAlignment().getQuantity();
953 uint64_t unadjustedAlignInBytes =
954 astLayout.getUnadjustedAlignment().getQuantity();
958 auto addLayout = [&](mlir::StringAttr name, uint64_t align,
959 uint64_t unadjusted) {
960 cgm.addRecordLayout(name, cir::RecordLayoutAttr::get(
961 mlirCtx, apk, hasTrivialDestructor, align,
963 hasFlexibleArrayMember));
965 addLayout(ty->
getName(), recordAlignInBytes, unadjustedAlignInBytes);
971 if (baseTy && baseTy != *ty) {
972 uint64_t baseAlign = astLayout.getNonVirtualAlignment().getQuantity();
973 uint64_t baseUnadjusted = std::min(unadjustedAlignInBytes, baseAlign);
974 addLayout(baseTy.
getName(), baseAlign, baseUnadjusted);
978 auto rl = std::make_unique<CIRGenRecordLayout>(
980 (bool)lowering.zeroInitializable, (bool)lowering.zeroInitializableAsBase);
982 rl->nonVirtualBases.swap(lowering.nonVirtualBases);
983 rl->completeObjectVirtualBases.swap(lowering.virtualBases);
986 rl->fieldIdxMap.swap(lowering.fieldIdxMap);
988 rl->bitFields.swap(lowering.bitFields);
992 llvm::outs() <<
"\n*** Dumping CIRgen Record Layout\n";
993 llvm::outs() <<
"Record: ";
994 rd->
dump(llvm::outs());
995 llvm::outs() <<
"\nLayout: ";
996 rl->print(llvm::outs());
1004 os <<
"<CIRecordLayout\n";
1005 os <<
" CIR Type:" << completeObjectType <<
"\n";
1006 if (baseSubobjectType)
1007 os <<
" NonVirtualBaseCIRType:" << baseSubobjectType <<
"\n";
1008 os <<
" IsZeroInitializable:" << zeroInitializable <<
"\n";
1009 os <<
" BitFields:[\n";
1010 std::vector<std::pair<unsigned, const CIRGenBitFieldInfo *>> bitInfo;
1011 for (
auto &[
decl, info] : bitFields) {
1016 bitInfo.push_back(std::make_pair(
index, &info));
1018 llvm::array_pod_sort(bitInfo.begin(), bitInfo.end());
1019 for (std::pair<unsigned, const CIRGenBitFieldInfo *> &info : bitInfo) {
1021 info.second->print(os);
1028 os <<
"<CIRBitFieldInfo" <<
" name:" <<
name <<
" offset:" <<
offset
1041void CIRRecordLowering::lowerUnion(
bool nonVirtualBaseType) {
1053 mlir::Type fieldType;
1054 cir::RecordMemberKind fieldKind;
1055 if (field->isBitField()) {
1056 if (field->isZeroLengthBitField())
1060 mlir::Type unitStorage =
1061 getBitfieldStorageType(field->getBitWidthValue());
1064 getBitFieldDecl(field));
1065 fieldType = unit.data;
1066 fieldKind = unit.memberKind;
1068 fieldType = getStorageType(field);
1069 fieldKind = getFieldMemberKind(field);
1072 fieldIdxMap[field->getCanonicalDecl()] = 0;
1073 addField(fieldType, fieldKind);
1081 auto hasNamedMember = [](
const FieldDecl *curField) ->
bool {
1082 const auto *rd = curField->getType()->getAsRecordDecl();
1088 auto isNamedMember = [hasNamedMember](
const FieldDecl *curField) ->
bool {
1089 if (curField->isZeroLengthBitField())
1091 return curField->getIdentifier() || hasNamedMember(curField);
1093 auto firstNamedMemberItr = llvm::find_if(
recordDecl->fields(), isNamedMember);
1095 if (firstNamedMemberItr !=
recordDecl->fields().end() &&
1096 !isZeroInitializable(*firstNamedMemberItr))
1097 zeroInitializable = zeroInitializableAsBase =
false;
1100 if (getFieldTypes().empty()) {
1101 appendPaddingBytes(layoutSize);
1105 mlir::Type storageType =
1106 cir::UnionType::getUnionStorageType(dataLayout.
layout, getFieldTypes());
1110 if (layoutSize < getSize(storageType))
1111 storageType = getByteArrayType(layoutSize);
1116 if (nonVirtualBaseType) {
1124 const cir::RecordMemberKind storageKind = makeMemberKind(
1126 llvm::any_of(getFieldKinds(),
1129 addField(storageType, storageKind);
1130 CharUnits padding = layoutSize - getSize(storageType);
1132 addField(getByteArrayType(padding), cir::RecordMemberKind::Pad);
1135 appendPaddingBytes(layoutSize - getSize(storageType));
1137 packed = !layoutSize.
isMultipleOf(getMemberAlignment(storageType));
1153void CIRRecordLowering::computeVolatileBitfields() {
1158 for (
auto &[field, info] : bitFields) {
1162 getSizeInBits(resLTy).getQuantity())
1170 const unsigned oldOffset =
1171 isBigEndian() ?
info.storageSize - (
info.offset +
info.size)
1174 const unsigned absoluteOffset =
1175 astContext.
toBits(
info.storageOffset) + oldOffset;
1178 const unsigned storageSize = getSizeInBits(resLTy).getQuantity();
1181 if (
info.storageSize == storageSize && (oldOffset % storageSize == 0))
1185 unsigned offset = absoluteOffset & (storageSize - 1);
1190 if (offset +
info.size > storageSize)
1195 offset = storageSize - (offset +
info.size);
1197 const CharUnits storageOffset =
1199 const CharUnits end = storageOffset +
1203 const ASTRecordLayout &layout =
1206 const CharUnits recordSize = layout.
getSize();
1207 if (end >= recordSize)
1211 bool conflict =
false;
1214 if (f->isBitField() && !f->isZeroLengthBitField())
1224 if (f->isZeroLengthBitField()) {
1225 if (end > fOffset && storageOffset < fOffset) {
1231 const CharUnits fEnd =
1238 if (end < fOffset || fEnd < storageOffset)
1251 info.volatileStorageOffset =
1254 info.volatileStorageSize = storageSize;
1255 info.volatileOffset = offset;
1259void CIRRecordLowering::accumulateBases() {
1262 const CXXRecordDecl *baseDecl = astRecordLayout.
getPrimaryBase();
1263 members.push_back(MemberInfo(
CharUnits::Zero(), MemberInfo::InfoKind::Base,
1264 getStorageType(baseDecl),
1265 getBaseMemberKind(baseDecl), baseDecl));
1270 if (base.isVirtual())
1274 const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl();
1278 MemberInfo::InfoKind::Base,
1279 getStorageType(baseDecl),
1280 getBaseMemberKind(baseDecl), baseDecl));
1285void CIRRecordLowering::accumulateVBases() {
1287 const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl();
1296 members.push_back(MemberInfo(offset, MemberInfo::InfoKind::VBase,
nullptr,
1297 cir::RecordMemberKind::Data, baseDecl));
1303 ->second.hasVtorDisp())
1306 cir::RecordMemberKind::Data));
1307 members.push_back(MemberInfo(offset, MemberInfo::InfoKind::VBase,
1308 getStorageType(baseDecl),
1309 getBaseMemberKind(baseDecl), baseDecl));
1313void CIRRecordLowering::accumulateVPtrs() {
1315 members.push_back(MemberInfo(
CharUnits::Zero(), MemberInfo::InfoKind::VFPtr,
1316 getVFPtrType(), cir::RecordMemberKind::Data));
1320 "accumulateVPtrs: hasOwnVBPtr");
1323mlir::Type CIRRecordLowering::getVFPtrType() {
1324 return cir::VPtrType::get(builder.getContext());
Defines the clang::ASTContext interface.
static bool marksMatchABIEmptiness(const ASTContext &astContext, const RecordDecl *rd, cir::RecordType recordTy)
Whether the member kinds on recordTy answer the record's ABI emptiness the same way the AST predicate...
static cir::ArgPassingKind convertRecordArgPassingKind(RecordArgPassingKind kind)
static bool isTransparentUnion(QualType T)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx, QualType Ty)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
llvm::TypeSize getTypeAllocSizeInBits(mlir::Type ty) const
Returns the offset in bits between successive objects of the specified type, including alignment padd...
C++ view class that accepts both !cir.struct and !cir.union types.
bool isIncomplete() const
bool isEmptyForABI() const
Whether no member holds data.
mlir::StringAttr getName() const
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
padding is union-only.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
bool isNearlyEmpty(const CXXRecordDecl *RD) const
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
const TargetInfo & getTargetInfo() const
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
CanQualType getCanonicalTagType(const TagDecl *TD) const
uint64_t getCharWidth() const
Return the size of the character type, in bits.
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
bool hasOwnVBPtr() const
hasOwnVBPtr - Does this class provide its own virtual-base table pointer, rather than inheriting one ...
CharUnits getSize() const
getSize - Get the record size in characters.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getDataSize() const
getDataSize() - Get the record data size, which is the record size without tail padding,...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const VBaseOffsetsMapTy & getVBaseOffsetsMap() const
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
bool isPrimaryBaseVirtual() const
isPrimaryBaseVirtual - Get whether the primary base for this record is virtual or not.
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
const clang::CodeGenOptions & getCodeGenOpts() const
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &os) const
This class organizes the cross-module state that is used while lowering AST types to CIR types.
CIRGenModule & getCGModule() const
std::string getRecordTypeName(const clang::RecordDecl *, llvm::StringRef suffix)
clang::ASTContext & getASTContext() const
std::unique_ptr< CIRGenRecordLayout > computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty)
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
Represents a C++ struct/union/class.
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
This is an opaque type for sizes expressed in character units.
bool isZero() const
Test whether the quantity equals zero.
CharUnits alignTo(CharUnits Align) const
Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is a multiple...
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
static CharUnits One()
Construct a CharUnits quantity of one.
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
static CharUnits Zero()
Construct a CharUnits quantity of zero.
Represents a member of a struct/union/class.
bool isBitField() const
Determines whether this field is a bitfield.
unsigned getBitWidthValue() const
Computes the bit width of this field, if this is a bit field.
FieldDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this field.
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
bool isZeroLengthBitField() const
Is this a zero-length bit-field?
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Represents a struct/union/class.
RecordArgPassingKind getArgPassingRestrictions() const
bool hasFlexibleArrayMember() const
const FieldDecl * findFirstNamedDataMember() const
Finds the first data member which has a name.
specific_decl_iterator< FieldDecl > field_iterator
field_iterator field_begin() const
virtual unsigned getRegisterWidth() const
Return the "preferred" register width on this target.
bool hasCheapUnalignedBitFieldAccess() const
Return true iff unaligned accesses are cheap.
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
mlir::Type memberStorageType(mlir::Type memberTy)
The storage a member is stored as: the access unit for a bit-field member, and the member type itself...
bool memberOwnsBytes(mlir::Type memberTy)
Whether a record member occupies bytes of its record.
bool isValidFundamentalIntWidth(unsigned width)
bool anyMemberHoldsDataForABI(llvm::ArrayRef< RecordMemberKind > kinds)
Whether any member holds data for argument passing on its mark alone.
bool isNamedBitField(RecordMemberKind kind)
Whether a member of this kind is an access unit the source can read a bit-field of.
bool isEmptyRecordForABI(const ASTContext &context, QualType t)
isEmptyRecordForABI - Return true if a structure contains only empty base classes and fields.
bool isEmptyFieldForABI(const ASTContext &context, const FieldDecl *fd)
isEmptyFieldForABI - Return true if the field is "empty", that is, it is a zero-width bit-field or an...
bool isAAPCS(const TargetInfo &TargetInfo)
Helper method to check if the underlying ABI is AAPCS.
bool isDiscreteBitFieldABI(const ASTContext &Ctx, const RecordDecl *RD)
The Microsoft bitfield layout rule allocates discrete storage units of the field's formal type and on...
bool hasOwnStorage(const ASTContext &Ctx, const CXXRecordDecl *Decl, const CXXRecordDecl *Query)
Recursively searches all of the bases of Decl to find out whether Query is not the primary vbase of s...
bool isOverlappingVBaseABI(const ASTContext &Ctx)
The Itanium base layout rule allows virtual bases to overlap other bases, which complicates layout in...
bool isEmptyFieldForLayout(const ASTContext &Ctx, const FieldDecl *FD)
Return true iff the field is "empty", that is, either a zero-width bit-field or an isEmptyRecordForLa...
bool isEmptyRecordForLayout(const ASTContext &Ctx, QualType T)
Return true iff a structure contains only empty base classes (per isEmptyRecordForLayout) and fields ...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Decl, FieldDecl > fieldDecl
Matches field declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args)
Prints an indented note to stderr when Verbose is set.
Top level wrappers for InstallAPI frontend operations.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
RecordArgPassingKind
Enum that represents the different ways arguments are passed to and returned from function calls.
@ CanPassInRegs
The argument of this type can be passed directly in registers.
@ CanNeverPassInRegs
The argument of this type cannot be passed directly in registers.
@ CannotPassInRegs
The argument of this type cannot be passed directly in registers.
Diagnostic wrappers for TextAPI types for error reporting.
void __ovld __conv barrier(cl_mem_fence_flags)
All work-items in a work-group executing the kernel on a processor must execute this function before ...
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 unsigned
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
static bool noUniqueAddressLayout()
static bool checkBitfieldClipping()
static bool astRecordDeclAttr()
unsigned offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the c...
void print(llvm::raw_ostream &os) const
LLVM_DUMP_METHOD void dump() const
unsigned storageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned volatileStorageSize
The storage size in bits which should be used when accessing this bitfield.
clang::CharUnits storageOffset
The offset of the bitfield storage from the start of the record.
unsigned size
The total size of the bit-field, in bits.
unsigned isSigned
Whether the bit-field is signed.
clang::CharUnits volatileStorageOffset
The offset of the bitfield storage from the start of the record.
unsigned volatileOffset
The offset within a contiguous run of bitfields that are represented as a single "field" within the c...
llvm::StringRef name
The name of a bitfield.