Skip to content

Commit 13cd05c

Browse files
committed
TypeLayout: Add containsResilient and containsArchetype
We'll use that in a follow-up commit.
1 parent 976a1d7 commit 13cd05c

File tree

2 files changed

+78
-40
lines changed

2 files changed

+78
-40
lines changed

lib/IRGen/TypeLayout.cpp

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ using namespace irgen;
2525

2626
TypeLayoutEntry::~TypeLayoutEntry() {}
2727

28+
void TypeLayoutEntry::computeProperties() {
29+
// does not add anything.
30+
}
31+
32+
void TypeLayoutEntry::gatherProperties(TypeLayoutEntry *fromEntry) {
33+
hasArchetypeField |= fromEntry->hasArchetypeField;
34+
hasResilientField |= fromEntry->hasResilientField;
35+
hasDependentResilientField |= fromEntry->hasDependentResilientField;
36+
37+
assert(!(!hasResilientField && hasDependentResilientField));
38+
}
39+
2840
const EnumTypeLayoutEntry *TypeLayoutEntry::getAsEnum() const {
2941
if (getKind() == TypeLayoutEntryKind::Enum) {
3042
return static_cast<const EnumTypeLayoutEntry *>(this);
@@ -99,8 +111,16 @@ void TypeLayoutEntry::initWithTake(IRGenFunction &IGF, Address dest,
99111
// Nothing to copy.
100112
}
101113

102-
bool TypeLayoutEntry::containsEnum() const {
103-
return false;
114+
bool TypeLayoutEntry::containsResilientField() const {
115+
return hasResilientField;
116+
}
117+
118+
bool TypeLayoutEntry::containsArchetypeField() const {
119+
return hasArchetypeField;
120+
}
121+
122+
bool TypeLayoutEntry::containsDependentResilientField() const {
123+
return hasDependentResilientField;
104124
}
105125

106126
llvm::Value *TypeLayoutEntry::getEnumTagSinglePayload(
@@ -571,6 +591,10 @@ llvm::Value *TypeLayoutEntry::initBufferWithCopyOfBuffer(IRGenFunction &IGF,
571591
return pointerToObject;
572592
}
573593

594+
void ScalarTypeLayoutEntry::computeProperties() {
595+
// does not add anything.
596+
}
597+
574598
void ScalarTypeLayoutEntry::Profile(llvm::FoldingSetNodeID &id) const {
575599
ScalarTypeLayoutEntry::Profile(id, typeInfo);
576600
}
@@ -608,10 +632,6 @@ llvm::Value *ScalarTypeLayoutEntry::isBitwiseTakable(IRGenFunction &IGF) const {
608632
ResilienceExpansion::Maximal));
609633
}
610634

611-
bool ScalarTypeLayoutEntry::containsEnum() const {
612-
return false;
613-
}
614-
615635
void ScalarTypeLayoutEntry::destroy(IRGenFunction &IGF, Address addr) const {
616636
auto alignment = cast<FixedTypeInfo>(typeInfo).getFixedAlignment();
617637
auto addressType = typeInfo.getStorageType()->getPointerTo();
@@ -709,6 +729,12 @@ LLVM_DUMP_METHOD void ScalarTypeLayoutEntry::dump() const {
709729
}
710730
#endif
711731

732+
void AlignedGroupEntry::computeProperties() {
733+
for (auto *entry : entries) {
734+
gatherProperties(entry);
735+
}
736+
}
737+
712738
void AlignedGroupEntry::Profile(llvm::FoldingSetNodeID &id) const {
713739
AlignedGroupEntry::Profile(id, entries, minimumAlignment, isFixedSize);
714740
}
@@ -763,14 +789,6 @@ llvm::Value *AlignedGroupEntry::size(IRGenFunction &IGF) const {
763789
return currentSize;
764790
}
765791

766-
bool AlignedGroupEntry::containsEnum() const {
767-
for (auto *entry : entries) {
768-
if (entry->containsEnum())
769-
return true;
770-
}
771-
return false;
772-
}
773-
774792
llvm::Value *AlignedGroupEntry::extraInhabitantCount(IRGenFunction &IGF) const {
775793
llvm::Value *currentMaxXICount = IGF.IGM.getInt32(0);
776794
auto &Builder = IGF.Builder;
@@ -1001,6 +1019,8 @@ LLVM_DUMP_METHOD void AlignedGroupEntry::dump() const {
10011019
}
10021020
#endif
10031021

1022+
void ArchetypeLayoutEntry::computeProperties() { hasArchetypeField = true; }
1023+
10041024
void ArchetypeLayoutEntry::Profile(llvm::FoldingSetNodeID &id) const {
10051025
ArchetypeLayoutEntry::Profile(id, archetype);
10061026
}
@@ -1030,10 +1050,6 @@ ArchetypeLayoutEntry::isBitwiseTakable(IRGenFunction &IGF) const {
10301050
return emitLoadOfIsBitwiseTakable(IGF, archetype);
10311051
}
10321052

1033-
bool ArchetypeLayoutEntry::containsEnum() const {
1034-
return false;
1035-
}
1036-
10371053
void ArchetypeLayoutEntry::destroy(IRGenFunction &IGF, Address addr) const {
10381054
emitDestroyCall(IGF, archetype, addr);
10391055
}
@@ -1139,10 +1155,10 @@ llvm::Value *EnumTypeLayoutEntry::isBitwiseTakable(IRGenFunction &IGF) const {
11391155
return isBitwiseTakable;
11401156
}
11411157

1142-
bool EnumTypeLayoutEntry::containsEnum() const {
1143-
if (cases.size() == 1)
1144-
return false;
1145-
return false;
1158+
void EnumTypeLayoutEntry::computeProperties() {
1159+
for (auto c: cases) {
1160+
gatherProperties(c);
1161+
}
11461162
}
11471163

11481164
llvm::Value *EnumTypeLayoutEntry::size(IRGenFunction &IGF) const {
@@ -2006,8 +2022,10 @@ ResilientTypeLayoutEntry::isBitwiseTakable(IRGenFunction &IGF) const {
20062022
return emitLoadOfIsBitwiseTakable(IGF, ty);
20072023
}
20082024

2009-
bool ResilientTypeLayoutEntry::containsEnum() const {
2010-
return false;
2025+
void ResilientTypeLayoutEntry::computeProperties() {
2026+
hasResilientField = true;
2027+
if (ty.getASTType()->hasArchetype())
2028+
hasDependentResilientField = true;
20112029
}
20122030

20132031
void ResilientTypeLayoutEntry::destroy(IRGenFunction &IGF, Address addr) const {
@@ -2076,6 +2094,7 @@ TypeLayoutCache::getOrCreateScalarEntry(const TypeInfo &ti,
20762094
auto mem = bumpAllocator.Allocate(bytes, alignof(ScalarTypeLayoutEntry));
20772095
auto newEntry = new (mem) ScalarTypeLayoutEntry(ti, representative);
20782096
scalarEntries.InsertNode(newEntry, insertPos);
2097+
newEntry->computeProperties();
20792098
return newEntry;
20802099
}
20812100

@@ -2091,6 +2110,7 @@ TypeLayoutCache::getOrCreateArchetypeEntry(SILType archetype) {
20912110
auto mem = bumpAllocator.Allocate(bytes, alignof(ArchetypeLayoutEntry));
20922111
auto newEntry = new (mem) ArchetypeLayoutEntry(archetype);
20932112
archetypeEntries.InsertNode(newEntry, insertPos);
2113+
newEntry->computeProperties();
20942114
return newEntry;
20952115
}
20962116

@@ -2108,6 +2128,7 @@ AlignedGroupEntry *TypeLayoutCache::getOrCreateAlignedGroupEntry(
21082128
auto newEntry =
21092129
new (mem) AlignedGroupEntry(entries, minimumAlignment, isFixedSize);
21102130
alignedGroupEntries.InsertNode(newEntry, insertPos);
2131+
newEntry->computeProperties();
21112132
return newEntry;
21122133
}
21132134

@@ -2127,6 +2148,7 @@ EnumTypeLayoutEntry *TypeLayoutCache::getOrCreateEnumEntry(
21272148
auto mem = bumpAllocator.Allocate(bytes, alignof(EnumTypeLayoutEntry));
21282149
auto newEntry = new (mem) EnumTypeLayoutEntry(numEmptyCases, nonEmptyCases);
21292150
enumEntries.InsertNode(newEntry, insertPos);
2151+
newEntry->computeProperties();
21302152
return newEntry;
21312153
}
21322154

@@ -2142,6 +2164,7 @@ TypeLayoutCache::getOrCreateResilientEntry(SILType ty) {
21422164
auto mem = bumpAllocator.Allocate(bytes, alignof(ResilientTypeLayoutEntry));
21432165
auto newEntry = new (mem) ResilientTypeLayoutEntry(ty);
21442166
resilientEntries.InsertNode(newEntry, insertPos);
2167+
newEntry->computeProperties();
21452168
return newEntry;
21462169
}
21472170

lib/IRGen/TypeLayout.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace irgen {
2222

2323
class EnumTypeLayoutEntry;
2424

25-
enum class TypeLayoutEntryKind {
25+
enum class TypeLayoutEntryKind : uint8_t {
2626
Empty,
2727
Scalar,
2828
Archetype,
@@ -34,11 +34,27 @@ enum class TypeLayoutEntryKind {
3434
class TypeLayoutEntry {
3535
public:
3636
TypeLayoutEntryKind kind;
37-
TypeLayoutEntry() : kind(TypeLayoutEntryKind::Empty) {}
38-
TypeLayoutEntry(TypeLayoutEntryKind kind) : kind(kind) {}
37+
uint8_t hasArchetypeField : 1;
38+
uint8_t hasResilientField : 1;
39+
uint8_t hasDependentResilientField : 1;
40+
41+
TypeLayoutEntry()
42+
: kind(TypeLayoutEntryKind::Empty), hasArchetypeField(false),
43+
hasResilientField(false), hasDependentResilientField(false) {}
44+
45+
TypeLayoutEntry(TypeLayoutEntryKind kind)
46+
: kind(kind), hasArchetypeField(false), hasResilientField(false),
47+
hasDependentResilientField(false) {}
3948

4049
virtual ~TypeLayoutEntry();
4150

51+
virtual void computeProperties();
52+
53+
bool containsResilientField() const;
54+
bool containsArchetypeField() const;
55+
bool containsDependentResilientField() const;
56+
57+
4258
bool isEmpty() const { return kind == TypeLayoutEntryKind::Empty; }
4359

4460
TypeLayoutEntryKind getKind() const { return kind; }
@@ -78,9 +94,6 @@ class TypeLayoutEntry {
7894
llvm::Value *numEmptyCases,
7995
Address enumAddr) const;
8096

81-
82-
virtual bool containsEnum() const;
83-
8497
const EnumTypeLayoutEntry *getAsEnum() const;
8598

8699
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -101,6 +114,8 @@ class TypeLayoutEntry {
101114
Address addr,
102115
llvm::function_ref<void(Address addr, llvm::Value *tag)>
103116
storeExtraInhabitantIndexFun) const;
117+
118+
void gatherProperties(TypeLayoutEntry *fromEntry);
104119
};
105120

106121
class ScalarTypeLayoutEntry : public TypeLayoutEntry,
@@ -114,6 +129,8 @@ class ScalarTypeLayoutEntry : public TypeLayoutEntry,
114129

115130
~ScalarTypeLayoutEntry();
116131

132+
void computeProperties() override;
133+
117134
// Support for FoldingSet.
118135
void Profile(llvm::FoldingSetNodeID &id) const;
119136
static void Profile(llvm::FoldingSetNodeID &ID, const TypeInfo &ti);
@@ -143,8 +160,6 @@ class ScalarTypeLayoutEntry : public TypeLayoutEntry,
143160
llvm::Value *numEmptyCases,
144161
Address enumAddr) const override;
145162

146-
bool containsEnum() const override;
147-
148163
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
149164
void dump() const override;
150165
#endif
@@ -161,6 +176,8 @@ class ArchetypeLayoutEntry : public TypeLayoutEntry,
161176

162177
~ArchetypeLayoutEntry();
163178

179+
void computeProperties() override;
180+
164181
// Support for FoldingSet.
165182
void Profile(llvm::FoldingSetNodeID &id) const;
166183
static void Profile(llvm::FoldingSetNodeID &ID, SILType archetype);
@@ -190,8 +207,6 @@ class ArchetypeLayoutEntry : public TypeLayoutEntry,
190207
llvm::Value *numEmptyCases,
191208
Address enumAddr) const override;
192209

193-
bool containsEnum() const override;
194-
195210
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
196211
void dump() const override;
197212
#endif
@@ -207,6 +222,8 @@ class ResilientTypeLayoutEntry : public TypeLayoutEntry,
207222

208223
~ResilientTypeLayoutEntry();
209224

225+
void computeProperties() override;
226+
210227
// Support for FoldingSet.
211228
void Profile(llvm::FoldingSetNodeID &id) const;
212229
static void Profile(llvm::FoldingSetNodeID &ID, SILType ty);
@@ -236,8 +253,6 @@ class ResilientTypeLayoutEntry : public TypeLayoutEntry,
236253
llvm::Value *numEmptyCases,
237254
Address enumAddr) const override;
238255

239-
bool containsEnum() const override;
240-
241256
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
242257
void dump() const override;
243258
#endif
@@ -256,6 +271,8 @@ class AlignedGroupEntry : public TypeLayoutEntry, public llvm::FoldingSetNode {
256271

257272
~AlignedGroupEntry();
258273

274+
void computeProperties() override;
275+
259276
// Support for FoldingSet.
260277
void Profile(llvm::FoldingSetNodeID &id) const;
261278
static void Profile(llvm::FoldingSetNodeID &ID,
@@ -287,8 +304,6 @@ class AlignedGroupEntry : public TypeLayoutEntry, public llvm::FoldingSetNode {
287304
llvm::Value *numEmptyCases,
288305
Address enumAddr) const override;
289306

290-
bool containsEnum() const override;
291-
292307
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
293308
void dump() const override;
294309
#endif
@@ -321,6 +336,8 @@ class EnumTypeLayoutEntry : public TypeLayoutEntry,
321336

322337
~EnumTypeLayoutEntry();
323338

339+
void computeProperties() override;
340+
324341
// Support for FoldingSet.
325342
void Profile(llvm::FoldingSetNodeID &id) const;
326343
static void Profile(llvm::FoldingSetNodeID &ID, unsigned numEmptyCases,
@@ -358,8 +375,6 @@ class EnumTypeLayoutEntry : public TypeLayoutEntry,
358375
void destructiveInjectEnumTag(IRGenFunction &IGF, llvm::Value *tag,
359376
Address enumAddr) const;
360377

361-
bool containsEnum() const override;
362-
363378
bool isMultiPayloadEnum() const;
364379

365380
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

0 commit comments

Comments
 (0)