Skip to content

[.NET 7]Integer auto-vectorization bug #83387

@koszeggy

Description

@koszeggy

Description

The detailed description is here.

Summary: On some platforms uint/int vector initialization is wrong from ushort fields or arrays. Vector<T> and Vector128<T> are affected for sure, maybe others as well.

Reproduction Steps

using System;
using System.Numerics;
using System.Runtime.Intrinsics;

(ushort A, ushort R, ushort G, ushort B) c = (32768, 65535, 32768, 16384);

Vector128<uint> v1 = Vector128.Create(c.R, c.G, c.B, 0u);
Console.WriteLine(v1);
v1 = v1 * c.A / Vector128.Create(0xFFFFu);
Console.WriteLine(v1);

Span<uint> span = stackalloc uint[Vector<uint>.Count];
span[0] = c.R;
span[1] = c.G;
span[2] = c.B;
Vector<uint> v2 = new Vector<uint>(span) * c.A / new Vector<uint>(0xFFFF);
Console.WriteLine(v2);

Console.WriteLine();
Console.WriteLine(Vector128.Create((int)c.A));
Console.WriteLine(Vector128.Create((int)32768));
Console.WriteLine(Vector128.Create((int)c.A, (int)c.A, (int)c.A, (int)c.A));

Please note that the results are correct in SharpLab Release mode but are incorrect in Debug mode.

Expected behavior

The expected output (works in SharpLab Release mode):

<65535, 32768, 16384, 0>
<32768, 16384, 8192, 0>
<32768, 16384, 8192, 0, 0, 0, 0, 0>

<32768, 32768, 32768, 32768>
<32768, 32768, 32768, 32768>
<32768, 32768, 32768, 32768>

Actual behavior

Incorrect output (eg. in SharpLab Debug mode, .NET Fiddle or on my computer both in Release and Debug builds):

<65535, 32768, 16384, 0>
<32769, 49152, 57344, 0>
<32769, 49152, 57344, 0, 0, 0, 0, 0>

<-32768, -32768, -32768, -32768>
<32768, 32768, 32768, 32768>
<32768, 32768, 32768, 32768>

Regression?

In .NET Fiddle the .NET 6 platform appears to be working; however, some of the used overloaded operators were not available in .NET 6 so I'm not sure what it actually executes.

Known Workarounds

Vanilla operations, explicit usage of HW intrinsics, copying the ushort field into an uint first.

Configuration

.NET 7.0.200
Intel i5-8300H
Windows 11 (Version 10.0.22621.1265)

Other information

No response

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions