Skip to content

[embedded] Add support for (non-generic) classes in embedded Swift #68434

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 2 commits into from
Sep 12, 2023

Conversation

kubamracek
Copy link
Contributor

Add support for (non-generic) classes in embedded Swift

  • In embedded Swift, classes get a simplified metadata: Basically just a vtable + destructor + superclass pointer.
  • Only non-resilient (intended as permanent restriction), non-generic classes (for now) supported.
  • Relax the check that prohibits metadata emission and usage to allow classes.

- In embedded Swift, classes get a simplified metadata: Basically just a vtable + destructor + superclass pointer.
- Only non-resilient (intended as permanent restriction), non-generic classes (for now) supported.
- Relax the check that prohibits metadata emission and usage to allow classes.
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek added the embedded Embedded Swift label Sep 11, 2023
asImpl().noteStartOfImmediateMembers(theClass);

// Add vtable entries.
asImpl().addVTableEntries(theClass);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want the full vtable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be more lazy and just emit the methods that are used? Or are we assuming that lto will strip them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm actually assuming that SILOptimizer (namely PruneVTables and DeadFunctionElimination) will take care of that, because we can internalize public declarations in embedded Swift.

void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
const ClassLayout &fragileLayout) {
assert(!classDecl->isForeign());
PrettyStackTraceDecl stackTraceRAII("emitting metadata for", classDecl);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put this above the assertion? If the assertion fires, might be nice to have this diagnostic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@@ -0,0 +1,32 @@
// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Test for calling methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added!

// RUN: %target-swift-emit-ir %s -parse-stdlib -enable-experimental-feature Embedded -target arm64e-apple-none | %FileCheck %s

// TODO: investigate why windows is generating more metadata.
// XFAIL: OS=windows-msvc
Copy link
Contributor

Choose a reason for hiding this comment

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

No investigation needed. This is because swift compiler sources doesn't run on windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed!

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

This is so awesome and exciting to see!!

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek merged commit dc6d784 into swiftlang:main Sep 12, 2023
@@ -4967,6 +4973,36 @@ void irgen::emitClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
}
}

void irgen::emitEmbeddedClassMetadata(IRGenModule &IGM, ClassDecl *classDecl,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need class embedded class metadata at all?
The vtable + pointer to superclass should provide everything which is needed for method dispatch and conditional down casting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, and that's what this function is trying to do (to emit a simple metadata record with a vtable + superclass pointer), I think the only extra leftover is metadataBuilder.createMetadataAccessFunction(); which I'm removing in #68461

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

Successfully merging this pull request may close these issues.

3 participants