| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| // | |
| // This contains code to compute the layout of a record. | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "CIRGenBuilder.h" | |
| #include "CIRGenModule.h" | |
| #include "CIRGenTypes.h" | |
| #include "TargetInfo.h" | |
| #include "clang/AST/ASTContext.h" | |
| #include "clang/AST/Decl.h" | |
| #include "clang/AST/DeclCXX.h" | |
| #include "clang/AST/RecordLayout.h" | |
| #include "clang/CIR/Dialect/IR/CIRAttrs.h" | |
| #include "clang/CIR/Dialect/IR/CIRDataLayout.h" | |
| #include "clang/CIR/MissingFeatures.h" | |
| #include "clang/CodeGenUtils/RecordLayoutUtils.h" | |
| #include "clang/CodeGenUtils/TargetUtils.h" | |
| #include "llvm/Support/Casting.h" | |
| #include <memory> | |
| using namespace llvm; | |
| using namespace clang; | |
| using namespace clang::CIRGen; | |
| namespace { | |
| /// The CIRRecordLowering is responsible for lowering an ASTRecordLayout to an | |
| /// mlir::Type. Some of the lowering is straightforward, some is not. | |
| // TODO: Detail some of the complexities and weirdnesses? | |
| // (See CGRecordLayoutBuilder.cpp) | |
| struct CIRRecordLowering final { | |
| // MemberInfo is a helper structure that contains information about a record | |
| // member. In addition to the standard member types, there exists a sentinel | |
| // member type that ensures correct rounding. | |
| struct MemberInfo final { | |
| CharUnits offset; | |
| enum class InfoKind { VFPtr, Field, Base, VBase } kind; | |
| mlir::Type data; | |
| /// What this member holds, recorded on the CIR record type. | |
| cir::RecordMemberKind memberKind; | |
| union { | |
| const FieldDecl *fieldDecl; | |
| const CXXRecordDecl *cxxRecordDecl; | |
| }; | |
| MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data, | |
| cir::RecordMemberKind memberKind, | |
| const FieldDecl *fieldDecl = nullptr) | |
| : offset{offset}, kind{kind}, data{data}, memberKind{memberKind}, | |
| fieldDecl{fieldDecl} {} | |
| MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data, | |
| cir::RecordMemberKind memberKind, const CXXRecordDecl *rd) | |
| : offset{offset}, kind{kind}, data{data}, memberKind{memberKind}, | |
| cxxRecordDecl{rd} {} | |
| // MemberInfos are sorted so we define a < operator. | |
| bool operator<(const MemberInfo &other) const { | |
| return offset < other.offset; | |
| } | |
| }; | |
| static bool ownsBytes(const MemberInfo &member) { | |
| return member.data && cir::memberOwnsBytes(member.data); | |
| } | |
| cir::BitFieldDeclAttr getBitFieldDecl(const FieldDecl *field) { | |
| return cir::BitFieldDeclAttr::get( | |
| cirGenTypes.convertTypeForMem(field->getType()), | |
| field->getBitWidthValue(), field->isUnnamedBitField()); | |
| } | |
| MemberInfo makeAccessUnitInfo(CharUnits offset, mlir::Type storage, | |
| llvm::ArrayRef<cir::BitFieldDeclAttr> fields) { | |
| mlir::Type unitTy = | |
| cir::BitFieldType::get(&cirGenTypes.getMLIRContext(), storage, fields); | |
| // If the access unit doesn't hold any named fields, it's empty. | |
| const bool holdsNamedField = | |
| mlir::cast<cir::BitFieldType>(unitTy).holdsNamedField(); | |
| return makeStorageInfo(offset, unitTy, | |
| holdsNamedField ? cir::RecordMemberKind::BitField | |
| : cir::RecordMemberKind::Empty); | |
| } | |
| /// The member for a zero-width bit-field, which belongs to no access unit | |
| /// and owns no bytes. It sits at its own offset, where the run it ended | |
| /// stops. | |
| MemberInfo makeZeroWidthBitFieldInfo(const FieldDecl *field) { | |
| assert(field->isZeroLengthBitField() && "not a zero-width bit-field"); | |
| return makeAccessUnitInfo(bitsToCharUnits(getFieldBitOffset(field)), | |
| /*storage=*/mlir::Type{}, getBitFieldDecl(field)); | |
| } | |
| // The constructor. | |
| CIRRecordLowering(CIRGenTypes &cirGenTypes, const RecordDecl *recordDecl, | |
| bool packed); | |
| /// Constructs a MemberInfo instance from an offset and mlir::Type. | |
| MemberInfo makeStorageInfo(CharUnits offset, mlir::Type data, | |
| cir::RecordMemberKind memberKind) { | |
| return MemberInfo(offset, MemberInfo::InfoKind::Field, data, memberKind); | |
| } | |
| // Layout routines. | |
| void setBitFieldInfo(const FieldDecl *fd, CharUnits startOffset, | |
| mlir::Type storageType); | |
| void lower(bool nonVirtualBaseType); | |
| void lowerUnion(bool nonVirtualBaseType); | |
| /// Determines if we need a packed llvm struct. | |
| void determinePacked(bool nvBaseType); | |
| /// Inserts padding everywhere it's needed. | |
| void insertPadding(); | |
| void computeVolatileBitfields(); | |
| void accumulateBases(); | |
| void accumulateVPtrs(); | |
| void accumulateVBases(); | |
| void accumulateFields(bool nonVirtualBaseType); | |
| RecordDecl::field_iterator | |
| accumulateBitFields(RecordDecl::field_iterator field, | |
| RecordDecl::field_iterator fieldEnd); | |
| mlir::Type getVFPtrType(); | |
| /// Helper function to check if the target machine is BigEndian. | |
| bool isBigEndian() const { return astContext.getTargetInfo().isBigEndian(); } | |
| CharUnits bitsToCharUnits(uint64_t bitOffset) { | |
| return astContext.toCharUnitsFromBits(bitOffset); | |
| } | |
| void calculateZeroInit(); | |
| CharUnits getSize(mlir::Type Ty) { | |
| return CharUnits::fromQuantity(dataLayout.getTypeAllocSize(Ty)); | |
| } | |
| CharUnits getSizeInBits(mlir::Type ty) { | |
| return CharUnits::fromQuantity(dataLayout.layout.getTypeSizeInBits(ty)); | |
| } | |
| /// The record is marked packed when a member's offset is not a multiple of | |
| /// this alignment. | |
| CharUnits getMemberAlignment(mlir::Type ty) { | |
| return CharUnits::fromQuantity(dataLayout.layout.getTypeABIAlignment(ty)); | |
| } | |
| bool isZeroInitializable(const FieldDecl *fd) { | |
| return cirGenTypes.isZeroInitializable(fd->getType()); | |
| } | |
| bool isZeroInitializable(const RecordDecl *rd) { | |
| return cirGenTypes.isZeroInitializable(rd); | |
| } | |
| /// The mark for a member, given whether it holds data for argument passing | |
| /// and whether it is a bit-field. A run of bit-fields shares one access | |
| /// unit whose width can be narrower than the declared type of the bit-field | |
| /// it holds. | |
| static cir::RecordMemberKind makeMemberKind(bool holdsData, | |
| bool isNamedBitField) { | |
| if (!holdsData) | |
| return cir::RecordMemberKind::Empty; | |
| return isNamedBitField ? cir::RecordMemberKind::BitField | |
| : cir::RecordMemberKind::Data; | |
| } | |
| /// The mark for a field that is not a bit-field. A run of bit-fields is one | |
| /// member per access unit rather than one per field, so a bit-field is | |
| /// marked where its unit is built. | |
| cir::RecordMemberKind getFieldMemberKind(const FieldDecl *fd) { | |
| assert(!fd->isBitField() && "a bit-field is marked with its access unit"); | |
| return makeMemberKind(/*holdsData=*/!isEmptyFieldForABI(astContext, fd), | |
| /*isNamedBitField=*/false); | |
| } | |
| /// The mark for a base subobject. A base contributes no ABI data when it is | |
| /// empty for the ABI, which is not the same as CXXRecordDecl::isEmpty(): a | |
| /// base holding only unnamed bit-fields is laid out but carries no data. | |
| cir::RecordMemberKind getBaseMemberKind(const CXXRecordDecl *baseDecl) { | |
| return isEmptyRecordForABI(astContext, | |
| astContext.getCanonicalTagType(baseDecl)) | |
| ? cir::RecordMemberKind::Empty | |
| : cir::RecordMemberKind::Data; | |
| } | |
| /// Wraps cir::IntType with some implicit arguments. | |
| mlir::Type getUIntNType(uint64_t numBits) { | |
| unsigned alignedBits = llvm::PowerOf2Ceil(numBits); | |
| alignedBits = std::max(8u, alignedBits); | |
| return cir::IntType::get(&cirGenTypes.getMLIRContext(), alignedBits, | |
| /*isSigned=*/false); | |
| } | |
| mlir::Type getCharType() { | |
| return cir::IntType::get(&cirGenTypes.getMLIRContext(), | |
| astContext.getCharWidth(), | |
| /*isSigned=*/false); | |
| } | |
| mlir::Type getByteArrayType(CharUnits numberOfChars) { | |
| assert(!numberOfChars.isZero() && "Empty byte arrays aren't allowed."); | |
| mlir::Type type = getCharType(); | |
| return numberOfChars == CharUnits::One() | |
| ? type | |
| : cir::ArrayType::get(type, numberOfChars.getQuantity()); | |
| } | |
| // Gets the CIR BaseSubobject type from a CXXRecordDecl. | |
| mlir::Type getStorageType(const CXXRecordDecl *RD) { | |
| return cirGenTypes.getCIRGenRecordLayout(RD).getBaseSubobjectCIRType(); | |
| } | |
| // This is different from LLVM traditional codegen because CIRGen uses arrays | |
| // of bytes instead of arbitrary-sized integers. This is important for packed | |
| // structures support. | |
| mlir::Type getBitfieldStorageType(unsigned numBits) { | |
| unsigned alignedBits = llvm::alignTo(numBits, astContext.getCharWidth()); | |
| if (cir::isValidFundamentalIntWidth(alignedBits)) | |
| return builder.getUIntNTy(alignedBits); | |
| mlir::Type type = getCharType(); | |
| return cir::ArrayType::get(type, alignedBits / astContext.getCharWidth()); | |
| } | |
| mlir::Type getStorageType(const FieldDecl *fieldDecl) { | |
| mlir::Type type = cirGenTypes.convertTypeForMem(fieldDecl->getType()); | |
| if (fieldDecl->isBitField()) { | |
| cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), | |
| "getStorageType for bitfields"); | |
| } | |
| if (hasBoolVectorStorageAlignMismatch(type)) | |
| cirGenTypes.getCGModule().errorNYI( | |
| fieldDecl->getSourceRange(), | |
| "getStorageType for bool vector whose storage integer is aligned " | |
| "differently from the vector"); | |
| return type; | |
| } | |
| /// Whether \p type, or the innermost element type of an array \p type, is a | |
| /// bool vector whose storage integer is aligned differently from the | |
| /// vector. A bool vector is stored as an integer with one bit per element, | |
| /// at least a byte wide. The vector is aligned to its own size, so an | |
| /// integer with the same alignment also has the same allocation size. | |
| /// Classic CodeGen lays a mismatched member out as that integer and fills | |
| /// the difference with padding bytes, which this layout does not do yet. | |
| bool hasBoolVectorStorageAlignMismatch(mlir::Type type) { | |
| while (auto arrTy = mlir::dyn_cast<cir::ArrayType>(type)) | |
| type = arrTy.getElementType(); | |
| auto vecTy = mlir::dyn_cast<cir::VectorType>(type); | |
| if (!vecTy || !mlir::isa<cir::BoolType>(vecTy.getElementType())) | |
| return false; | |
| auto storageTy = | |
| mlir::IntegerType::get(type.getContext(), vecTy.getBoolStorageWidth()); | |
| return dataLayout.layout.getTypeABIAlignment(storageTy) != | |
| dataLayout.layout.getTypeABIAlignment(vecTy); | |
| } | |
| uint64_t getFieldBitOffset(const FieldDecl *fieldDecl) { | |
| return astRecordLayout.getFieldOffset(fieldDecl->getFieldIndex()); | |
| } | |
| /// Fills out the structures that are ultimately consumed. | |
| void fillOutputFields(); | |
| void appendPaddingBytes(CharUnits size) { | |
| if (size.isZero()) | |
| return; | |
| mlir::Type padTy = getByteArrayType(size); | |
| if (recordDecl->isUnion()) { | |
| assert(!unionPadding && "at most one union tail-padding type"); | |
| unionPadding = padTy; | |
| } else { | |
| addField(padTy, cir::RecordMemberKind::Pad); | |
| } | |
| } | |
| void addField(mlir::Type ty, cir::RecordMemberKind memberKind) { | |
| fieldTypes.push_back(ty); | |
| fieldKinds.push_back(memberKind); | |
| } | |
| void clearFields() { | |
| fieldTypes.clear(); | |
| fieldKinds.clear(); | |
| } | |
| llvm::ArrayRef<mlir::Type> getFieldTypes() const { return fieldTypes; } | |
| llvm::ArrayRef<cir::RecordMemberKind> getFieldKinds() const { | |
| return fieldKinds; | |
| } | |
| CIRGenTypes &cirGenTypes; | |
| CIRGenBuilderTy &builder; | |
| const ASTContext &astContext; | |
| const RecordDecl *recordDecl; | |
| const CXXRecordDecl *cxxRecordDecl; | |
| const ASTRecordLayout &astRecordLayout; | |
| // Helpful intermediate data-structures | |
| std::vector<MemberInfo> members; | |
| mlir::Type unionPadding; | |
| llvm::DenseMap<const FieldDecl *, CIRGenBitFieldInfo> bitFields; | |
| llvm::DenseMap<const FieldDecl *, unsigned> fieldIdxMap; | |
| llvm::DenseMap<const CXXRecordDecl *, unsigned> nonVirtualBases; | |
| llvm::DenseMap<const CXXRecordDecl *, unsigned> virtualBases; | |
| cir::CIRDataLayout dataLayout; | |
| LLVM_PREFERRED_TYPE(bool) | |
| unsigned zeroInitializable : 1; | |
| LLVM_PREFERRED_TYPE(bool) | |
| unsigned zeroInitializableAsBase : 1; | |
| LLVM_PREFERRED_TYPE(bool) | |
| unsigned packed : 1; | |
| private: | |
| // Output fields, consumed by CIRGenTypes::computeRecordLayout. Private so | |
| // that every append goes through addField and fieldKinds stays parallel to | |
| // fieldTypes. | |
| llvm::SmallVector<mlir::Type, 16> fieldTypes; | |
| llvm::SmallVector<cir::RecordMemberKind> fieldKinds; | |
| CIRRecordLowering(const CIRRecordLowering &) = delete; | |
| void operator=(const CIRRecordLowering &) = delete; | |
| }; // CIRRecordLowering | |
| } // namespace | |
| CIRRecordLowering::CIRRecordLowering(CIRGenTypes &cirGenTypes, | |
| const RecordDecl *recordDecl, bool packed) | |
| : cirGenTypes{cirGenTypes}, builder{cirGenTypes.getBuilder()}, | |
| astContext{cirGenTypes.getASTContext()}, recordDecl{recordDecl}, | |
| cxxRecordDecl{llvm::dyn_cast<CXXRecordDecl>(recordDecl)}, | |
| astRecordLayout{ | |
| cirGenTypes.getASTContext().getASTRecordLayout(recordDecl)}, | |
| dataLayout{cirGenTypes.getCGModule().getModule()}, | |
| zeroInitializable{true}, zeroInitializableAsBase{true}, packed{packed} {} | |
| void CIRRecordLowering::setBitFieldInfo(const FieldDecl *fd, | |
| CharUnits startOffset, | |
| mlir::Type storageType) { | |
| CIRGenBitFieldInfo &info = bitFields[fd->getCanonicalDecl()]; | |
| info.isSigned = fd->getType()->isSignedIntegerOrEnumerationType(); | |
| info.offset = | |
| (unsigned)(getFieldBitOffset(fd) - astContext.toBits(startOffset)); | |
| info.size = fd->getBitWidthValue(); | |
| info.storageSize = getSizeInBits(storageType).getQuantity(); | |
| info.storageOffset = startOffset; | |
| info.storageType = storageType; | |
| info.name = fd->getName(); | |
| if (info.size > info.storageSize) | |
| info.size = info.storageSize; | |
| // Reverse the bit offsets for big endian machines. Since bitfields are laid | |
| // out as packed bits within an integer-sized unit, we can imagine the bits | |
| // counting from the most-significant-bit instead of the | |
| // least-significant-bit. | |
| if (dataLayout.isBigEndian()) | |
| info.offset = info.storageSize - (info.offset + info.size); | |
| info.volatileStorageSize = 0; | |
| info.volatileOffset = 0; | |
| info.volatileStorageOffset = CharUnits::Zero(); | |
| } | |
| void CIRRecordLowering::lower(bool nonVirtualBaseType) { | |
| if (recordDecl->isUnion()) { | |
| lowerUnion(nonVirtualBaseType); | |
| computeVolatileBitfields(); | |
| return; | |
| } | |
| CharUnits size = nonVirtualBaseType ? astRecordLayout.getNonVirtualSize() | |
| : astRecordLayout.getSize(); | |
| accumulateFields(nonVirtualBaseType); | |
| if (cxxRecordDecl) { | |
| accumulateVPtrs(); | |
| accumulateBases(); | |
| if (members.empty()) { | |
| appendPaddingBytes(size); | |
| computeVolatileBitfields(); | |
| return; | |
| } | |
| if (!nonVirtualBaseType) | |
| accumulateVBases(); | |
| } | |
| llvm::stable_sort(members); | |
| // TODO: Verify bitfield clipping | |
| assert(!cir::MissingFeatures::checkBitfieldClipping()); | |
| // The sentinel is popped before fillOutputFields, so its kind never reaches | |
| // the type. | |
| members.push_back( | |
| makeStorageInfo(size, getUIntNType(8), cir::RecordMemberKind::Data)); | |
| determinePacked(nonVirtualBaseType); | |
| insertPadding(); | |
| members.pop_back(); | |
| calculateZeroInit(); | |
| fillOutputFields(); | |
| computeVolatileBitfields(); | |
| } | |
| void CIRRecordLowering::fillOutputFields() { | |
| for (const MemberInfo &member : members) { | |
| if (member.data) | |
| addField(member.data, member.memberKind); | |
| if (member.kind == MemberInfo::InfoKind::Field) { | |
| if (member.fieldDecl) | |
| fieldIdxMap[member.fieldDecl->getCanonicalDecl()] = | |
| fieldTypes.size() - 1; | |
| // A bit-field carries no member of its own. It is numbered as the access | |
| // unit ahead of it, whose storage it is read and written through. | |
| if (!member.data) { | |
| assert(member.fieldDecl && | |
| "member.data is a nullptr so member.fieldDecl should not be"); | |
| setBitFieldInfo(member.fieldDecl, member.offset, | |
| cir::memberStorageType(fieldTypes.back())); | |
| } | |
| } else if (member.kind == MemberInfo::InfoKind::Base) { | |
| nonVirtualBases[member.cxxRecordDecl] = fieldTypes.size() - 1; | |
| } else if (member.kind == MemberInfo::InfoKind::VBase) { | |
| virtualBases[member.cxxRecordDecl] = fieldTypes.size() - 1; | |
| } | |
| } | |
| } | |
| RecordDecl::field_iterator | |
| CIRRecordLowering::accumulateBitFields(RecordDecl::field_iterator field, | |
| RecordDecl::field_iterator fieldEnd) { | |
| if (CodeGenUtils::isDiscreteBitFieldABI(astContext, recordDecl)) { | |
| // run stores the first element of the current run of bitfields. fieldEnd is | |
| // used as a special value to note that we don't have a current run. A | |
| // bitfield run is a contiguous collection of bitfields that can be stored | |
| // in the same storage block. Zero-sized bitfields and bitfields that would | |
| // cross an alignment boundary break a run and start a new one. | |
| RecordDecl::field_iterator run = fieldEnd; | |
| // tail is the offset of the first bit off the end of the current run. It's | |
| // used to determine if the ASTRecordLayout is treating these two bitfields | |
| // as contiguous. StartBitOffset is offset of the beginning of the Run. | |
| uint64_t startBitOffset, tail = 0; | |
| // Where the current run's access unit sits in members, and the bit-fields | |
| // it holds so far. The unit grows as new bit-fields are added. | |
| size_t unitIdx = 0; | |
| mlir::Type unitStorage; | |
| llvm::SmallVector<cir::BitFieldDeclAttr> unitFields; | |
| for (; field != fieldEnd && field->isBitField(); ++field) { | |
| // Zero-width bitfields end runs. | |
| if (field->isZeroLengthBitField()) { | |
| members.push_back(makeZeroWidthBitFieldInfo(*field)); | |
| run = fieldEnd; | |
| continue; | |
| } | |
| uint64_t bitOffset = getFieldBitOffset(*field); | |
| mlir::Type type = cirGenTypes.convertTypeForMem(field->getType()); | |
| cir::BitFieldDeclAttr decl = getBitFieldDecl(*field); | |
| // If we don't have a run yet, or don't live within the previous run's | |
| // allocated storage then we allocate some storage and start a new run. | |
| if (run == fieldEnd || bitOffset >= tail) { | |
| run = field; | |
| startBitOffset = bitOffset; | |
| tail = startBitOffset + dataLayout.getTypeAllocSizeInBits(type); | |
| // The access unit is added to the record before the bit-fields it | |
| // holds so that it gets laid out ahead of them. | |
| unitIdx = members.size(); | |
| unitStorage = type; | |
| unitFields.assign(1, decl); | |
| members.push_back(makeAccessUnitInfo(bitsToCharUnits(startBitOffset), | |
| unitStorage, unitFields)); | |
| } else { | |
| unitFields.push_back(decl); | |
| members[unitIdx] = makeAccessUnitInfo(bitsToCharUnits(startBitOffset), | |
| unitStorage, unitFields); | |
| } | |
| assert(members[unitIdx].offset == bitsToCharUnits(startBitOffset) && | |
| "unitIdx must name the current run's access unit"); | |
| // Bitfields get the offset of their access unit but come afterward and | |
| // remain there after a stable sort. | |
| members.push_back(MemberInfo(bitsToCharUnits(startBitOffset), | |
| MemberInfo::InfoKind::Field, nullptr, | |
| cir::RecordMemberKind::Data, *field)); | |
| } | |
| return field; | |
| } | |
| CharUnits regSize = | |
| bitsToCharUnits(astContext.getTargetInfo().getRegisterWidth()); | |
| unsigned charBits = astContext.getCharWidth(); | |
| // Data about the start of the span we're accumulating to create an access | |
| // unit from. 'Begin' is the first bitfield of the span. If 'begin' is | |
| // 'fieldEnd', we've not got a current span. The span starts at the | |
| // 'beginOffset' character boundary. 'bitSizeSinceBegin' is the size (in bits) | |
| // of the span -- this might include padding when we've advanced to a | |
| // subsequent bitfield run. | |
| RecordDecl::field_iterator begin = fieldEnd; | |
| CharUnits beginOffset; | |
| uint64_t bitSizeSinceBegin; | |
| // The (non-inclusive) end of the largest acceptable access unit we've found | |
| // since 'begin'. If this is 'begin', we're gathering the initial set of | |
| // bitfields of a new span. 'bestEndOffset' is the end of that acceptable | |
| // access unit -- it might extend beyond the last character of the bitfield | |
| // run, using available padding characters. | |
| RecordDecl::field_iterator bestEnd = begin; | |
| CharUnits bestEndOffset; | |
| bool bestClipped; // Whether the representation must be in a byte array. | |
| for (;;) { | |
| // atAlignedBoundary is true if 'field' is the (potential) start of a new | |
| // span (or the end of the bitfields). When true, limitOffset is the | |
| // character offset of that span and barrier indicates whether the new | |
| // span cannot be merged into the current one. | |
| bool atAlignedBoundary = false; | |
| bool barrier = false; // a barrier can be a zero Bit Width or non bit member | |
| if (field != fieldEnd && field->isBitField()) { | |
| uint64_t bitOffset = getFieldBitOffset(*field); | |
| if (begin == fieldEnd) { | |
| // Beginning a new span. | |
| begin = field; | |
| bestEnd = begin; | |
| assert((bitOffset % charBits) == 0 && "Not at start of char"); | |
| beginOffset = bitsToCharUnits(bitOffset); | |
| bitSizeSinceBegin = 0; | |
| } else if ((bitOffset % charBits) != 0) { | |
| // Bitfield occupies the same character as previous bitfield, it must be | |
| // part of the same span. This can include zero-length bitfields, should | |
| // the target not align them to character boundaries. Such non-alignment | |
| // is at variance with the standards, which require zero-length | |
| // bitfields be a barrier between access units. But of course we can't | |
| // achieve that in the middle of a character. | |
| assert(bitOffset == | |
| astContext.toBits(beginOffset) + bitSizeSinceBegin && | |
| "Concatenating non-contiguous bitfields"); | |
| } else { | |
| // Bitfield potentially begins a new span. This includes zero-length | |
| // bitfields on non-aligning targets that lie at character boundaries | |
| // (those are barriers to merging). | |
| if (field->isZeroLengthBitField()) | |
| barrier = true; | |
| atAlignedBoundary = true; | |
| } | |
| } else { | |
| // We've reached the end of the bitfield run. Either we're done, or this | |
| // is a barrier for the current span. | |
| if (begin == fieldEnd) | |
| break; | |
| barrier = true; | |
| atAlignedBoundary = true; | |
| } | |
| // 'installBest' indicates whether we should create an access unit for the | |
| // current best span: fields ['begin', 'bestEnd') occupying characters | |
| // ['beginOffset', 'bestEndOffset'). | |
| bool installBest = false; | |
| if (atAlignedBoundary) { | |
| // 'field' is the start of a new span or the end of the bitfields. The | |
| // just-seen span now extends to 'bitSizeSinceBegin'. | |
| // Determine if we can accumulate that just-seen span into the current | |
| // accumulation. | |
| CharUnits accessSize = bitsToCharUnits(bitSizeSinceBegin + charBits - 1); | |
| if (bestEnd == begin) { | |
| // This is the initial run at the start of a new span. By definition, | |
| // this is the best seen so far. | |
| bestEnd = field; | |
| bestEndOffset = beginOffset + accessSize; | |
| // Assume clipped until proven not below. | |
| bestClipped = true; | |
| if (!bitSizeSinceBegin) | |
| // A zero-sized initial span -- this will install nothing and reset | |
| // for another. | |
| installBest = true; | |
| } else if (accessSize > regSize) { | |
| // Accumulating the just-seen span would create a multi-register access | |
| // unit, which would increase register pressure. | |
| installBest = true; | |
| } | |
| if (!installBest) { | |
| // Determine if accumulating the just-seen span will create an expensive | |
| // access unit or not. | |
| mlir::Type type = getUIntNType(astContext.toBits(accessSize)); | |
| if (!astContext.getTargetInfo().hasCheapUnalignedBitFieldAccess()) { | |
| // Unaligned accesses are expensive. Only accumulate if the new unit | |
| // is naturally aligned. Otherwise install the best we have, which is | |
| // either the initial access unit (can't do better), or a naturally | |
| // aligned accumulation (since we would have already installed it if | |
| // it wasn't naturally aligned). | |
| CharUnits align = getMemberAlignment(type); | |
| if (align > astRecordLayout.getAlignment()) { | |
| // The alignment required is greater than the containing structure | |
| // itself. | |
| installBest = true; | |
| } else if (!beginOffset.isMultipleOf(align)) { | |
| // The access unit is not at a naturally aligned offset within the | |
| // structure. | |
| installBest = true; | |
| } | |
| if (installBest && bestEnd == field) { | |
| // We're installing the first span, whose clipping was presumed | |
| // above. Compute it correctly. | |
| if (getSize(type) == accessSize) | |
| bestClipped = false; | |
| } | |
| } | |
| if (!installBest) { | |
| // Find the next used storage offset to determine what the limit of | |
| // the current span is. That's either the offset of the next field | |
| // with storage (which might be field itself) or the end of the | |
| // non-reusable tail padding. | |
| CharUnits limitOffset; | |
| for (auto probe = field; probe != fieldEnd; ++probe) | |
| if (!CodeGenUtils::isEmptyFieldForLayout(astContext, *probe)) { | |
| // A member with storage sets the limit. | |
| assert((getFieldBitOffset(*probe) % charBits) == 0 && | |
| "Next storage is not byte-aligned"); | |
| limitOffset = bitsToCharUnits(getFieldBitOffset(*probe)); | |
| goto FoundLimit; | |
| } | |
| limitOffset = cxxRecordDecl ? astRecordLayout.getNonVirtualSize() | |
| : astRecordLayout.getDataSize(); | |
| FoundLimit: | |
| CharUnits typeSize = getSize(type); | |
| if (beginOffset + typeSize <= limitOffset) { | |
| // There is space before limitOffset to create a naturally-sized | |
| // access unit. | |
| bestEndOffset = beginOffset + typeSize; | |
| bestEnd = field; | |
| bestClipped = false; | |
| } | |
| if (barrier) { | |
| // The next field is a barrier that we cannot merge across. | |
| installBest = true; | |
| } else if (cirGenTypes.getCGModule() | |
| .getCodeGenOpts() | |
| .FineGrainedBitfieldAccesses) { | |
| installBest = true; | |
| } else { | |
| // Otherwise, we're not installing. Update the bit size | |
| // of the current span to go all the way to limitOffset, which is | |
| // the (aligned) offset of next bitfield to consider. | |
| bitSizeSinceBegin = astContext.toBits(limitOffset - beginOffset); | |
| } | |
| } | |
| } | |
| } | |
| if (installBest) { | |
| assert((field == fieldEnd || !field->isBitField() || | |
| (getFieldBitOffset(*field) % charBits) == 0) && | |
| "Installing but not at an aligned bitfield or limit"); | |
| CharUnits accessSize = bestEndOffset - beginOffset; | |
| if (!accessSize.isZero()) { | |
| // Add the storage member for the access unit to the record. The | |
| // bitfields get the offset of their storage but come afterward and | |
| // remain there after a stable sort. | |
| mlir::Type type; | |
| if (bestClipped) { | |
| assert(getSize(getUIntNType(astContext.toBits(accessSize))) > | |
| accessSize && | |
| "Clipped access need not be clipped"); | |
| type = getByteArrayType(accessSize); | |
| } else { | |
| type = getUIntNType(astContext.toBits(accessSize)); | |
| assert(getSize(type) == accessSize && | |
| "Unclipped access must be clipped"); | |
| } | |
| // A zero-length bit-field in the span was given a member of its own | |
| // when it was accumulated, and belongs to no access unit, so it is not | |
| // one of the fields this unit holds. | |
| llvm::SmallVector<cir::BitFieldDeclAttr> unitFields; | |
| for (auto occupant = begin; occupant != bestEnd; ++occupant) | |
| if (!occupant->isZeroLengthBitField()) | |
| unitFields.push_back(getBitFieldDecl(*occupant)); | |
| assert(!unitFields.empty() && "an access unit holds a bit-field"); | |
| members.push_back(makeAccessUnitInfo(beginOffset, type, unitFields)); | |
| for (; begin != bestEnd; ++begin) | |
| if (!begin->isZeroLengthBitField()) | |
| members.push_back(MemberInfo(beginOffset, | |
| MemberInfo::InfoKind::Field, nullptr, | |
| cir::RecordMemberKind::Data, *begin)); | |
| } | |
| // Reset to start a new span. | |
| field = bestEnd; | |
| begin = fieldEnd; | |
| } else { | |
| assert(field != fieldEnd && field->isBitField() && | |
| "Accumulating past end of bitfields"); | |
| assert(!barrier && "Accumulating across barrier"); | |
| if (field->isZeroLengthBitField()) | |
| members.push_back(makeZeroWidthBitFieldInfo(*field)); | |
| // Accumulate this bitfield into the current (potential) span. | |
| bitSizeSinceBegin += field->getBitWidthValue(); | |
| ++field; | |
| } | |
| } | |
| return field; | |
| } | |
| void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) { | |
| for (RecordDecl::field_iterator field = recordDecl->field_begin(), | |
| fieldEnd = recordDecl->field_end(); | |
| field != fieldEnd;) { | |
| if (field->isBitField()) { | |
| field = accumulateBitFields(field, fieldEnd); | |
| assert((field == fieldEnd || !field->isBitField()) && | |
| "Failed to accumulate all the bitfields"); | |
| } else if (CodeGenUtils::isEmptyFieldForLayout(astContext, *field) && | |
| field->isPotentiallyOverlapping()) { | |
| // We lay out normal empty fields, as they are required for GEPs/getting | |
| // function pointers. However 'no-unique-address' lends some additional | |
| // complexity. These fields take up no real space, but would also have to | |
| // be the correct 'GEP' offset to work here, but then mess with the | |
| // layout. We likely need to come up with a new 'type' to support these, | |
| // then figure out some way to lower these to the correct offset, likely | |
| // by calculating the correct offset into the struct, and forming a | |
| // replacement by-byte GEP in LowerToLLVM. However, this is only a | |
| // problem with taking the address of one of these, so it is in practice | |
| // not a horrifyingly problematic issue. | |
| assert(!cir::MissingFeatures::noUniqueAddressLayout()); | |
| // Dropping the field leaves no member to mark, so its bytes read as | |
| // padding. That is only sound when the field carries no ABI data, so | |
| // make sure we add a member for it. if it does. | |
| if (!isEmptyFieldForABI(astContext, *field)) | |
| members.push_back( | |
| MemberInfo(bitsToCharUnits(getFieldBitOffset(*field)), | |
| MemberInfo::InfoKind::Field, | |
| getStorageType(field->getType()->getAsCXXRecordDecl()), | |
| getFieldMemberKind(*field), *field)); | |
| ++field; | |
| } else { | |
| // Use base subobject layout for potentially-overlapping fields, | |
| // as it is done in RecordLayoutBuilder. | |
| // | |
| // The mark comes from isEmptyFieldForABI, not the isEmptyFieldForLayout | |
| // above. Neither predicate subsumes the other. | |
| members.push_back(MemberInfo( | |
| bitsToCharUnits(getFieldBitOffset(*field)), | |
| MemberInfo::InfoKind::Field, | |
| field->isPotentiallyOverlapping() | |
| ? getStorageType(field->getType()->getAsCXXRecordDecl()) | |
| : getStorageType(*field), | |
| getFieldMemberKind(*field), *field)); | |
| ++field; | |
| } | |
| } | |
| } | |
| void CIRRecordLowering::calculateZeroInit() { | |
| for (const MemberInfo &member : members) { | |
| if (member.kind == MemberInfo::InfoKind::Field) { | |
| if (!member.fieldDecl || isZeroInitializable(member.fieldDecl)) | |
| continue; | |
| zeroInitializable = zeroInitializableAsBase = false; | |
| return; | |
| } else if (member.kind == MemberInfo::InfoKind::Base || | |
| member.kind == MemberInfo::InfoKind::VBase) { | |
| if (isZeroInitializable(member.cxxRecordDecl)) | |
| continue; | |
| zeroInitializable = false; | |
| if (member.kind == MemberInfo::InfoKind::Base) | |
| zeroInitializableAsBase = false; | |
| } | |
| } | |
| } | |
| void CIRRecordLowering::determinePacked(bool nvBaseType) { | |
| if (packed) | |
| return; | |
| CharUnits alignment = CharUnits::One(); | |
| CharUnits nvAlignment = CharUnits::One(); | |
| CharUnits nvSize = !nvBaseType && cxxRecordDecl | |
| ? astRecordLayout.getNonVirtualSize() | |
| : CharUnits::Zero(); | |
| for (const MemberInfo &member : members) { | |
| // A member that owns no bytes sits inside another member's storage, whose | |
| // own offset and alignment are what decide packing. | |
| if (!ownsBytes(member)) | |
| continue; | |
| // If any member falls at an offset that it not a multiple of its alignment, | |
| // then the entire record must be packed. | |
| if (!member.offset.isMultipleOf(getMemberAlignment(member.data))) | |
| packed = true; | |
| if (member.offset < nvSize) | |
| nvAlignment = std::max(nvAlignment, getMemberAlignment(member.data)); | |
| alignment = std::max(alignment, getMemberAlignment(member.data)); | |
| } | |
| // If the size of the record (the capstone's offset) is not a multiple of the | |
| // record's alignment, it must be packed. | |
| if (!members.back().offset.isMultipleOf(alignment)) | |
| packed = true; | |
| // If the non-virtual sub-object is not a multiple of the non-virtual | |
| // sub-object's alignment, it must be packed. We cannot have a packed | |
| // non-virtual sub-object and an unpacked complete object or vise versa. | |
| if (!nvSize.isMultipleOf(nvAlignment)) | |
| packed = true; | |
| // Update the alignment of the sentinel. | |
| if (!packed) | |
| members.back().data = getUIntNType(astContext.toBits(alignment)); | |
| } | |
| void CIRRecordLowering::insertPadding() { | |
| std::vector<std::pair<CharUnits, CharUnits>> padding; | |
| CharUnits size = CharUnits::Zero(); | |
| for (const MemberInfo &member : members) { | |
| if (!member.data) | |
| continue; | |
| // A consumer recovers a member's offset by accumulating the sizes of the | |
| // members before it, so the padding a zero-width bit-field sits inside has | |
| // to be split at its offset for that sum to come out right. The offset | |
| // can sit behind the running size, since the access unit covering the bits | |
| // around it may be wider than the offset it was declared at. | |
| if (!ownsBytes(member)) { | |
| if (member.offset > size) { | |
| padding.push_back(std::make_pair(size, member.offset - size)); | |
| size = member.offset; | |
| } | |
| continue; | |
| } | |
| CharUnits offset = member.offset; | |
| assert(offset >= size); | |
| // Insert padding if we need to. | |
| if (offset != size.alignTo(packed ? CharUnits::One() | |
| : getMemberAlignment(member.data))) | |
| padding.push_back(std::make_pair(size, offset - size)); | |
| size = offset + getSize(member.data); | |
| } | |
| if (padding.empty()) | |
| return; | |
| // Add the padding to the Members list and sort it. | |
| for (const std::pair<CharUnits, CharUnits> &paddingPair : padding) | |
| members.push_back(makeStorageInfo(paddingPair.first, | |
| getByteArrayType(paddingPair.second), | |
| cir::RecordMemberKind::Pad)); | |
| llvm::stable_sort(members); | |
| } | |
| static cir::ArgPassingKind | |
| convertRecordArgPassingKind(RecordArgPassingKind kind) { | |
| switch (kind) { | |
| case RecordArgPassingKind::CanPassInRegs: | |
| return cir::ArgPassingKind::CanPassInRegs; | |
| case RecordArgPassingKind::CannotPassInRegs: | |
| return cir::ArgPassingKind::CannotPassInRegs; | |
| case RecordArgPassingKind::CanNeverPassInRegs: | |
| return cir::ArgPassingKind::CanNeverPassInRegs; | |
| } | |
| llvm_unreachable("unknown RecordArgPassingKind"); | |
| } | |
| /// Whether the member kinds on \p recordTy answer the record's ABI emptiness | |
| /// the same way the AST predicate does. | |
| [[maybe_unused]] static bool | |
| marksMatchABIEmptiness(const ASTContext &astContext, const RecordDecl *rd, | |
| cir::RecordType recordTy) { | |
| return recordTy.isEmptyForABI() == | |
| isEmptyRecordForABI(astContext, astContext.getCanonicalTagType(rd)); | |
| } | |
| std::unique_ptr<CIRGenRecordLayout> | |
| CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) { | |
| CIRRecordLowering lowering(*this, rd, /*packed=*/false); | |
| assert(ty->isIncomplete() && "recomputing record layout?"); | |
| lowering.lower(/*nonVirtualBaseType=*/false); | |
| // If we're in C++, compute the base subobject type. For C++ records baseTy | |
| // defaults to the complete object type and is replaced by a distinct, | |
| // smaller record only when the record has tail padding an enclosing | |
| // [[no_unique_address]] field can reuse. We must populate baseTy even when | |
| // it equals ty because callers such as getStorageType(const CXXRecordDecl *) | |
| // read it unconditionally when laying out potentially-overlapping | |
| // ([[no_unique_address]]) fields; a null baseTy would otherwise propagate as | |
| // a null mlir::Type into the members vector and trip the !empty() assertion | |
| // in fillOutputFields. | |
| cir::RecordType baseTy; | |
| if (llvm::isa<CXXRecordDecl>(rd)) { | |
| baseTy = *ty; | |
| // A record needs a distinct base-subobject type when its tail padding can | |
| // be reused by an enclosing [[no_unique_address]] field, i.e. when the | |
| // non-virtual size differs from the complete size. This matches classic | |
| // CodeGen and covers unions too: a union's non-virtual size already tracks | |
| // its reusable tail padding (and stays at the minimum union size when the | |
| // union is empty, so a zero-data union does not spuriously qualify). | |
| if (lowering.astRecordLayout.getNonVirtualSize() != | |
| lowering.astRecordLayout.getSize()) { | |
| CIRRecordLowering baseLowering(*this, rd, /*Packed=*/lowering.packed); | |
| baseLowering.lower(/*nonVirtualBaseType=*/true); | |
| std::string baseIdentifier = getRecordTypeName(rd, ".base"); | |
| baseTy = builder.getCompleteNamedRecordType( | |
| baseLowering.getFieldTypes(), baseLowering.packed, baseIdentifier, | |
| baseLowering.getFieldKinds()); | |
| // TODO(cir): add something like addRecordTypeName | |
| // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work | |
| // on both of them with the same index. Unions are exempt: CIR derives a | |
| // union's packedness from its layout size, which is the data size for the | |
| // base subobject but the full size for the complete object, so the two | |
| // can legitimately disagree. (Classic CodeGen derives both from the data | |
| // size and so needs no such exemption.) | |
| assert((rd->isUnion() || lowering.packed == baseLowering.packed) && | |
| "Non-virtual and complete types must agree on packedness"); | |
| // Emptiness is a property of the decl, so the base subobject must answer | |
| // the same way the complete object does. The two are not comparable | |
| // mark by mark: they see different sizes and so different tail padding. | |
| assert((marksMatchABIEmptiness(astContext, rd, baseTy) || | |
| cgm.getDiags().hasErrorOccurred()) && | |
| "base subobject member kinds must reproduce its ABI emptiness"); | |
| } | |
| } | |
| // Fill in the record *after* computing the base type. Filling in the body | |
| // signifies that the type is no longer opaque and record layout is complete, | |
| // but we may need to recursively layout rd while laying D out as a base type. | |
| assert(!cir::MissingFeatures::astRecordDeclAttr()); | |
| ty->complete(lowering.getFieldTypes(), lowering.packed, lowering.unionPadding, | |
| lowering.getFieldKinds()); | |
| // The marks exist so that emptiness can be read off the type, so check that | |
| // answer against the AST predicate on every record CIRGen lays out. This | |
| // does not check the individual marks, only what they add up to. The marks | |
| // themselves are pinned by clang/test/CIR/CodeGen/record-member-kinds.*. | |
| assert((marksMatchABIEmptiness(astContext, rd, *ty) || | |
| cgm.getDiags().hasErrorOccurred()) && | |
| "member kinds must reproduce the ABI emptiness of the record"); | |
| // Queue ABI metadata for the module-level cir.record_layouts attribute. | |
| if (ty->getName()) { | |
| mlir::MLIRContext *mlirCtx = ty->getContext(); | |
| cir::ArgPassingKind apk = | |
| convertRecordArgPassingKind(rd->getArgPassingRestrictions()); | |
| bool hasTrivialDestructor = true; | |
| if (auto *cxxRD = dyn_cast<CXXRecordDecl>(rd)) | |
| hasTrivialDestructor = cxxRD->hasTrivialDestructor(); | |
| const auto &astLayout = astContext.getASTRecordLayout(rd); | |
| uint64_t recordAlignInBytes = astLayout.getAlignment().getQuantity(); | |
| uint64_t unadjustedAlignInBytes = | |
| astLayout.getUnadjustedAlignment().getQuantity(); | |
| bool isTransparentUnion = rd->hasAttr<TransparentUnionAttr>(); | |
| 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>( | |
| ty ? *ty : cir::RecordType{}, baseTy ? baseTy : cir::RecordType{}, | |
| (bool)lowering.zeroInitializable, (bool)lowering.zeroInitializableAsBase); | |
| rl->nonVirtualBases.swap(lowering.nonVirtualBases); | |
| rl->completeObjectVirtualBases.swap(lowering.virtualBases); | |
| // Add all the field numbers. | |
| rl->fieldIdxMap.swap(lowering.fieldIdxMap); | |
| rl->bitFields.swap(lowering.bitFields); | |
| // Dump the layout, if requested. | |
| if (getASTContext().getLangOpts().DumpRecordLayouts) { | |
| llvm::outs() << "\n*** Dumping CIRgen Record Layout\n"; | |
| llvm::outs() << "Record: "; | |
| rd->dump(llvm::outs()); | |
| llvm::outs() << "\nLayout: "; | |
| rl->print(llvm::outs()); | |
| } | |
| // TODO: implement verification | |
| return rl; | |
| } | |
| void CIRGenRecordLayout::print(raw_ostream &os) const { | |
| os << "<CIRecordLayout\n"; | |
| os << " CIR Type:" << completeObjectType << "\n"; | |
| if (baseSubobjectType) | |
| os << " NonVirtualBaseCIRType:" << baseSubobjectType << "\n"; | |
| os << " IsZeroInitializable:" << zeroInitializable << "\n"; | |
| os << " BitFields:[\n"; | |
| std::vector<std::pair<unsigned, const CIRGenBitFieldInfo *>> bitInfo; | |
| for (auto &[decl, info] : bitFields) { | |
| const RecordDecl *rd = decl->getParent(); | |
| unsigned index = 0; | |
| for (RecordDecl::field_iterator it = rd->field_begin(); *it != decl; ++it) | |
| ++index; | |
| bitInfo.push_back(std::make_pair(index, &info)); | |
| } | |
| llvm::array_pod_sort(bitInfo.begin(), bitInfo.end()); | |
| for (std::pair<unsigned, const CIRGenBitFieldInfo *> &info : bitInfo) { | |
| os.indent(4); | |
| info.second->print(os); | |
| os << "\n"; | |
| } | |
| os << " ]>\n"; | |
| } | |
| void CIRGenBitFieldInfo::print(raw_ostream &os) const { | |
| os << "<CIRBitFieldInfo" << " name:" << name << " offset:" << offset | |
| << " size:" << size << " isSigned:" << isSigned | |
| << " storageSize:" << storageSize | |
| << " storageOffset:" << storageOffset.getQuantity() | |
| << " volatileOffset:" << volatileOffset | |
| << " volatileStorageSize:" << volatileStorageSize | |
| << " volatileStorageOffset:" << volatileStorageOffset.getQuantity() << ">"; | |
| } | |
| void CIRGenRecordLayout::dump() const { print(llvm::errs()); } | |
| void CIRGenBitFieldInfo::dump() const { print(llvm::errs()); } | |
| void CIRRecordLowering::lowerUnion(bool nonVirtualBaseType) { | |
| // The base-subobject layout of a union is sized to its data size rather than | |
| // its full size. A union can have reusable tail padding when one of its | |
| // members is a [[no_unique_address]] field that itself has tail padding, so | |
| // an enclosing [[no_unique_address]] union field must use this smaller type. | |
| CharUnits layoutSize = nonVirtualBaseType ? astRecordLayout.getDataSize() | |
| : astRecordLayout.getSize(); | |
| // Accumulate bitfields and fields, and figure out what our difference between | |
| // storage type and padding is. | |
| // First, accumulate all the types. | |
| for (const FieldDecl *field : recordDecl->fields()) { | |
| mlir::Type fieldType; | |
| cir::RecordMemberKind fieldKind; | |
| if (field->isBitField()) { | |
| if (field->isZeroLengthBitField()) | |
| continue; | |
| // Every variant starts at offset zero, so a bit-field variant is an | |
| // access unit of its own, holding that one field. | |
| mlir::Type unitStorage = | |
| getBitfieldStorageType(field->getBitWidthValue()); | |
| setBitFieldInfo(field, CharUnits::Zero(), unitStorage); | |
| MemberInfo unit = makeAccessUnitInfo(CharUnits::Zero(), unitStorage, | |
| getBitFieldDecl(field)); | |
| fieldType = unit.data; | |
| fieldKind = unit.memberKind; | |
| } else { | |
| fieldType = getStorageType(field); | |
| fieldKind = getFieldMemberKind(field); | |
| } | |
| fieldIdxMap[field->getCanonicalDecl()] = 0; | |
| addField(fieldType, fieldKind); | |
| } | |
| // Compute zero-initializable status. | |
| // This union might not be zero initialized: it may contain a pointer to | |
| // data member which might have some exotic initialization sequence. | |
| // This chooses the first 'named' member and is zero-initializable based on | |
| // that. | |
| auto hasNamedMember = [](const FieldDecl *curField) -> bool { | |
| const auto *rd = curField->getType()->getAsRecordDecl(); | |
| return rd && rd->findFirstNamedDataMember(); | |
| }; | |
| // Whether this is usable for zero-init as a 'named member'. It is a field | |
| // with a name (or a record type with a named member itself), that isn't a | |
| // zero-width bitfield. | |
| auto isNamedMember = [hasNamedMember](const FieldDecl *curField) -> bool { | |
| if (curField->isZeroLengthBitField()) | |
| return false; | |
| return curField->getIdentifier() || hasNamedMember(curField); | |
| }; | |
| auto firstNamedMemberItr = llvm::find_if(recordDecl->fields(), isNamedMember); | |
| if (firstNamedMemberItr != recordDecl->fields().end() && | |
| !isZeroInitializable(*firstNamedMemberItr)) | |
| zeroInitializable = zeroInitializableAsBase = false; | |
| // If we have no candidates for storage, we are JUST padding. | |
| if (getFieldTypes().empty()) { | |
| appendPaddingBytes(layoutSize); | |
| return; | |
| } | |
| mlir::Type storageType = | |
| cir::UnionType::getUnionStorageType(dataLayout.layout, getFieldTypes()); | |
| // If our storage size was bigger than our required size (can happen in the | |
| // case of packed bitfields on Itanium) then just use an I8 array. | |
| if (layoutSize < getSize(storageType)) | |
| storageType = getByteArrayType(layoutSize); | |
| // The base-subobject record is built as a struct from fieldTypes, so add | |
| // the storage type and any trailing padding as ordinary fields rather than | |
| // routing padding through the union's single tail-padding slot. | |
| if (nonVirtualBaseType) { | |
| // A bit-field mark here says the stand-in's extent is not a declared | |
| // extent, not that its storage came from a bit-field: UnionBitAndWide in | |
| // clang/test/CIR/CodeGen/no-unique-address.cpp takes its double as storage | |
| // and still marks bitfield. The stand-in describes every variant, so it | |
| // takes the storage type rather than any one variant's `!cir.bitfield`, | |
| // and is the one bit-field-marked member that is not one. Computed before | |
| // clearFields() drops the variant marks. | |
| const cir::RecordMemberKind storageKind = makeMemberKind( | |
| /*holdsData=*/cir::anyMemberHoldsDataForABI(getFieldKinds()), | |
| /*isNamedBitField=*/llvm::any_of(getFieldKinds(), | |
| cir::isNamedBitField)); | |
| clearFields(); | |
| addField(storageType, storageKind); | |
| CharUnits padding = layoutSize - getSize(storageType); | |
| if (!padding.isZero()) | |
| addField(getByteArrayType(padding), cir::RecordMemberKind::Pad); | |
| } else { | |
| // Else we just add padding normally. | |
| appendPaddingBytes(layoutSize - getSize(storageType)); | |
| } | |
| packed = !layoutSize.isMultipleOf(getMemberAlignment(storageType)); | |
| } | |
| /// The AAPCS that defines that, when possible, bit-fields should | |
| /// be accessed using containers of the declared type width: | |
| /// When a volatile bit-field is read, and its container does not overlap with | |
| /// any non-bit-field member or any zero length bit-field member, its container | |
| /// must be read exactly once using the access width appropriate to the type of | |
| /// the container. When a volatile bit-field is written, and its container does | |
| /// not overlap with any non-bit-field member or any zero-length bit-field | |
| /// member, its container must be read exactly once and written exactly once | |
| /// using the access width appropriate to the type of the container. The two | |
| /// accesses are not atomic. | |
| /// | |
| /// Enforcing the width restriction can be disabled using | |
| /// -fno-aapcs-bitfield-width. | |
| void CIRRecordLowering::computeVolatileBitfields() { | |
| if (!CodeGenUtils::isAAPCS(astContext.getTargetInfo()) || | |
| !cirGenTypes.getCGModule().getCodeGenOpts().AAPCSBitfieldWidth) | |
| return; | |
| for (auto &[field, info] : bitFields) { | |
| mlir::Type resLTy = cirGenTypes.convertTypeForMem(field->getType()); | |
| if (astContext.toBits(astRecordLayout.getAlignment()) < | |
| getSizeInBits(resLTy).getQuantity()) | |
| continue; | |
| // CIRRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets | |
| // for big-endian targets, but it assumes a container of width | |
| // info.storageSize. Since AAPCS uses a different container size (width | |
| // of the type), we first undo that calculation here and redo it once | |
| // the bit-field offset within the new container is calculated. | |
| const unsigned oldOffset = | |
| isBigEndian() ? info.storageSize - (info.offset + info.size) | |
| : info.offset; | |
| // Offset to the bit-field from the beginning of the struct. | |
| const unsigned absoluteOffset = | |
| astContext.toBits(info.storageOffset) + oldOffset; | |
| // Container size is the width of the bit-field type. | |
| const unsigned storageSize = getSizeInBits(resLTy).getQuantity(); | |
| // Nothing to do if the access uses the desired | |
| // container width and is naturally aligned. | |
| if (info.storageSize == storageSize && (oldOffset % storageSize == 0)) | |
| continue; | |
| // Offset within the container. | |
| unsigned offset = absoluteOffset & (storageSize - 1); | |
| // Bail out if an aligned load of the container cannot cover the entire | |
| // bit-field. This can happen for example, if the bit-field is part of a | |
| // packed struct. AAPCS does not define access rules for such cases, we let | |
| // clang to follow its own rules. | |
| if (offset + info.size > storageSize) | |
| continue; | |
| // Re-adjust offsets for big-endian targets. | |
| if (isBigEndian()) | |
| offset = storageSize - (offset + info.size); | |
| const CharUnits storageOffset = | |
| astContext.toCharUnitsFromBits(absoluteOffset & ~(storageSize - 1)); | |
| const CharUnits end = storageOffset + | |
| astContext.toCharUnitsFromBits(storageSize) - | |
| CharUnits::One(); | |
| const ASTRecordLayout &layout = | |
| astContext.getASTRecordLayout(field->getParent()); | |
| // If we access outside memory outside the record, than bail out. | |
| const CharUnits recordSize = layout.getSize(); | |
| if (end >= recordSize) | |
| continue; | |
| // Bail out if performing this load would access non-bit-fields members. | |
| bool conflict = false; | |
| for (const auto *f : recordDecl->fields()) { | |
| // Allow sized bit-fields overlaps. | |
| if (f->isBitField() && !f->isZeroLengthBitField()) | |
| continue; | |
| const CharUnits fOffset = astContext.toCharUnitsFromBits( | |
| layout.getFieldOffset(f->getFieldIndex())); | |
| // As C11 defines, a zero sized bit-field defines a barrier, so | |
| // fields after and before it should be race condition free. | |
| // The AAPCS acknowledges it and imposes no restritions when the | |
| // natural container overlaps a zero-length bit-field. | |
| if (f->isZeroLengthBitField()) { | |
| if (end > fOffset && storageOffset < fOffset) { | |
| conflict = true; | |
| break; | |
| } | |
| } | |
| const CharUnits fEnd = | |
| fOffset + | |
| astContext.toCharUnitsFromBits( | |
| getSizeInBits(cirGenTypes.convertTypeForMem(f->getType())) | |
| .getQuantity()) - | |
| CharUnits::One(); | |
| // If no overlap, continue. | |
| if (end < fOffset || fEnd < storageOffset) | |
| continue; | |
| // The desired load overlaps a non-bit-field member, bail out. | |
| conflict = true; | |
| break; | |
| } | |
| if (conflict) | |
| continue; | |
| // Write the new bit-field access parameters. | |
| // As the storage offset now is defined as the number of elements from the | |
| // start of the structure, we should divide the Offset by the element size. | |
| info.volatileStorageOffset = | |
| storageOffset / | |
| astContext.toCharUnitsFromBits(storageSize).getQuantity(); | |
| info.volatileStorageSize = storageSize; | |
| info.volatileOffset = offset; | |
| } | |
| } | |
| void CIRRecordLowering::accumulateBases() { | |
| // If we've got a primary virtual base, we need to add it with the bases. | |
| if (astRecordLayout.isPrimaryBaseVirtual()) { | |
| const CXXRecordDecl *baseDecl = astRecordLayout.getPrimaryBase(); | |
| members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::InfoKind::Base, | |
| getStorageType(baseDecl), | |
| getBaseMemberKind(baseDecl), baseDecl)); | |
| } | |
| // Accumulate the non-virtual bases. | |
| for (const auto &base : cxxRecordDecl->bases()) { | |
| if (base.isVirtual()) | |
| continue; | |
| // Bases can be zero-sized even if not technically empty if they | |
| // contain only a trailing array member. | |
| const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl(); | |
| if (!baseDecl->isEmpty() && | |
| !astContext.getASTRecordLayout(baseDecl).getNonVirtualSize().isZero()) { | |
| members.push_back(MemberInfo(astRecordLayout.getBaseClassOffset(baseDecl), | |
| MemberInfo::InfoKind::Base, | |
| getStorageType(baseDecl), | |
| getBaseMemberKind(baseDecl), baseDecl)); | |
| } | |
| } | |
| } | |
| void CIRRecordLowering::accumulateVBases() { | |
| for (const auto &base : cxxRecordDecl->vbases()) { | |
| const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl(); | |
| if (CodeGenUtils::isEmptyRecordForLayout(astContext, base.getType())) | |
| continue; | |
| CharUnits offset = astRecordLayout.getVBaseClassOffset(baseDecl); | |
| // If the vbase is a primary virtual base of some base, then it doesn't | |
| // get its own storage location but instead lives inside of that base. | |
| if (CodeGenUtils::isOverlappingVBaseABI(astContext) && | |
| astContext.isNearlyEmpty(baseDecl) && | |
| !CodeGenUtils::hasOwnStorage(astContext, cxxRecordDecl, baseDecl)) { | |
| members.push_back(MemberInfo(offset, MemberInfo::InfoKind::VBase, nullptr, | |
| cir::RecordMemberKind::Data, baseDecl)); | |
| continue; | |
| } | |
| // If we've got a vtordisp, add it as a storage type. | |
| if (astRecordLayout.getVBaseOffsetsMap() | |
| .find(baseDecl) | |
| ->second.hasVtorDisp()) | |
| members.push_back(makeStorageInfo(offset - CharUnits::fromQuantity(4), | |
| getUIntNType(32), | |
| cir::RecordMemberKind::Data)); | |
| members.push_back(MemberInfo(offset, MemberInfo::InfoKind::VBase, | |
| getStorageType(baseDecl), | |
| getBaseMemberKind(baseDecl), baseDecl)); | |
| } | |
| } | |
| void CIRRecordLowering::accumulateVPtrs() { | |
| if (astRecordLayout.hasOwnVFPtr()) | |
| members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::InfoKind::VFPtr, | |
| getVFPtrType(), cir::RecordMemberKind::Data)); | |
| if (astRecordLayout.hasOwnVBPtr()) | |
| cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), | |
| "accumulateVPtrs: hasOwnVBPtr"); | |
| } | |
| mlir::Type CIRRecordLowering::getVFPtrType() { | |
| return cir::VPtrType::get(builder.getContext()); | |
| } |