-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
emit test for bounds checks against a 0 index #42295
emit test for bounds checks against a 0 index #42295
Conversation
Nice!
|
(oops forgot my approves are "green" in this repo - treat it is a "like" 🙂) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you, do you know if we can do something similar for arm64?
cc @dotnet/jit-contrib |
Thanks @nathan-moore for your contribution. |
static int Test(int[] arr) => arr[0]; Current codegen: G_M50965_IG01:
A9BF7BFD stp fp, lr, [sp,#-16]!
910003FD mov fp, sp
;; bbWeight=1 PerfScore 1.50
G_M50965_IG02:
B9400801 ldr w1, [x0,#8]
7100003F cmp w1, #0
54000089 bls G_M50965_IG04
B9401000 ldr w0, [x0,#16]
;; bbWeight=1 PerfScore 7.50
G_M50965_IG03:
A8C17BFD ldp fp, lr, [sp],#16
D65F03C0 ret lr
;; bbWeight=1 PerfScore 2.00
G_M50965_IG04:
97FF2D9A bl CORINFO_HELP_RNGCHKFAIL
D4200000 brk #0
;; bbWeight=0 PerfScore 0.00 this cmp w1, #0
bls G_M50965_IG04 can be optimized to: cbz w1, G_M50965_IG04 ;; or CBNZ branch if (not)zero. |
@sandreenko @EgorBo I looked into what it will take to emit cbz here for arm. There isn't a way to emit it with genJumpToThrowHlpBlk, which is what everything currently uses. You can add a way, but since cbz is internally represented very differently from a normal jump, I don't see a good way of not duplicating the logic, which is what I did here: nathan-moore@8500e53. Does that seem like the right approach? Also, I checked and there's already a peephole for this in the general case. |
Thank you @nathan-moore and @EgorBo for the analysis. I think it is valuable to add this optimization for arm64 if it produces similar diffs there. However, it could be done in a separate PR and this one can be merged now. Thanks @nathan-moore for this optimization. |
Opened a follow-up for arm64: #42514. |
Since array length is always non-negative, when we have a bounds check against a constant 0 index, we can emit a slightly cheaper/smaller test.