Skip to content

Commit 66dc1cc

Browse files
authored
Merge pull request #83902 from slavapestov/lowered-type-in-context
Cache SILFunction::getLoweredTypeInContext()
2 parents 687e09d + 9d30684 commit 66dc1cc

File tree

8 files changed

+35
-19
lines changed

8 files changed

+35
-19
lines changed

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
6060

6161
public var hasLoweredAddresses: Bool { bridged.hasLoweredAddresses() }
6262

63-
/// The lowered function type in the expansion context of self.
63+
public var loweredFunctionType: CanonicalType {
64+
CanonicalType(bridged: bridged.getLoweredFunctionType())
65+
}
66+
67+
/// The lowered function type, with opaque archetypes erased.
6468
///
65-
/// Always expanding a function type means that the opaque result types
66-
/// have the correct generic signature. For example:
69+
/// For example:
6770
/// @substituted <τ_0_0> () -> @out τ_0_0 for <some P>
6871
/// is lowered to this inside its module:
6972
/// @substituted <τ_0_0> () -> @out τ_0_0 for <ActualResultType>
7073
/// and this outside its module
7174
/// @substituted <τ_0_0> () -> @out τ_0_0 for <some P>
72-
public var loweredFunctionType: CanonicalType {
75+
public var loweredFunctionTypeInContext: CanonicalType {
7376
CanonicalType(bridged: bridged.getLoweredFunctionTypeInContext())
7477
}
7578

@@ -354,7 +357,7 @@ public func != (lhs: Function, rhs: Function) -> Bool { lhs !== rhs }
354357
// Function conventions.
355358
extension Function {
356359
public var convention: FunctionConvention {
357-
FunctionConvention(for: loweredFunctionType, in: self)
360+
FunctionConvention(for: loweredFunctionTypeInContext, in: self)
358361
}
359362

360363
public var argumentConventions: ArgumentConventions {

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ struct BridgedFunction {
486486
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getAccessorName() const;
487487
BRIDGED_INLINE bool hasOwnership() const;
488488
BRIDGED_INLINE bool hasLoweredAddresses() const;
489+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getLoweredFunctionType() const;
489490
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getLoweredFunctionTypeInContext() const;
490491
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const;
491492
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getForwardingSubstitutionMap() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,12 @@ bool BridgedFunction::hasOwnership() const { return getFunction()->hasOwnership(
705705

706706
bool BridgedFunction::hasLoweredAddresses() const { return getFunction()->getModule().useLoweredAddresses(); }
707707

708+
BridgedCanType BridgedFunction::getLoweredFunctionType() const {
709+
return getFunction()->getLoweredFunctionType();
710+
}
711+
708712
BridgedCanType BridgedFunction::getLoweredFunctionTypeInContext() const {
709-
auto expansion = getFunction()->getTypeExpansionContext();
710-
return getFunction()->getLoweredFunctionTypeInContext(expansion);
713+
return getFunction()->getLoweredFunctionTypeInContext();
711714
}
712715

713716
BridgedGenericSignature BridgedFunction::getGenericSignature() const {

include/swift/SIL/SILFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class SILFunction
236236
/// The lowered type of the function.
237237
CanSILFunctionType LoweredType;
238238

239+
CanSILFunctionType LoweredTypeInContext;
240+
239241
/// The context archetypes of the function.
240242
GenericEnvironment *GenericEnv = nullptr;
241243

@@ -572,6 +574,9 @@ class SILFunction
572574
CanSILFunctionType getLoweredFunctionType() const {
573575
return LoweredType;
574576
}
577+
578+
CanSILFunctionType getLoweredFunctionTypeInContext() const;
579+
575580
CanSILFunctionType
576581
getLoweredFunctionTypeInContext(TypeExpansionContext context) const;
577582

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5296,11 +5296,20 @@ TypeExpansionContext::TypeExpansionContext(const SILFunction &f)
52965296
inContext(f.getModule().getAssociatedContext()),
52975297
isContextWholeModule(f.getModule().isWholeModule()) {}
52985298

5299+
CanSILFunctionType SILFunction::getLoweredFunctionTypeInContext() const {
5300+
if (!LoweredTypeInContext) {
5301+
const_cast<SILFunction *>(this)->LoweredTypeInContext
5302+
= getLoweredFunctionTypeInContext(
5303+
getTypeExpansionContext());
5304+
}
5305+
return LoweredTypeInContext;
5306+
}
5307+
52995308
CanSILFunctionType SILFunction::getLoweredFunctionTypeInContext(
53005309
TypeExpansionContext context) const {
53015310
auto origFunTy = getLoweredFunctionType();
53025311
auto &M = getModule();
5303-
auto funTy = M.Types.getLoweredType(origFunTy , context);
5312+
auto funTy = M.Types.getLoweredType(origFunTy, context);
53045313
return cast<SILFunctionType>(funTy.getASTType());
53055314
}
53065315

lib/SILGen/SILGenApply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6499,8 +6499,7 @@ void SILGenFunction::emitYield(SILLocation loc,
64996499
SmallVector<ManagedValue, 4> yieldArgs;
65006500
SmallVector<DelayedArgument, 2> delayedArgs;
65016501

6502-
auto fnType = F.getLoweredFunctionTypeInContext(getTypeExpansionContext())
6503-
->getUnsubstitutedType(SGM.M);
6502+
auto fnType = F.getLoweredFunctionTypeInContext()->getUnsubstitutedType(SGM.M);
65046503
SmallVector<SILParameterInfo, 4> substYieldTys;
65056504
for (auto origYield : fnType->getYields()) {
65066505
substYieldTys.push_back(

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,8 +3734,7 @@ static SILFunction *getOrCreateKeyPathGetter(
37343734
thunk->setGenericEnvironment(genericEnv);
37353735
}
37363736
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
3737-
signature = subSGF.F.getLoweredFunctionTypeInContext(
3738-
subSGF.F.getTypeExpansionContext());
3737+
signature = subSGF.F.getLoweredFunctionTypeInContext();
37393738
auto resultArgTy =
37403739
subSGF.silConv.getSILType(signature->getSingleResult(), signature,
37413740
subSGF.F.getTypeExpansionContext());
@@ -3832,8 +3831,7 @@ static SILFunction *getOrCreateKeyPathSetter(
38323831
thunk->setGenericEnvironment(genericEnv);
38333832
}
38343833
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
3835-
signature = subSGF.F.getLoweredFunctionTypeInContext(
3836-
subSGF.F.getTypeExpansionContext());
3834+
signature = subSGF.F.getLoweredFunctionTypeInContext();
38373835
auto valueArgTy =
38383836
subSGF.silConv.getSILType(signature->getParameters()[0], signature,
38393837
subSGF.getTypeExpansionContext());
@@ -3968,8 +3966,7 @@ static SILFunction *getOrCreateKeyPathAppliedMethod(
39683966
thunk->setGenericEnvironment(genericEnv);
39693967
}
39703968
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
3971-
signature = subSGF.F.getLoweredFunctionTypeInContext(
3972-
subSGF.F.getTypeExpansionContext());
3969+
signature = subSGF.F.getLoweredFunctionTypeInContext();
39733970
auto resultArgTy =
39743971
subSGF.silConv.getSILType(signature->getSingleResult(), signature,
39753972
subSGF.F.getTypeExpansionContext());
@@ -4058,8 +4055,7 @@ static SILFunction *getOrCreateUnappliedKeypathMethod(
40584055
thunk->setGenericEnvironment(genericEnv);
40594056
}
40604057
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
4061-
signature = subSGF.F.getLoweredFunctionTypeInContext(
4062-
subSGF.F.getTypeExpansionContext());
4058+
signature = subSGF.F.getLoweredFunctionTypeInContext();
40634059
auto resultArgTy =
40644060
subSGF.silConv.getSILType(signature->getSingleResult(), signature,
40654061
subSGF.F.getTypeExpansionContext());

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct LoweredParamGenerator {
9292
unsigned numIgnoredTrailingParameters)
9393
: SGF(SGF), fnTy(SGF.F.getLoweredFunctionType()),
9494
parameterTypes(
95-
SGF.F.getLoweredFunctionTypeInContext(SGF.B.getTypeExpansionContext())
95+
SGF.F.getLoweredFunctionTypeInContext()
9696
->getParameters().drop_back(numIgnoredTrailingParameters)) {}
9797

9898
ParamDecl *paramDecl = nullptr;

0 commit comments

Comments
 (0)