Skip to content

[IRGen] Simplify the ownership of ProtocolInfo #18704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,18 +2108,17 @@ const ProtocolInfo &TypeConverter::getProtocolInfo(ProtocolDecl *protocol,
ProtocolInfoKind kind) {
// Check whether we've already translated this protocol.
auto it = Protocols.find(protocol);
if (it != Protocols.end() && it->second->getKind() >= kind)
return *it->second;
if (it != Protocols.end() && it->getSecond()->getKind() >= kind)
return *it->getSecond();

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

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

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

// Memoize.
Protocols[protocol] = info;
std::unique_ptr<const ProtocolInfo> &cachedInfo = Protocols[protocol];
cachedInfo = std::move(info);

// Done.
return *info;
return *cachedInfo;
}

/// Allocate a new ProtocolInfo.
ProtocolInfo *ProtocolInfo::create(ArrayRef<WitnessTableEntry> table,
ProtocolInfoKind kind) {
std::unique_ptr<ProtocolInfo>
ProtocolInfo::create(ArrayRef<WitnessTableEntry> table, ProtocolInfoKind kind) {
size_t bufferSize = totalSizeToAlloc<WitnessTableEntry>(table.size());
void *buffer = ::operator new(bufferSize);
return new(buffer) ProtocolInfo(table, kind);
return std::unique_ptr<ProtocolInfo>(new(buffer) ProtocolInfo(table, kind));
}

// Provide a unique home for the ConformanceInfo vtable.
Expand Down
6 changes: 0 additions & 6 deletions lib/IRGen/GenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,12 +1078,6 @@ TypeConverter::~TypeConverter() {
I = Cur->NextConverted;
delete Cur;
}

for (const ProtocolInfo *I = FirstProtocol; I != nullptr; ) {
const ProtocolInfo *Cur = I;
I = Cur->NextConverted.getPointer();
delete Cur;
}
}

void TypeConverter::pushGenericContext(CanGenericSignature signature) {
Expand Down
3 changes: 1 addition & 2 deletions lib/IRGen/GenType.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class TypeConverter {
private:
bool CompletelyFragile = false;

llvm::DenseMap<ProtocolDecl*, const ProtocolInfo*> Protocols;
llvm::DenseMap<ProtocolDecl*, std::unique_ptr<const ProtocolInfo>> Protocols;
const TypeInfo *FirstType;

const ProtocolInfo *FirstProtocol = nullptr;
const LoadableTypeInfo *NativeObjectTI = nullptr;
const LoadableTypeInfo *UnknownObjectTI = nullptr;
const LoadableTypeInfo *BridgeObjectTI = nullptr;
Expand Down
19 changes: 9 additions & 10 deletions lib/IRGen/ProtocolInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,25 @@ enum class ProtocolInfoKind : uint8_t {
class ProtocolInfo final :
private llvm::TrailingObjects<ProtocolInfo, WitnessTableEntry> {
friend TrailingObjects;

/// A singly-linked-list of all the protocols that have been laid out.
llvm::PointerIntPair<const ProtocolInfo *, 1, ProtocolInfoKind> NextConverted;
friend class TypeConverter;

ProtocolInfoKind getKind() const {
return NextConverted.getInt();
}

/// The number of table entries in this protocol layout.
unsigned NumTableEntries;

ProtocolInfoKind Kind;

ProtocolInfoKind getKind() const {
return Kind;
}

ProtocolInfo(ArrayRef<WitnessTableEntry> table, ProtocolInfoKind kind)
: NextConverted(nullptr, kind), NumTableEntries(table.size()) {
: NumTableEntries(table.size()), Kind(kind) {
std::uninitialized_copy(table.begin(), table.end(),
getTrailingObjects<WitnessTableEntry>());
}

static ProtocolInfo *create(ArrayRef<WitnessTableEntry> table,
ProtocolInfoKind kind);
static std::unique_ptr<ProtocolInfo> create(ArrayRef<WitnessTableEntry> table,
ProtocolInfoKind kind);

public:
/// The number of witness slots in a conformance to this protocol;
Expand Down