|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This contains code to compute the layout of a record. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "CIRGenBuilder.h" |
| 14 | +#include "CIRGenModule.h" |
| 15 | +#include "CIRGenTypes.h" |
| 16 | + |
| 17 | +#include "clang/AST/ASTContext.h" |
| 18 | +#include "clang/AST/Decl.h" |
| 19 | +#include "clang/AST/DeclCXX.h" |
| 20 | +#include "clang/AST/RecordLayout.h" |
| 21 | +#include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 22 | +#include "llvm/Support/Casting.h" |
| 23 | + |
| 24 | +#include <memory> |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | +using namespace clang; |
| 28 | +using namespace clang::CIRGen; |
| 29 | + |
| 30 | +namespace { |
| 31 | +/// The CIRRecordLowering is responsible for lowering an ASTRecordLayout to an |
| 32 | +/// mlir::Type. Some of the lowering is straightforward, some is not. |
| 33 | +// TODO: Detail some of the complexities and weirdnesses? |
| 34 | +// (See CGRecordLayoutBuilder.cpp) |
| 35 | +struct CIRRecordLowering final { |
| 36 | + |
| 37 | + // MemberInfo is a helper structure that contains information about a record |
| 38 | + // member. In addition to the standard member types, there exists a sentinel |
| 39 | + // member type that ensures correct rounding. |
| 40 | + struct MemberInfo final { |
| 41 | + CharUnits offset; |
| 42 | + enum class InfoKind { Field } kind; |
| 43 | + mlir::Type data; |
| 44 | + union { |
| 45 | + const FieldDecl *fieldDecl; |
| 46 | + // CXXRecordDecl will be used here when base types are supported. |
| 47 | + }; |
| 48 | + MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data, |
| 49 | + const FieldDecl *fieldDecl = nullptr) |
| 50 | + : offset(offset), kind(kind), data(data), fieldDecl(fieldDecl) {}; |
| 51 | + // MemberInfos are sorted so we define a < operator. |
| 52 | + bool operator<(const MemberInfo &other) const { |
| 53 | + return offset < other.offset; |
| 54 | + } |
| 55 | + }; |
| 56 | + // The constructor. |
| 57 | + CIRRecordLowering(CIRGenTypes &cirGenTypes, const RecordDecl *recordDecl, |
| 58 | + bool isPacked); |
| 59 | + |
| 60 | + void lower(); |
| 61 | + |
| 62 | + void accumulateFields(); |
| 63 | + |
| 64 | + CharUnits bitsToCharUnits(uint64_t bitOffset) { |
| 65 | + return astContext.toCharUnitsFromBits(bitOffset); |
| 66 | + } |
| 67 | + |
| 68 | + CharUnits getSize(mlir::Type Ty) { |
| 69 | + assert(!cir::MissingFeatures::recordTypeLayoutInfo()); |
| 70 | + return CharUnits::One(); |
| 71 | + } |
| 72 | + CharUnits getAlignment(mlir::Type Ty) { |
| 73 | + assert(!cir::MissingFeatures::recordTypeLayoutInfo()); |
| 74 | + return CharUnits::One(); |
| 75 | + } |
| 76 | + |
| 77 | + mlir::Type getStorageType(const FieldDecl *fieldDecl) { |
| 78 | + mlir::Type type = cirGenTypes.convertTypeForMem(fieldDecl->getType()); |
| 79 | + if (fieldDecl->isBitField()) { |
| 80 | + cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), |
| 81 | + "getStorageType for bitfields"); |
| 82 | + } |
| 83 | + return type; |
| 84 | + } |
| 85 | + |
| 86 | + uint64_t getFieldBitOffset(const FieldDecl *fieldDecl) { |
| 87 | + return astRecordLayout.getFieldOffset(fieldDecl->getFieldIndex()); |
| 88 | + } |
| 89 | + |
| 90 | + /// Fills out the structures that are ultimately consumed. |
| 91 | + void fillOutputFields(); |
| 92 | + |
| 93 | + CIRGenTypes &cirGenTypes; |
| 94 | + CIRGenBuilderTy &builder; |
| 95 | + const ASTContext &astContext; |
| 96 | + const RecordDecl *recordDecl; |
| 97 | + const ASTRecordLayout &astRecordLayout; |
| 98 | + // Helpful intermediate data-structures |
| 99 | + std::vector<MemberInfo> members; |
| 100 | + // Output fields, consumed by CIRGenTypes::computeRecordLayout |
| 101 | + llvm::SmallVector<mlir::Type, 16> fieldTypes; |
| 102 | + llvm::DenseMap<const FieldDecl *, unsigned> fields; |
| 103 | + |
| 104 | + LLVM_PREFERRED_TYPE(bool) |
| 105 | + unsigned zeroInitializable : 1; |
| 106 | + LLVM_PREFERRED_TYPE(bool) |
| 107 | + unsigned packed : 1; |
| 108 | + LLVM_PREFERRED_TYPE(bool) |
| 109 | + unsigned padded : 1; |
| 110 | + |
| 111 | +private: |
| 112 | + CIRRecordLowering(const CIRRecordLowering &) = delete; |
| 113 | + void operator=(const CIRRecordLowering &) = delete; |
| 114 | +}; // CIRRecordLowering |
| 115 | +} // namespace |
| 116 | + |
| 117 | +CIRRecordLowering::CIRRecordLowering(CIRGenTypes &cirGenTypes, |
| 118 | + const RecordDecl *recordDecl, |
| 119 | + bool isPacked) |
| 120 | + : cirGenTypes(cirGenTypes), builder(cirGenTypes.getBuilder()), |
| 121 | + astContext(cirGenTypes.getASTContext()), recordDecl(recordDecl), |
| 122 | + astRecordLayout( |
| 123 | + cirGenTypes.getASTContext().getASTRecordLayout(recordDecl)), |
| 124 | + zeroInitializable(true), packed(isPacked), padded(false) {} |
| 125 | + |
| 126 | +void CIRRecordLowering::lower() { |
| 127 | + if (recordDecl->isUnion()) { |
| 128 | + cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), |
| 129 | + "lower: union"); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + if (isa<CXXRecordDecl>(recordDecl)) { |
| 134 | + cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), |
| 135 | + "lower: class"); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + assert(!cir::MissingFeatures::cxxSupport()); |
| 140 | + |
| 141 | + accumulateFields(); |
| 142 | + |
| 143 | + llvm::stable_sort(members); |
| 144 | + // TODO: implement clipTailPadding once bitfields are implemented |
| 145 | + assert(!cir::MissingFeatures::bitfields()); |
| 146 | + // TODO: implemented packed records |
| 147 | + assert(!cir::MissingFeatures::packedRecords()); |
| 148 | + // TODO: implement padding |
| 149 | + assert(!cir::MissingFeatures::recordPadding()); |
| 150 | + // TODO: support zeroInit |
| 151 | + assert(!cir::MissingFeatures::recordZeroInit()); |
| 152 | + |
| 153 | + fillOutputFields(); |
| 154 | +} |
| 155 | + |
| 156 | +void CIRRecordLowering::fillOutputFields() { |
| 157 | + for (const MemberInfo &member : members) { |
| 158 | + if (member.data) |
| 159 | + fieldTypes.push_back(member.data); |
| 160 | + if (member.kind == MemberInfo::InfoKind::Field) { |
| 161 | + if (member.fieldDecl) |
| 162 | + fields[member.fieldDecl->getCanonicalDecl()] = fieldTypes.size() - 1; |
| 163 | + // A field without storage must be a bitfield. |
| 164 | + assert(!cir::MissingFeatures::bitfields()); |
| 165 | + } |
| 166 | + assert(!cir::MissingFeatures::cxxSupport()); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +void CIRRecordLowering::accumulateFields() { |
| 171 | + for (const FieldDecl *field : recordDecl->fields()) { |
| 172 | + if (field->isBitField()) { |
| 173 | + cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(), |
| 174 | + "accumulate bitfields"); |
| 175 | + ++field; |
| 176 | + } else if (!field->isZeroSize(astContext)) { |
| 177 | + members.push_back(MemberInfo(bitsToCharUnits(getFieldBitOffset(field)), |
| 178 | + MemberInfo::InfoKind::Field, |
| 179 | + getStorageType(field), field)); |
| 180 | + ++field; |
| 181 | + } else { |
| 182 | + // TODO(cir): do we want to do anything special about zero size members? |
| 183 | + assert(!cir::MissingFeatures::zeroSizeRecordMembers()); |
| 184 | + ++field; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +std::unique_ptr<CIRGenRecordLayout> |
| 190 | +CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) { |
| 191 | + CIRRecordLowering lowering(*this, rd, /*packed=*/false); |
| 192 | + assert(ty->isIncomplete() && "recomputing record layout?"); |
| 193 | + lowering.lower(); |
| 194 | + |
| 195 | + // If we're in C++, compute the base subobject type. |
| 196 | + if (llvm::isa<CXXRecordDecl>(rd) && !rd->isUnion() && |
| 197 | + !rd->hasAttr<FinalAttr>()) { |
| 198 | + cgm.errorNYI(rd->getSourceRange(), "computeRecordLayout: CXXRecordDecl"); |
| 199 | + } |
| 200 | + |
| 201 | + // Fill in the record *after* computing the base type. Filling in the body |
| 202 | + // signifies that the type is no longer opaque and record layout is complete, |
| 203 | + // but we may need to recursively layout rd while laying D out as a base type. |
| 204 | + assert(!cir::MissingFeatures::astRecordDeclAttr()); |
| 205 | + ty->complete(lowering.fieldTypes, lowering.packed, lowering.padded); |
| 206 | + |
| 207 | + auto rl = std::make_unique<CIRGenRecordLayout>(ty ? *ty : cir::RecordType()); |
| 208 | + |
| 209 | + assert(!cir::MissingFeatures::recordZeroInit()); |
| 210 | + assert(!cir::MissingFeatures::cxxSupport()); |
| 211 | + assert(!cir::MissingFeatures::bitfields()); |
| 212 | + |
| 213 | + // Add all the field numbers. |
| 214 | + rl->fieldInfo.swap(lowering.fields); |
| 215 | + |
| 216 | + // Dump the layout, if requested. |
| 217 | + if (getASTContext().getLangOpts().DumpRecordLayouts) { |
| 218 | + cgm.errorNYI(rd->getSourceRange(), "computeRecordLayout: dump layout"); |
| 219 | + } |
| 220 | + |
| 221 | + // TODO: implement verification |
| 222 | + return rl; |
| 223 | +} |
0 commit comments