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#include "TargetInfo.h"
17
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/RecordLayout.h"
22#include "clang/CIR/Dialect/IR/CIRAttrs.h"
23#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
24#include "clang/CIR/MissingFeatures.h"
25#include "clang/CodeGenUtils/RecordLayoutUtils.h"
26#include "clang/CodeGenUtils/TargetUtils.h"
27#include "llvm/Support/Casting.h"
28
29#include <memory>
30
31using namespace llvm;
32using namespace clang;
33using namespace clang::CIRGen;
34
35namespace {
36/// The CIRRecordLowering is responsible for lowering an ASTRecordLayout to an
37/// mlir::Type. Some of the lowering is straightforward, some is not.
38// TODO: Detail some of the complexities and weirdnesses?
39// (See CGRecordLayoutBuilder.cpp)
40struct CIRRecordLowering final {
41
42 // MemberInfo is a helper structure that contains information about a record
43 // member. In addition to the standard member types, there exists a sentinel
44 // member type that ensures correct rounding.
45 struct MemberInfo final {
46 CharUnits offset;
47 enum class InfoKind { VFPtr, Field, Base, VBase } kind;
48 mlir::Type data;
49 /// What this member holds, recorded on the CIR record type.
50 cir::RecordMemberKind memberKind;
51 union {
52 const FieldDecl *fieldDecl;
53 const CXXRecordDecl *cxxRecordDecl;
54 };
55 MemberInfo(CharUnits offset, InfoKind kind, mlir::Type data,
56 cir::RecordMemberKind memberKind,
57 const FieldDecl *fieldDecl = nullptr)
58 : offset{offset}, kind{kind}, data{data}, memberKind{memberKind},
59 fieldDecl{fieldDecl} {}
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},
63 cxxRecordDecl{rd} {}
64 // MemberInfos are sorted so we define a < operator.
65 bool operator<(const MemberInfo &other) const {
66 return offset < other.offset;
67 }
68 };
69
70 static bool ownsBytes(const MemberInfo &member) {
71 return member.data && cir::memberOwnsBytes(memberTy: member.data);
72 }
73
74 cir::BitFieldDeclAttr getBitFieldDecl(const FieldDecl *field) {
75 return cir::BitFieldDeclAttr::get(
76 declaredType: cirGenTypes.convertTypeForMem(field->getType()),
77 width: field->getBitWidthValue(), isUnnamed: field->isUnnamedBitField());
78 }
79
80 MemberInfo makeAccessUnitInfo(CharUnits offset, mlir::Type storage,
81 llvm::ArrayRef<cir::BitFieldDeclAttr> fields) {
82 mlir::Type unitTy =
83 cir::BitFieldType::get(context: &cirGenTypes.getMLIRContext(), storage_type: storage, fields);
84 // If the access unit doesn't hold any named fields, it's empty.
85 const bool holdsNamedField =
86 mlir::cast<cir::BitFieldType>(Val&: unitTy).holdsNamedField();
87 return makeStorageInfo(offset, data: unitTy,
88 memberKind: holdsNamedField ? cir::RecordMemberKind::BitField
89 : cir::RecordMemberKind::Empty);
90 }
91
92 /// The member for a zero-width bit-field, which belongs to no access unit
93 /// and owns no bytes. It sits at its own offset, where the run it ended
94 /// stops.
95 MemberInfo makeZeroWidthBitFieldInfo(const FieldDecl *field) {
96 assert(field->isZeroLengthBitField() && "not a zero-width bit-field");
97 return makeAccessUnitInfo(offset: bitsToCharUnits(bitOffset: getFieldBitOffset(fieldDecl: field)),
98 /*storage=*/mlir::Type{}, fields: getBitFieldDecl(field));
99 }
100
101 // The constructor.
102 CIRRecordLowering(CIRGenTypes &cirGenTypes, const RecordDecl *recordDecl,
103 bool packed);
104
105 /// Constructs a MemberInfo instance from an offset and mlir::Type.
106 MemberInfo makeStorageInfo(CharUnits offset, mlir::Type data,
107 cir::RecordMemberKind memberKind) {
108 return MemberInfo(offset, MemberInfo::InfoKind::Field, data, memberKind);
109 }
110
111 // Layout routines.
112 void setBitFieldInfo(const FieldDecl *fd, CharUnits startOffset,
113 mlir::Type storageType);
114
115 void lower(bool nonVirtualBaseType);
116 void lowerUnion(bool nonVirtualBaseType);
117
118 /// Determines if we need a packed llvm struct.
119 void determinePacked(bool nvBaseType);
120 /// Inserts padding everywhere it's needed.
121 void insertPadding();
122
123 void computeVolatileBitfields();
124 void accumulateBases();
125 void accumulateVPtrs();
126 void accumulateVBases();
127 void accumulateFields(bool nonVirtualBaseType);
128 RecordDecl::field_iterator
129 accumulateBitFields(RecordDecl::field_iterator field,
130 RecordDecl::field_iterator fieldEnd);
131
132 mlir::Type getVFPtrType();
133
134 /// Helper function to check if the target machine is BigEndian.
135 bool isBigEndian() const { return astContext.getTargetInfo().isBigEndian(); }
136
137 CharUnits bitsToCharUnits(uint64_t bitOffset) {
138 return astContext.toCharUnitsFromBits(BitSize: bitOffset);
139 }
140
141 void calculateZeroInit();
142
143 CharUnits getSize(mlir::Type Ty) {
144 return CharUnits::fromQuantity(Quantity: dataLayout.getTypeAllocSize(ty: Ty));
145 }
146 CharUnits getSizeInBits(mlir::Type ty) {
147 return CharUnits::fromQuantity(Quantity: dataLayout.layout.getTypeSizeInBits(t: ty));
148 }
149
150 /// The record is marked packed when a member's offset is not a multiple of
151 /// this alignment.
152 CharUnits getMemberAlignment(mlir::Type ty) {
153 return CharUnits::fromQuantity(Quantity: dataLayout.layout.getTypeABIAlignment(t: ty));
154 }
155
156 bool isZeroInitializable(const FieldDecl *fd) {
157 return cirGenTypes.isZeroInitializable(ty: fd->getType());
158 }
159 bool isZeroInitializable(const RecordDecl *rd) {
160 return cirGenTypes.isZeroInitializable(rd);
161 }
162
163 /// The mark for a member, given whether it holds data for argument passing
164 /// and whether it is a bit-field. A run of bit-fields shares one access
165 /// unit whose width can be narrower than the declared type of the bit-field
166 /// it holds.
167 static cir::RecordMemberKind makeMemberKind(bool holdsData,
168 bool isNamedBitField) {
169 if (!holdsData)
170 return cir::RecordMemberKind::Empty;
171 return isNamedBitField ? cir::RecordMemberKind::BitField
172 : cir::RecordMemberKind::Data;
173 }
174
175 /// The mark for a field that is not a bit-field. A run of bit-fields is one
176 /// member per access unit rather than one per field, so a bit-field is
177 /// marked where its unit is built.
178 cir::RecordMemberKind getFieldMemberKind(const FieldDecl *fd) {
179 assert(!fd->isBitField() && "a bit-field is marked with its access unit");
180 return makeMemberKind(/*holdsData=*/!isEmptyFieldForABI(context: astContext, fd),
181 /*isNamedBitField=*/false);
182 }
183
184 /// The mark for a base subobject. A base contributes no ABI data when it is
185 /// empty for the ABI, which is not the same as CXXRecordDecl::isEmpty(): a
186 /// base holding only unnamed bit-fields is laid out but carries no data.
187 cir::RecordMemberKind getBaseMemberKind(const CXXRecordDecl *baseDecl) {
188 return isEmptyRecordForABI(context: astContext,
189 t: astContext.getCanonicalTagType(TD: baseDecl))
190 ? cir::RecordMemberKind::Empty
191 : cir::RecordMemberKind::Data;
192 }
193
194 /// Wraps cir::IntType with some implicit arguments.
195 mlir::Type getUIntNType(uint64_t numBits) {
196 unsigned alignedBits = llvm::PowerOf2Ceil(A: numBits);
197 alignedBits = std::max(a: 8u, b: alignedBits);
198 return cir::IntType::get(context: &cirGenTypes.getMLIRContext(), width: alignedBits,
199 /*isSigned=*/is_signed: false);
200 }
201
202 mlir::Type getCharType() {
203 return cir::IntType::get(context: &cirGenTypes.getMLIRContext(),
204 width: astContext.getCharWidth(),
205 /*isSigned=*/is_signed: false);
206 }
207
208 mlir::Type getByteArrayType(CharUnits numberOfChars) {
209 assert(!numberOfChars.isZero() && "Empty byte arrays aren't allowed.");
210 mlir::Type type = getCharType();
211 return numberOfChars == CharUnits::One()
212 ? type
213 : cir::ArrayType::get(elementType: type, size: numberOfChars.getQuantity());
214 }
215
216 // Gets the CIR BaseSubobject type from a CXXRecordDecl.
217 mlir::Type getStorageType(const CXXRecordDecl *RD) {
218 return cirGenTypes.getCIRGenRecordLayout(rd: RD).getBaseSubobjectCIRType();
219 }
220 // This is different from LLVM traditional codegen because CIRGen uses arrays
221 // of bytes instead of arbitrary-sized integers. This is important for packed
222 // structures support.
223 mlir::Type getBitfieldStorageType(unsigned numBits) {
224 unsigned alignedBits = llvm::alignTo(Value: numBits, Align: astContext.getCharWidth());
225 if (cir::isValidFundamentalIntWidth(width: alignedBits))
226 return builder.getUIntNTy(n: alignedBits);
227
228 mlir::Type type = getCharType();
229 return cir::ArrayType::get(elementType: type, size: alignedBits / astContext.getCharWidth());
230 }
231
232 mlir::Type getStorageType(const FieldDecl *fieldDecl) {
233 mlir::Type type = cirGenTypes.convertTypeForMem(fieldDecl->getType());
234 if (fieldDecl->isBitField()) {
235 cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
236 "getStorageType for bitfields");
237 }
238 if (hasBoolVectorStorageAlignMismatch(type))
239 cirGenTypes.getCGModule().errorNYI(
240 fieldDecl->getSourceRange(),
241 "getStorageType for bool vector whose storage integer is aligned "
242 "differently from the vector");
243 return type;
244 }
245
246 /// Whether \p type, or the innermost element type of an array \p type, is a
247 /// bool vector whose storage integer is aligned differently from the
248 /// vector. A bool vector is stored as an integer with one bit per element,
249 /// at least a byte wide. The vector is aligned to its own size, so an
250 /// integer with the same alignment also has the same allocation size.
251 /// Classic CodeGen lays a mismatched member out as that integer and fills
252 /// the difference with padding bytes, which this layout does not do yet.
253 bool hasBoolVectorStorageAlignMismatch(mlir::Type type) {
254 while (auto arrTy = mlir::dyn_cast<cir::ArrayType>(Val&: type))
255 type = arrTy.getElementType();
256 auto vecTy = mlir::dyn_cast<cir::VectorType>(Val&: type);
257 if (!vecTy || !mlir::isa<cir::BoolType>(Val: vecTy.getElementType()))
258 return false;
259 auto storageTy =
260 mlir::IntegerType::get(context: type.getContext(), width: vecTy.getBoolStorageWidth());
261 return dataLayout.layout.getTypeABIAlignment(t: storageTy) !=
262 dataLayout.layout.getTypeABIAlignment(t: vecTy);
263 }
264
265 uint64_t getFieldBitOffset(const FieldDecl *fieldDecl) {
266 return astRecordLayout.getFieldOffset(FieldNo: fieldDecl->getFieldIndex());
267 }
268
269 /// Fills out the structures that are ultimately consumed.
270 void fillOutputFields();
271
272 void appendPaddingBytes(CharUnits size) {
273 if (size.isZero())
274 return;
275 mlir::Type padTy = getByteArrayType(numberOfChars: size);
276 if (recordDecl->isUnion()) {
277 assert(!unionPadding && "at most one union tail-padding type");
278 unionPadding = padTy;
279 } else {
280 addField(ty: padTy, memberKind: cir::RecordMemberKind::Pad);
281 }
282 }
283
284 void addField(mlir::Type ty, cir::RecordMemberKind memberKind) {
285 fieldTypes.push_back(Elt: ty);
286 fieldKinds.push_back(Elt: memberKind);
287 }
288
289 void clearFields() {
290 fieldTypes.clear();
291 fieldKinds.clear();
292 }
293
294 llvm::ArrayRef<mlir::Type> getFieldTypes() const { return fieldTypes; }
295 llvm::ArrayRef<cir::RecordMemberKind> getFieldKinds() const {
296 return fieldKinds;
297 }
298
299 CIRGenTypes &cirGenTypes;
300 CIRGenBuilderTy &builder;
301 const ASTContext &astContext;
302 const RecordDecl *recordDecl;
303 const CXXRecordDecl *cxxRecordDecl;
304 const ASTRecordLayout &astRecordLayout;
305 // Helpful intermediate data-structures
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;
313
314 LLVM_PREFERRED_TYPE(bool)
315 unsigned zeroInitializable : 1;
316 LLVM_PREFERRED_TYPE(bool)
317 unsigned zeroInitializableAsBase : 1;
318 LLVM_PREFERRED_TYPE(bool)
319 unsigned packed : 1;
320
321private:
322 // Output fields, consumed by CIRGenTypes::computeRecordLayout. Private so
323 // that every append goes through addField and fieldKinds stays parallel to
324 // fieldTypes.
325 llvm::SmallVector<mlir::Type, 16> fieldTypes;
326 llvm::SmallVector<cir::RecordMemberKind> fieldKinds;
327
328 CIRRecordLowering(const CIRRecordLowering &) = delete;
329 void operator=(const CIRRecordLowering &) = delete;
330}; // CIRRecordLowering
331} // namespace
332
333CIRRecordLowering::CIRRecordLowering(CIRGenTypes &cirGenTypes,
334 const RecordDecl *recordDecl, bool packed)
335 : cirGenTypes{cirGenTypes}, builder{cirGenTypes.getBuilder()},
336 astContext{cirGenTypes.getASTContext()}, recordDecl{recordDecl},
337 cxxRecordDecl{llvm::dyn_cast<CXXRecordDecl>(Val: recordDecl)},
338 astRecordLayout{
339 cirGenTypes.getASTContext().getASTRecordLayout(D: recordDecl)},
340 dataLayout{cirGenTypes.getCGModule().getModule()},
341 zeroInitializable{true}, zeroInitializableAsBase{true}, packed{packed} {}
342
343void CIRRecordLowering::setBitFieldInfo(const FieldDecl *fd,
344 CharUnits startOffset,
345 mlir::Type storageType) {
346 CIRGenBitFieldInfo &info = bitFields[fd->getCanonicalDecl()];
347 info.isSigned = fd->getType()->isSignedIntegerOrEnumerationType();
348 info.offset =
349 (unsigned)(getFieldBitOffset(fieldDecl: fd) - astContext.toBits(CharSize: startOffset));
350 info.size = fd->getBitWidthValue();
351 info.storageSize = getSizeInBits(ty: storageType).getQuantity();
352 info.storageOffset = startOffset;
353 info.storageType = storageType;
354 info.name = fd->getName();
355
356 if (info.size > info.storageSize)
357 info.size = info.storageSize;
358 // Reverse the bit offsets for big endian machines. Since bitfields are laid
359 // out as packed bits within an integer-sized unit, we can imagine the bits
360 // counting from the most-significant-bit instead of the
361 // least-significant-bit.
362 if (dataLayout.isBigEndian())
363 info.offset = info.storageSize - (info.offset + info.size);
364
365 info.volatileStorageSize = 0;
366 info.volatileOffset = 0;
367 info.volatileStorageOffset = CharUnits::Zero();
368}
369
370void CIRRecordLowering::lower(bool nonVirtualBaseType) {
371 if (recordDecl->isUnion()) {
372 lowerUnion(nonVirtualBaseType);
373 computeVolatileBitfields();
374 return;
375 }
376
377 CharUnits size = nonVirtualBaseType ? astRecordLayout.getNonVirtualSize()
378 : astRecordLayout.getSize();
379
380 accumulateFields(nonVirtualBaseType);
381
382 if (cxxRecordDecl) {
383 accumulateVPtrs();
384 accumulateBases();
385 if (members.empty()) {
386 appendPaddingBytes(size);
387 computeVolatileBitfields();
388 return;
389 }
390 if (!nonVirtualBaseType)
391 accumulateVBases();
392 }
393
394 llvm::stable_sort(Range&: members);
395 // TODO: Verify bitfield clipping
396 assert(!cir::MissingFeatures::checkBitfieldClipping());
397
398 // The sentinel is popped before fillOutputFields, so its kind never reaches
399 // the type.
400 members.push_back(
401 x: makeStorageInfo(offset: size, data: getUIntNType(numBits: 8), memberKind: cir::RecordMemberKind::Data));
402 determinePacked(nvBaseType: nonVirtualBaseType);
403 insertPadding();
404 members.pop_back();
405
406 calculateZeroInit();
407 fillOutputFields();
408 computeVolatileBitfields();
409}
410
411void CIRRecordLowering::fillOutputFields() {
412 for (const MemberInfo &member : members) {
413 if (member.data)
414 addField(ty: member.data, memberKind: member.memberKind);
415 if (member.kind == MemberInfo::InfoKind::Field) {
416 if (member.fieldDecl)
417 fieldIdxMap[member.fieldDecl->getCanonicalDecl()] =
418 fieldTypes.size() - 1;
419 // A bit-field carries no member of its own. It is numbered as the access
420 // unit ahead of it, whose storage it is read and written through.
421 if (!member.data) {
422 assert(member.fieldDecl &&
423 "member.data is a nullptr so member.fieldDecl should not be");
424 setBitFieldInfo(fd: member.fieldDecl, startOffset: member.offset,
425 storageType: cir::memberStorageType(memberTy: fieldTypes.back()));
426 }
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;
431 }
432 }
433}
434
435RecordDecl::field_iterator
436CIRRecordLowering::accumulateBitFields(RecordDecl::field_iterator field,
437 RecordDecl::field_iterator fieldEnd) {
438 if (CodeGenUtils::isDiscreteBitFieldABI(Ctx: astContext, RD: recordDecl)) {
439 // run stores the first element of the current run of bitfields. fieldEnd is
440 // used as a special value to note that we don't have a current run. A
441 // bitfield run is a contiguous collection of bitfields that can be stored
442 // in the same storage block. Zero-sized bitfields and bitfields that would
443 // cross an alignment boundary break a run and start a new one.
444 RecordDecl::field_iterator run = fieldEnd;
445 // tail is the offset of the first bit off the end of the current run. It's
446 // used to determine if the ASTRecordLayout is treating these two bitfields
447 // as contiguous. StartBitOffset is offset of the beginning of the Run.
448 uint64_t startBitOffset, tail = 0;
449 // Where the current run's access unit sits in members, and the bit-fields
450 // it holds so far. The unit grows as new bit-fields are added.
451 size_t unitIdx = 0;
452 mlir::Type unitStorage;
453 llvm::SmallVector<cir::BitFieldDeclAttr> unitFields;
454 for (; field != fieldEnd && field->isBitField(); ++field) {
455 // Zero-width bitfields end runs.
456 if (field->isZeroLengthBitField()) {
457 members.push_back(x: makeZeroWidthBitFieldInfo(field: *field));
458 run = fieldEnd;
459 continue;
460 }
461 uint64_t bitOffset = getFieldBitOffset(fieldDecl: *field);
462 mlir::Type type = cirGenTypes.convertTypeForMem(field->getType());
463 cir::BitFieldDeclAttr decl = getBitFieldDecl(field: *field);
464 // If we don't have a run yet, or don't live within the previous run's
465 // allocated storage then we allocate some storage and start a new run.
466 if (run == fieldEnd || bitOffset >= tail) {
467 run = field;
468 startBitOffset = bitOffset;
469 tail = startBitOffset + dataLayout.getTypeAllocSizeInBits(ty: type);
470 // The access unit is added to the record before the bit-fields it
471 // holds so that it gets laid out ahead of them.
472 unitIdx = members.size();
473 unitStorage = type;
474 unitFields.assign(NumElts: 1, Elt: decl);
475 members.push_back(x: makeAccessUnitInfo(offset: bitsToCharUnits(bitOffset: startBitOffset),
476 storage: unitStorage, fields: unitFields));
477 } else {
478 unitFields.push_back(Elt: decl);
479 members[unitIdx] = makeAccessUnitInfo(offset: bitsToCharUnits(bitOffset: startBitOffset),
480 storage: unitStorage, fields: unitFields);
481 }
482 assert(members[unitIdx].offset == bitsToCharUnits(startBitOffset) &&
483 "unitIdx must name the current run's access unit");
484 // Bitfields get the offset of their access unit but come afterward and
485 // remain there after a stable sort.
486 members.push_back(x: MemberInfo(bitsToCharUnits(bitOffset: startBitOffset),
487 MemberInfo::InfoKind::Field, nullptr,
488 cir::RecordMemberKind::Data, *field));
489 }
490 return field;
491 }
492
493 CharUnits regSize =
494 bitsToCharUnits(bitOffset: astContext.getTargetInfo().getRegisterWidth());
495 unsigned charBits = astContext.getCharWidth();
496
497 // Data about the start of the span we're accumulating to create an access
498 // unit from. 'Begin' is the first bitfield of the span. If 'begin' is
499 // 'fieldEnd', we've not got a current span. The span starts at the
500 // 'beginOffset' character boundary. 'bitSizeSinceBegin' is the size (in bits)
501 // of the span -- this might include padding when we've advanced to a
502 // subsequent bitfield run.
503 RecordDecl::field_iterator begin = fieldEnd;
504 CharUnits beginOffset;
505 uint64_t bitSizeSinceBegin;
506
507 // The (non-inclusive) end of the largest acceptable access unit we've found
508 // since 'begin'. If this is 'begin', we're gathering the initial set of
509 // bitfields of a new span. 'bestEndOffset' is the end of that acceptable
510 // access unit -- it might extend beyond the last character of the bitfield
511 // run, using available padding characters.
512 RecordDecl::field_iterator bestEnd = begin;
513 CharUnits bestEndOffset;
514 bool bestClipped; // Whether the representation must be in a byte array.
515
516 for (;;) {
517 // atAlignedBoundary is true if 'field' is the (potential) start of a new
518 // span (or the end of the bitfields). When true, limitOffset is the
519 // character offset of that span and barrier indicates whether the new
520 // span cannot be merged into the current one.
521 bool atAlignedBoundary = false;
522 bool barrier = false; // a barrier can be a zero Bit Width or non bit member
523 if (field != fieldEnd && field->isBitField()) {
524 uint64_t bitOffset = getFieldBitOffset(fieldDecl: *field);
525 if (begin == fieldEnd) {
526 // Beginning a new span.
527 begin = field;
528 bestEnd = begin;
529
530 assert((bitOffset % charBits) == 0 && "Not at start of char");
531 beginOffset = bitsToCharUnits(bitOffset);
532 bitSizeSinceBegin = 0;
533 } else if ((bitOffset % charBits) != 0) {
534 // Bitfield occupies the same character as previous bitfield, it must be
535 // part of the same span. This can include zero-length bitfields, should
536 // the target not align them to character boundaries. Such non-alignment
537 // is at variance with the standards, which require zero-length
538 // bitfields be a barrier between access units. But of course we can't
539 // achieve that in the middle of a character.
540 assert(bitOffset ==
541 astContext.toBits(beginOffset) + bitSizeSinceBegin &&
542 "Concatenating non-contiguous bitfields");
543 } else {
544 // Bitfield potentially begins a new span. This includes zero-length
545 // bitfields on non-aligning targets that lie at character boundaries
546 // (those are barriers to merging).
547 if (field->isZeroLengthBitField())
548 barrier = true;
549 atAlignedBoundary = true;
550 }
551 } else {
552 // We've reached the end of the bitfield run. Either we're done, or this
553 // is a barrier for the current span.
554 if (begin == fieldEnd)
555 break;
556
557 barrier = true;
558 atAlignedBoundary = true;
559 }
560
561 // 'installBest' indicates whether we should create an access unit for the
562 // current best span: fields ['begin', 'bestEnd') occupying characters
563 // ['beginOffset', 'bestEndOffset').
564 bool installBest = false;
565 if (atAlignedBoundary) {
566 // 'field' is the start of a new span or the end of the bitfields. The
567 // just-seen span now extends to 'bitSizeSinceBegin'.
568
569 // Determine if we can accumulate that just-seen span into the current
570 // accumulation.
571 CharUnits accessSize = bitsToCharUnits(bitOffset: bitSizeSinceBegin + charBits - 1);
572 if (bestEnd == begin) {
573 // This is the initial run at the start of a new span. By definition,
574 // this is the best seen so far.
575 bestEnd = field;
576 bestEndOffset = beginOffset + accessSize;
577 // Assume clipped until proven not below.
578 bestClipped = true;
579 if (!bitSizeSinceBegin)
580 // A zero-sized initial span -- this will install nothing and reset
581 // for another.
582 installBest = true;
583 } else if (accessSize > regSize) {
584 // Accumulating the just-seen span would create a multi-register access
585 // unit, which would increase register pressure.
586 installBest = true;
587 }
588
589 if (!installBest) {
590 // Determine if accumulating the just-seen span will create an expensive
591 // access unit or not.
592 mlir::Type type = getUIntNType(numBits: astContext.toBits(CharSize: accessSize));
593 if (!astContext.getTargetInfo().hasCheapUnalignedBitFieldAccess()) {
594 // Unaligned accesses are expensive. Only accumulate if the new unit
595 // is naturally aligned. Otherwise install the best we have, which is
596 // either the initial access unit (can't do better), or a naturally
597 // aligned accumulation (since we would have already installed it if
598 // it wasn't naturally aligned).
599 CharUnits align = getMemberAlignment(ty: type);
600 if (align > astRecordLayout.getAlignment()) {
601 // The alignment required is greater than the containing structure
602 // itself.
603 installBest = true;
604 } else if (!beginOffset.isMultipleOf(N: align)) {
605 // The access unit is not at a naturally aligned offset within the
606 // structure.
607 installBest = true;
608 }
609
610 if (installBest && bestEnd == field) {
611 // We're installing the first span, whose clipping was presumed
612 // above. Compute it correctly.
613 if (getSize(Ty: type) == accessSize)
614 bestClipped = false;
615 }
616 }
617
618 if (!installBest) {
619 // Find the next used storage offset to determine what the limit of
620 // the current span is. That's either the offset of the next field
621 // with storage (which might be field itself) or the end of the
622 // non-reusable tail padding.
623 CharUnits limitOffset;
624 for (auto probe = field; probe != fieldEnd; ++probe)
625 if (!CodeGenUtils::isEmptyFieldForLayout(Ctx: astContext, FD: *probe)) {
626 // A member with storage sets the limit.
627 assert((getFieldBitOffset(*probe) % charBits) == 0 &&
628 "Next storage is not byte-aligned");
629 limitOffset = bitsToCharUnits(bitOffset: getFieldBitOffset(fieldDecl: *probe));
630 goto FoundLimit;
631 }
632 limitOffset = cxxRecordDecl ? astRecordLayout.getNonVirtualSize()
633 : astRecordLayout.getDataSize();
634
635 FoundLimit:
636 CharUnits typeSize = getSize(Ty: type);
637 if (beginOffset + typeSize <= limitOffset) {
638 // There is space before limitOffset to create a naturally-sized
639 // access unit.
640 bestEndOffset = beginOffset + typeSize;
641 bestEnd = field;
642 bestClipped = false;
643 }
644 if (barrier) {
645 // The next field is a barrier that we cannot merge across.
646 installBest = true;
647 } else if (cirGenTypes.getCGModule()
648 .getCodeGenOpts()
649 .FineGrainedBitfieldAccesses) {
650 installBest = true;
651 } else {
652 // Otherwise, we're not installing. Update the bit size
653 // of the current span to go all the way to limitOffset, which is
654 // the (aligned) offset of next bitfield to consider.
655 bitSizeSinceBegin = astContext.toBits(CharSize: limitOffset - beginOffset);
656 }
657 }
658 }
659 }
660
661 if (installBest) {
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()) {
667 // Add the storage member for the access unit to the record. The
668 // bitfields get the offset of their storage but come afterward and
669 // remain there after a stable sort.
670 mlir::Type type;
671 if (bestClipped) {
672 assert(getSize(getUIntNType(astContext.toBits(accessSize))) >
673 accessSize &&
674 "Clipped access need not be clipped");
675 type = getByteArrayType(numberOfChars: accessSize);
676 } else {
677 type = getUIntNType(numBits: astContext.toBits(CharSize: accessSize));
678 assert(getSize(type) == accessSize &&
679 "Unclipped access must be clipped");
680 }
681 // A zero-length bit-field in the span was given a member of its own
682 // when it was accumulated, and belongs to no access unit, so it is not
683 // one of the fields this unit holds.
684 llvm::SmallVector<cir::BitFieldDeclAttr> unitFields;
685 for (auto occupant = begin; occupant != bestEnd; ++occupant)
686 if (!occupant->isZeroLengthBitField())
687 unitFields.push_back(Elt: getBitFieldDecl(field: *occupant));
688 assert(!unitFields.empty() && "an access unit holds a bit-field");
689 members.push_back(x: makeAccessUnitInfo(offset: beginOffset, storage: type, fields: unitFields));
690
691 for (; begin != bestEnd; ++begin)
692 if (!begin->isZeroLengthBitField())
693 members.push_back(x: MemberInfo(beginOffset,
694 MemberInfo::InfoKind::Field, nullptr,
695 cir::RecordMemberKind::Data, *begin));
696 }
697 // Reset to start a new span.
698 field = bestEnd;
699 begin = fieldEnd;
700 } else {
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(x: makeZeroWidthBitFieldInfo(field: *field));
706 // Accumulate this bitfield into the current (potential) span.
707 bitSizeSinceBegin += field->getBitWidthValue();
708 ++field;
709 }
710 }
711
712 return field;
713}
714
715void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) {
716 for (RecordDecl::field_iterator field = recordDecl->field_begin(),
717 fieldEnd = recordDecl->field_end();
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");
723 } else if (CodeGenUtils::isEmptyFieldForLayout(Ctx: astContext, FD: *field) &&
724 field->isPotentiallyOverlapping()) {
725 // We lay out normal empty fields, as they are required for GEPs/getting
726 // function pointers. However 'no-unique-address' lends some additional
727 // complexity. These fields take up no real space, but would also have to
728 // be the correct 'GEP' offset to work here, but then mess with the
729 // layout. We likely need to come up with a new 'type' to support these,
730 // then figure out some way to lower these to the correct offset, likely
731 // by calculating the correct offset into the struct, and forming a
732 // replacement by-byte GEP in LowerToLLVM. However, this is only a
733 // problem with taking the address of one of these, so it is in practice
734 // not a horrifyingly problematic issue.
735 assert(!cir::MissingFeatures::noUniqueAddressLayout());
736 // Dropping the field leaves no member to mark, so its bytes read as
737 // padding. That is only sound when the field carries no ABI data, so
738 // make sure we add a member for it. if it does.
739 if (!isEmptyFieldForABI(context: astContext, fd: *field))
740 members.push_back(
741 x: MemberInfo(bitsToCharUnits(bitOffset: getFieldBitOffset(fieldDecl: *field)),
742 MemberInfo::InfoKind::Field,
743 getStorageType(RD: field->getType()->getAsCXXRecordDecl()),
744 getFieldMemberKind(fd: *field), *field));
745 ++field;
746 } else {
747 // Use base subobject layout for potentially-overlapping fields,
748 // as it is done in RecordLayoutBuilder.
749 //
750 // The mark comes from isEmptyFieldForABI, not the isEmptyFieldForLayout
751 // above. Neither predicate subsumes the other.
752 members.push_back(x: MemberInfo(
753 bitsToCharUnits(bitOffset: getFieldBitOffset(fieldDecl: *field)),
754 MemberInfo::InfoKind::Field,
755 field->isPotentiallyOverlapping()
756 ? getStorageType(RD: field->getType()->getAsCXXRecordDecl())
757 : getStorageType(fieldDecl: *field),
758 getFieldMemberKind(fd: *field), *field));
759 ++field;
760 }
761 }
762}
763
764void CIRRecordLowering::calculateZeroInit() {
765 for (const MemberInfo &member : members) {
766 if (member.kind == MemberInfo::InfoKind::Field) {
767 if (!member.fieldDecl || isZeroInitializable(fd: member.fieldDecl))
768 continue;
769 zeroInitializable = zeroInitializableAsBase = false;
770 return;
771 } else if (member.kind == MemberInfo::InfoKind::Base ||
772 member.kind == MemberInfo::InfoKind::VBase) {
773 if (isZeroInitializable(rd: member.cxxRecordDecl))
774 continue;
775 zeroInitializable = false;
776 if (member.kind == MemberInfo::InfoKind::Base)
777 zeroInitializableAsBase = false;
778 }
779 }
780}
781
782void CIRRecordLowering::determinePacked(bool nvBaseType) {
783 if (packed)
784 return;
785 CharUnits alignment = CharUnits::One();
786 CharUnits nvAlignment = CharUnits::One();
787 CharUnits nvSize = !nvBaseType && cxxRecordDecl
788 ? astRecordLayout.getNonVirtualSize()
789 : CharUnits::Zero();
790
791 for (const MemberInfo &member : members) {
792 // A member that owns no bytes sits inside another member's storage, whose
793 // own offset and alignment are what decide packing.
794 if (!ownsBytes(member))
795 continue;
796 // If any member falls at an offset that it not a multiple of its alignment,
797 // then the entire record must be packed.
798 if (!member.offset.isMultipleOf(N: getMemberAlignment(ty: member.data)))
799 packed = true;
800 if (member.offset < nvSize)
801 nvAlignment = std::max(a: nvAlignment, b: getMemberAlignment(ty: member.data));
802 alignment = std::max(a: alignment, b: getMemberAlignment(ty: member.data));
803 }
804 // If the size of the record (the capstone's offset) is not a multiple of the
805 // record's alignment, it must be packed.
806 if (!members.back().offset.isMultipleOf(N: alignment))
807 packed = true;
808 // If the non-virtual sub-object is not a multiple of the non-virtual
809 // sub-object's alignment, it must be packed. We cannot have a packed
810 // non-virtual sub-object and an unpacked complete object or vise versa.
811 if (!nvSize.isMultipleOf(N: nvAlignment))
812 packed = true;
813 // Update the alignment of the sentinel.
814 if (!packed)
815 members.back().data = getUIntNType(numBits: astContext.toBits(CharSize: alignment));
816}
817
818void CIRRecordLowering::insertPadding() {
819 std::vector<std::pair<CharUnits, CharUnits>> padding;
820 CharUnits size = CharUnits::Zero();
821 for (const MemberInfo &member : members) {
822 if (!member.data)
823 continue;
824 // A consumer recovers a member's offset by accumulating the sizes of the
825 // members before it, so the padding a zero-width bit-field sits inside has
826 // to be split at its offset for that sum to come out right. The offset
827 // can sit behind the running size, since the access unit covering the bits
828 // around it may be wider than the offset it was declared at.
829 if (!ownsBytes(member)) {
830 if (member.offset > size) {
831 padding.push_back(x: std::make_pair(x&: size, y: member.offset - size));
832 size = member.offset;
833 }
834 continue;
835 }
836 CharUnits offset = member.offset;
837 assert(offset >= size);
838 // Insert padding if we need to.
839 if (offset != size.alignTo(Align: packed ? CharUnits::One()
840 : getMemberAlignment(ty: member.data)))
841 padding.push_back(x: std::make_pair(x&: size, y: offset - size));
842 size = offset + getSize(Ty: member.data);
843 }
844 if (padding.empty())
845 return;
846 // Add the padding to the Members list and sort it.
847 for (const std::pair<CharUnits, CharUnits> &paddingPair : padding)
848 members.push_back(x: makeStorageInfo(offset: paddingPair.first,
849 data: getByteArrayType(numberOfChars: paddingPair.second),
850 memberKind: cir::RecordMemberKind::Pad));
851 llvm::stable_sort(Range&: members);
852}
853
854static cir::ArgPassingKind
855convertRecordArgPassingKind(RecordArgPassingKind kind) {
856 switch (kind) {
857 case RecordArgPassingKind::CanPassInRegs:
858 return cir::ArgPassingKind::CanPassInRegs;
859 case RecordArgPassingKind::CannotPassInRegs:
860 return cir::ArgPassingKind::CannotPassInRegs;
861 case RecordArgPassingKind::CanNeverPassInRegs:
862 return cir::ArgPassingKind::CanNeverPassInRegs;
863 }
864 llvm_unreachable("unknown RecordArgPassingKind");
865}
866
867/// Whether the member kinds on \p recordTy answer the record's ABI emptiness
868/// the same way the AST predicate does.
869[[maybe_unused]] static bool
870marksMatchABIEmptiness(const ASTContext &astContext, const RecordDecl *rd,
871 cir::RecordType recordTy) {
872 return recordTy.isEmptyForABI() ==
873 isEmptyRecordForABI(context: astContext, t: astContext.getCanonicalTagType(TD: rd));
874}
875
876std::unique_ptr<CIRGenRecordLayout>
877CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) {
878 CIRRecordLowering lowering(*this, rd, /*packed=*/false);
879 assert(ty->isIncomplete() && "recomputing record layout?");
880 lowering.lower(/*nonVirtualBaseType=*/false);
881
882 // If we're in C++, compute the base subobject type. For C++ records baseTy
883 // defaults to the complete object type and is replaced by a distinct,
884 // smaller record only when the record has tail padding an enclosing
885 // [[no_unique_address]] field can reuse. We must populate baseTy even when
886 // it equals ty because callers such as getStorageType(const CXXRecordDecl *)
887 // read it unconditionally when laying out potentially-overlapping
888 // ([[no_unique_address]]) fields; a null baseTy would otherwise propagate as
889 // a null mlir::Type into the members vector and trip the !empty() assertion
890 // in fillOutputFields.
891 cir::RecordType baseTy;
892 if (llvm::isa<CXXRecordDecl>(Val: rd)) {
893 baseTy = *ty;
894 // A record needs a distinct base-subobject type when its tail padding can
895 // be reused by an enclosing [[no_unique_address]] field, i.e. when the
896 // non-virtual size differs from the complete size. This matches classic
897 // CodeGen and covers unions too: a union's non-virtual size already tracks
898 // its reusable tail padding (and stays at the minimum union size when the
899 // union is empty, so a zero-data union does not spuriously qualify).
900 if (lowering.astRecordLayout.getNonVirtualSize() !=
901 lowering.astRecordLayout.getSize()) {
902 CIRRecordLowering baseLowering(*this, rd, /*Packed=*/lowering.packed);
903 baseLowering.lower(/*nonVirtualBaseType=*/true);
904 std::string baseIdentifier = getRecordTypeName(rd, suffix: ".base");
905 baseTy = builder.getCompleteNamedRecordType(
906 members: baseLowering.getFieldTypes(), packed: baseLowering.packed, name: baseIdentifier,
907 memberKinds: baseLowering.getFieldKinds());
908 // TODO(cir): add something like addRecordTypeName
909
910 // BaseTy and Ty must agree on their packedness for getCIRFieldNo to work
911 // on both of them with the same index. Unions are exempt: CIR derives a
912 // union's packedness from its layout size, which is the data size for the
913 // base subobject but the full size for the complete object, so the two
914 // can legitimately disagree. (Classic CodeGen derives both from the data
915 // size and so needs no such exemption.)
916 assert((rd->isUnion() || lowering.packed == baseLowering.packed) &&
917 "Non-virtual and complete types must agree on packedness");
918 // Emptiness is a property of the decl, so the base subobject must answer
919 // the same way the complete object does. The two are not comparable
920 // mark by mark: they see different sizes and so different tail padding.
921 assert((marksMatchABIEmptiness(astContext, rd, baseTy) ||
922 cgm.getDiags().hasErrorOccurred()) &&
923 "base subobject member kinds must reproduce its ABI emptiness");
924 }
925 }
926
927 // Fill in the record *after* computing the base type. Filling in the body
928 // signifies that the type is no longer opaque and record layout is complete,
929 // but we may need to recursively layout rd while laying D out as a base type.
930 assert(!cir::MissingFeatures::astRecordDeclAttr());
931 ty->complete(members: lowering.getFieldTypes(), packed: lowering.packed, padding: lowering.unionPadding,
932 memberKinds: lowering.getFieldKinds());
933
934 // The marks exist so that emptiness can be read off the type, so check that
935 // answer against the AST predicate on every record CIRGen lays out. This
936 // does not check the individual marks, only what they add up to. The marks
937 // themselves are pinned by clang/test/CIR/CodeGen/record-member-kinds.*.
938 assert((marksMatchABIEmptiness(astContext, rd, *ty) ||
939 cgm.getDiags().hasErrorOccurred()) &&
940 "member kinds must reproduce the ABI emptiness of the record");
941
942 // Queue ABI metadata for the module-level cir.record_layouts attribute.
943 if (ty->getName()) {
944 mlir::MLIRContext *mlirCtx = ty->getContext();
945 cir::ArgPassingKind apk =
946 convertRecordArgPassingKind(kind: rd->getArgPassingRestrictions());
947
948 bool hasTrivialDestructor = true;
949 if (auto *cxxRD = dyn_cast<CXXRecordDecl>(Val: rd))
950 hasTrivialDestructor = cxxRD->hasTrivialDestructor();
951 const auto &astLayout = astContext.getASTRecordLayout(D: rd);
952 uint64_t recordAlignInBytes = astLayout.getAlignment().getQuantity();
953 uint64_t unadjustedAlignInBytes =
954 astLayout.getUnadjustedAlignment().getQuantity();
955 bool isTransparentUnion = rd->hasAttr<TransparentUnionAttr>();
956 bool hasFlexibleArrayMember = rd->hasFlexibleArrayMember();
957
958 auto addLayout = [&](mlir::StringAttr name, uint64_t align,
959 uint64_t unadjusted) {
960 cgm.addRecordLayout(name, attr: cir::RecordLayoutAttr::get(
961 context: mlirCtx, arg_passing_kind: apk, has_trivial_dtor: hasTrivialDestructor, record_align: align,
962 unadjusted_align: unadjusted, transparent_union: isTransparentUnion,
963 flexible_array_member: hasFlexibleArrayMember));
964 };
965 addLayout(ty->getName(), recordAlignInBytes, unadjustedAlignInBytes);
966
967 // The base subobject is a separate named type. Record the non-virtual
968 // alignment for it, and keep the unadjusted alignment within that
969 // alignment. The AST has no non-virtual unadjusted alignment, so use the
970 // record's unadjusted alignment, capped at the non-virtual alignment.
971 if (baseTy && baseTy != *ty) {
972 uint64_t baseAlign = astLayout.getNonVirtualAlignment().getQuantity();
973 uint64_t baseUnadjusted = std::min(a: unadjustedAlignInBytes, b: baseAlign);
974 addLayout(baseTy.getName(), baseAlign, baseUnadjusted);
975 }
976 }
977
978 auto rl = std::make_unique<CIRGenRecordLayout>(
979 args: ty ? *ty : cir::RecordType{}, args: baseTy ? baseTy : cir::RecordType{},
980 args: (bool)lowering.zeroInitializable, args: (bool)lowering.zeroInitializableAsBase);
981
982 rl->nonVirtualBases.swap(RHS&: lowering.nonVirtualBases);
983 rl->completeObjectVirtualBases.swap(RHS&: lowering.virtualBases);
984
985 // Add all the field numbers.
986 rl->fieldIdxMap.swap(RHS&: lowering.fieldIdxMap);
987
988 rl->bitFields.swap(RHS&: lowering.bitFields);
989
990 // Dump the layout, if requested.
991 if (getASTContext().getLangOpts().DumpRecordLayouts) {
992 llvm::outs() << "\n*** Dumping CIRgen Record Layout\n";
993 llvm::outs() << "Record: ";
994 rd->dump(Out&: llvm::outs());
995 llvm::outs() << "\nLayout: ";
996 rl->print(os&: llvm::outs());
997 }
998
999 // TODO: implement verification
1000 return rl;
1001}
1002
1003void CIRGenRecordLayout::print(raw_ostream &os) const {
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) {
1012 const RecordDecl *rd = decl->getParent();
1013 unsigned index = 0;
1014 for (RecordDecl::field_iterator it = rd->field_begin(); *it != decl; ++it)
1015 ++index;
1016 bitInfo.push_back(x: std::make_pair(x&: index, y: &info));
1017 }
1018 llvm::array_pod_sort(Start: bitInfo.begin(), End: bitInfo.end());
1019 for (std::pair<unsigned, const CIRGenBitFieldInfo *> &info : bitInfo) {
1020 os.indent(NumSpaces: 4);
1021 info.second->print(os);
1022 os << "\n";
1023 }
1024 os << " ]>\n";
1025}
1026
1027void CIRGenBitFieldInfo::print(raw_ostream &os) const {
1028 os << "<CIRBitFieldInfo" << " name:" << name << " offset:" << offset
1029 << " size:" << size << " isSigned:" << isSigned
1030 << " storageSize:" << storageSize
1031 << " storageOffset:" << storageOffset.getQuantity()
1032 << " volatileOffset:" << volatileOffset
1033 << " volatileStorageSize:" << volatileStorageSize
1034 << " volatileStorageOffset:" << volatileStorageOffset.getQuantity() << ">";
1035}
1036
1037void CIRGenRecordLayout::dump() const { print(os&: llvm::errs()); }
1038
1039void CIRGenBitFieldInfo::dump() const { print(os&: llvm::errs()); }
1040
1041void CIRRecordLowering::lowerUnion(bool nonVirtualBaseType) {
1042 // The base-subobject layout of a union is sized to its data size rather than
1043 // its full size. A union can have reusable tail padding when one of its
1044 // members is a [[no_unique_address]] field that itself has tail padding, so
1045 // an enclosing [[no_unique_address]] union field must use this smaller type.
1046 CharUnits layoutSize = nonVirtualBaseType ? astRecordLayout.getDataSize()
1047 : astRecordLayout.getSize();
1048 // Accumulate bitfields and fields, and figure out what our difference between
1049 // storage type and padding is.
1050
1051 // First, accumulate all the types.
1052 for (const FieldDecl *field : recordDecl->fields()) {
1053 mlir::Type fieldType;
1054 cir::RecordMemberKind fieldKind;
1055 if (field->isBitField()) {
1056 if (field->isZeroLengthBitField())
1057 continue;
1058 // Every variant starts at offset zero, so a bit-field variant is an
1059 // access unit of its own, holding that one field.
1060 mlir::Type unitStorage =
1061 getBitfieldStorageType(numBits: field->getBitWidthValue());
1062 setBitFieldInfo(fd: field, startOffset: CharUnits::Zero(), storageType: unitStorage);
1063 MemberInfo unit = makeAccessUnitInfo(offset: CharUnits::Zero(), storage: unitStorage,
1064 fields: getBitFieldDecl(field));
1065 fieldType = unit.data;
1066 fieldKind = unit.memberKind;
1067 } else {
1068 fieldType = getStorageType(fieldDecl: field);
1069 fieldKind = getFieldMemberKind(fd: field);
1070 }
1071
1072 fieldIdxMap[field->getCanonicalDecl()] = 0;
1073 addField(ty: fieldType, memberKind: fieldKind);
1074 }
1075
1076 // Compute zero-initializable status.
1077 // This union might not be zero initialized: it may contain a pointer to
1078 // data member which might have some exotic initialization sequence.
1079 // This chooses the first 'named' member and is zero-initializable based on
1080 // that.
1081 auto hasNamedMember = [](const FieldDecl *curField) -> bool {
1082 const auto *rd = curField->getType()->getAsRecordDecl();
1083 return rd && rd->findFirstNamedDataMember();
1084 };
1085 // Whether this is usable for zero-init as a 'named member'. It is a field
1086 // with a name (or a record type with a named member itself), that isn't a
1087 // zero-width bitfield.
1088 auto isNamedMember = [hasNamedMember](const FieldDecl *curField) -> bool {
1089 if (curField->isZeroLengthBitField())
1090 return false;
1091 return curField->getIdentifier() || hasNamedMember(curField);
1092 };
1093 auto firstNamedMemberItr = llvm::find_if(Range: recordDecl->fields(), P: isNamedMember);
1094
1095 if (firstNamedMemberItr != recordDecl->fields().end() &&
1096 !isZeroInitializable(fd: *firstNamedMemberItr))
1097 zeroInitializable = zeroInitializableAsBase = false;
1098
1099 // If we have no candidates for storage, we are JUST padding.
1100 if (getFieldTypes().empty()) {
1101 appendPaddingBytes(size: layoutSize);
1102 return;
1103 }
1104
1105 mlir::Type storageType =
1106 cir::UnionType::getUnionStorageType(dataLayout: dataLayout.layout, members: getFieldTypes());
1107
1108 // If our storage size was bigger than our required size (can happen in the
1109 // case of packed bitfields on Itanium) then just use an I8 array.
1110 if (layoutSize < getSize(Ty: storageType))
1111 storageType = getByteArrayType(numberOfChars: layoutSize);
1112
1113 // The base-subobject record is built as a struct from fieldTypes, so add
1114 // the storage type and any trailing padding as ordinary fields rather than
1115 // routing padding through the union's single tail-padding slot.
1116 if (nonVirtualBaseType) {
1117 // A bit-field mark here says the stand-in's extent is not a declared
1118 // extent, not that its storage came from a bit-field: UnionBitAndWide in
1119 // clang/test/CIR/CodeGen/no-unique-address.cpp takes its double as storage
1120 // and still marks bitfield. The stand-in describes every variant, so it
1121 // takes the storage type rather than any one variant's `!cir.bitfield`,
1122 // and is the one bit-field-marked member that is not one. Computed before
1123 // clearFields() drops the variant marks.
1124 const cir::RecordMemberKind storageKind = makeMemberKind(
1125 /*holdsData=*/cir::anyMemberHoldsDataForABI(kinds: getFieldKinds()),
1126 /*isNamedBitField=*/llvm::any_of(Range: getFieldKinds(),
1127 P: cir::isNamedBitField));
1128 clearFields();
1129 addField(ty: storageType, memberKind: storageKind);
1130 CharUnits padding = layoutSize - getSize(Ty: storageType);
1131 if (!padding.isZero())
1132 addField(ty: getByteArrayType(numberOfChars: padding), memberKind: cir::RecordMemberKind::Pad);
1133 } else {
1134 // Else we just add padding normally.
1135 appendPaddingBytes(size: layoutSize - getSize(Ty: storageType));
1136 }
1137 packed = !layoutSize.isMultipleOf(N: getMemberAlignment(ty: storageType));
1138}
1139
1140/// The AAPCS that defines that, when possible, bit-fields should
1141/// be accessed using containers of the declared type width:
1142/// When a volatile bit-field is read, and its container does not overlap with
1143/// any non-bit-field member or any zero length bit-field member, its container
1144/// must be read exactly once using the access width appropriate to the type of
1145/// the container. When a volatile bit-field is written, and its container does
1146/// not overlap with any non-bit-field member or any zero-length bit-field
1147/// member, its container must be read exactly once and written exactly once
1148/// using the access width appropriate to the type of the container. The two
1149/// accesses are not atomic.
1150///
1151/// Enforcing the width restriction can be disabled using
1152/// -fno-aapcs-bitfield-width.
1153void CIRRecordLowering::computeVolatileBitfields() {
1154 if (!CodeGenUtils::isAAPCS(TargetInfo: astContext.getTargetInfo()) ||
1155 !cirGenTypes.getCGModule().getCodeGenOpts().AAPCSBitfieldWidth)
1156 return;
1157
1158 for (auto &[field, info] : bitFields) {
1159 mlir::Type resLTy = cirGenTypes.convertTypeForMem(field->getType());
1160
1161 if (astContext.toBits(CharSize: astRecordLayout.getAlignment()) <
1162 getSizeInBits(ty: resLTy).getQuantity())
1163 continue;
1164
1165 // CIRRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets
1166 // for big-endian targets, but it assumes a container of width
1167 // info.storageSize. Since AAPCS uses a different container size (width
1168 // of the type), we first undo that calculation here and redo it once
1169 // the bit-field offset within the new container is calculated.
1170 const unsigned oldOffset =
1171 isBigEndian() ? info.storageSize - (info.offset + info.size)
1172 : info.offset;
1173 // Offset to the bit-field from the beginning of the struct.
1174 const unsigned absoluteOffset =
1175 astContext.toBits(CharSize: info.storageOffset) + oldOffset;
1176
1177 // Container size is the width of the bit-field type.
1178 const unsigned storageSize = getSizeInBits(ty: resLTy).getQuantity();
1179 // Nothing to do if the access uses the desired
1180 // container width and is naturally aligned.
1181 if (info.storageSize == storageSize && (oldOffset % storageSize == 0))
1182 continue;
1183
1184 // Offset within the container.
1185 unsigned offset = absoluteOffset & (storageSize - 1);
1186 // Bail out if an aligned load of the container cannot cover the entire
1187 // bit-field. This can happen for example, if the bit-field is part of a
1188 // packed struct. AAPCS does not define access rules for such cases, we let
1189 // clang to follow its own rules.
1190 if (offset + info.size > storageSize)
1191 continue;
1192
1193 // Re-adjust offsets for big-endian targets.
1194 if (isBigEndian())
1195 offset = storageSize - (offset + info.size);
1196
1197 const CharUnits storageOffset =
1198 astContext.toCharUnitsFromBits(BitSize: absoluteOffset & ~(storageSize - 1));
1199 const CharUnits end = storageOffset +
1200 astContext.toCharUnitsFromBits(BitSize: storageSize) -
1201 CharUnits::One();
1202
1203 const ASTRecordLayout &layout =
1204 astContext.getASTRecordLayout(D: field->getParent());
1205 // If we access outside memory outside the record, than bail out.
1206 const CharUnits recordSize = layout.getSize();
1207 if (end >= recordSize)
1208 continue;
1209
1210 // Bail out if performing this load would access non-bit-fields members.
1211 bool conflict = false;
1212 for (const auto *f : recordDecl->fields()) {
1213 // Allow sized bit-fields overlaps.
1214 if (f->isBitField() && !f->isZeroLengthBitField())
1215 continue;
1216
1217 const CharUnits fOffset = astContext.toCharUnitsFromBits(
1218 BitSize: layout.getFieldOffset(FieldNo: f->getFieldIndex()));
1219
1220 // As C11 defines, a zero sized bit-field defines a barrier, so
1221 // fields after and before it should be race condition free.
1222 // The AAPCS acknowledges it and imposes no restritions when the
1223 // natural container overlaps a zero-length bit-field.
1224 if (f->isZeroLengthBitField()) {
1225 if (end > fOffset && storageOffset < fOffset) {
1226 conflict = true;
1227 break;
1228 }
1229 }
1230
1231 const CharUnits fEnd =
1232 fOffset +
1233 astContext.toCharUnitsFromBits(
1234 BitSize: getSizeInBits(ty: cirGenTypes.convertTypeForMem(f->getType()))
1235 .getQuantity()) -
1236 CharUnits::One();
1237 // If no overlap, continue.
1238 if (end < fOffset || fEnd < storageOffset)
1239 continue;
1240
1241 // The desired load overlaps a non-bit-field member, bail out.
1242 conflict = true;
1243 break;
1244 }
1245
1246 if (conflict)
1247 continue;
1248 // Write the new bit-field access parameters.
1249 // As the storage offset now is defined as the number of elements from the
1250 // start of the structure, we should divide the Offset by the element size.
1251 info.volatileStorageOffset =
1252 storageOffset /
1253 astContext.toCharUnitsFromBits(BitSize: storageSize).getQuantity();
1254 info.volatileStorageSize = storageSize;
1255 info.volatileOffset = offset;
1256 }
1257}
1258
1259void CIRRecordLowering::accumulateBases() {
1260 // If we've got a primary virtual base, we need to add it with the bases.
1261 if (astRecordLayout.isPrimaryBaseVirtual()) {
1262 const CXXRecordDecl *baseDecl = astRecordLayout.getPrimaryBase();
1263 members.push_back(x: MemberInfo(CharUnits::Zero(), MemberInfo::InfoKind::Base,
1264 getStorageType(RD: baseDecl),
1265 getBaseMemberKind(baseDecl), baseDecl));
1266 }
1267
1268 // Accumulate the non-virtual bases.
1269 for (const auto &base : cxxRecordDecl->bases()) {
1270 if (base.isVirtual())
1271 continue;
1272 // Bases can be zero-sized even if not technically empty if they
1273 // contain only a trailing array member.
1274 const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl();
1275 if (!baseDecl->isEmpty() &&
1276 !astContext.getASTRecordLayout(D: baseDecl).getNonVirtualSize().isZero()) {
1277 members.push_back(x: MemberInfo(astRecordLayout.getBaseClassOffset(Base: baseDecl),
1278 MemberInfo::InfoKind::Base,
1279 getStorageType(RD: baseDecl),
1280 getBaseMemberKind(baseDecl), baseDecl));
1281 }
1282 }
1283}
1284
1285void CIRRecordLowering::accumulateVBases() {
1286 for (const auto &base : cxxRecordDecl->vbases()) {
1287 const CXXRecordDecl *baseDecl = base.getType()->getAsCXXRecordDecl();
1288 if (CodeGenUtils::isEmptyRecordForLayout(Ctx: astContext, T: base.getType()))
1289 continue;
1290 CharUnits offset = astRecordLayout.getVBaseClassOffset(VBase: baseDecl);
1291 // If the vbase is a primary virtual base of some base, then it doesn't
1292 // get its own storage location but instead lives inside of that base.
1293 if (CodeGenUtils::isOverlappingVBaseABI(Ctx: astContext) &&
1294 astContext.isNearlyEmpty(RD: baseDecl) &&
1295 !CodeGenUtils::hasOwnStorage(Ctx: astContext, Decl: cxxRecordDecl, Query: baseDecl)) {
1296 members.push_back(x: MemberInfo(offset, MemberInfo::InfoKind::VBase, nullptr,
1297 cir::RecordMemberKind::Data, baseDecl));
1298 continue;
1299 }
1300 // If we've got a vtordisp, add it as a storage type.
1301 if (astRecordLayout.getVBaseOffsetsMap()
1302 .find(Val: baseDecl)
1303 ->second.hasVtorDisp())
1304 members.push_back(x: makeStorageInfo(offset: offset - CharUnits::fromQuantity(Quantity: 4),
1305 data: getUIntNType(numBits: 32),
1306 memberKind: cir::RecordMemberKind::Data));
1307 members.push_back(x: MemberInfo(offset, MemberInfo::InfoKind::VBase,
1308 getStorageType(RD: baseDecl),
1309 getBaseMemberKind(baseDecl), baseDecl));
1310 }
1311}
1312
1313void CIRRecordLowering::accumulateVPtrs() {
1314 if (astRecordLayout.hasOwnVFPtr())
1315 members.push_back(x: MemberInfo(CharUnits::Zero(), MemberInfo::InfoKind::VFPtr,
1316 getVFPtrType(), cir::RecordMemberKind::Data));
1317
1318 if (astRecordLayout.hasOwnVBPtr())
1319 cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
1320 "accumulateVPtrs: hasOwnVBPtr");
1321}
1322
1323mlir::Type CIRRecordLowering::getVFPtrType() {
1324 return cir::VPtrType::get(ctx: builder.getContext());
1325}
1326