Skip to content

Commit 452ee89

Browse files
authored
Merge pull request #66915 from slavapestov/rdar111219086
SIL: Pass SubstFlags::PreservePackExpansionLevel in a few places
2 parents e05e3cd + 2146a5a commit 452ee89

File tree

7 files changed

+54
-31
lines changed

7 files changed

+54
-31
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class SubstitutionMap {
168168

169169
/// Query whether any replacement types in the map contain an opened
170170
/// existential.
171-
bool hasOpenedExistential() const;
171+
bool hasLocalArchetypes() const;
172172

173173
/// Query whether any replacement types in the map contain dynamic Self.
174174
bool hasDynamicSelf() const;

include/swift/SIL/SILCloner.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,11 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
192192
// If we have local archetypes to substitute, check whether that's
193193
// relevant to this particular substitution.
194194
if (!LocalArchetypeSubs.empty()) {
195-
for (auto ty : Subs.getReplacementTypes()) {
195+
if (Subs.hasLocalArchetypes()) {
196196
// If we found a type containing a local archetype, substitute
197197
// open existentials throughout the substitution map.
198-
if (ty->hasLocalArchetype()) {
199-
Subs = Subs.subst(QueryTypeSubstitutionMapOrIdentity{
200-
LocalArchetypeSubs},
201-
MakeAbstractConformanceForGenericType());
202-
break;
203-
}
198+
Subs = Subs.subst(QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
199+
MakeAbstractConformanceForGenericType());
204200
}
205201
}
206202

@@ -223,7 +219,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
223219
return Ty.subst(
224220
Builder.getModule(),
225221
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
226-
MakeAbstractConformanceForGenericType());
222+
MakeAbstractConformanceForGenericType(),
223+
CanGenericSignature());
227224
}
228225
SILType getOpType(SILType Ty) {
229226
Ty = getTypeInClonedContext(Ty);

lib/AST/ASTMangler.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,14 @@ std::string ASTMangler::mangleAutoDiffGeneratedDeclaration(
653653
static Type getTypeForDWARFMangling(Type t) {
654654
return t.subst(
655655
[](SubstitutableType *t) -> Type {
656-
if (isa<GenericTypeParamType>(t))
657-
return t->getCanonicalType();
658-
return t;
656+
if (isa<GenericTypeParamType>(t) &&
657+
cast<GenericTypeParamType>(t)->isParameterPack()) {
658+
return PackType::getSingletonPackExpansion(t->getCanonicalType());
659+
}
660+
return t->getCanonicalType();
659661
},
660662
MakeAbstractConformanceForGenericType(),
661-
SubstFlags::AllowLoweredTypes |
662-
SubstFlags::PreservePackExpansionLevel);
663+
SubstFlags::AllowLoweredTypes);
663664
}
664665

665666
std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {

lib/AST/RequirementEnvironment.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ RequirementEnvironment::RequirementEnvironment(
4343
auto concreteType = conformanceDC->getSelfInterfaceType();
4444
auto conformanceSig = conformanceDC->getGenericSignatureOfContext();
4545

46+
auto conformanceToWitnessThunkGenericParamFn = [&](GenericTypeParamType *genericParam)
47+
-> GenericTypeParamType * {
48+
return GenericTypeParamType::get(genericParam->isParameterPack(),
49+
genericParam->getDepth() + (covariantSelf ? 1 : 0),
50+
genericParam->getIndex(), ctx);
51+
};
52+
4653
// This is a substitution function from the generic parameters of the
4754
// conforming type to the witness thunk environment.
4855
//
@@ -53,25 +60,20 @@ RequirementEnvironment::RequirementEnvironment(
5360
// This is a raw function rather than a substitution map because we need to
5461
// keep generic parameters as generic, even if the conformanceSig (the best
5562
// way to create the substitution map) equates them to concrete types.
56-
auto conformanceToWitnessThunkTypeFn = [&](SubstitutableType *type) {
63+
auto conformanceToWitnessThunkTypeFn = [&](SubstitutableType *type) -> Type {
5764
auto *genericParam = cast<GenericTypeParamType>(type);
58-
if (covariantSelf) {
59-
return GenericTypeParamType::get(genericParam->isParameterPack(),
60-
genericParam->getDepth() + 1,
61-
genericParam->getIndex(), ctx);
62-
}
65+
auto t = conformanceToWitnessThunkGenericParamFn(genericParam);
66+
if (t->isParameterPack())
67+
return PackType::getSingletonPackExpansion(t);
6368

64-
return GenericTypeParamType::get(genericParam->isParameterPack(),
65-
genericParam->getDepth(),
66-
genericParam->getIndex(), ctx);
69+
return t;
6770
};
6871
auto conformanceToWitnessThunkConformanceFn =
6972
MakeAbstractConformanceForGenericType();
7073

7174
auto substConcreteType = concreteType.subst(
7275
conformanceToWitnessThunkTypeFn,
73-
conformanceToWitnessThunkConformanceFn,
74-
SubstFlags::PreservePackExpansionLevel);
76+
conformanceToWitnessThunkConformanceFn);
7577

7678
// Calculate the depth at which the requirement's generic parameters
7779
// appear in the witness thunk signature.
@@ -168,9 +170,8 @@ RequirementEnvironment::RequirementEnvironment(
168170
// Now, add all generic parameters from the conforming type.
169171
if (conformanceSig) {
170172
for (auto param : conformanceSig.getGenericParams()) {
171-
auto substParam = Type(param).subst(conformanceToWitnessThunkTypeFn,
172-
conformanceToWitnessThunkConformanceFn);
173-
genericParamTypes.push_back(substParam->castTo<GenericTypeParamType>());
173+
auto substParam = conformanceToWitnessThunkGenericParamFn(param);
174+
genericParamTypes.push_back(substParam);
174175
}
175176
}
176177

lib/AST/SubstitutionMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ bool SubstitutionMap::hasArchetypes() const {
132132
return false;
133133
}
134134

135-
bool SubstitutionMap::hasOpenedExistential() const {
135+
bool SubstitutionMap::hasLocalArchetypes() const {
136136
for (Type replacementTy : getReplacementTypesBuffer()) {
137-
if (replacementTy && replacementTy->hasOpenedExistential())
137+
if (replacementTy && replacementTy->hasLocalArchetype())
138138
return true;
139139
}
140140
return false;

lib/AST/TypeSubstitution.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ QueryTypeSubstitutionMapOrIdentity::operator()(SubstitutableType *type) const {
5555
auto known = substitutions.find(key);
5656
if (known != substitutions.end() && known->second)
5757
return known->second;
58-
58+
59+
if (isa<PackArchetypeType>(type) ||
60+
(isa<GenericTypeParamType>(type) &&
61+
cast<GenericTypeParamType>(type)->isParameterPack())) {
62+
return PackType::getSingletonPackExpansion(type);
63+
}
64+
5965
return type;
6066
}
6167

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -emit-sil -O %s | %FileCheck %s
2+
3+
@_optimize(none)
4+
public func callee<T, each X>(_: T, _: repeat each X) {}
5+
6+
@_transparent
7+
public func caller<each X>(_ x: repeat each X) {
8+
repeat callee(each x, repeat each x)
9+
}
10+
11+
public func outerCaller<each X>(_ x: repeat each X) {
12+
caller(repeat each x)
13+
}
14+
15+
// CHECK-LABEL: sil @$s28variadic_generics_sil_cloner11outerCalleryyxxQpRvzlF : $@convention(thin) <each X> (@pack_guaranteed Pack{repeat each X}) -> () {
16+
// CHECK: [[CALLEE:%.*]] = function_ref @$s28variadic_generics_sil_cloner6calleeyyx_q_q_QptRv_r0_lF : $@convention(thin) <τ_0_0, each τ_0_1> (@in_guaranteed τ_0_0, @pack_guaranteed Pack{repeat each τ_0_1}) -> ()
17+
// CHECK: apply [[CALLEE]]<@pack_element("{{.*}}") each X, Pack{repeat each X}>(
18+
// CHECK: // end sil function '$s28variadic_generics_sil_cloner11outerCalleryyxxQpRvzlF'

0 commit comments

Comments
 (0)