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
Consider detecting and optimizing common range check patterns #13347
changed the title [-]Consider detecting and optimizing common range check paterns[/-][+]Consider detecting and optimizing common range check patterns[/+]on Jun 21, 2017
If you have gaps in the range but the total span can fit in a mask for the integer type
And of course, it works well if it fits in a long and you're running on 64 bit. The trouble with doing IL optimizations is that you can't be sure what CPU the code will end up running on.
It's interesting that gcc does this no matter how you write it,
Yes, and all other C/C++ compilers. These optimizations (range check, the sparse range check mentioned by @sharwell and some forms of switches like the one in your example) work together. The compiler is supposed to recognize all these is var x in set of constants/ranges patterns and generate efficient code.
We are considering adding a range pattern, e.g. (num is '0'..'9'). If we do that, we would generate "optimized" code for the range pattern from the start.
Reacted by Nobuyuki Iwanaga, Ganbarukamo41, Huo Yaoyuan and Viacheslav Ivanov
MSVC can emit a bit test x86 instruction for this. That would be available to the JIT or as an intrinsic to the C# compiler (assuming that it is OK to take a dependency on something like that).
Activity
[-]Consider detecting and optimizing common range check paterns[/-][+]Consider detecting and optimizing common range check patterns[/+]jkotas commentedon Jun 21, 2017
Or do this in the dedicated IL optimizer: dotnet/roslyn#15929
sharwell commentedon Jun 21, 2017
If you have gaps in the range but the total span can fit in a mask for the integer type, you can use this (showing the case for [0-46-9]):
We used this in antlr/antlr4#89.
mikedn commentedon Jun 22, 2017
And of course, it works well if it fits in a long and you're running on 64 bit. The trouble with doing IL optimizations is that you can't be sure what CPU the code will end up running on.
alrz commentedon Jun 24, 2017
Is this really going to be in the compiler or we should regex the heck out of the source?
mikedn commentedon Jun 24, 2017
LOL. IMO it should be in the JIT compiler.
alrz commentedon Jun 25, 2017
It's interesting that gcc does this no matter how you write it,
try on https://godbolt.org.
mikedn commentedon Jun 25, 2017
Yes, and all other C/C++ compilers. These optimizations (range check, the sparse range check mentioned by @sharwell and some forms of switches like the one in your example) work together. The compiler is supposed to recognize all these
is var x in set of constants/rangespatterns and generate efficient code.gafter commentedon Nov 19, 2017
We are considering adding a range pattern, e.g.
(num is '0'..'9'). If we do that, we would generate "optimized" code for the range pattern from the start.GSPP commentedon Aug 30, 2019
MSVC can emit a bit test x86 instruction for this. That would be available to the JIT or as an intrinsic to the C# compiler (assuming that it is OK to take a dependency on something like that).
mikedn commentedon Aug 30, 2019
FWIW the JIT already emits BT for certain switch instructions:
generates:
gafter commentedon Aug 31, 2019
Moving to the coreclr so it can be done for all languages.
mikedn commentedon Sep 2, 2019
Duplicate of #8418
BruceForstall commentedon Sep 4, 2019
Closing as dup
EgorBo commentedon Apr 4, 2020
@gafter @BruceForstall actually it makes sense to do in Roslyn too
RyuJIT imports it as 3 basic-blocks:
And if you optimize it in roslyn it will be a single-bb during RyuJIT import phase, e.g.:
RyuJIT's inliner simply gives up if a method has more than 5 basic-blocks.
Just check this example: EgorBo@b7f15b7
Also it'd make IL code (= dll size) smaller as a bonus.