diff options
Diffstat (limited to 'tests/src/JIT/Performance/CodeQuality/SIMD')
4 files changed, 209 insertions, 4 deletions
diff --git a/tests/src/JIT/Performance/CodeQuality/SIMD/ConsoleMandel/ConsoleMandel.cs b/tests/src/JIT/Performance/CodeQuality/SIMD/ConsoleMandel/ConsoleMandel.cs index ed7f675700..5ab08202b6 100644 --- a/tests/src/JIT/Performance/CodeQuality/SIMD/ConsoleMandel/ConsoleMandel.cs +++ b/tests/src/JIT/Performance/CodeQuality/SIMD/ConsoleMandel/ConsoleMandel.cs @@ -11,9 +11,9 @@ using Xunit; [assembly: OptimizeForBenchmarks] [assembly: MeasureInstructionsRetired] -namespace ConsoleMandel +namespace SIMD { - public static class Program + public static class ConsoleMandel { private const int Pass = 100; private const int Fail = -1; diff --git a/tests/src/JIT/Performance/CodeQuality/SIMD/RayTracer/RayTracerBench.cs b/tests/src/JIT/Performance/CodeQuality/SIMD/RayTracer/RayTracerBench.cs index 064f860611..254672b462 100644 --- a/tests/src/JIT/Performance/CodeQuality/SIMD/RayTracer/RayTracerBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/SIMD/RayTracer/RayTracerBench.cs @@ -2,8 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // -// Based on the Raytracer example from -// Samples for Parallel Programming with the .NET Framework +// Based on the Raytracer example from +// Samples for Parallel Programming with the .NET Framework // https://code.msdn.microsoft.com/windowsdesktop/Samples-for-Parallel-b4b76364 using Microsoft.Xunit.Performance; @@ -16,6 +16,8 @@ using System.Collections.Concurrent; [assembly: OptimizeForBenchmarks] [assembly: MeasureInstructionsRetired] +namespace SIMD +{ public class RayTracerBench { #if DEBUG @@ -142,3 +144,4 @@ public class RayTracerBench return (result ? 100 : -1); } } +} diff --git a/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.cs b/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.cs new file mode 100644 index 0000000000..34c0ab888d --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.cs @@ -0,0 +1,159 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Xunit.Performance; +using System; +using System.Numerics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Reflection; +using System.Collections.Generic; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class SeekUnroll +{ + + // The purpose of this micro-benchmark is to measure the effect of unrolling + // on this loop (taken from https://github.com/aspnet/KestrelHttpServer/pull/1138) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int FindByte(ref Vector<byte> byteEquals) + { + var vector64 = Vector.AsVectorInt64(byteEquals); + long longValue = 0; + var i = 0; + for (; i < Vector<long>.Count; i++) + { + longValue = vector64[i]; + if (longValue == 0) continue; + break; + } + + // Flag least significant power of two bit + var powerOfTwoFlag = (ulong)(longValue ^ (longValue - 1)); + // Shift all powers of two into the high byte and extract + var foundByteIndex = (int)((powerOfTwoFlag * _xorPowerOfTwoToHighByte) >> 57); + // Single LEA instruction with jitted const (using function result) + return i * 8 + foundByteIndex; + } + + // Magic constant used in FindByte + const ulong _xorPowerOfTwoToHighByte = (0x07ul | + 0x06ul << 8 | + 0x05ul << 16 | + 0x04ul << 24 | + 0x03ul << 32 | + 0x02ul << 40 | + 0x01ul << 48) + 1; + + // Inner loop to repeatedly call FindByte + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static void InnerLoop(ref int foundIndex, ref Vector<Byte> vector) + { + for (int i = 0; i < InnerIterations; i++) + { + foundIndex = FindByte(ref vector); + } + } + + // Iteration counts for inner loop set to have each call take 1 or + // 2 seconds or so in release, finish quickly in debug. +#if DEBUG + const int InnerIterations = 1; +#else + const int InnerIterations = 1000000000; +#endif + + // Function to meaure InnerLoop using the xunit-perf benchmark measurement facilities + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static void XunitBenchmarkLoop(ref int foundIndex, ref Vector<Byte> vector) + { + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + InnerLoop(ref foundIndex, ref vector); + } + } + } + + // Function to measure InnerLoop with manual use of a stopwatch timer + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static void ManualTimerLoop(ref int foundIndex, ref Vector<Byte> vector) + { + for (int iteration = 0; iteration < ManualLoopTimes.Length; ++iteration) + { + var timer = System.Diagnostics.Stopwatch.StartNew(); + InnerLoop(ref foundIndex, ref vector); + timer.Stop(); + ManualLoopTimes[iteration] = timer.ElapsedMilliseconds; + } + } + static long[] ManualLoopTimes; + + // Function that tests one input, dispatching to either the xunit-perf + // loop or the manual timer loop + static bool Test(int index, bool isXunitBenchmark) + { + if (index >= Vector<Byte>.Count) + { + // FindByte assumes index is in range + index = 0; + } + var bytes = new Byte[Vector<Byte>.Count]; + bytes[index] = 255; + Vector<Byte> vector = new Vector<Byte>(bytes); + + int foundIndex = -1; + + if (isXunitBenchmark) + { + XunitBenchmarkLoop(ref foundIndex, ref vector); + } + else + { + ManualTimerLoop(ref foundIndex, ref vector); + } + + Assert.Equal(index, foundIndex); + return (index == foundIndex); + } + + // Set of indices to pass to Test(int, bool) + static int[] IndicesToTest = new int[] { 1, 3, 11, 19, 27 }; + + // Entrypoint for xunit-perf to call the benchmark + [Benchmark] + [MemberData(nameof(ArrayedBoxedIndicesToTest))] + public static bool Test(object boxedIndex) + { + return Test((int)boxedIndex, true); + } + + // IndicesToTest wrapped up in arrays of boxes since that's + // what xunit-perf needs + public static IEnumerable<object[]> ArrayedBoxedIndicesToTest = + IndicesToTest.Select((int index) => new object[] { index }); + + + // Main method entrypoint runs the manual timer loop + public static int Main() + { + int failures = 0; + foreach(int index in IndicesToTest) + { + ManualLoopTimes = new long[10]; + bool passed = Test(index, false); + if (!passed) + { + ++failures; + } + Console.WriteLine("Index {0}, times (ms) [{1}]", index, String.Join(", ", ManualLoopTimes)); + } + + return 100 + failures; + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.csproj b/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.csproj new file mode 100644 index 0000000000..53cfe42001 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/SIMD/SeekUnroll/SeekUnroll.csproj @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{9AE6E18D-B3F9-4216-9809-125824387175}</ProjectGuid> + <OutputType>Exe</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <FileAlignment>512</FileAlignment> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + </PropertyGroup> + <ItemGroup> + <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> + <Visible>False</Visible> + </CodeAnalysisDependentAssemblyPaths> + </ItemGroup> + <ItemGroup> + <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> + </ItemGroup> + <ItemGroup> + <Compile Include="SeekUnroll.cs" /> + </ItemGroup> + <PropertyGroup> + <ProjectJson>$(JitPackagesConfigFileDirectory)benchmark\project.json</ProjectJson> + <ProjectLockJson>$(JitPackagesConfigFileDirectory)benchmark\project.lock.json</ProjectLockJson> + </PropertyGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> + <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "> + </PropertyGroup> +</Project> |