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-12715] Optimiser fails to elide bounds check that is guaranteed to succeed on constant-size array. #55160

Open
Lukasa opened this issue May 1, 2020 · 1 comment

Comments

@Lukasa
Copy link
Contributor

Lukasa commented May 1, 2020

Previous ID SR-12715
Radar rdar://problem/62894039
Original Reporter @Lukasa
Type Bug
Environment

swift-5.2-RELEASE

Additional Detail from JIRA
Votes 1
Component/s
Labels Bug, Optimizer
Assignee None
Priority Medium

md5: a42e10c38447fad0977a4ea580b205ea

Issue Description:

The following code:

func test(index: Int) -> Int {
    var array = Array(0..<1024)
    return array[index & 1023]
}

Generates this assembly in Swift 5.2 when compiled with -O:

output.test(index: Swift.Int) -> Swift.Int:
        push    rbp
        mov     rbp, rsp
        push    rbx
        push    rax
        mov     rbx, rdi
        mov     esi, 1024
        xor     edi, edi
        call    (generic specialization <Swift.Range<Swift.Int>> of Swift._copyCollectionToContiguousArray<A where A: Swift.Collection>(A) -> Swift.ContiguousArray<A.Element>)
        and     ebx, 1023
        cmp     rbx, qword ptr [rax + 16]
        jae     .LBB1_2
        mov     rbx, qword ptr [rax + 8*rbx + 32]
        mov     rdi, rax
        call    swift_release@PLT
        mov     rax, rbx
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret
.LBB1_2:
        ud2

Due to the outlined call to generic specialization <Swift.Range<Swift.Int>> of Swift._copyCollectionToContiguousArray<A where A: Swift.Collection>(A) -> Swift.ContiguousArray<A.Element> the compiler has failed to observe that [rax + 16] will always be 1024, and so the jae branch will never be taken. It would be good if the compiler could elide the bounds check.

@beccadax
Copy link
Contributor

beccadax commented May 5, 2020

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants