Skip to content

[sil-serializer] Do not use RPOT order for serializing SIL basic blocks #3546

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
Jul 17, 2016

Conversation

swiftix
Copy link
Contributor

@swiftix swiftix commented Jul 16, 2016

What's in this pull request?

Resolved bug number: (SR-)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

My earlier patch started serializing SIL basic blocks using the RPOT order. While it works, changing the existing order of BBs during the serialization may be very surprising for users. After all, serialization is not supposed to transform the code.

Therefore, this patch follows a different approach. It uses the existing order of BBs during the serialization. When it deserializes SIL and detects a use of an opened archetype before its definition, it basically introduced a forward definition of this opened archetype. Later on, when the actual definition of the opened archetype is found, it replaces the forward definition. There is a correctness check at the end of a SIL function deserialization, which verifies that there are no forward definitions of opened archetypes left unresoved.

@swiftix
Copy link
Contributor Author

swiftix commented Jul 16, 2016

This is a re-worked version of SIL basic block serialization bits from #3456. It does not silently changes the order of BBs during the serialization anymore.

@swiftix
Copy link
Contributor Author

swiftix commented Jul 16, 2016

@swift-ci Please test

@swiftix
Copy link
Contributor Author

swiftix commented Jul 16, 2016

@gottesmm This is the alternative solution I was talking about. Do you like it better? :-)

swiftix referenced this pull request Jul 16, 2016
Serialize SIL basic blocks in the RPOT order to make sure that instructions defining open archetypes are serialized before instructions using those opened archetypes.
@swiftix
Copy link
Contributor Author

swiftix commented Jul 16, 2016

Essentially, the patch does the following:

  • All MF->getType(TypeID) calls are replaced by the call of a new wrapper getType(Builder, MF, TypeID). This is the reason why you see so many small changes in DeserializeSIL.cpp
  • The getType wrapper first does MF->getType(TypeID), but then checks if it has an opened archetype whose definition was not seen yet. If this is the case, it creates a placeholder SILValue representing a forward definition.
  • Later on, when the actual definition is seen, it replaces the original placeholder
  • At the end of deserialization, there is a check that all forward definitions are resolved, i.e. all placeholders are gone.

Type SILDeserializer::getType(SILBuilder &Builder, ModuleFile *MF,
serialization::TypeID TID) {
auto Ty = MF->getType(TID);
if (Ty && Ty->hasOpenedExistential()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not invert this condition and early exit. It eliminates a level of indentation.

@swiftix swiftix force-pushed the cse-of-open-existentials branch from 8690c4c to e4dd8d0 Compare July 16, 2016 18:41
llvm_unreachable(
"All forward definitions of opened archetypes should be resolved");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What about:

if (std::none_of(OpenedArchetypesTracker.getOpenedArchetypeDefs()),
[](KVTy &KV) -> Bool { return !KV.getSecond() || isa(KV.getSecond())) {
llvm_unreachable("All forward definitions of opened archetypes should be resolved");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Closures are cool, but I'm not sure it makes the code more understandable in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then at least move it into a helper function. In general, these sorts of predicate loops are not LLVM style:

http://llvm.org/docs/CodingStandards.html#turn-predicate-loops-into-predicate-functions

@swiftix swiftix force-pushed the cse-of-open-existentials branch 3 times, most recently from b5261ad to 1be4db0 Compare July 17, 2016 04:30
@swiftix
Copy link
Contributor Author

swiftix commented Jul 17, 2016

@swift-ci Please smoke test

My earlier patch started serializing SIL basic blocks using the RPOT order. While it works, changing the existing order of BBs during the serialization may be very surprising for users. After all, serialization is not supposed to transform the code.

Therefore, this patch follows a different approach. It uses the existing order of BBs during the serialization. When it deserializes SIL and detects a use of an opened archetype before its definition, it basically introduced a forward definition of this opened archetype. Later on, when the actual definition of the opened archetype is found, it replaces the forward definition. There is a correctness check at the end of a SIL function deserialization, which verifies that there are no forward definitions of opened archetypes left unresoved.
@swiftix swiftix force-pushed the cse-of-open-existentials branch from 1be4db0 to 2f9ee20 Compare July 17, 2016 04:53
@swiftix
Copy link
Contributor Author

swiftix commented Jul 17, 2016

@swift-ci Please smoke test

@gottesmm
Copy link
Contributor

Looks beautiful!

@swiftix
Copy link
Contributor Author

swiftix commented Jul 17, 2016

The OS X smoke test failure seems to be unrelated. I cannot reproduce it locally.

@swiftix swiftix merged commit 9806161 into swiftlang:master Jul 17, 2016
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.

2 participants