Description
Reading the new memory model document, we can see:
Memory accesses to properly aligned data of primitive types are always atomic. The value that is observed is always a result of complete read and write operations.
However, take this sample:
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Problem(float[] p, Vector4 a)
{
p[0] = a.X;
p[1] = a.Y;
p[2] = a.Z;
p[3] = a.W;
}
It can emit a store 128 bits in size:
IN0003: 00000D vmovupd xmm0, xmmword ptr [rdx]
IN0004: 000011 vmovupd xmmword ptr [rcx+10H], xmm0
x64/x86 does not guarantee these stores will be atomic, so it is questionable whether this transformation is correct. Of course, it is a bit hard to imagine this being an actual problem in practice.
Bonus bug: the transform in question doesn't look at volatility, so it will coalesce volatile stores, dropping any barriers (compiler or harware) in the process, though this is a bit harder to reproduce in C#.
category:cq
theme:vector-codegen
skill-level:expert
cost:medium
impact:medium