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

[RyuJIT][ARM] Redundant overflow check in "C / X" #46010

Closed
Tracked by #77010
EgorBo opened this issue Dec 12, 2020 · 5 comments · Fixed by #82924
Closed
Tracked by #77010

[RyuJIT][ARM] Redundant overflow check in "C / X" #46010

EgorBo opened this issue Dec 12, 2020 · 5 comments · Fixed by #82924
Assignees
Labels
arch-arm64 area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI optimization tenet-performance Performance related issue
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Dec 12, 2020

int Test(int a) => 1000 / b; // same for 'C % b'

Current arm64 codegen:

G_M17171_IG01: 
        A9BF7BFD          stp     fp, lr, [sp,#-16]!
        910003FD          mov     fp, sp
G_M17171_IG02:       
        52807D00          mov     w0, #0x3e8
        7100005F          cmp     w2, #0
        54000140          beq     G_M17171_IG06
        3100045F          cmn     w2, #1
        54000081          bne     G_M17171_IG03
        2B00001F          adds    wzr, w0, w0
        54000041          bne     G_M17171_IG03 ; <-- btw, two subsequent jumps?? 
        54000086          bvs     G_M17171_IG05
G_M17171_IG03:       
        1AC20C00          sdiv    w0, w0, w2
G_M17171_IG04:       
        A8C17BFD          ldp     fp, lr, [sp],#16
        D65F03C0          ret     lr
G_M17171_IG05:           
        94000000          bl      CORINFO_HELP_OVERFLOW
G_M17171_IG06:            
        94000000          bl      CORINFO_HELP_THROWDIVZERO
        D43E0000          bkpt
; Total bytes of code 64

As far as I understand overflow can only happen if we divide int.MinValue by -1 so it's not the case for C / x where C is not int.MinValue

Also, GT_ARRLEN can't be -1 and int.MinValue so e.g. array.Length % x or x % array.Length shouldn't produce any overflow checks as well.

category:cq
theme:div-mod-rem
skill-level:intermediate
cost:medium
impact:medium

@EgorBo EgorBo added the tenet-performance Performance related issue label Dec 12, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Dec 12, 2020
@JulieLeeMSFT JulieLeeMSFT added arch-arm64 needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner labels Dec 24, 2020
@JulieLeeMSFT JulieLeeMSFT removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jun 7, 2021
@JulieLeeMSFT JulieLeeMSFT added this to the Future milestone Jun 7, 2021
@echesakov
Copy link
Contributor

Un-assigning myself
cc @BruceForstall

@echesakov echesakov removed their assignment Mar 15, 2022
@TIHan TIHan modified the milestones: Future, 8.0.0 Jun 6, 2022
@TIHan TIHan modified the milestones: 8.0.0, Future Jul 5, 2022
@a74nh
Copy link
Contributor

a74nh commented Aug 11, 2022

@TIHan are you actively looking at this? Otherwise could we assign it to @SwapnilGaikwad

@TIHan
Copy link
Contributor

TIHan commented Aug 13, 2022

I marked it as future, but if you all want to look at it that is fine with me. If you have any questions, I am happy to answer.

@SwapnilGaikwad
Copy link
Contributor

That's great. I'll take a look then.

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Sep 8, 2022
@a74nh
Copy link
Contributor

a74nh commented Sep 23, 2022

As far as I understand overflow can only happen if we divide int.MinValue by -1 so it's not the case for C / x where C is not int.MinValue

This part was fixed by #75272

Also, GT_ARRLEN can't be -1 and int.MinValue so e.g. array.Length % x or x % array.Length shouldn't produce any overflow checks as well.

This part is a much bigger fix. After the CIL has been parsed into IR, the array.Length has been turned into an indirect load, and there is nothing to indicate that it's different to any other indirect load. You would need to add something into the IR to indicate the value is >=0 during parsing.

#64795 is a similar at first glance, but it doesn't really overlap with this.

My thought would be to solve this with a new gtFlag entry. You could have GT_IS_GE_0 and GT_IS_GT_0. Both of these could then be used in other optimisations eg: to get rid of some comparisons.

However, I'm not sure how many other places we can guarantee that a value is >0 or >=0. If it's small, then I doubt the work here is worthwhile.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Mar 3, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 10, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-arm64 area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI optimization tenet-performance Performance related issue
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

8 participants