Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue promoting unsigned values on the evaluation stack. #822

Merged
merged 1 commit into from Jul 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions Src/ILGPU.Tests/ConvertIntOperations.cs
Expand Up @@ -75,6 +75,70 @@ public void TruncateSmallerConversion()
Verify(output.View, expected);
}

internal static void PromoteLarger_ByteToInt_Kernel(
Index1D index,
byte threshold,
ArrayView1D<byte, Stride1D.Dense> input,
ArrayView1D<int, Stride1D.Dense> output)
{
var lhs = input[index] > threshold ? input[index] : default;
output[index] = lhs;
}

[Fact]
[KernelMethod(nameof(PromoteLarger_ByteToInt_Kernel))]
public void PromoteLarger_ByteToInt_Conversion()
{
const int Start = 200;
const int Length = 64;
const byte Threshold = Start + 32;
var inputValues = Enumerable.Range(Start, Length)
.Select(x => (byte)x)
.ToArray();
var expected = Enumerable.Range(Start, Length)
.Select(x => (byte)x)
.Select(x => x > Threshold ? x : default)
.Select(x => (int)x)
.ToArray();

using var input = Accelerator.Allocate1D<byte>(inputValues);
using var output = Accelerator.Allocate1D<int>(input.Length);
Execute(Length, Threshold, input.View, output.View);
Verify(output.View, expected);
}

internal static void PromoteLarger_UShortToInt_Kernel(
Index1D index,
ushort threshold,
ArrayView1D<ushort, Stride1D.Dense> input,
ArrayView1D<int, Stride1D.Dense> output)
{
var lhs = input[index] > threshold ? input[index] : default;
output[index] = lhs;
}

[Fact]
[KernelMethod(nameof(PromoteLarger_UShortToInt_Kernel))]
public void PromoteLarger_UShortToInt_Conversion()
{
const int Start = 60_000;
const int Length = 64;
const ushort Threshold = Start + 32;
var inputValues = Enumerable.Range(Start, Length)
.Select(x => (ushort)x)
.ToArray();
var expected = Enumerable.Range(Start, Length)
.Select(x => (ushort)x)
.Select(x => x > Threshold ? x : default)
.Select(x => (int)x)
.ToArray();

using var input = Accelerator.Allocate1D<ushort>(inputValues);
using var output = Accelerator.Allocate1D<int>(input.Length);
Execute(Length, Threshold, input.View, output.View);
Verify(output.View, expected);
}

internal static void ImplicitCastAdditionKernel(
Index1D index,
ArrayView1D<uint, Stride1D.Dense> input,
Expand Down
84 changes: 84 additions & 0 deletions Src/ILGPU/CompatibilitySuppressions.xml
@@ -1,3 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.Overflow</Target>
<Left>lib/net471/ILGPU.dll</Left>
<Right>lib/net471/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.OverflowSourceUnsigned</Target>
<Left>lib/net471/ILGPU.dll</Left>
<Right>lib/net471/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:ILGPU.IR.Values.ConvertValue.get_CanOverflow</Target>
<Left>lib/net471/ILGPU.dll</Left>
<Right>lib/net471/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.Overflow</Target>
<Left>lib/net5.0/ILGPU.dll</Left>
<Right>lib/net5.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.OverflowSourceUnsigned</Target>
<Left>lib/net5.0/ILGPU.dll</Left>
<Right>lib/net5.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:ILGPU.IR.Values.ConvertValue.get_CanOverflow</Target>
<Left>lib/net5.0/ILGPU.dll</Left>
<Right>lib/net5.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.Overflow</Target>
<Left>lib/net6.0/ILGPU.dll</Left>
<Right>lib/net6.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.OverflowSourceUnsigned</Target>
<Left>lib/net6.0/ILGPU.dll</Left>
<Right>lib/net6.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:ILGPU.IR.Values.ConvertValue.get_CanOverflow</Target>
<Left>lib/net6.0/ILGPU.dll</Left>
<Right>lib/net6.0/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.Overflow</Target>
<Left>lib/netstandard2.1/ILGPU.dll</Left>
<Right>lib/netstandard2.1/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:ILGPU.IR.Values.ConvertFlags.OverflowSourceUnsigned</Target>
<Left>lib/netstandard2.1/ILGPU.dll</Left>
<Right>lib/netstandard2.1/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:ILGPU.IR.Values.ConvertValue.get_CanOverflow</Target>
<Left>lib/netstandard2.1/ILGPU.dll</Left>
<Right>lib/netstandard2.1/ILGPU.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
4 changes: 1 addition & 3 deletions Src/ILGPU/Frontend/CodeGenerator/Convert.cs
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2021 ILGPU Project
// Copyright (c) 2018-2022 ILGPU Project
// www.ilgpu.net
//
// File: Convert.cs
Expand Down Expand Up @@ -32,8 +32,6 @@ partial class CodeGenerator
var convertFlags = ConvertFlags.None;
if (instructionFlags.HasFlags(ILInstructionFlags.Unsigned))
convertFlags |= ConvertFlags.SourceUnsigned;
if (instructionFlags.HasFlags(ILInstructionFlags.Overflow))
convertFlags |= ConvertFlags.Overflow;
if (targetType.IsUnsignedInt())
{
convertFlags |= ConvertFlags.SourceUnsigned;
Expand Down
7 changes: 4 additions & 3 deletions Src/ILGPU/IR/Construction/Convert.cs
Expand Up @@ -152,10 +152,11 @@ partial class IRBuilder
targetBasicType < sourceBasicType;
}

// If the existing conversion produces an unsigned result, mark that
// the source of the new conversion is unsigned.
ConvertFlags newFlags =
(convert.Flags & ~ConvertFlags.TargetUnsigned) |
flags & ~(ConvertFlags.SourceUnsigned |
ConvertFlags.OverflowSourceUnsigned);
convert.Flags.ToSourceUnsignedFlags() |
flags & ~ConvertFlags.SourceUnsigned;
return canSimplify
? CreateConvert(
location,
Expand Down
23 changes: 3 additions & 20 deletions Src/ILGPU/IR/Values/Convert.cs
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2021 ILGPU Project
// Copyright (c) 2018-2022 ILGPU Project
// www.ilgpu.net
//
// File: Convert.cs
Expand All @@ -27,26 +27,15 @@ public enum ConvertFlags
/// </summary>
None,

/// <summary>
/// The convert operation has overflow semantics.
/// </summary>
Overflow = 1,

/// <summary>
/// The convert operation treats the input value as unsigned.
/// </summary>
SourceUnsigned = 2,

/// <summary>
/// The convert operation has overflow semantics and the
/// overflow check is based on unsigned semantics.
/// </summary>
OverflowSourceUnsigned = 3,
SourceUnsigned = 1,

/// <summary>
/// The convert operation results in an unsigned value.
/// </summary>
TargetUnsigned = 4,
TargetUnsigned = 2,
}

/// <summary>
Expand Down Expand Up @@ -121,12 +110,6 @@ public sealed class ConvertValue : Value
public ArithmeticBasicValueType TargetType =>
BasicValueType.GetArithmeticBasicValueType(IsResultUnsigned);

/// <summary>
/// Returns true if the operation has enabled overflow semantics.
/// </summary>
public bool CanOverflow => (Flags & ConvertFlags.Overflow) ==
ConvertFlags.Overflow;

/// <summary>
/// Returns true if the operation has enabled unsigned semantics.
/// </summary>
Expand Down