Skip to content

Commit ef06972

Browse files
authored
[IRGen] Simplify the ownership of ProtocolInfo (#18704)
Rather than keep a singly-linked list of ProtocolInfo objects to free, just rely on them being cached in the single DenseMap in TypeConverter.
1 parent 1f9ef88 commit ef06972

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

lib/IRGen/GenProto.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,18 +2109,17 @@ const ProtocolInfo &TypeConverter::getProtocolInfo(ProtocolDecl *protocol,
21092109
ProtocolInfoKind kind) {
21102110
// Check whether we've already translated this protocol.
21112111
auto it = Protocols.find(protocol);
2112-
if (it != Protocols.end() && it->second->getKind() >= kind)
2113-
return *it->second;
2112+
if (it != Protocols.end() && it->getSecond()->getKind() >= kind)
2113+
return *it->getSecond();
21142114

21152115
// If not, lay out the protocol's witness table, if it needs one.
21162116
WitnessTableLayout layout(kind);
21172117
if (Lowering::TypeConverter::protocolRequiresWitnessTable(protocol))
21182118
layout.visitProtocolDecl(protocol);
21192119

21202120
// Create a ProtocolInfo object from the layout.
2121-
ProtocolInfo *info = ProtocolInfo::create(layout.getEntries(), kind);
2122-
info->NextConverted.setPointer(FirstProtocol);
2123-
FirstProtocol = info;
2121+
std::unique_ptr<ProtocolInfo> info = ProtocolInfo::create(layout.getEntries(),
2122+
kind);
21242123

21252124
// Verify that we haven't generated an incompatible layout.
21262125
if (it != Protocols.end()) {
@@ -2134,18 +2133,19 @@ const ProtocolInfo &TypeConverter::getProtocolInfo(ProtocolDecl *protocol,
21342133
}
21352134

21362135
// Memoize.
2137-
Protocols[protocol] = info;
2136+
std::unique_ptr<const ProtocolInfo> &cachedInfo = Protocols[protocol];
2137+
cachedInfo = std::move(info);
21382138

21392139
// Done.
2140-
return *info;
2140+
return *cachedInfo;
21412141
}
21422142

21432143
/// Allocate a new ProtocolInfo.
2144-
ProtocolInfo *ProtocolInfo::create(ArrayRef<WitnessTableEntry> table,
2145-
ProtocolInfoKind kind) {
2144+
std::unique_ptr<ProtocolInfo>
2145+
ProtocolInfo::create(ArrayRef<WitnessTableEntry> table, ProtocolInfoKind kind) {
21462146
size_t bufferSize = totalSizeToAlloc<WitnessTableEntry>(table.size());
21472147
void *buffer = ::operator new(bufferSize);
2148-
return new(buffer) ProtocolInfo(table, kind);
2148+
return std::unique_ptr<ProtocolInfo>(new(buffer) ProtocolInfo(table, kind));
21492149
}
21502150

21512151
// Provide a unique home for the ConformanceInfo vtable.

lib/IRGen/GenType.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,12 +1078,6 @@ TypeConverter::~TypeConverter() {
10781078
I = Cur->NextConverted;
10791079
delete Cur;
10801080
}
1081-
1082-
for (const ProtocolInfo *I = FirstProtocol; I != nullptr; ) {
1083-
const ProtocolInfo *Cur = I;
1084-
I = Cur->NextConverted.getPointer();
1085-
delete Cur;
1086-
}
10871081
}
10881082

10891083
void TypeConverter::pushGenericContext(CanGenericSignature signature) {

lib/IRGen/GenType.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ class TypeConverter {
6767
private:
6868
bool CompletelyFragile = false;
6969

70-
llvm::DenseMap<ProtocolDecl*, const ProtocolInfo*> Protocols;
70+
llvm::DenseMap<ProtocolDecl*, std::unique_ptr<const ProtocolInfo>> Protocols;
7171
const TypeInfo *FirstType;
7272

73-
const ProtocolInfo *FirstProtocol = nullptr;
7473
const LoadableTypeInfo *NativeObjectTI = nullptr;
7574
const LoadableTypeInfo *UnknownObjectTI = nullptr;
7675
const LoadableTypeInfo *BridgeObjectTI = nullptr;

lib/IRGen/ProtocolInfo.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,25 @@ enum class ProtocolInfoKind : uint8_t {
165165
class ProtocolInfo final :
166166
private llvm::TrailingObjects<ProtocolInfo, WitnessTableEntry> {
167167
friend TrailingObjects;
168-
169-
/// A singly-linked-list of all the protocols that have been laid out.
170-
llvm::PointerIntPair<const ProtocolInfo *, 1, ProtocolInfoKind> NextConverted;
171168
friend class TypeConverter;
172169

173-
ProtocolInfoKind getKind() const {
174-
return NextConverted.getInt();
175-
}
176-
177170
/// The number of table entries in this protocol layout.
178171
unsigned NumTableEntries;
179172

173+
ProtocolInfoKind Kind;
174+
175+
ProtocolInfoKind getKind() const {
176+
return Kind;
177+
}
178+
180179
ProtocolInfo(ArrayRef<WitnessTableEntry> table, ProtocolInfoKind kind)
181-
: NextConverted(nullptr, kind), NumTableEntries(table.size()) {
180+
: NumTableEntries(table.size()), Kind(kind) {
182181
std::uninitialized_copy(table.begin(), table.end(),
183182
getTrailingObjects<WitnessTableEntry>());
184183
}
185184

186-
static ProtocolInfo *create(ArrayRef<WitnessTableEntry> table,
187-
ProtocolInfoKind kind);
185+
static std::unique_ptr<ProtocolInfo> create(ArrayRef<WitnessTableEntry> table,
186+
ProtocolInfoKind kind);
188187

189188
public:
190189
/// The number of witness slots in a conformance to this protocol;

0 commit comments

Comments
 (0)