Skip to content

Commit 6069861

Browse files
Fix optComputeLoopSideEffects to account for HWI stores (#61911)
Otherwise we can end up not seeing the loop has memory havoc. Also added an assert that will prevent this issue from arising in the future.
1 parent d9a5789 commit 6069861

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/coreclr/jit/optimizer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7539,6 +7539,15 @@ bool Compiler::optComputeLoopSideEffectsOfBlock(BasicBlock* blk)
75397539
}
75407540
break;
75417541

7542+
#ifdef FEATURE_HW_INTRINSICS
7543+
case GT_HWINTRINSIC:
7544+
if (tree->AsHWIntrinsic()->OperIsMemoryStore())
7545+
{
7546+
memoryHavoc |= memoryKindSet(GcHeap, ByrefExposed);
7547+
}
7548+
break;
7549+
#endif // FEATURE_HW_INTRINSICS
7550+
75427551
case GT_LOCKADD:
75437552
case GT_XORR:
75447553
case GT_XAND:
@@ -7547,7 +7556,6 @@ bool Compiler::optComputeLoopSideEffectsOfBlock(BasicBlock* blk)
75477556
case GT_CMPXCHG:
75487557
case GT_MEMORYBARRIER:
75497558
{
7550-
assert(!tree->OperIs(GT_LOCKADD) && "LOCKADD should not appear before lowering");
75517559
memoryHavoc |= memoryKindSet(GcHeap, ByrefExposed);
75527560
}
75537561
break;
@@ -7587,6 +7595,7 @@ bool Compiler::optComputeLoopSideEffectsOfBlock(BasicBlock* blk)
75877595

75887596
default:
75897597
// All other gtOper node kinds, leave 'memoryHavoc' unchanged (i.e. false)
7598+
assert(!tree->OperRequiresAsgFlag());
75907599
break;
75917600
}
75927601
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.Intrinsics;
6+
using System.Runtime.Intrinsics.X86;
7+
using System.Runtime.Intrinsics.Arm;
8+
9+
unsafe class LoopSideEffectsForHwiStores
10+
{
11+
public static int Main()
12+
{
13+
static bool VerifyExpectedVtor(Vector128<int> a) => a.Equals(Vector128.Create(4));
14+
15+
var a = new ClassWithVtor();
16+
17+
fixed (Vector128<int>* p = &a.VtorField)
18+
{
19+
if (Sse2.IsSupported && !VerifyExpectedVtor(ProblemWithSse2(a, (byte*)p)))
20+
{
21+
System.Console.WriteLine("ProblemWithSse2 failed!");
22+
return 101;
23+
}
24+
25+
if (AdvSimd.IsSupported && !VerifyExpectedVtor(ProblemWithAdvSimd(a, (byte*)p)))
26+
{
27+
System.Console.WriteLine("ProblemWithAdvSimd failed!");
28+
return 101;
29+
}
30+
}
31+
32+
return 100;
33+
}
34+
35+
[MethodImpl(MethodImplOptions.NoInlining)]
36+
static unsafe Vector128<int> ProblemWithSse2(ClassWithVtor a, byte* p)
37+
{
38+
Vector128<int> vtor = Vector128<int>.Zero;
39+
40+
a.VtorField = Vector128.Create(1);
41+
a.VtorField = Sse2.Add(a.VtorField, a.VtorField);
42+
43+
for (int i = 0; i < 10; i++)
44+
{
45+
vtor = Sse2.Add(vtor, Sse2.Add(a.VtorField, a.VtorField));
46+
Sse2.Store(p, Vector128<byte>.Zero);
47+
}
48+
49+
return vtor;
50+
}
51+
52+
[MethodImpl(MethodImplOptions.NoInlining)]
53+
static unsafe Vector128<int> ProblemWithAdvSimd(ClassWithVtor a, byte* p)
54+
{
55+
Vector128<int> vtor = Vector128<int>.Zero;
56+
57+
a.VtorField = Vector128.Create(1);
58+
a.VtorField = AdvSimd.Add(a.VtorField, a.VtorField);
59+
60+
for (int i = 0; i < 10; i++)
61+
{
62+
vtor = AdvSimd.Add(vtor, AdvSimd.Add(a.VtorField, a.VtorField));
63+
AdvSimd.Store(p, Vector128<byte>.Zero);
64+
}
65+
66+
return vtor;
67+
}
68+
69+
class ClassWithVtor
70+
{
71+
public Vector128<int> VtorField;
72+
}
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
<CLRTestPriority>1</CLRTestPriority>
6+
</PropertyGroup>
7+
<PropertyGroup>
8+
<DebugType>PdbOnly</DebugType>
9+
<Optimize>True</Optimize>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<Compile Include="$(MSBuildProjectName).cs" />
13+
</ItemGroup>
14+
</Project>

0 commit comments

Comments
 (0)