Skip to content

Commit a0d69b9

Browse files
authored
Merge pull request swiftlang#33495 from slavapestov/simplify-archetype-requires-class
Simplify ArchetypeType::requiresClass()
2 parents ba9921b + 2a199e8 commit a0d69b9

File tree

9 files changed

+38
-96
lines changed

9 files changed

+38
-96
lines changed

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,6 +3835,11 @@ CanOpenedArchetypeType OpenedArchetypeType::get(Type existential,
38353835
protos.push_back(proto->getDecl());
38363836

38373837
auto layoutConstraint = layout.getLayoutConstraint();
3838+
if (!layoutConstraint && layout.requiresClass()) {
3839+
layoutConstraint = LayoutConstraint::getLayoutConstraint(
3840+
LayoutConstraintKind::Class);
3841+
}
3842+
38383843
auto layoutSuperclass = layout.getSuperclass();
38393844

38403845
auto arena = AllocationArena::Permanent;

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3780,8 +3780,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37803780
}
37813781

37823782
void visitErrorType(ErrorType *T) {
3783-
if (auto originalType = T->getOriginalType())
3783+
if (auto originalType = T->getOriginalType()) {
3784+
if (Options.PrintInSILBody)
3785+
Printer << "@error_type ";
37843786
visit(originalType);
3787+
}
37853788
else
37863789
Printer << "<<error type>>";
37873790
}

lib/AST/Type.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,14 +3206,8 @@ PrimaryArchetypeType::getNew(const ASTContext &Ctx,
32063206
}
32073207

32083208
bool ArchetypeType::requiresClass() const {
3209-
if (Bits.ArchetypeType.HasSuperclass)
3210-
return true;
32113209
if (auto layout = getLayoutConstraint())
3212-
if (layout->isClass())
3213-
return true;
3214-
for (ProtocolDecl *conformed : getConformsTo())
3215-
if (conformed->requiresClass())
3216-
return true;
3210+
return layout->isClass();
32173211
return false;
32183212
}
32193213

@@ -4952,8 +4946,7 @@ ReferenceCounting TypeBase::getReferenceCounting() {
49524946
auto archetype = cast<ArchetypeType>(type);
49534947
auto layout = archetype->getLayoutConstraint();
49544948
(void)layout;
4955-
assert(archetype->requiresClass() ||
4956-
(layout && layout->isRefCounted()));
4949+
assert(layout && layout->isRefCounted());
49574950
if (auto supertype = archetype->getSuperclass())
49584951
return supertype->getReferenceCounting();
49594952
return ReferenceCounting::Unknown;

lib/IRGen/GenArchetype.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ const TypeInfo *TypeConverter::convertArchetypeType(ArchetypeType *archetype) {
284284

285285
// If the archetype is class-constrained, use a class pointer
286286
// representation.
287-
if (archetype->requiresClass() ||
288-
(layout && layout->isRefCounted())) {
287+
if (layout && layout->isRefCounted()) {
289288
auto refcount = archetype->getReferenceCounting();
290289

291290
llvm::PointerType *reprTy;

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ bool AbstractionPattern::requiresClass() const {
236236
// ObjC generics are always class constrained.
237237
return true;
238238
}
239-
239+
240240
assert(GenericSig &&
241241
"Dependent type in pattern without generic signature?");
242242
return GenericSig->requiresClass(type);
@@ -250,30 +250,23 @@ bool AbstractionPattern::requiresClass() const {
250250
}
251251

252252
LayoutConstraint AbstractionPattern::getLayoutConstraint() const {
253-
// TODO: `ArchetypeType::getLayoutConstraint` and
254-
// `GenericSignature::getLayoutConstraint` don't always propagate implied
255-
// layout constraints from protocol/class constraints. `requiresClass`
256-
// is, for the time being, the only one we really care about, though, and
257-
// it behaves correctly.
258-
if (requiresClass()) {
259-
return LayoutConstraint::getLayoutConstraint(LayoutConstraintKind::Class);
260-
}
261-
return LayoutConstraint();
262-
263-
#if GET_LAYOUT_CONSTRAINT_WORKED_THE_WAY_I_WANT
264253
switch (getKind()) {
265254
case Kind::Opaque:
266255
return LayoutConstraint();
267256
case Kind::Type:
268-
case Kind::Discard: {
257+
case Kind::Discard:
258+
case Kind::ClangType: {
269259
auto type = getType();
270260
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
271-
auto archetypeSig = archetype->getGenericEnvironment()
272-
->getGenericSignature();
273-
return archetypeSig->getLayoutConstraint(archetype->getInterfaceType());
274-
}
275-
else if (isa<DependentMemberType>(type) ||
276-
isa<GenericTypeParamType>(type)) {
261+
return archetype->getLayoutConstraint();
262+
} else if (isa<DependentMemberType>(type) ||
263+
isa<GenericTypeParamType>(type)) {
264+
if (getKind() == Kind::ClangType) {
265+
// ObjC generics are always class constrained.
266+
return LayoutConstraint::getLayoutConstraint(
267+
LayoutConstraintKind::Class);
268+
}
269+
277270
assert(GenericSig &&
278271
"Dependent type in pattern without generic signature?");
279272
return GenericSig->getLayoutConstraint(type);
@@ -283,7 +276,6 @@ LayoutConstraint AbstractionPattern::getLayoutConstraint() const {
283276
default:
284277
return LayoutConstraint();
285278
}
286-
#endif
287279
}
288280

289281
bool AbstractionPattern::matchesTuple(CanTupleType substType) {

lib/SIL/IR/TypeLowering.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,6 @@ namespace {
421421

422422
RetTy visitArchetypeType(CanArchetypeType type,
423423
AbstractionPattern origType) {
424-
if (type->requiresClass()) {
425-
return asImpl().handleReference(type);
426-
}
427-
428424
auto LayoutInfo = type->getLayoutConstraint();
429425
if (LayoutInfo) {
430426
if (LayoutInfo->isFixedSizeTrivial()) {

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,6 @@ bool llvm::DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS,
428428
return false;
429429

430430
// ... and other constraints are equal.
431-
if (LHSArchetypeTy->requiresClass() != RHSArchetypeTy->requiresClass())
432-
return false;
433-
434431
if (LHSArchetypeTy->getSuperclass().getPointer() !=
435432
RHSArchetypeTy->getSuperclass().getPointer())
436433
return false;

lib/SILOptimizer/Utils/PerformanceInlinerUtils.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -622,14 +622,20 @@ static bool isCallerAndCalleeLayoutConstraintsCompatible(FullApplySite AI) {
622622
// The generic parameter has a layout constraint.
623623
// Check that the substitution has the same constraint.
624624
auto AIReplacement = Type(Param).subst(AISubs);
625-
auto AIArchetype = AIReplacement->getAs<ArchetypeType>();
626-
if (!AIArchetype)
627-
return false;
628-
auto AILayout = AIArchetype->getLayoutConstraint();
629-
if (!AILayout)
630-
return false;
631-
if (AILayout != Layout)
632-
return false;
625+
626+
if (Layout->isClass()) {
627+
if (!AIReplacement->satisfiesClassConstraint())
628+
return false;
629+
} else {
630+
auto AIArchetype = AIReplacement->getAs<ArchetypeType>();
631+
if (!AIArchetype)
632+
return false;
633+
auto AILayout = AIArchetype->getLayoutConstraint();
634+
if (!AILayout)
635+
return false;
636+
if (AILayout != Layout)
637+
return false;
638+
}
633639
}
634640
return true;
635641
}

test/SILOptimizer/inline_generics.sil

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,55 +47,6 @@ bb0(%0 : $*T, %1 : $*T):
4747
return %9 : $()
4848
} // end sil function 'testInliningOfGenerics'
4949

50-
sil hidden @P_combine : $@convention(method) <Self where Self : P><T> (@in T, @guaranteed Self) -> @owned @callee_owned (@in T) -> Bool {
51-
bb0(%0 : $*T, %1 : $Self):
52-
// function_ref P.(combine<A> (first : A1) -> (A1) -> Bool).(closure #1)
53-
%4 = function_ref @_TFFE50generic_inlining_partial_apply_opened_existentialsPS_1P7combineurFT5firstqd___Fqd__SbU_Fqd__Sb : $@convention(thin) <τ_0_0 where τ_0_0 : P><τ_1_0> (@in τ_1_0) -> Bool
54-
%5 = partial_apply %4<Self, T>() : $@convention(thin) <τ_0_0 where τ_0_0 : P><τ_1_0> (@in τ_1_0) -> Bool
55-
destroy_addr %0 : $*T
56-
return %5 : $@callee_owned (@in T) -> Bool
57-
} // end sil function 'P_combine'
58-
59-
// P.(combine<A> (first : A1) -> (A1) -> Bool).(closure #1)
60-
sil shared @_TFFE50generic_inlining_partial_apply_opened_existentialsPS_1P7combineurFT5firstqd___Fqd__SbU_Fqd__Sb : $@convention(thin) <Self where Self : P><T> (@in T) -> Bool {
61-
bb0(%0 : $*T):
62-
%2 = integer_literal $Builtin.Int1, -1
63-
%3 = struct $Bool (%2 : $Builtin.Int1)
64-
destroy_addr %0 : $*T
65-
return %3 : $Bool
66-
} // end sil function '_TFFE50generic_inlining_partial_apply_opened_existentialsPS_1P7combineurFT5firstqd___Fqd__SbU_Fqd__Sb'
67-
68-
69-
// Check that P_combine is not inlined into the generic function, because
70-
// doing so would result in a partial_apply instruction, with an opened existential
71-
// in the substitution list. And this cannot be handled by the IRGen yet.
72-
// CHECK-LABEL: sil @dont_inline_callee_with_opened_existential_in_partial_apply_substitution_list
73-
// CHECK: [[FUN_REF:%[0-9]+]] = function_ref @P_combine
74-
// CHECK: apply [[FUN_REF]]
75-
// CHECK: end sil function 'dont_inline_callee_with_opened_existential_in_partial_apply_substitution_list'
76-
sil @dont_inline_callee_with_opened_existential_in_partial_apply_substitution_list : $@convention(thin) <T where T : P> (@owned P) -> @owned @callee_owned (@owned T) -> Bool {
77-
bb0(%0 : $P):
78-
%2 = open_existential_ref %0 : $P to $@opened("41B148C8-F49C-11E6-BE69-A45E60E99281") P
79-
// function_ref P.combine<A> (first : A1) -> (A1) -> Bool
80-
%3 = function_ref @P_combine : $@convention(method) <τ_0_0 where τ_0_0 : P><τ_1_0> (@in τ_1_0, @guaranteed τ_0_0) -> @owned @callee_owned (@in τ_1_0) -> Bool
81-
%4 = open_existential_ref %0 : $P to $@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P
82-
%5 = witness_method $@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P, #P.getSelf : <Self where Self : P> (Self) -> () -> Self, %4 : $@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@guaranteed τ_0_0) -> @owned τ_0_0
83-
%6 = apply %5<@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P>(%4) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@guaranteed τ_0_0) -> @owned τ_0_0
84-
%7 = init_existential_ref %6 : $@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P : $@opened("41B14A4E-F49C-11E6-BE69-A45E60E99281") P, $P
85-
%8 = alloc_stack $P
86-
store %7 to %8 : $*P
87-
%10 = apply %3<@opened("41B148C8-F49C-11E6-BE69-A45E60E99281") P, P>(%8, %2) : $@convention(method) <τ_0_0 where τ_0_0 : P><τ_1_0> (@in τ_1_0, @guaranteed τ_0_0) -> @owned @callee_owned (@in τ_1_0) -> Bool
88-
// function_ref thunk
89-
%11 = function_ref @thunk1 : $@convention(thin) (@owned P, @owned @callee_owned (@in P) -> Bool) -> Bool
90-
%12 = partial_apply %11(%10) : $@convention(thin) (@owned P, @owned @callee_owned (@in P) -> Bool) -> Bool
91-
// function_ref thunk
92-
%13 = function_ref @thunk2 : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@owned τ_0_0, @owned @callee_owned (@owned P) -> Bool) -> Bool
93-
%14 = partial_apply %13<T>(%12) : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@owned τ_0_0, @owned @callee_owned (@owned P) -> Bool) -> Bool
94-
dealloc_stack %8 : $*P
95-
strong_release %0 : $P
96-
return %14 : $@callee_owned (@owned T) -> Bool
97-
} // end sil function 'dont_inline_callee_with_opened_existential_in_partial_apply_substitution_list'
98-
9950
// thunk
10051
sil shared [transparent] [reabstraction_thunk] @thunk1 : $@convention(thin) (@owned P, @owned @callee_owned (@in P) -> Bool) -> Bool {
10152
bb0(%0 : $P, %1 : $@callee_owned (@in P) -> Bool):

0 commit comments

Comments
 (0)