Skip to content

Commit 0a318a3

Browse files
authored
Merge pull request swiftlang#29644 from jckarter/subst-function-type-shakeout
Enable substituted SIL function types
2 parents 5b046c6 + 3d377fb commit 0a318a3

File tree

122 files changed

+1585
-777
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+1585
-777
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ ERROR(sil_box_expected_r_angle,none,
821821
ERROR(sil_function_subst_expected_l_angle,none,
822822
"expected '<' to begin SIL function type substitution list after 'for'", ())
823823
ERROR(sil_function_subst_expected_r_angle,none,
824-
"expected '>' to begin SIL function type substitution list after 'for'", ())
824+
"expected '>' to end SIL function type substitution list after 'for <...'", ())
825825

826826
// Opaque types
827827
ERROR(opaque_mid_composition,none,

include/swift/AST/Type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class NormalProtocolConformance;
4747
class ProtocolConformanceRef;
4848
class ProtocolDecl;
4949
class ProtocolType;
50+
class SILModule;
5051
class StructDecl;
5152
class SubstitutableType;
5253
class SubstitutionMap;

include/swift/AST/Types.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,14 +892,26 @@ class alignas(1 << TypeAlignInBits) TypeBase {
892892
/// Visit this type and the argument type in parallel, invoking the callback
893893
/// function with each archetype-to-substituted-type binding. The callback
894894
/// may return a new type to substitute into the result type, or return
895-
/// CanType() to error out of the operation.
895+
/// CanType() to error out of the operation. Each invocation of the callback
896+
/// receives three arguments:
897+
/// - The `orig` archetype from a position in `this` type.
898+
/// - The `subst` type in the same structural position of `ty` that is trying to be bound
899+
/// to `orig`.
900+
/// - The `upperBound` archetype, which if set, indicates the minimum set of constraints
901+
/// that any type substituted in this structural position must conform to. May be null,
902+
/// indicating an unconstrained context.
903+
/// - If `upperBound` is set, then the `substConformances` array will contain the
904+
/// protocol conformances for `subst` to each of the protocol requirements
905+
/// on `upperBound` in `getConformsTo` order.
896906
///
897907
/// Returns the substituted type, or a null CanType() if this type
898908
/// is not bindable to the substituted type, or the callback returns
899909
/// CanType().
900910
CanType substituteBindingsTo(Type ty,
901-
llvm::function_ref<CanType(ArchetypeType*, CanType)> substFn);
902-
911+
llvm::function_ref<CanType(ArchetypeType *orig,
912+
CanType subst,
913+
ArchetypeType *upperBound,
914+
ArrayRef<ProtocolConformanceRef> substConformances)> substFn);
903915

904916
/// Determines whether this type is similar to \p other as defined by
905917
/// \p matchOptions.
@@ -1161,6 +1173,8 @@ class alignas(1 << TypeAlignInBits) TypeBase {
11611173
/// object type.
11621174
TypeTraitResult canBeClass();
11631175

1176+
Type replaceSubstitutedSILFunctionTypesWithUnsubstituted(SILModule &M) const; // in SILType.cpp
1177+
11641178
/// Return the tangent space of the given type, if it exists. Otherwise,
11651179
/// return `None`.
11661180
Optional<TangentSpace>
@@ -3962,7 +3976,7 @@ class SILResultInfo {
39623976
}
39633977

39643978
ValueOwnershipKind
3965-
getOwnershipKind(SILFunction &) const; // in SILType.cpp
3979+
getOwnershipKind(SILFunction &, CanSILFunctionType fTy) const; // in SILType.cpp
39663980

39673981
bool operator==(SILResultInfo rhs) const {
39683982
return TypeAndConvention == rhs.TypeAndConvention;

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ namespace swift {
192192
bool BuildRequestDependencyGraph = false;
193193

194194
/// Enable SIL type lowering
195-
bool EnableSubstSILFunctionTypesForFunctionValues = false;
195+
bool EnableSubstSILFunctionTypesForFunctionValues = true;
196196

197197
/// Whether to diagnose an ephemeral to non-ephemeral conversion as an
198198
/// error.

include/swift/SIL/ApplySite.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ class ApplySite {
195195
SILType getSubstCalleeSILType() const {
196196
FOREACH_IMPL_RETURN(getSubstCalleeSILType());
197197
}
198+
void setSubstCalleeType(CanSILFunctionType t) {
199+
FOREACH_IMPL_RETURN(setSubstCalleeType(t));
200+
}
201+
198202
/// Get the conventions of the callee with the applied substitutions.
199203
SILFunctionConventions getSubstCalleeConv() const {
200204
return SILFunctionConventions(getSubstCalleeType(), getModule());

include/swift/SIL/SILInstruction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,11 @@ class ApplyInstBase<Impl, Base, false> : public Base {
19331933
SILType getSubstCalleeSILType() const {
19341934
return SubstCalleeType;
19351935
}
1936+
1937+
void setSubstCalleeType(CanSILFunctionType t) {
1938+
SubstCalleeType = SILType::getPrimitiveObjectType(t);
1939+
}
1940+
19361941
SILFunctionConventions getSubstCalleeConv() const {
19371942
return SILFunctionConventions(getSubstCalleeType(), this->getModule());
19381943
}
@@ -4314,6 +4319,11 @@ class ConvertFunctionInst final
43144319
bool withoutActuallyEscaping() const {
43154320
return SILInstruction::Bits.ConvertFunctionInst.WithoutActuallyEscaping;
43164321
}
4322+
4323+
/// Returns `true` if the function conversion is between types with the same
4324+
/// argument and return types, as well as all other attributes, after substitution,
4325+
/// such as converting `$<A, B> in (A) -> B for <Int, String>` to `(Int) -> String`.
4326+
bool onlyConvertsSubstitutions() const;
43174327
};
43184328

43194329
/// ConvertEscapeToNoEscapeInst - Change the type of a escaping function value

lib/AST/ASTContext.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,17 @@ using AssociativityCacheType =
101101

102102
struct OverrideSignatureKey {
103103
GenericSignature baseMethodSig;
104-
GenericSignature derivedClassSig;
105-
Type superclassTy;
104+
GenericSignature derivedMethodSig;
105+
Type superclassTy, subclassTy;
106106

107107
OverrideSignatureKey(GenericSignature baseMethodSignature,
108-
GenericSignature derivedClassSignature,
109-
Type superclassType)
110-
: baseMethodSig(baseMethodSignature),
111-
derivedClassSig(derivedClassSignature), superclassTy(superclassType) {}
108+
GenericSignature derivedMethodSignature,
109+
Type superclassType,
110+
Type subclassType)
111+
: baseMethodSig(baseMethodSignature),
112+
derivedMethodSig(derivedMethodSignature),
113+
superclassTy(superclassType),
114+
subclassTy(subclassType) {}
112115
};
113116

114117
namespace llvm {
@@ -119,28 +122,32 @@ template <> struct DenseMapInfo<OverrideSignatureKey> {
119122
static bool isEqual(const OverrideSignatureKey lhs,
120123
const OverrideSignatureKey rhs) {
121124
return lhs.baseMethodSig.getPointer() == rhs.baseMethodSig.getPointer() &&
122-
lhs.derivedClassSig.getPointer() == rhs.derivedClassSig.getPointer() &&
123-
lhs.superclassTy.getPointer() == rhs.superclassTy.getPointer();
125+
lhs.derivedMethodSig.getPointer() == rhs.derivedMethodSig.getPointer() &&
126+
lhs.superclassTy.getPointer() == rhs.superclassTy.getPointer() &&
127+
lhs.subclassTy.getPointer() == rhs.subclassTy.getPointer();
124128
}
125129

126130
static inline OverrideSignatureKey getEmptyKey() {
127131
return OverrideSignatureKey(DenseMapInfo<GenericSignature>::getEmptyKey(),
128132
DenseMapInfo<GenericSignature>::getEmptyKey(),
133+
DenseMapInfo<Type>::getEmptyKey(),
129134
DenseMapInfo<Type>::getEmptyKey());
130135
}
131136

132137
static inline OverrideSignatureKey getTombstoneKey() {
133138
return OverrideSignatureKey(
134139
DenseMapInfo<GenericSignature>::getTombstoneKey(),
135140
DenseMapInfo<GenericSignature>::getTombstoneKey(),
141+
DenseMapInfo<Type>::getTombstoneKey(),
136142
DenseMapInfo<Type>::getTombstoneKey());
137143
}
138144

139145
static unsigned getHashValue(const OverrideSignatureKey &Val) {
140146
return hash_combine(
141147
DenseMapInfo<GenericSignature>::getHashValue(Val.baseMethodSig),
142-
DenseMapInfo<GenericSignature>::getHashValue(Val.derivedClassSig),
143-
DenseMapInfo<Type>::getHashValue(Val.superclassTy));
148+
DenseMapInfo<GenericSignature>::getHashValue(Val.derivedMethodSig),
149+
DenseMapInfo<Type>::getHashValue(Val.superclassTy),
150+
DenseMapInfo<Type>::getHashValue(Val.subclassTy));
144151
}
145152
};
146153
} // namespace llvm
@@ -3377,7 +3384,9 @@ CanSILFunctionType SILFunctionType::get(
33773384
assert(coroutineKind == SILCoroutineKind::None || normalResults.empty());
33783385
assert(coroutineKind != SILCoroutineKind::None || yields.empty());
33793386
assert(!ext.isPseudogeneric() || genericSig);
3380-
3387+
3388+
substitutions = substitutions.getCanonical();
3389+
33813390
llvm::FoldingSetNodeID id;
33823391
SILFunctionType::Profile(id, genericSig, ext, coroutineKind, callee, params,
33833392
yields, normalResults, errorResult,
@@ -4493,8 +4502,9 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44934502
unsigned derivedDepth = 0;
44944503

44954504
auto key = OverrideSignatureKey(baseGenericCtx->getGenericSignature(),
4496-
derivedClass->getGenericSignature(),
4497-
derivedClass->getSuperclass());
4505+
derivedGenericCtx->getGenericSignature(),
4506+
derivedClass->getSuperclass(),
4507+
derivedClass->getDeclaredInterfaceType());
44984508

44994509
if (getImpl().overrideSigCache.find(key) !=
45004510
getImpl().overrideSigCache.end()) {

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4545,6 +4545,14 @@ void LayoutConstraintInfo::print(ASTPrinter &Printer,
45454545
break;
45464546
}
45474547
}
4548+
4549+
void LayoutConstraint::dump() const {
4550+
if (!*this) {
4551+
llvm::errs() << "(null)\n";
4552+
return;
4553+
}
4554+
getPointer()->print(llvm::errs());
4555+
}
45484556

45494557
void GenericSignatureImpl::print(raw_ostream &OS, PrintOptions PO) const {
45504558
GenericSignature(const_cast<GenericSignatureImpl *>(this)).print(OS, PO);

0 commit comments

Comments
 (0)