Skip to content

Devirtualize: Skip requirements with covariant 'Self' nested inside a collection #39137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion lib/SILOptimizer/Utils/Devirtualize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,48 @@ static bool canDevirtualizeWitnessMethod(ApplySite applySite) {
return false;
}

return true;
// FIXME: devirtualizeWitnessMethod below does not support cases with
// covariant 'Self' nested inside a collection type,
// like '[Self]' or '[* : Self]'.
const Type interfaceTy = wmi->getMember()
.getDecl()
->getInterfaceType()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just getResultInterfaceType()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getResultInterfaceType() is not a member of AbstractFunctionDecl, only FuncDecl

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConstructorDecl and DestructorDecl cannot return a collection of Self though, so FuncDecl is the only relevant case here.

Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to exclude other covariant positions as well, like init(([Self])->Void).

// Skip the 'self' parameter.
->castTo<AnyFunctionType>()
->getResult();

if (!interfaceTy->hasTypeParameter())
return true;

class HasSelfNestedInsideCollection final : public TypeWalker {
unsigned CollectionDepth;

public:
Action walkToTypePre(Type T) override {
if (!T->hasTypeParameter())
return Action::SkipChildren;

if (auto *GP = T->getAs<GenericTypeParamType>()) {
// Only 'Self' will have zero depth in the type of a requirement.
if (GP->getDepth() == 0 && CollectionDepth)
return Action::Stop;
}

if (T->isArray() || T->isDictionary())
++CollectionDepth;

return Action::Continue;
}

Action walkToTypePost(Type T) override {
if (T->isArray() || T->isDictionary())
--CollectionDepth;

return Action::Continue;
}
};

return !interfaceTy.walk(HasSelfNestedInsideCollection());
}

/// In the cases where we can statically determine the function that
Expand Down
27 changes: 27 additions & 0 deletions test/SILOptimizer/devirt_protocol_method_invocations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,30 @@ func testReabstractedWitness(_ f: ReabstractedP) {
public func testReabstracted(f: Optional<()->()>) {
testReabstractedWitness(f)
}


// Test that we don't devirtualize calls to protocol requirements with covariant `Self` nested
// inside a collection type – the devirtualizer does not support handling these yet.
protocol CovariantSelfInsideCollection {
func array() -> Array<Self>
func dictionary() -> Dictionary<String, Self>
func mixed(_: (Array<(Dictionary<String, String>, Self)>) -> Void)
}
// CHECK-LABEL: sil @$s34devirt_protocol_method_invocations12testNoDevirtyyF
//
// CHECK: witness_method $S, #CovariantSelfInsideCollection.array
// CHECK: witness_method $S, #CovariantSelfInsideCollection.dictionary
// CHECK: witness_method $S, #CovariantSelfInsideCollection.mixed
// CHECK: end sil function '$s34devirt_protocol_method_invocations12testNoDevirtyyF'
public func testNoDevirt() {
struct S: CovariantSelfInsideCollection {
func array() -> Array<Self> { fatalError() }
func dictionary() -> Dictionary<String, Self> { fatalError() }
func mixed(_: (Array<(Dictionary<String, String>, Self)>) -> Void) {}
}

let p: CovariantSelfInsideCollection = S()
_ = p.array()
_ = p.dictionary()
p.mixed { _ in }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ Tests.test("Basic") {
expectEqual(3, collection.count)
}

// FIXME: Teach the devirtualizer how to handle calls to requirements with covariant `Self` nested
// inside known-covariant stdlib types such as an array or dictionary.
@_optimize(none)
func convariantSelfErasureTest() {
Tests.test("Covariant 'Self' erasure") {
struct S: P {
static let str = "Success"
func getString() -> String { Self.str }
Expand Down Expand Up @@ -107,7 +104,4 @@ func convariantSelfErasureTest() {
expectEqual(true, S() is P)
}


Tests.test("Covariant 'Self' erasure", convariantSelfErasureTest)

runAllTests()