Skip to content

SE-0333: Expand usability of withMemoryRebound #39529

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 14 commits into from
Feb 5, 2022

Conversation

glessard
Copy link
Contributor

@glessard glessard commented Sep 30, 2021

Implementation of SE-0333, "Expand usability of withMemoryRebound. See proposal here.

Addresses bugs SR-11082, SR-11087, rdar://88087816

@glessard glessard marked this pull request as draft September 30, 2021 21:31
@glessard glessard force-pushed the se-withMemoryRebound branch from 483dcf6 to 712918c Compare October 7, 2021 21:07
@glessard glessard force-pushed the se-withMemoryRebound branch 4 times, most recently from f072d4b to 23fa269 Compare November 17, 2021 22:00
@glessard glessard force-pushed the se-withMemoryRebound branch 2 times, most recently from 7753049 to cc85b89 Compare December 3, 2021 11:09
- updated documentation and adjust implementations
@glessard glessard force-pushed the se-withMemoryRebound branch from cc85b89 to b850838 Compare February 3, 2022 01:16
@glessard glessard changed the title [DNM] Draft implementation for pitch: Expand usability of withMemoryRebound [DNM] Implement SE-0333: Expand usability of withMemoryRebound Feb 3, 2022
@glessard glessard changed the title [DNM] Implement SE-0333: Expand usability of withMemoryRebound [DNM] SE-0333: Expand usability of withMemoryRebound Feb 3, 2022
@glessard glessard force-pushed the se-withMemoryRebound branch from b850838 to 133222d Compare February 3, 2022 15:43
@glessard
Copy link
Contributor Author

glessard commented Feb 3, 2022

@swift-ci please test

@glessard glessard marked this pull request as ready for review February 3, 2022 18:49
@glessard glessard changed the title [DNM] SE-0333: Expand usability of withMemoryRebound SE-0333: Expand usability of withMemoryRebound Feb 3, 2022
@glessard
Copy link
Contributor Author

glessard commented Feb 3, 2022

@swift-ci please test

@glessard glessard force-pushed the se-withMemoryRebound branch from 16cca8c to 5c29236 Compare February 3, 2022 21:03
/// - Returns: The return value, if any, of the `body` closure parameter.
@inlinable // unsafe-performance
@_alwaysEmitIntoClient
% if Mutable:
@_silgen_name("$_swift_UnsafeMutableBufferPointer_se0333_withMemoryRebound")
Copy link
Contributor Author

@glessard glessard Feb 3, 2022

Choose a reason for hiding this comment

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

These getting-out-of-the-way silgen names are a new thing in the stdlib, as far as I can tell.
Is this naming scheme reasonable? Too fussy?
This is likely to be the precedent for other similar actions in the future, so agreement is desirable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that these names are fine; I like that they explicitly point to the proposal for reference.

% else:
@_silgen_name("$sSR17withMemoryRebound2to_qd_0_qd__m_qd_0_SRyqd__GKXEtKr0_lF")
% end
public func _legacy_withMemoryRebound<T, Result>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similarly, the public name for a symbol that being pushed out of the way is a new thing.
Is this naming scheme reasonable? This is likely to be the precedent for other similar actions in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I might explicitly put _legacy_se0333_withMemoryRebound or similar here, but I also don't really want to bikeshed these, because users should never need to worry about 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.

Good idea.

@glessard
Copy link
Contributor Author

glessard commented Feb 3, 2022

Note that the additions to UnsafeRawPointer, UnsafeMutableRawPointer, UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer are @_alwaysEmitIntoClient at the moment. This is debatable; we could decide to make them simply @inlinable, and live with the ABI implications.

% else:
@_silgen_name("$sSR17withMemoryRebound2to_qd_0_qd__m_qd_0_SRyqd__GKXEtKr0_lF")
% end
public func _legacy_withMemoryRebound<T, Result>(
to type: T.Type, _ body: (${Self}<T>) throws -> Result
) rethrows -> Result {
if let base = _position {
Copy link
Contributor Author

@glessard glessard Feb 3, 2022

Choose a reason for hiding this comment

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

This preserves the old implementation verbatim, but we could also forward to the new implementation. Forwarding to the new implementation would be better from a testability standpoint, but less rigourously correct from a compatibility standpoint. However, we are lifting a restriction so the new implementation should be compatible (e.g. it passes the old test). Thoughts?

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 I agree we should forward it! The code looks clearly compatible for well-written code.

There is a minor chance of a binary compatibility issue if someone is calling this with mismatching strides; they might rely on the resulting buffer pointer having the same count as the original. But the original debug precondition seems like a big enough obstacle to prevent that for actually occurring in practice.

@glessard
Copy link
Contributor Author

glessard commented Feb 3, 2022

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Feb 3, 2022

Build failed
Swift Test OS X Platform
Git Sha - 5c29236a8c88e480ffa87b3b36606ed7164fbcbe

@swift-ci
Copy link
Contributor

swift-ci commented Feb 4, 2022

Build failed
Swift Test Linux Platform
Git Sha - 5c29236a8c88e480ffa87b3b36606ed7164fbcbe

@glessard glessard force-pushed the se-withMemoryRebound branch from 3ed0741 to 78a2558 Compare February 4, 2022 01:32
@glessard
Copy link
Contributor Author

glessard commented Feb 4, 2022

@swift-ci please smoke test

@glessard glessard force-pushed the se-withMemoryRebound branch from 78a2558 to 72f4e65 Compare February 4, 2022 03:03
@glessard
Copy link
Contributor Author

glessard commented Feb 4, 2022

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

glessard commented Feb 4, 2022

The output of nm -j <path-to>/libswiftCore.dylib is only one line different after applying this series of changes, and that’s _$ss6UInt32V_A63BtMD. I don’t know where that comes from, but it doesn't register with the ABI checker test.

I believe the ABI checker test is in error when it says that

+Func UnsafeBufferPointer.withMemoryRebound(to:_:) has been removed
+Func UnsafeMutableBufferPointer.withMemoryRebound(to:_:) has been removed
+Func UnsafeMutablePointer.withMemoryRebound(to:capacity:_:) has been removed
+Func UnsafePointer.withMemoryRebound(to:capacity:_:) has been removed

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

This looks good to me!

Having nothing else to complain about, I picked a few ridiculously tiny nits; take them or ignore them as you like. 😉

% else:
@_silgen_name("$sSR17withMemoryRebound2to_qd_0_qd__m_qd_0_SRyqd__GKXEtKr0_lF")
% end
public func _legacy_withMemoryRebound<T, Result>(
to type: T.Type, _ body: (${Self}<T>) throws -> Result
) rethrows -> Result {
if let base = _position {
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 I agree we should forward it! The code looks clearly compatible for well-written code.

There is a minor chance of a binary compatibility issue if someone is calling this with mismatching strides; they might rely on the resulting buffer pointer having the same count as the original. But the original debug precondition seems like a big enough obstacle to prevent that for actually occurring in practice.

return try body(.init(start: nil, count: 0))
}
_debugPrecondition(
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0
Copy link
Member

Choose a reason for hiding this comment

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

(Low prio note) We should probably introduce a utility predicate for this. (s._isAligned(to: T.self), or something like that?)

@glessard
Copy link
Contributor Author

glessard commented Feb 4, 2022

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

glessard commented Feb 5, 2022

The test "api-digester/stability-stdlib-abi-with-asserts.test" says:

+Func UnsafeBufferPointer.withMemoryRebound(to:_:) has been removed
+Func UnsafeMutableBufferPointer.withMemoryRebound(to:_:) has been removed
+Func UnsafeMutablePointer.withMemoryRebound(to:capacity:_:) has been removed
+Func UnsafePointer.withMemoryRebound(to:capacity:_:) has been removed

This is an error on the part of the test.
The output of nm -j <path-to>/libswiftCore.dylib is identical before and after this change (after filtering out local symbols.)

@glessard
Copy link
Contributor Author

glessard commented Feb 5, 2022

@swift-ci please smoke test

@glessard glessard force-pushed the se-withMemoryRebound branch from 2a2bb17 to b2aaaeb Compare February 5, 2022 09:44
@glessard
Copy link
Contributor Author

glessard commented Feb 5, 2022

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

glessard commented Feb 5, 2022

@swift-ci please test macOS platform

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.

4 participants