Skip to content

SE-0309: SILOptimizer fixes & reenable executable tests #58513

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 6 commits into from
May 20, 2022

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Apr 29, 2022

Note

The executable test was XFAILed in #39334 due to a crash on builds with optimizations enabled. The issue turned out to be unrelated to the feature and may have had something to do with usage of strings in the test.

…'apply' involves associated types

This optimization rewrites only the 'self' argument, and does not know how to
substitute types in the users of the given apply instruction in case the
underlying protocol method returns a `Self`-dependent type. With SE-0309 in
motion, the bail-out logic must be generalized to `Self`-rooted type parameters.
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test linux

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test Windows

lib/AST/Type.cpp Outdated
@@ -54,6 +54,12 @@ using namespace swift;
#include "swift/AST/TypeNodes.def"

Type QueryTypeSubstitutionMap::operator()(SubstitutableType *type) const {
if (auto *archetype = dyn_cast<ArchetypeType>(type)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary? When do non-root archetypes appear in the map?

Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Apr 29, 2022

Choose a reason for hiding this comment

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

They don't, well, hopefully. It's Type::subst that queries the map with non-root archetypes, and it probably has been since the dinosaurs. Do you reckon we should fix that instead?

SubstitutionMap::lookupSubstitution has this early return as well btw.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove this part then, or change it to an assertion.

Type::subst() passing in non-root archetypes is something that I believe only associated type inference relies on. You can try taking it out and see what breaks.

Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Apr 29, 2022

Choose a reason for hiding this comment

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

I still need this tweak, otherwise QueryTypeSubstitutionMapOrIdentity won't work with non-root opened archetypes, and that makes the SIL cloner crash. I can use a custom substitution fn instead, WDYT?

P.S. This is about getASTTypeInClonedContext.

Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Apr 29, 2022

Choose a reason for hiding this comment

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

Would it not be safer to leave this part as is and add a FIXME, rather than propagate the check to clients? Associated type inference itself does not appear to be using these function objects.

@slavapestov
Copy link
Contributor

CC @atrick

@slavapestov
Copy link
Contributor

@AnthonyLatsis I was actually looking at this disabled test the other day and when I reduced the failure it did not involve non-root opened archetypes. What I had was this:

protocol P {
  var covariantSelfPropClosure: ((Self) -> Void) -> Void { get }
}
extension P {
  var covariantSelfPropClosure: ((Self) -> Void) -> Void { { $0(self) } }
}

struct S: P {}

let p: P = S()

p.covariantSelfPropClosure { _ in }

It was crashing because IRGen was trying to allocate a zero-sized heap context for the closure. Is this a different bug?

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Apr 29, 2022

It was crashing because IRGen was trying to allocate a zero-sized heap context for the closure. Is this a different bug?

Interesting, I will take a look in a minute. For me it stopped crashing the moment I switched from strings to integers, which made me think whatever issue was unrelated.

when I reduced the failure it did not involve non-root opened archetypes

The issues with non-root archetypes were revealed after adding more tests for methods with covariant associated types.

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Apr 29, 2022

@slavapestov How do I reproduce the crash? It compiles fine on main if I pass -emit-ir -O (should have figured it was a runtime crash).

@AnthonyLatsis
Copy link
Collaborator Author

@slavapestov It is a different issue. How did you manage to dig out the root cause?

@AnthonyLatsis
Copy link
Collaborator Author

Simplified the crasher a bit more:

protocol P {
  var prop: () -> Self { get }
}
extension P {
  var prop: () -> Self {
    { self }
  }
}

struct S: P {}
let p: P = S()

let y = p.prop()

@slavapestov
Copy link
Contributor

It's an IRGen issue. @drexin is working on a fix: #58532

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Apr 29, 2022

Thanks for the link. I definitely lack domain knowledge to fully understand why this happens with this specific closure and not others, but I have a rough idea of what's going on now. Interestingly, the crash won't manifest if Self is replaced with something else, regardless of whether self is captured by the closure.

@gottesmm
Copy link
Contributor

gottesmm commented May 2, 2022

Can you be sure to push this through full testing rather than smoke testing?

…tutionMapOrIdentity`

This makes `SILCloner::getASTTypeInClonedContext` and `getTypeInClonedContext`,
which use `QueryTypeSubstitutionMapOrIdentity`, work for nested opened archetypes
and consequently fixes inlining of instructions involving these archetypes.
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test

@AnthonyLatsis AnthonyLatsis requested a review from atrick May 10, 2022 21:18
@AnthonyLatsis
Copy link
Collaborator Author

@slavapestov is there anyone else we need an approval from?

@slavapestov
Copy link
Contributor

I think it's fine, but let's re-run tests since it's been more than 2 weeks now.

@slavapestov
Copy link
Contributor

@swift-ci Please test

@AnthonyLatsis
Copy link
Collaborator Author

I think it's fine, but let's re-run tests since it's been more than 2 weeks now.

May I ask you to sign it off then?

@slavapestov slavapestov merged commit ac74b84 into swiftlang:main May 20, 2022
@AnthonyLatsis
Copy link
Collaborator Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants