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 eliding a fixed statement if the thing being pinned is a local #40553
C# disallows taking the address of an already fixed expression. This means that logic such as the following will result in CS0213:
intx=0;
fixed (int*px=&x){}
This normally means that you can't write code which unnecessarily pins data. However, the JIT will also inline methods and so it is possible to have something like the following:
In this code, M1 is being aggressively inlined and that means it will be pinning the local x, which is "already fixed" and therefore does not need to be pinned. However, the JIT does not currently elide this case and it still emits the logic for performing the pin. In an "ideal" scenario, it would see that the reference being pinned is to a local and allow it to be elided allowing improved code generation.
In particular, I believe this to be a common pattern for P/Invoke like operations. In particular, it isn't uncommon to see code such as:
Reacted by Sergio Pedri, Marcel W., Jeremy Koritzinsky, Clinton Ingram, SteveKCheng, Alexandre Mutel, James Jackson-South, aL3891, David Maas, Steve and 5 more
@EgorBo Is that type of allocation observed by the JIT here? Seem like the GC.AllocateArray<T>() would need to be special cased in the JIT to know the allocation was special.
does it make sense to also elide it here?
The offline question I had for @tannergooding about this feature is the savings relative to the added complexity. I can see the simple cases like the QueryPerformanceCounter scenario above being possible and I think I have the conceptual understanding to accept it probably isn't too costly. Extending this to special case the results of specific function calls seems rather dubious in the cost/benefit calculus, but would welcome being be educated that it isn't difficult.
I think understanding the potential savings would be a benefit to this issue.
@EgorBo, my understanding is that it is more expensive to check if an arbitrary ref is already pinned than it is do just perform the pin, thus we could realistically only do it for cases like ref T where the ref is to a known local. I believe this would have direct benefit to the inlining scenario, especially around P/Invokes (possibly around the existing pattern and definitely around the new recommended pattern using source generators/automated tooling).
In the case of the GC.AllocateArray, I would imagine it is likely better to just use Unsafe.AsPointer as I wouldn't expect the pattern to be nearly as common nor as easy to detect.
(We already have some optimizations around ref T where T is a local; eliding a pin is just not one of them).
Activity
Dotnet-GitSync-Bot commentedon Aug 7, 2020
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
tannergooding commentedon Aug 7, 2020
Ideally,
T1andT2would generate identical code: https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxNMAfAAgBgARYCMAdAEoCuAdjAJZhzEDCA9mAA40A2iAyogG40AxnCgBuALAAoLAGY8VKBABmcAgCY8jPAG9peAwXlEAbHhrU8AFUIAKAJS79hlxZh4AHngC8eAKySUi4uWADseACydghwyp72gS4Avs4GqXgA2hFwMAAWzAAmAJLsnLbZeYUlbJwA8my0zJRQxACCAObtMVBQNPxwRZScFhbt9gC66XIEhGZukdGx5pYejnpBwQbKNB5wBXi2bgBUeGwAGj54AGSr6S7rm5theEfniY8pGwafLlPGs8t3FZ1A4nF9gvMvL4AndDM8IiCbgl0j9DOksjl8sVSuVMVVSvVGs02p1ur1+oNhpRRhM/jM5pYEYdqCdzmtYQZnq8zu9vtIkkA=AaronRobinsonMSFT commentedon Aug 7, 2020
@AndyAyersMS and @BruceForstall I think this is a codegen issue, but could also see it being something the interop team needs to do to help.
AndyAyersMS commentedon Aug 8, 2020
No, it is codegen. We should be able to tell if a pinned local resolves to the address of some other local.
EgorBo commentedon Aug 8, 2020
does it make sense to also elide it here?
AaronRobinsonMSFT commentedon Aug 8, 2020
@EgorBo Is that type of allocation observed by the JIT here? Seem like the
GC.AllocateArray<T>()would need to be special cased in the JIT to know the allocation was special.The offline question I had for @tannergooding about this feature is the savings relative to the added complexity. I can see the simple cases like the
QueryPerformanceCounterscenario above being possible and I think I have the conceptual understanding to accept it probably isn't too costly. Extending this to special case the results of specific function calls seems rather dubious in the cost/benefit calculus, but would welcome being be educated that it isn't difficult.I think understanding the potential savings would be a benefit to this issue.
tannergooding commentedon Aug 8, 2020
@EgorBo, my understanding is that it is more expensive to check if an arbitrary
refis already pinned than it is do just perform the pin, thus we could realistically only do it for cases likeref Twhere the ref is to a known local. I believe this would have direct benefit to the inlining scenario, especially around P/Invokes (possibly around the existing pattern and definitely around the new recommended pattern using source generators/automated tooling).In the case of the GC.AllocateArray, I would imagine it is likely better to just use
Unsafe.AsPointeras I wouldn't expect the pattern to be nearly as common nor as easy to detect.(We already have some optimizations around
ref TwhereTis a local; eliding a pin is just not one of them).Unpin locals (#70264)
Unpin locals (dotnet#70264)
Unpin locals, attempt 2 (#70655)
Sfh fix tests (#8)
AndyAyersMS commentedon Sep 19, 2022
I believe this is fixed, but somebody should verify.
aromaa commentedon Jan 20, 2023
This is fixed for the examples above but there's still more to do to make this light up for
Span<T>or structs with ref fields.For example, the following is not optimized:
Or replace the usage of
Span<T>withPointer:For
Pointerwe have simpler IR. The V04 is the pinned local:But for
Span<T>we have a bit more complex IR. The V04 is the pinned local: