forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unroll byref struct copies (dotnet#86820)
If a struct contains a byref, then it is known to be on the stack/regs (not in the heap), so GC write barriers are not required. This adds that case to lower*.cpp and attempts to make the code more similar. I didn't actually factor them (especially with a few subtle differences such as the call to `getUnrollThreshold`). This partially handles dotnet#80086. It improves the code for common cases, but since the strategy is not always used, the correctness issue in it is not completely handled. Next step is to apply the fix for that and see how bad the regressions are; this change will reduce the impact. Example: ``` C# static Span<int> Copy1(Span<int> s) => s; ``` ``` asm G_M44162_IG01: ;; offset=0000H vzeroupper ;; size=3 bbWeight=1 PerfScore 1.00 G_M44162_IG02: ;; offset=0003H vmovdqu xmm0, xmmword ptr [rdx] vmovdqu xmmword ptr [rcx], xmm0 ;; size=8 bbWeight=1 PerfScore 6.00 G_M44162_IG03: ;; offset=000BH mov rax, rcx ;; size=3 bbWeight=1 PerfScore 0.25 G_M44162_IG04: ;; offset=000EH ret ;; size=1 bbWeight=1 PerfScore 1.00 ; Total bytes of code 15, prolog size 3, PerfScore 9.75, instruction count 5, allocated bytes for code 15 (MethodHash=4d5b537d) for method ``` Platform | Overall | MinOpts | FullOpts --------------|---------|---------|--------- linux arm64 | -5,232 | -3,260 | -1,972 linux x64 | -1,142 | -750 | -392 osx arm64 | -5,732 | -3,276 | -2,456 windows arm64 | -4,416 | -2,580 | -1,836 windows x64 | -8,993 | -5,772 | -3,221 linux arm | -13,518 | -9,530 | -3,988 windows x86 | 0 | 0 | 0
- Loading branch information
Showing
8 changed files
with
169 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
#pragma warning disable CS8500 | ||
|
||
namespace Runtime_80086 | ||
{ | ||
public static unsafe class Test | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static Span<int> Marshal1(Span<int> a) | ||
{ | ||
return MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(a), a.Length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static Span<int> Marshal2(Span<int>* a) | ||
{ | ||
return MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(*a), a->Length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static Span<int> Copy1(Span<int> s) => s; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static Span<int> Copy2(Span<int> a) | ||
{ | ||
ref Span<int> ra = ref a; | ||
return ra; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static Span<int> Copy3(Span<int>* a) | ||
{ | ||
return *a; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static Span<int> Copy4(scoped ref Span<int> a) | ||
{ | ||
return a; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static Span<int> Copy5(ReadOnlySpan<int> a) | ||
{ | ||
// Example is used to check code generation but shouldn't be used elsewhere | ||
return *(Span<int>*)&a; | ||
} | ||
|
||
[Fact] | ||
public static void TestEntryPoint() | ||
{ | ||
Span<int> s = new int[1] { 13 }; | ||
s = Marshal1(s); | ||
s = Marshal2(&s); | ||
s = Copy1(s); | ||
s = Copy2(s); | ||
s = Copy3(&s); | ||
s = Copy4(ref s); | ||
s = Copy5(s); | ||
Assert.Equal(13, s[0]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |