-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fold bound checks for static readonly arrays/strings #77593
Conversation
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsCloses #76578 This PR fold static readonly int[] MyArr = { 1, 2, 3, 4, 5 };
static readonly string MyStr = (42).ToString();
int Test1() => MyArr[3];
int Test2() => MyStr.Length; Old codegen: ; Assembly listing for method Program:Test1():int:this
sub rsp, 40
mov rax, 0xD1FFAB1E
mov rax, gword ptr [rax]
cmp dword ptr [rax+08H], 3
jbe SHORT G_M18247_IG04
mov eax, dword ptr [rax+1CH]
add rsp, 40
ret
G_M18247_IG04:
call CORINFO_HELP_RNGCHKFAIL
int3
; Total bytes of code 37
; Assembly listing for method Program:Test2():int:this
mov rax, 0xD1FFAB1E
mov rax, gword ptr [rax]
mov eax, dword ptr [rax+08H]
ret
; Total bytes of code 17 New codegen: ; Assembly listing for method Program:Test1():int:this
mov rax, 0xD1FFAB1E
mov rax, gword ptr [rax]
mov eax, dword ptr [rax+1CH]
ret
; Total bytes of code 17
; Assembly listing for method Program:Test2():int:this
mov eax, 2
ret
; Total bytes of code 6
|
Would like to see the codegen of method Test1 being optimized to just
;) |
Unfortunately it's not legal, array is mutable 😞 |
da5a062
to
f207378
Compare
@dotnet/jit-contrib @jakobbotsch PTAL |
f207378
to
0fe7042
Compare
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
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. The encoding of field sequence in VNF_InvariantNonNullLoad
/VNF_InvariantLoad
still feels a bit unfortunate to me, but seems ok to be pragmatic about it for now, we can always try to clean this up later. @SingleAccretion do you have an opinion on this?
It does look suboptimal; how much more code would using |
@jakobbotsch @SingleAccretion just for my understanding - what exactly we try to avoid by using that?
? |
Yes. Q: what's the advantage: |
Although, actually, thinking a bit more about it, I realize this is not currently possible due to how the addresses are constant handles (and "renumbering" them in invariant load numbering defeats the purpose). So my actual suggestion is to encode the addresses for invariant loads as field sequence VNs ( |
Couldn't we do this in early prop? Or do we somehow get a lot more cases running it later? |
Do you mind if I do it separately, once we get SPMI collection for this and it will be easier to determinate if it's profitable or not to do it in the early prop. This OPT handles |
Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Fine by me. I was mostly curious why you chose VN over early prop, since early prop already does similar things, and the transformation is probably simpler there. |
Co-authored-by: SingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
improvements on arm64 dotnet/perf-autofiling-issues#9676 |
Closes #76578
Closes #42087
This PR folds
obj.Length
into a constant forstatic readonly
initialized arrays/strings - as a side effect - it removes bound checks. Example:Codegen diff:
Jit-diffs (-f -pmi --cctors):
Size regressions are actually improvements, e.g.:
https://www.diffchecker.com/rJTFoUpk (
mov reg, imm
vsmov reg, [reg]
)https://www.diffchecker.com/Rt1IPpnb (idiv is expanded to mul + magic constants)