Skip to content

[NFC] Adjust SILFunctionType layout to use TrailingObjects methods. #29124

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
Jan 15, 2020
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
46 changes: 18 additions & 28 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3854,15 +3854,23 @@ namespace Lowering {
/// function parameter and result types.
class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
private llvm::TrailingObjects<SILFunctionType, SILParameterInfo,
SILResultInfo> {
SILResultInfo, SILYieldInfo, CanType> {
friend TrailingObjects;

size_t numTrailingObjects(OverloadToken<SILParameterInfo>) const {
return NumParameters;
}

size_t numTrailingObjects(OverloadToken<SILResultInfo>) const {
return hasErrorResult() ? 1 : 0;
return getNumResults() + (hasErrorResult() ? 1 : 0);
}

size_t numTrailingObjects(OverloadToken<SILYieldInfo>) const {
return getNumYields();
}

size_t numTrailingObjects(OverloadToken<CanType>) const {
return hasResultCache() ? 2 : 0;
}

public:
Expand Down Expand Up @@ -4035,12 +4043,13 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
unsigned NumAnyResults : 16; // Not including the ErrorResult.
unsigned NumAnyIndirectFormalResults : 16; // Subset of NumAnyResults.

// [SILFunctionType-layout]
// The layout of a SILFunctionType in memory is:
// SILFunctionType
// SILParameterInfo[NumParameters]
// SILResultInfo[isCoroutine() ? 0 : NumAnyResults]
// SILYieldInfo[isCoroutine() ? NumAnyResults : 0]
// SILResultInfo? // if hasErrorResult()
// SILYieldInfo[isCoroutine() ? NumAnyResults : 0]
// CanType? // if !isCoro && NumAnyResults > 1, formal result cache
// CanType? // if !isCoro && NumAnyResults > 1, all result cache

Expand All @@ -4053,34 +4062,16 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
}

MutableArrayRef<SILResultInfo> getMutableResults() {
auto *ptr = reinterpret_cast<SILResultInfo *>(getMutableParameters().end());
return {ptr, getNumResults()};
return {getTrailingObjects<SILResultInfo>(), getNumResults()};
}

MutableArrayRef<SILYieldInfo> getMutableYields() {
auto *ptr = reinterpret_cast<SILYieldInfo *>(getMutableParameters().end());
return {ptr, getNumYields()};
}

/// Return a pointer past the end of the formal results, whether they
/// are yield-results or normal results.
void *getEndOfFormalResults() {
return isCoroutine() ? static_cast<void*>(getMutableYields().end())
: static_cast<void*>(getMutableResults().end());
return {getTrailingObjects<SILYieldInfo>(), getNumYields()};
}

SILResultInfo &getMutableErrorResult() {
assert(hasErrorResult());
return *reinterpret_cast<SILResultInfo*>(getEndOfFormalResults());
}

/// Return a pointer past the end of all of the results, including the
/// error result if one is present.
void *getEndOfAllResults() {
void *end = getEndOfFormalResults();
if (hasErrorResult())
end = reinterpret_cast<char*>(end) + sizeof(SILResultInfo);
return end;
return *(getTrailingObjects<SILResultInfo>() + getNumResults());
}

/// Do we have slots for caches of the normal-result tuple type?
Expand All @@ -4090,14 +4081,13 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,

CanType &getMutableFormalResultsCache() const {
assert(hasResultCache());
auto *ptr = const_cast<SILFunctionType *>(this)->getEndOfAllResults();
return *reinterpret_cast<CanType*>(ptr);
return *const_cast<SILFunctionType *>(this)->getTrailingObjects<CanType>();
}

CanType &getMutableAllResultsCache() const {
assert(hasResultCache());
auto *ptr = const_cast<SILFunctionType *>(this)->getEndOfAllResults();
return *(reinterpret_cast<CanType *>(ptr) + 1);
return *(const_cast<SILFunctionType *>(this)->getTrailingObjects<CanType>()
+ 1);
}

SILFunctionType(GenericSignature genericSig, ExtInfo ext,
Expand Down
16 changes: 9 additions & 7 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3374,13 +3374,13 @@ CanSILFunctionType SILFunctionType::get(

// All SILFunctionTypes are canonical.

// Allocate storage for the object.
size_t bytes = sizeof(SILFunctionType)
+ sizeof(SILParameterInfo) * params.size()
+ sizeof(SILYieldInfo) * yields.size()
+ sizeof(SILResultInfo) * normalResults.size()
+ (errorResult ? sizeof(SILResultInfo) : 0)
+ (normalResults.size() > 1 ? sizeof(CanType) * 2 : 0);
// See [SILFunctionType-layout]
bool hasResultCache = normalResults.size() > 1;
size_t bytes =
totalSizeToAlloc<SILParameterInfo, SILResultInfo, SILYieldInfo, CanType>(
params.size(), normalResults.size() + (errorResult ? 1 : 0),
yields.size(), hasResultCache ? 2 : 0);

void *mem = ctx.Allocate(bytes, alignof(SILFunctionType));

RecursiveTypeProperties properties;
Expand Down Expand Up @@ -3411,6 +3411,8 @@ CanSILFunctionType SILFunctionType::get(
params, yields, normalResults, errorResult,
substitutions, genericSigIsImplied,
ctx, properties, witnessMethodConformance);
assert(fnType->hasResultCache() == hasResultCache);

ctx.getImpl().SILFunctionTypes.InsertNode(fnType, insertPos);
return CanSILFunctionType(fnType);
}
Expand Down