Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Converting more of the x86 HWIntrinsic tests to be generated from a template #16115

Merged
merged 4 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jit/lsraxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ void LinearScan::TreeNodeInfoInitHWIntrinsic(GenTreeHWIntrinsic* intrinsicTree,
case NI_SSE_CompareNotEqualOrderedScalar:
case NI_SSE_CompareNotEqualUnorderedScalar:
info->internalIntCount = 1;
info->setInternalCandidates(this, allRegs(TYP_INT));
info->setInternalCandidates(this, RBM_BYTE_REGS);
break;

case NI_SSE_SetScalar:
Expand Down
196 changes: 196 additions & 0 deletions tests/src/JIT/HardwareIntrinsics/X86/Shared/BooleanCmpOpTest.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// 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.

/******************************************************************************
* This file is auto-generated from a template file by the GenerateTests.csx *
* script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make *
* changes, please update the corresponding template and run according to the *
* directions listed in the file. *
******************************************************************************/

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace JIT.HardwareIntrinsics.X86
{{
public static partial class Program
{{
private static void {1}{2}()
{{
var test = new BooleanComparisonOpTest__{1}{2}();

if (test.IsSupported)
{{
// Validates basic functionality works
test.RunBasicScenario();

// Validates calling via reflection works
test.RunReflectionScenario();

// Validates passing a static member works
test.RunClsVarScenario();

// Validates passing a local works
test.RunLclVarScenario();

// Validates passing the field of a local works
test.RunLclFldScenario();

// Validates passing an instance member works
test.RunFldScenario();
}}
else
{{
// Validates we throw on unsupported hardware
test.RunUnsupportedScenario();
}}

if (!test.Succeeded)
{{
throw new Exception("One or more scenarios did not complete as expected.");
}}
}}
}}

public sealed unsafe class BooleanComparisonOpTest__{1}{2}
{{
private const int VectorSize = {4};
private const int ElementCount = VectorSize / sizeof({2});

private static {2}[] _data1 = new {2}[ElementCount];
private static {2}[] _data2 = new {2}[ElementCount];

private static {3}<{2}> _clsVar1;
private static {3}<{2}> _clsVar2;

private {3}<{2}> _fld1;
private {3}<{2}> _fld2;

private BooleanComparisonOpTest__DataTable<{2}> _dataTable;

static BooleanComparisonOpTest__{1}{2}()
{{
var random = new Random();

for (var i = 0; i < ElementCount; i++) {{ _data1[i] = {5}; _data2[i] = {5}; }}
Unsafe.CopyBlockUnaligned(ref Unsafe.As<{3}<{2}>, byte>(ref _clsVar1), ref Unsafe.As<{2}, byte>(ref _data2[0]), VectorSize);
Unsafe.CopyBlockUnaligned(ref Unsafe.As<{3}<{2}>, byte>(ref _clsVar2), ref Unsafe.As<{2}, byte>(ref _data1[0]), VectorSize);
}}

public BooleanComparisonOpTest__{1}{2}()
{{
Succeeded = true;

var random = new Random();

for (var i = 0; i < ElementCount; i++) {{ _data1[i] = {5}; _data2[i] = {5}; }}
Unsafe.CopyBlockUnaligned(ref Unsafe.As<{3}<{2}>, byte>(ref _fld1), ref Unsafe.As<{2}, byte>(ref _data1[0]), VectorSize);
Unsafe.CopyBlockUnaligned(ref Unsafe.As<{3}<{2}>, byte>(ref _fld2), ref Unsafe.As<{2}, byte>(ref _data2[0]), VectorSize);

for (var i = 0; i < ElementCount; i++) {{ _data1[i] = {5}; _data2[i] = {5}; }}
_dataTable = new BooleanComparisonOpTest__DataTable<{2}>(_data1, _data2);
}}

public bool IsSupported => {0}.IsSupported;

public bool Succeeded {{ get; set; }}

public void RunBasicScenario()
{{
var result = {0}.{1}(
Unsafe.Read<{3}<{2}>>(_dataTable.inArray1Ptr),
Unsafe.Read<{3}<{2}>>(_dataTable.inArray2Ptr)
);

ValidateResult(_dataTable.inArray1, _dataTable.inArray2, result);
}}

public void RunReflectionScenario()
{{
var result = typeof({0}).GetMethod(nameof({0}.{1}), new Type[] {{ typeof({3}<{2}>), typeof({3}<{2}>) }})
.Invoke(null, new object[] {{
Unsafe.Read<{3}<{2}>>(_dataTable.inArray1Ptr),
Unsafe.Read<{3}<{2}>>(_dataTable.inArray2Ptr)
}});

ValidateResult(_dataTable.inArray1, _dataTable.inArray2, (bool)(result));
}}

public void RunClsVarScenario()
{{
var result = {0}.{1}(
_clsVar1,
_clsVar2
);

ValidateResult(_clsVar1, _clsVar2, result);
}}

public void RunLclVarScenario()
{{
var left = Unsafe.Read<{3}<{2}>>(_dataTable.inArray1Ptr);
var right = Unsafe.Read<{3}<{2}>>(_dataTable.inArray2Ptr);
var result = {0}.{1}(left, right);

ValidateResult(left, right, result);
}}

public void RunLclFldScenario()
{{
var test = new BooleanComparisonOpTest__{1}{2}();
var result = {0}.{1}(test._fld1, test._fld2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. Looks like we need another template for Avx comparisons that take two vectors and an imm value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we will end up having at least 5-10 templates to support all tests, but this is much more manageable than 500-1000 individual tests that need handling 😉

I think it is even fine if the templates are "nearly" identical in some cases, as they are still few in number and it makes the overall logic to maintain them simpler.


ValidateResult(test._fld1, test._fld2, result);
}}

public void RunFldScenario()
{{
var result = {0}.{1}(_fld1, _fld2);

ValidateResult(_fld1, _fld2, result);
}}

public void RunUnsupportedScenario()
{{
Succeeded = false;

try
{{
RunBasicScenario();
}}
catch (PlatformNotSupportedException)
{{
Succeeded = true;
}}
}}

private void ValidateResult({3}<{2}> left, {3}<{2}> right, bool result, [CallerMemberName] string method = "")
{{
{2}[] inArray1 = new {2}[ElementCount];
{2}[] inArray2 = new {2}[ElementCount];

Unsafe.Write(Unsafe.AsPointer(ref inArray1[0]), left);
Unsafe.Write(Unsafe.AsPointer(ref inArray2[0]), right);

ValidateResult(inArray1, inArray2, result, method);
}}

private void ValidateResult({2}[] left, {2}[] right, bool result, [CallerMemberName] string method = "")
{{
if ({6})
{{
Succeeded = false;

Console.WriteLine($"{{nameof({0})}}.{{nameof({0}.{1})}}<{2}>: {{method}} failed:");
Console.WriteLine($" left: ({{string.Join(", ", left)}})");
Console.WriteLine($" right: ({{string.Join(", ", right)}})");
Console.WriteLine($" result: ({{string.Join(", ", result)}})");
Console.WriteLine();
}}
}}
}}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace JIT.HardwareIntrinsics.X86
{
public unsafe struct BooleanComparisonOpTest__DataTable<T> : IDisposable where T : struct
{
private GCHandle inHandle1;
private GCHandle inHandle2;

public T[] inArray1;
public T[] inArray2;

public BooleanComparisonOpTest__DataTable(T[] inArray1, T[] inArray2)
{
this.inArray1 = inArray1;
this.inArray2 = inArray2;

this.inHandle1 = GCHandle.Alloc(inArray1, GCHandleType.Pinned);
this.inHandle2 = GCHandle.Alloc(inArray2, GCHandleType.Pinned);
}

public void* inArray1Ptr => inHandle1.AddrOfPinnedObject().ToPointer();
public void* inArray2Ptr => inHandle2.AddrOfPinnedObject().ToPointer();

public void Dispose()
{
inHandle1.Free();
inHandle2.Free();
}
}
}
Loading