Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Sep 10, 2023
1 parent 40cf1e5 commit 5f176f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ bool Lowering::TryLowerSwitchToBitTest(
// We'll ensure that there are only 2 targets when building the bit table.
//

if (targetCount > 0) // test
if (targetCount > 3)
{
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/switchrecognition.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "jitpch.h"
Expand Down Expand Up @@ -127,6 +127,8 @@ bool Compiler::optSwitchConvert(BasicBlock* firstBlock, int testsCount, ssize_t*
maxValue = newMaxValue;
}

assert(testIdx <= testsCount);

if (testIdx < SWITCH_MIN_TESTS)
{
// Make sure we still have at least SWITCH_MIN_TESTS values after we filtered out some of them
Expand Down
73 changes: 21 additions & 52 deletions src/tests/JIT/opt/OptSwitchRecognition/optSwitchRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

// unit test for Switch recognition optimization

using System;
using System.Runtime.CompilerServices;
using Xunit;

namespace optSwitchRecognition;

public class CSwitchRecognitionTest
{
// Test sorted char cases
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchSortedChar(char c)
private static bool RecSwitchSortedChar(char c)
{
return (c == 'a' || c == 'b' || c == 'd' || c == 'f');
return (c == 'a' || c == 'b' || c == 'd' || c == 'f') ? true : false;
}

[Theory]
Expand All @@ -27,13 +28,13 @@ public static bool RecSwitchSortedChar(char c)
[InlineData('A', false)]
[InlineData('Z', false)]
[InlineData('?', false)]
public static void TestRecSwitchSortedChar(char arg1,bool expected) => Assert.Equal(RecSwitchSortedChar(arg1), expected);
public static void TestRecSwitchSortedChar(char arg1, bool expected) => Assert.Equal(expected, RecSwitchSortedChar(arg1));

// Test unsorted char cases
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchUnsortedChar(char c)
private static bool RecSwitchUnsortedChar(char c)
{
return (c == 'd' || c == 'f' || c == 'a' || c == 'b');
return (c == 'd' || c == 'f' || c == 'a' || c == 'b') ? true : false;
}

[Theory]
Expand All @@ -47,13 +48,13 @@ public static bool RecSwitchUnsortedChar(char c)
[InlineData('A', false)]
[InlineData('Z', false)]
[InlineData('?', false)]
public static void TestRecSwitchUnsortedChar(char arg1, bool expected) => Assert.Equal(RecSwitchUnsortedChar(arg1), expected);
public static void TestRecSwitchUnsortedChar(char arg1, bool expected) => Assert.Equal(expected, RecSwitchUnsortedChar(arg1));

// Test sorted int cases
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchSortedInt(int i)
private static bool RecSwitchSortedInt(int i)
{
return (i == -10 || i == -20 || i == 30 || i == 40);
return (i == -10 || i == -20 || i == 30 || i == 40) ? true : false;
}

[Theory]
Expand All @@ -66,13 +67,13 @@ public static bool RecSwitchSortedInt(int i)
[InlineData(40, true)]
[InlineData(70, false)]
[InlineData(100, false)]
public static void TestRecSwitchSortedInt(int arg1, bool expected) => Assert.Equal(RecSwitchSortedInt(arg1), expected);
public static void TestRecSwitchSortedInt(int arg1, bool expected) => Assert.Equal(expected, RecSwitchSortedInt(arg1));

// Test unsorted int cases
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchUnsortedInt(int i)
private static bool RecSwitchUnsortedInt(int i)
{
return (i == 30 || i == 40 || i == -10 || i == -20);
return (i == 30 || i == 40 || i == -10 || i == -20) ? true : false;
}

[Theory]
Expand All @@ -85,61 +86,29 @@ public static bool RecSwitchUnsortedInt(int i)
[InlineData(40, true)]
[InlineData(70, false)]
[InlineData(100, false)]
public static void TestRecSwitchUnsortedInt(int arg1, bool expected) => Assert.Equal(RecSwitchUnsortedInt(arg1), expected);

// Test min limits
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchMinLimits(int i)
{
return (i == -9223372036854775807 || i == -9223372036854775806
|| i == -9223372036854775804 || i == -9223372036854775802);
}

[Theory]
[InlineData(-9223372036854775807, true)]
[InlineData(-9223372036854775806, true)]
[InlineData(-9223372036854775805, false)]
[InlineData(-9223372036854775804, true)]
[InlineData(-9223372036854775802, true)]
public static void TestRecSwitchMinLimits(int arg1, bool expected) => Assert.Equal(RecSwitchMinLimits(arg1), expected);

// Test max limits
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitchMaxLimits(int i)
{
return (i == 9223372036854775807 || i == 9223372036854775806
|| i == 9223372036854775804 || i == 9223372036854775802);
}

[Theory]
[InlineData(9223372036854775807, true)]
[InlineData(9223372036854775806, true)]
[InlineData(9223372036854775805, false)]
[InlineData(9223372036854775804, true)]
[InlineData(9223372036854775802, true)]
public static void TestRecSwitchMaxLimits(int arg1, bool expected) => Assert.Equal(RecSwitchMaxLimits(arg1), expected);
public static void TestRecSwitchUnsortedInt(int arg1, bool expected) => Assert.Equal(expected, RecSwitchUnsortedInt(arg1));

// Test <= 64 switch cases
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitch64JumpTables(int i)
private static bool RecSwitch64JumpTables(int i)
{
return (i == 0 || i == 4 || i == 6 || i == 63);
return (i == 0 || i == 4 || i == 6 || i == 63) ? true : false;
}

[Theory]
[InlineData(-63, false)]
[InlineData(60, false)]
[InlineData(63, true)]
[InlineData(64, false)]
public static void TestRecSwitch64JumpTables(int arg1, bool expected) => Assert.Equal(RecSwitch64JumpTables(arg1), expected);
public static void TestRecSwitch64JumpTables(int arg1, bool expected) => Assert.Equal(expected, RecSwitch64JumpTables(arg1));

//
// Skip optimization
//

// Test > 64 Switch cases (should skip Switch Recognition optimization)
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool RecSwitch128JumpTables(int i)
private static bool RecSwitch128JumpTables(int i)
{
return (i == 0 || i == 4 || i == 6 || i == 127);
}
Expand All @@ -149,11 +118,11 @@ public static bool RecSwitch128JumpTables(int i)
[InlineData(6, true)]
[InlineData(127, true)]
[InlineData(128, false)]
public static void TestRecSwitch128JumpTables(int arg1, bool expected) => Assert.Equal(RecSwitch128JumpTables(arg1), expected);
public static void TestRecSwitch128JumpTables(int arg1, bool expected) => Assert.Equal(expected, RecSwitch128JumpTables(arg1));

// Skips `bit test` conversion because Switch jump targets are > 2 (should skip Switch Recognition optimization)
[MethodImpl(MethodImplOptions.NoInlining)]
public static int RecSwitchSkipBitTest(int arch)
private static int RecSwitchSkipBitTest(int arch)
{
if (arch == 1)
return 2;
Expand All @@ -169,5 +138,5 @@ public static int RecSwitchSkipBitTest(int arch)
[InlineData(2, 4)]
[InlineData(6, 4)]
[InlineData(10, 1)]
public static void TestRecSwitchSkipBitTest(int arg1, int expected) => Assert.Equal(RecSwitchSkipBitTest(arg1), expected);
public static void TestRecSwitchSkipBitTest(int arg1, int expected) => Assert.Equal(expected, RecSwitchSkipBitTest(arg1));
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>PdbOnly</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit 5f176f7

Please sign in to comment.