Skip to content
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

[SR-14422] UnsafeRawBufferPointer.count can trap #56778

Closed
karwa opened this issue Mar 30, 2021 · 3 comments
Closed

[SR-14422] UnsafeRawBufferPointer.count can trap #56778

karwa opened this issue Mar 30, 2021 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@karwa
Copy link
Contributor

karwa commented Mar 30, 2021

Previous ID SR-14422
Radar rdar://problem/76057108
Original Reporter @karwa
Type Bug

Attachment: Download

Environment

My machine:

Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

macOS 10.15.7 (19H524)

Xcode Version 12.4 (12D4e)

Compiler Explorer:

x86-64 swiftc 5.3 and nightly as of 30.03.2021

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 67240c4fcdc42409fa2d67362fdcbcac

Issue Description:

See schemekind_rawbuffer_count.swift (Compiler explorer link), which contains part of a project I'm working on.

Looking at the generated assembly in Compiler Explorer (at -O), we can see that the "doIt" function checks to see whether the String's code-units are contiguous, and if they are, calls WebURL.SchemeKind.init(buffer: UnsafeRawBufferPointer). Here's what that function looks like:

function signature specialization <Arg[1] = Dead> of output.WebURL.SchemeKind.init(buffer: Swift.UnsafeRawBufferPointer) -> output.WebURL.SchemeKind:
        push    rbp
        mov     rbp, rsp
        mov     al, 6
        test    rdi, rdi
        je      .LBB154_10
        test    rsi, rsi
        je      .LBB154_12
        
...

.LBB154_12:
        ud2

...

Similarly, we can see a number of calls to WebURL.SchemeKind.init(buffer🙂 in the specialisation for String.UTF8View (i.e. when the code-units are not contiguous)

Now, let's change the function so that, rather than an UnsafeRawBufferPointer, it takes separate UnsafeRawPointer and count (Int) parameters. Attached as schemekind_rawptr_splitcount.swift (Compiler Explorer link)

The trap is now gone. Moreover, the function is now inlined - both in "doIt", and the String.UTF8View specialisation. My benchmarks show that this can have a substantial performance benefit.

@karwa
Copy link
Contributor Author

karwa commented Mar 30, 2021

@lorentey, I know you were looking at this recently. Is it possible to remove the trap from URBP.count? I don't see any reason why it should have to trap.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@glessard
Copy link
Contributor

This was fixed in commit b1683c8 (#37424)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants