Skip to content

[cxx-interop] Inline constexpr vars. #35311

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
Jan 11, 2021

Conversation

zoecarver
Copy link
Contributor

If a static variable can be evaluated at compile-time, create an accessor using that value. This means static variables will always be inlined and removed.

Note: currently this only works with numeric types.

Resolves SR-14023.

@zoecarver zoecarver added the c++ interop Feature: Interoperability with C++ label Jan 8, 2021
@zoecarver zoecarver requested review from compnerd and hlopko January 8, 2021 18:05
@zoecarver
Copy link
Contributor Author

I suppose I could create a benchmark for this now that we support C++ interop for benchmarks 😉

@zoecarver zoecarver force-pushed the cxx/inline-constexpr-vars branch 2 times, most recently from fc8c9f3 to 161e003 Compare January 8, 2021 19:11
@@ -209,8 +209,8 @@ void ASTScope::
}

void ASTScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
auto *const SF = AFD->getParentSourceFile();
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 think the problem here is that we've never actually had a C++ function with a body before. And because there's no source file associated with a C++ decl context, it makes sense that this might be null.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test macOS.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Windows.

If a static variable can be evaluated at compile time, create an
accessor using that value. This means static variables will always be
inlined and removed.

Note: currently this only works with numeric types.
@zoecarver zoecarver force-pushed the cxx/inline-constexpr-vars branch from 4e099ae to 6bc2696 Compare January 8, 2021 22:38
@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test macOS.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Windows.

@zoecarver
Copy link
Contributor Author

Unrelated issue on Linux.

@swift-ci please smoke test Linux.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Linux.

4 similar comments
@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Linux.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Linux.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Linux.

@zoecarver
Copy link
Contributor Author

@swift-ci please smoke test Linux.

@zoecarver
Copy link
Contributor Author

I'm going to go ahead and merge this shortly.

@zoecarver zoecarver merged commit 8fb106c into swiftlang:main Jan 11, 2021
@@ -209,8 +209,8 @@ void ASTScope::
}

void ASTScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
auto *const SF = AFD->getParentSourceFile();
SF->getScope().expandFunctionBodyImpl(AFD);
if (auto *const SF = AFD->getParentSourceFile())
Copy link
Member

Choose a reason for hiding this comment

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

This should be const auto * rather than auto * const which makes the pointer const. While the pointer is nice to make const, it is not as valuable as the value itself being marked const.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, that doesn't work because getScope is not a const member (and can't be because it lazily creates the scope). So, I'm going to leave this as-is.

@@ -209,8 +209,8 @@ void ASTScope::
}

void ASTScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
auto *const SF = AFD->getParentSourceFile();
SF->getScope().expandFunctionBodyImpl(AFD);
if (auto *const SF = AFD->getParentSourceFile())
Copy link
Member

Choose a reason for hiding this comment

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

I think that this case deserves a comment to explain why/when we expect a source to be missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed in a follow up here: #35364.

zoecarver added a commit to zoecarver/swift that referenced this pull request Jan 12, 2021
Add a comment explaining that C++ function wont have an attached parent
source file. Refs swiftlang#35311.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants