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-15209] Overflow traps not eliminated for simple calculations #57531

Open
karwa opened this issue Sep 17, 2021 · 1 comment
Open

[SR-15209] Overflow traps not eliminated for simple calculations #57531

karwa opened this issue Sep 17, 2021 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@karwa
Copy link
Contributor

karwa commented Sep 17, 2021

Previous ID SR-15209
Radar rdar://problem/83248769
Original Reporter @karwa
Type Bug
Environment

Swift version 5.6-dev (LLVM 70b82c8b83f6294, Swift a66435c)
Target: x86_64-unknown-linux-gnu

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

md5: 9c6a3cc01b75038ff3323416c212d827

Issue Description:

Consider the following:

func test(_ front: inout Int8, _ n: Int8) {
    if front > n, n >= 0 {
      front -= n
    }
}

There is no combination of values which can cause to "front -= n" to fail with overflow. I've used Int8 here because it's simple to test it exhaustively, but the same applies to Int:

 for f in Int8.min...Int8.max {
      for n in Int8.min...Int8.max {
        var front = f
        test(&front, n) // Does not trap
      }
    }

Unfortunately, Swift (latest nightly, -O) is unable to eliminate these simple traps:

output.test(inout Swift.Int8, Swift.Int8) -> ():
        test    sil, sil
        js      .LBB1_4
        mov     al, byte ptr [rdi]
        sub     al, sil
        jle     .LBB1_4
        jo      .LBB1_5
        mov     byte ptr [rdi], al
.LBB1_4:
        ret
.LBB1_5:
        ud2

Godbolt

This ends up having a huge impact on performance, meaning that in most cases, the onus falls on the user to prove overflow is impossible and use non-trapping arithmetic. The compiler should do a better job of automating that.

This is probably more of an LLVM deficiency, but it hits us hard due to the prevalence of signed integers and bounds-checking.

@typesanitizer
Copy link

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants