Uh oh!
There was an error while loading. Please reload this page.
Fold bound checks for static readonly arrays/strings - #77593
Conversation
ghost
commented
Oct 28, 2022
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsCloses #76578 This PR fold staticreadonlyint[]MyArr={1,2,3,4,5};staticreadonlystringMyStr=(42).ToString();intTest1()=>MyArr[3];intTest2()=>MyStr.Length;Old codegen: ; Assembly listing for method Program:Test1():int:thissubrsp,40movrax,0xD1FFAB1Emovrax, gword ptr [rax]cmp dword ptr [rax+08H],3jbe SHORT G_M18247_IG04moveax, dword ptr [rax+1CH]addrsp,40retG_M18247_IG04:call CORINFO_HELP_RNGCHKFAILint3; Total bytes of code 37; Assembly listing for method Program:Test2():int:thismovrax,0xD1FFAB1Emovrax, gword ptr [rax]moveax, dword ptr [rax+08H]ret; Total bytes of code 17New codegen: ; Assembly listing for method Program:Test1():int:thismovrax,0xD1FFAB1Emovrax, gword ptr [rax]moveax, dword ptr [rax+1CH]ret; Total bytes of code 17; Assembly listing for method Program:Test2():int:thismoveax,2ret; Total bytes of code 6
|
Would like to see the codegen of method Test1 being optimized to just ;) |
EgorBo
commented
Oct 28, 2022
Unfortunately it's not legal, array is mutable 😞 |
da5a062 to
f207378CompareEgorBo
commented
Oct 28, 2022
@dotnet/jit-contrib @jakobbotsch PTAL |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
f207378 to
0fe7042CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
jakobbotsch
left a comment
There was a problem hiding this comment.
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?
SingleAccretion
commented
Oct 31, 2022
It does look suboptimal; how much more code would using |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
@jakobbotsch@SingleAccretion just for my understanding - what exactly we try to avoid by using that? ? |
SingleAccretion
commented
Oct 31, 2022
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 ( |
Uh oh!
There was an error while loading. Please reload this page.
AndyAyersMS
commented
Oct 31, 2022
Couldn't we do this in early prop? Or do we somehow get a lot more cases running it later? |
Uh oh!
There was an error while loading. Please reload this page.
EgorBo
commented
Oct 31, 2022
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>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
AndyAyersMS
commented
Oct 31, 2022
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>
EgorBo
commented
Nov 10, 2022
improvements on arm64 dotnet/perf-autofiling-issues#9676 |
Closes#76578
Closes#42087
This PR folds
obj.Lengthinto a constant forstatic readonlyinitialized 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, immvsmov reg, [reg])https://www.diffchecker.com/Rt1IPpnb (idiv is expanded to mul + magic constants)