Skip to content

Commit 4f10b19

Browse files
[NFC] Adjust SILFunctionType layout to use TrailingObjects methods. (#29124)
This makes the implementation cleaner, as well as fixes a bug in the numTrailingObjects<SILResultInfo> method (which was unused).
1 parent c5461ce commit 4f10b19

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

include/swift/AST/Types.h

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,15 +3859,23 @@ namespace Lowering {
38593859
/// function parameter and result types.
38603860
class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
38613861
private llvm::TrailingObjects<SILFunctionType, SILParameterInfo,
3862-
SILResultInfo> {
3862+
SILResultInfo, SILYieldInfo, CanType> {
38633863
friend TrailingObjects;
38643864

38653865
size_t numTrailingObjects(OverloadToken<SILParameterInfo>) const {
38663866
return NumParameters;
38673867
}
38683868

38693869
size_t numTrailingObjects(OverloadToken<SILResultInfo>) const {
3870-
return hasErrorResult() ? 1 : 0;
3870+
return getNumResults() + (hasErrorResult() ? 1 : 0);
3871+
}
3872+
3873+
size_t numTrailingObjects(OverloadToken<SILYieldInfo>) const {
3874+
return getNumYields();
3875+
}
3876+
3877+
size_t numTrailingObjects(OverloadToken<CanType>) const {
3878+
return hasResultCache() ? 2 : 0;
38713879
}
38723880

38733881
public:
@@ -4040,12 +4048,13 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
40404048
unsigned NumAnyResults : 16; // Not including the ErrorResult.
40414049
unsigned NumAnyIndirectFormalResults : 16; // Subset of NumAnyResults.
40424050

4051+
// [SILFunctionType-layout]
40434052
// The layout of a SILFunctionType in memory is:
40444053
// SILFunctionType
40454054
// SILParameterInfo[NumParameters]
40464055
// SILResultInfo[isCoroutine() ? 0 : NumAnyResults]
4047-
// SILYieldInfo[isCoroutine() ? NumAnyResults : 0]
40484056
// SILResultInfo? // if hasErrorResult()
4057+
// SILYieldInfo[isCoroutine() ? NumAnyResults : 0]
40494058
// CanType? // if !isCoro && NumAnyResults > 1, formal result cache
40504059
// CanType? // if !isCoro && NumAnyResults > 1, all result cache
40514060

@@ -4058,34 +4067,16 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
40584067
}
40594068

40604069
MutableArrayRef<SILResultInfo> getMutableResults() {
4061-
auto *ptr = reinterpret_cast<SILResultInfo *>(getMutableParameters().end());
4062-
return {ptr, getNumResults()};
4070+
return {getTrailingObjects<SILResultInfo>(), getNumResults()};
40634071
}
40644072

40654073
MutableArrayRef<SILYieldInfo> getMutableYields() {
4066-
auto *ptr = reinterpret_cast<SILYieldInfo *>(getMutableParameters().end());
4067-
return {ptr, getNumYields()};
4068-
}
4069-
4070-
/// Return a pointer past the end of the formal results, whether they
4071-
/// are yield-results or normal results.
4072-
void *getEndOfFormalResults() {
4073-
return isCoroutine() ? static_cast<void*>(getMutableYields().end())
4074-
: static_cast<void*>(getMutableResults().end());
4074+
return {getTrailingObjects<SILYieldInfo>(), getNumYields()};
40754075
}
40764076

40774077
SILResultInfo &getMutableErrorResult() {
40784078
assert(hasErrorResult());
4079-
return *reinterpret_cast<SILResultInfo*>(getEndOfFormalResults());
4080-
}
4081-
4082-
/// Return a pointer past the end of all of the results, including the
4083-
/// error result if one is present.
4084-
void *getEndOfAllResults() {
4085-
void *end = getEndOfFormalResults();
4086-
if (hasErrorResult())
4087-
end = reinterpret_cast<char*>(end) + sizeof(SILResultInfo);
4088-
return end;
4079+
return *(getTrailingObjects<SILResultInfo>() + getNumResults());
40894080
}
40904081

40914082
/// Do we have slots for caches of the normal-result tuple type?
@@ -4095,14 +4086,13 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
40954086

40964087
CanType &getMutableFormalResultsCache() const {
40974088
assert(hasResultCache());
4098-
auto *ptr = const_cast<SILFunctionType *>(this)->getEndOfAllResults();
4099-
return *reinterpret_cast<CanType*>(ptr);
4089+
return *const_cast<SILFunctionType *>(this)->getTrailingObjects<CanType>();
41004090
}
41014091

41024092
CanType &getMutableAllResultsCache() const {
41034093
assert(hasResultCache());
4104-
auto *ptr = const_cast<SILFunctionType *>(this)->getEndOfAllResults();
4105-
return *(reinterpret_cast<CanType *>(ptr) + 1);
4094+
return *(const_cast<SILFunctionType *>(this)->getTrailingObjects<CanType>()
4095+
+ 1);
41064096
}
41074097

41084098
SILFunctionType(GenericSignature genericSig, ExtInfo ext,

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,13 +3374,13 @@ CanSILFunctionType SILFunctionType::get(
33743374

33753375
// All SILFunctionTypes are canonical.
33763376

3377-
// Allocate storage for the object.
3378-
size_t bytes = sizeof(SILFunctionType)
3379-
+ sizeof(SILParameterInfo) * params.size()
3380-
+ sizeof(SILYieldInfo) * yields.size()
3381-
+ sizeof(SILResultInfo) * normalResults.size()
3382-
+ (errorResult ? sizeof(SILResultInfo) : 0)
3383-
+ (normalResults.size() > 1 ? sizeof(CanType) * 2 : 0);
3377+
// See [SILFunctionType-layout]
3378+
bool hasResultCache = normalResults.size() > 1;
3379+
size_t bytes =
3380+
totalSizeToAlloc<SILParameterInfo, SILResultInfo, SILYieldInfo, CanType>(
3381+
params.size(), normalResults.size() + (errorResult ? 1 : 0),
3382+
yields.size(), hasResultCache ? 2 : 0);
3383+
33843384
void *mem = ctx.Allocate(bytes, alignof(SILFunctionType));
33853385

33863386
RecursiveTypeProperties properties;
@@ -3411,6 +3411,8 @@ CanSILFunctionType SILFunctionType::get(
34113411
params, yields, normalResults, errorResult,
34123412
substitutions, genericSigIsImplied,
34133413
ctx, properties, witnessMethodConformance);
3414+
assert(fnType->hasResultCache() == hasResultCache);
3415+
34143416
ctx.getImpl().SILFunctionTypes.InsertNode(fnType, insertPos);
34153417
return CanSILFunctionType(fnType);
34163418
}

0 commit comments

Comments
 (0)