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
Superflous store generated by JIT for embedded ReadOnlySpan<byte> #13190
That's how fixed works. The fixed pointer needs to be stored in a local variable that's reported to the GC such that it knows not to move the object. If you look later in the assembly code you'll also see that 0 gets stored to that variable, that's the end of the fixed block.
Ideally the JIT would figure out that in this very particular case the fixed machinery isn't needed and eliminate it. That's likely not going to be easy though.
@mikedn I do understand that, and I understand how this is a very particular case.
I just think that adding this ROS optimization into the JIT + Compiler was a lot of work for generating super efficient yet clean looking code, but I can't get the best outcome with x86 intrinsics, given that they mostly accept pointers and nothing else.
I think it could either be solved by detecting that bp is never written to again, and this is effectively a constant, but I'm way out of my depth here.
I can lift the ptr into a general purpose register (as I've done in my real code) and use that variable / register, it just seems like someone should be able to connect the dots here (e.g. the JIT)....
Can we detect this with a peephole optimization to propagate the constant address?
Unlikely. Normally the only way to eliminate a store is to prove that it's dead and that's not something peephole optimizations can do (well, except trivial cases such 2 consecutive stores to the same location). The fact that in this case a constant gets stored into a pinned variable is not sufficient.
And then the problem is that liveness analysis ignores pinned variables due the dependency between such variables and variables that may contain pointers derived from the original fixed pointer. That is, it's not sufficient to prove that the pinned variable is dead, you have to prove that all other variables containing derived pointers are dead too.
If one presumes that the ROS optimization is performed
Indeed, that's an interesting aspect of this issue. There are alternatives to fixed but only under the assumption that the C# compiler performs the optimization.
Well, there's also the possibility to not use lddqu (which AFAIR is kind of pointless on current CPUs) and instead just load the vector by casting the span ref to a ref Vector256<int>. But that's probably a different matter.
It might not be that hard to realize we're pinning an RVA static and just remove the pinned bit from the tracking local (since these tracking locals are single def we only need to check when we see the first store to the pinned local). That would let dead stores clean things up.
Well that's how C# does it, they don't try and reuse these locals. And we can watch for nonstandard uses since we now track single-defness in the importer. ... if local is pinned and store is rva static and local is single def -- unpin.
But even without reuse these variables can't be single-def due to the null store at the end of the fixed block, that guarantees that there are always at least 2 defs.
From C# there will be only a single reaching def, though saying this is a bit odd since there are no uses in the code (only implicit uses at GC safe points). But it might be hard to show that property holds definitively.
On the other hand, there is no reason to actually store the rva static pointer into the pinning slot, so we could at least get rid of the first stores ... we'd still "unpin" though. Any other pointer that can somehow reach the implicit uses will be stored, and that's sufficient.
Activity
mikedn commentedon Aug 1, 2019
That's how
fixedworks. The fixed pointer needs to be stored in a local variable that's reported to the GC such that it knows not to move the object. If you look later in the assembly code you'll also see that 0 gets stored to that variable, that's the end of thefixedblock.Ideally the JIT would figure out that in this very particular case the
fixedmachinery isn't needed and eliminate it. That's likely not going to be easy though.damageboy commentedon Aug 1, 2019
@mikedn I do understand that, and I understand how this is a very particular case.
I just think that adding this ROS optimization into the JIT + Compiler was a lot of work for generating super efficient yet clean looking code, but I can't get the best outcome with x86 intrinsics, given that they mostly accept pointers and nothing else.
I think it could either be solved by detecting that
bpis never written to again, and this is effectively a constant, but I'm way out of my depth here.I can lift the ptr into a general purpose register (as I've done in my real code) and use that variable / register, it just seems like someone should be able to connect the dots here (e.g. the JIT)....
Suchiman commentedon Aug 1, 2019
Can we detect this with a peephole optimization to propagate the constant address? Similiar to this trick?
mikedn commentedon Aug 1, 2019
Unlikely. Normally the only way to eliminate a store is to prove that it's dead and that's not something peephole optimizations can do (well, except trivial cases such 2 consecutive stores to the same location). The fact that in this case a constant gets stored into a pinned variable is not sufficient.
And then the problem is that liveness analysis ignores pinned variables due the dependency between such variables and variables that may contain pointers derived from the original fixed pointer. That is, it's not sufficient to prove that the pinned variable is dead, you have to prove that all other variables containing derived pointers are dead too.
Suchiman commentedon Aug 1, 2019
If one presumes that the ROS optimization is performed and then sprinkles a bit of
Unsafearound, you can shave off 5 instructionsmikedn commentedon Aug 1, 2019
Indeed, that's an interesting aspect of this issue. There are alternatives to
fixedbut only under the assumption that the C# compiler performs the optimization.Well, there's also the possibility to not use
lddqu(which AFAIR is kind of pointless on current CPUs) and instead just load the vector by casting the span ref to aref Vector256<int>. But that's probably a different matter.AndyAyersMS commentedon Aug 1, 2019
It might not be that hard to realize we're pinning an RVA static and just remove the pinned bit from the tracking local (since these tracking locals are single def we only need to check when we see the first store to the pinned local). That would let dead stores clean things up.
mikedn commentedon Aug 1, 2019
Hmm, why?
AndyAyersMS commentedon Aug 1, 2019
Well that's how C# does it, they don't try and reuse these locals. And we can watch for nonstandard uses since we now track single-defness in the importer. ... if local is pinned and store is rva static and local is single def -- unpin.
mikedn commentedon Aug 1, 2019
That doesn't seem to be the case:
generates:
But even without reuse these variables can't be single-def due to the null store at the end of the fixed block, that guarantees that there are always at least 2 defs.
AndyAyersMS commentedon Aug 1, 2019
Thanks, looks like I was way off here.
From C# there will be only a single reaching def, though saying this is a bit odd since there are no uses in the code (only implicit uses at GC safe points). But it might be hard to show that property holds definitively.
On the other hand, there is no reason to actually store the rva static pointer into the pinning slot, so we could at least get rid of the first stores ... we'd still "unpin" though. Any other pointer that can somehow reach the implicit uses will be stored, and that's sufficient.
damageboy commentedon Aug 2, 2019
@Suchiman
What you proposed is what I'm actually doing.
I just hide that ungodly mess in a static local function.
I thought it would be useful to report this since I don't know what people's expectation's are w.r.t this.
JIT: unpin locals whose value is provably non-movable