Skip to content

Deterministically order class members for emission #32581

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

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Jun 27, 2020

We already did this for vtable entries, because its required for correctness; sink the ordering down into EmittedMembersRequest in service of reproducible builds as well.

This fixes an issue found by @davidungar where the driver's batching could flip the order of a class's default init() and deinit members, which tripped up our incremental build testing.

Along the way, I discovered that caching the emitted members list broke deriving the Differentiable conformance, because we might cache the member list before synthesizing the members; fixing this by forcing the conformance in EmittedMembersRequest in turn uncovered some order dependencies in conformance derivation, which I already addressed in #32578.

@slavapestov slavapestov requested review from CodaFi and davidungar June 27, 2020 03:16
@@ -2451,16 +2503,40 @@ EmittedMembersRequest::evaluate(Evaluator &evaluator,
forceConformance(Context.getProtocol(KnownProtocolKind::Decodable));
forceConformance(Context.getProtocol(KnownProtocolKind::Encodable));
forceConformance(Context.getProtocol(KnownProtocolKind::Hashable));
forceConformance(Context.getProtocol(KnownProtocolKind::Differentiable));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dan-zheng @rxwei I think this should be uncontroversial. We need to force the derived members, if any, to be synthesized here, because the result of this request is now cached.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me.

Copy link
Contributor

@davidungar davidungar left a comment

Choose a reason for hiding this comment

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

LGTM! You'll see I have teeny nit, but it's probably not worth the time to address it.

@@ -4318,6 +4318,10 @@ GetDestructorRequest::evaluate(Evaluator &evaluator, ClassDecl *CD) const {
if (ctx.LangOpts.EnableObjCInterop)
CD->recordObjCMethod(DD, DD->getObjCSelector());

// Mark it as synthesized to make its location in getEmittedMembers()
// deterministic.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice "why" comment!

Comment on lines +2433 to +2438
if (auto *cd = dyn_cast<ConstructorDecl>(afd))
mangledName = mangler.mangleConstructorEntity(cd, /*allocator=*/false);
else if (auto *dd = dyn_cast<DestructorDecl>(afd))
mangledName = mangler.mangleDestructorEntity(dd, /*deallocating=*/false);
else
mangledName = mangler.mangleEntity(afd);
Copy link
Contributor

Choose a reason for hiding this comment

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

Too bad we don't just have a member function on AbstractFuncDecl to compute this "mangledNameForSorting". I wonder if this functionality is duplicated elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mangleEntity() will assert if you pass in a destructor or constructor. The reason we have separate entry points for those is that usually they have multiple manglings, for the (de)allocating and (de)initializing entry points, respectively. I don't think it's worth factoring this out.

@slavapestov slavapestov force-pushed the cached-emitted-members-request branch from c023ce5 to 141dfd0 Compare June 29, 2020 22:18
@slavapestov
Copy link
Contributor Author

@swift-ci Please test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 141dfd0e81e42fdea10295c3dedfc438759f0784

@slavapestov slavapestov force-pushed the cached-emitted-members-request branch from 141dfd0 to 84ec411 Compare June 29, 2020 22:57
@slavapestov
Copy link
Contributor Author

@swift-ci Please test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 141dfd0e81e42fdea10295c3dedfc438759f0784

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 84ec4117157d13d67e0f5d276662e68ef5001886

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 84ec4117157d13d67e0f5d276662e68ef5001886

We sometimes need to mangle invalid identifiers, for example when sorting
declarations in EmittedMembersRequest. Make sure this doesn't assert.
Previously we did this in the SILVTableVisitor, since maintaining a
consistent order between translation units is important for correctness.

However, we also want the order in which the members themselves are
emitted to remain consistent regardless of the order in which members
were synthesized. This is in service of reproducible builds.
… function order

We can synthesize the default init() and the implicit deinit in a class in
any order, depending on the order of primary files and how that class was
used in other primary files.

Make sure that EmittedMembersRequest puts the destructor at the end with
all other synthesized members, so that we produce the same object file in
any case.
@slavapestov slavapestov force-pushed the cached-emitted-members-request branch from 84ec411 to 89b2f27 Compare June 30, 2020 02:18
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov slavapestov merged commit faa06bf into swiftlang:master Jun 30, 2020
dan-zheng added a commit that referenced this pull request Jun 30, 2020
@compnerd
Copy link
Member

compnerd commented Jul 4, 2020

I think that this change might be the one responsible for a recent regression with @main usage (CC: @natecook1000):

// RUN: %target_swiftc -parse-as-library -parse-stdlib -module-name Swift -S -o - %s
@main
class S {
  public static func main() {
  }
}

The -parse-as-library is required for @main to be usable, -parse-stdlib -module-name Swift is purely to avoid the dependency on the standard library (trying to create a testcase for the future), You will note that there is a call emitted to $ss1SC5$mainyyFZ (on Linux x86_64, it will be callq ($ss1SC5$mainyyFZ)@PLT and on Windows x86_64 it will be callq ($ss1SC5$mainyyFZ)). This method is not being synthesized any longer, resulting in undefined references.

@lxbndr
Copy link
Contributor

lxbndr commented Jul 4, 2020

I have another sample code which not builds:

public class SomeClass {
}
extension SomeClass: Encodable {	
}

Linker tells about unresolved externals:

srx0-0f6207.o : error LNK2019: unresolved external symbol $s4srx09SomeClassC10CodingKeys33_0A88DACEEBAF8928C0B22F7967630AAFLLOs0D3KeyAAMc referenced in function $s4srx09SomeClassC10CodingKeys33_0A88DACEEBAF8928C0B22F7967630AAFLLOAFs0D3KeyAAWl
srx0.dll : fatal error LNK1120: 1 unresolved externals

Noticed on Windows, but I guess it is multiplatform. I am pretty sure it is caused by this change. Previous commit does not fail.

@slavapestov
Copy link
Contributor Author

@compnerd @lxbndr Thanks for the bug report, I'll take a look.

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.

7 participants