You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Difference in codegen of string.IsNullOrEmpty() #63116
I was playing with JIT a little bit recently, and found the below scenario where I am checking does give string is null or empty with two different styles,
With raw null and length check.
and with string.IsNullOrEmpty() built-in method, which does the same raw null and length check behind the scenes.
But the collagen is a little different for both cases!
For given a program below (corresponding sharplab link)
As we can see above, codegen for string.IsNullOrEmpty(string) is quite optimized, so my question is can we do the same optimization for the raw null check call as well (OR maybe fold that raw call with the built-in method by the compiler), given the fact that the built-in also does the same behind the scenes, but still codegen is quite different!
Activity
ghost added
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Ah, I missed that part.
Yes, it's a known problem in jit - it needs "Forward Substitution" feature to handle cases like this one. Honestly, I thought Roslyn always propagated ldstr by value 🤔
I created a new version of the same IsNullOrEmpty() with the same implementation and with aggressive inline, but still codegen is not optimized, not sure why JIT cannot inline that method as well!
Activity
huoyaoyuan commentedon Dec 24, 2021
See #63095 and #4207
ShreyasJejurkar commentedon Dec 24, 2021
I can see your PR linked to the issue, will that PR will fix this issue!?
I saw some regression in diff!
EgorBo commentedon Dec 24, 2021
@ShreyasJejurkar in your
Tmethod jit sees thatsomeStringis a string literal which is never null so it folded everythingShreyasJejurkar commentedon Dec 24, 2021
Thanks @EgorBo for the reply! Won't JIT see same thing for method
Mbecause it is the same thing there as well and can fold?EgorBo commentedon Dec 24, 2021
Ah, I missed that part.
Yes, it's a known problem in jit - it needs "Forward Substitution" feature to handle cases like this one. Honestly, I thought Roslyn always propagated ldstr by value 🤔
ShreyasJejurkar commentedon Dec 24, 2021
Even more what got me into thinking that, the
string.IsNullOrEmpty()under the hood implementation is the same as methodM, but still it did not fold! https://source.dot.net/#System.Private.CoreLib/String.cs,486EgorBo commentedon Dec 24, 2021
It's because JIT actually supports "Forward substitution" but only when it inlines functions.
ShreyasJejurkar commentedon Dec 24, 2021
I created a new version of the same
IsNullOrEmpty()with the same implementation and with aggressive inline, but still codegen is not optimized, not sure why JIT cannot inline that method as well!SharpLab link -> https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEmuAdAEoCuMCUYAplQMID2YADlADYsATgGVhANygBjFigDcRTAGYK2MuzIBvImV1kde5RQAsZALIAKAJRaDeveIhCyKbixEIhsAOZkAvGQARIEKhPb2duFQAGYWAJIoAHJ0/PwA8kIAorwIAJ4AasIWrqweXjDeVjbaYeF1XDCuglQA6l4ILBaBABYsqZyBVqF1ugC+keO1upEA2mYsCN2cACZxvPwW84srazzpPEycjVQAgt7eQrIoUOIscTD8sD5WALqRRsCcnPxkCcmpGWyBwKRUo5BQ1Ui9kwAHYXP4AjAUj90OgXFQADIsCqLBFkEjDPSTCJTfSkoyYUwAFWstlJ0Nw4LcZR8/iCIShek5uhiFjBVD+yMBOVyxWZnmekPpIwaTTYbSgHS6vX6g0JdWJRKIoyAA===
ShreyasJejurkar commentedon Dec 24, 2021
I saw this issue about "Forward Substitution", but it's dead I guess! #6973
EgorBo commentedon Dec 24, 2021
@ShreyasJejurkar it's Roslyn - it decided to save your "" into a temp. Make your
IsNullOrEmptyVerstatic.but when you make it static:
ShreyasJejurkar commentedon Dec 24, 2021
Thanks @EgorBo , it did. SharpLab link
EgorBo commentedon Dec 24, 2021
NOTE: I am not blaming Roslyn here 🙂 - it's a correct IL and it's jit's fault that it doesn't do forward sub.
ShreyasJejurkar commentedon Dec 24, 2021
@EgorBo ahh, it's ok. No one to blame here, just an opportunity.
Are there any plans to address this kind of issue!?
EgorBo commentedon Dec 24, 2021
Soon or later 🙂
JIT: simple forward substitution pass (#63720)
EgorBo commentedon Dec 3, 2022
Closing since it has expected codegen now (apparently, via Fsub)