Skip to content

Commit

Permalink
Enabled nullable annotations. (#953)
Browse files Browse the repository at this point in the history
* Applied breaking changes to ILGPU related to support for Nullable types.
* Enabled nullable annotations on ILGPU.
  • Loading branch information
m4rs-mt authored Jul 21, 2023
2 parents 00d5c98 + 0e8a1cd commit 42b4935
Show file tree
Hide file tree
Showing 261 changed files with 2,427 additions and 1,452 deletions.
10 changes: 10 additions & 0 deletions LICENSE-3RD-PARTY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ILGPU Dependencies:
(https://www.nuget.org/packages/System.Reflection.Metadata)
- System.Runtime.CompilerServices.Unsafe
(https://www.nuget.org/packages/system.runtime.CompilerServices.Unsafe/)
- .NET Runtime
(https://github.com/dotnet/runtime/)
********************************************************************************

********************************************************************************
Expand Down Expand Up @@ -38,3 +40,11 @@ http://go.microsoft.com/fwlink/?LinkId=329770

System.Runtime.CompilerServices.Unsafe license can be found via:
http://go.microsoft.com/fwlink/?LinkId=329770

********************************************************************************
ILGPU Compiler Dependency: .NET Runtime

For compatibility with older frameworks, parts of ILGPU makes use of source
code for Nullable Attributes from the .NET runtime. This code is licensed
under the MIT license, and can be found via:
https://raw.githubusercontent.com/dotnet/runtime/v7.0.0/LICENSE.TXT
2 changes: 2 additions & 0 deletions Src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!-- Library Project Configuration -->
<PropertyGroup>
<LibraryTargetFrameworks>net7.0</LibraryTargetFrameworks>
<LibraryLatestTargetFramework>$(LibraryTargetFrameworks)</LibraryLatestTargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<LibraryTargetFrameworks>$(LibraryTargetFrameworks);net471;netstandard2.1;net5.0;net6.0</LibraryTargetFrameworks>
Expand All @@ -18,6 +19,7 @@
<!-- Unit Test Project Configuration -->
<PropertyGroup>
<LibraryUnitTestTargetFrameworks>net7.0</LibraryUnitTestTargetFrameworks>
<LibraryLatestUnitTestTargetFramework>$(LibraryUnitTestTargetFrameworks)</LibraryLatestUnitTestTargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<LibraryUnitTestTargetFrameworks>$(LibraryUnitTestTargetFrameworks);net471;netcoreapp3.1;net5.0;net6.0</LibraryUnitTestTargetFrameworks>
Expand Down
10 changes: 7 additions & 3 deletions Src/ILGPU.Algorithms.Tests/Generic/AlgorithmsTestData.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: AlgorithmsTestData.tt/AlgorithmsTestData.cs
Expand All @@ -16,6 +16,7 @@

using ILGPU.Algorithms.RadixSortOperations;
using ILGPU.Algorithms.ScanReduceOperations;
using ILGPU.Util;
using System.Numerics;
using System.Runtime.CompilerServices;
using Xunit.Abstractions;
Expand All @@ -30,7 +31,10 @@ namespace ILGPU.Algorithms.Tests

public class AlgorithmsTestData<T> : IXunitSerializable
{
public AlgorithmsTestData() { }
public AlgorithmsTestData()
{
Value = Utilities.InitNotNullable<T>();
}

public AlgorithmsTestData(T value)
{
Expand All @@ -48,7 +52,7 @@ namespace ILGPU.Algorithms.Tests
info.AddValue(nameof(Value), Value);
}

public override string ToString() => Value.ToString();
public override string ToString() => $"{Value}";
}

#region Xunit Sequencer Structures
Expand Down
8 changes: 8 additions & 0 deletions Src/ILGPU.Algorithms.Tests/ILGPU.Algorithms.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<!-- Enable Nullable Reference Types, but only check for warnings on the latest framework -->
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != '$(LibraryLatestUnitTestTargetFramework)'">
<NoWarn>$(NoWarn);nullable</NoWarn>
</PropertyGroup>

<PropertyGroup>
<RunSettingsFilePath>$(MSBuildProjectDirectory)\..\ILGPU.Tests\.test.runsettings</RunSettingsFilePath>
</PropertyGroup>
Expand Down
22 changes: 14 additions & 8 deletions Src/ILGPU.Algorithms/CL/CLContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2019-2021 ILGPU Project
// Copyright (c) 2019-2023 ILGPU Project
// www.ilgpu.net
//
// File: CLContext.cs
Expand Down Expand Up @@ -40,7 +40,8 @@ static partial class CLContext
private static readonly MethodInfo MathCodeGenerator =
CLMathType.GetMethod(
nameof(CLMath.GenerateMathIntrinsic),
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();

/// <summary>
/// Represents the intrinsic representation of the
Expand All @@ -49,7 +50,8 @@ static partial class CLContext
private static readonly CLIntrinsic MathCodeGeneratorIntrinsic =
new CLIntrinsic(
MathCodeGenerator,
IntrinsicImplementationMode.GenerateCode);
IntrinsicImplementationMode.GenerateCode)
.ThrowIfNull();

/// <summary>
/// The <see cref="CLGroupExtensions"/> type.
Expand All @@ -74,7 +76,8 @@ private static CLIntrinsic GetMathIntrinsic(string name, params Type[] types)
AlgorithmContext.IntrinsicBindingFlags,
null,
types,
null);
null)
.ThrowIfNull();
return new CLIntrinsic(targetMethod, IntrinsicImplementationMode.Redirect);
}

Expand All @@ -93,7 +96,8 @@ private static void RegisterIntrinsicMapping(
{
var sourceMethod = sourceType.GetMethod(
name,
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();
manager.RegisterMethod(
sourceMethod,
new CLIntrinsic(targetType, name, IntrinsicImplementationMode.Redirect));
Expand All @@ -118,7 +122,8 @@ private static void RegisterIntrinsicCodeGenerator(
{
var sourceMethod = sourceType.GetMethod(
name,
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();
manager.RegisterMethod(
sourceMethod,
new CLIntrinsic(
Expand Down Expand Up @@ -150,7 +155,8 @@ private static void RegisterXMathCodeGenerator(
AlgorithmContext.IntrinsicBindingFlags,
null,
types,
null),
null)
.ThrowIfNull(),
new CLIntrinsic(
targetType,
codeGeneratorName,
Expand All @@ -176,7 +182,7 @@ internal static void GenerateScanReduce<T, TScanReduce>(
where TScanReduce : struct, IScanReduceOperation<T>
{
// Allocate target and load source argument
var reduce = value as MethodCall;
var reduce = value.AsNotNullCast<MethodCall>();
var sourceValue = codeGenerator.Load(reduce[0]);
var target = codeGenerator.Allocate(value);

Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Algorithms/CL/CLMath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2020-2021 ILGPU Project
// Copyright (c) 2020-2023 ILGPU Project
// www.ilgpu.net
//
// File: CLMath.cs
Expand Down Expand Up @@ -35,7 +35,7 @@ public static void GenerateMathIntrinsic(
Value value)
{
// Manually generate code for "1.0 / argument"
var arithmeticValue = value as UnaryArithmeticValue;
var arithmeticValue = value.AsNotNullCast<UnaryArithmeticValue>();
var argument = codeGenerator.Load(arithmeticValue.Value);
var target = codeGenerator.Allocate(arithmeticValue);
var operation = CLInstructions.GetArithmeticOperation(
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Algorithms/ConcurrentStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ConcurrentStreamProcessor : DisposeBase
public ConcurrentStreamProcessor(
Accelerator accelerator,
int maxNumConcurrentStreams = 0,
Func<Accelerator, AcceleratorStream> streamProvider = null)
Func<Accelerator, AcceleratorStream>? streamProvider = null)
{
maxNumConcurrentStreams = Math.Max(
Math.Max(maxNumConcurrentStreams, 1),
Expand Down Expand Up @@ -93,7 +93,7 @@ public void ProcessConcurrently(
/// The action to invoke on each stream to submit work.
/// </param>
public void ProcessConcurrently(
AcceleratorStream stream,
AcceleratorStream? stream,
int numActions,
Action<AcceleratorStream, int> action)
{
Expand Down
7 changes: 5 additions & 2 deletions Src/ILGPU.Algorithms/HistogramExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using ILGPU.Algorithms.HistogramOperations;
using ILGPU.Algorithms.Resources;
using ILGPU.Runtime;
using ILGPU.Util;
using System;
using System.Reflection;

Expand Down Expand Up @@ -145,12 +146,14 @@ private delegate void HistogramUncheckedDelegate<
private static readonly MethodInfo HistogramKernelMethod =
typeof(HistogramExtensions).GetMethod(
nameof(HistogramKernel),
BindingFlags.NonPublic | BindingFlags.Static);
BindingFlags.NonPublic | BindingFlags.Static)
.ThrowIfNull();

private static readonly MethodInfo HistogramUncheckedKernelMethod =
typeof(HistogramExtensions).GetMethod(
nameof(HistogramUncheckedKernel),
BindingFlags.NonPublic | BindingFlags.Static);
BindingFlags.NonPublic | BindingFlags.Static)
.ThrowIfNull();

/// <summary>
/// The actual histogram kernel implementation.
Expand Down
6 changes: 4 additions & 2 deletions Src/ILGPU.Algorithms/IL/ILContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2019-2021 ILGPU Project
// Copyright (c) 2019-2023 ILGPU Project
// www.ilgpu.net
//
// File: ILContext.cs
Expand All @@ -12,6 +12,7 @@
using ILGPU.Backends;
using ILGPU.Backends.IL;
using ILGPU.IR.Intrinsics;
using ILGPU.Util;
using System;

namespace ILGPU.Algorithms.IL
Expand Down Expand Up @@ -63,7 +64,8 @@ private static void RegisterIntrinsicMapping(
{
var sourceMethod = sourceType.GetMethod(
name,
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();
manager.RegisterMethod(
sourceMethod,
new ILIntrinsic(targetType, name, IntrinsicImplementationMode.Redirect));
Expand Down
12 changes: 12 additions & 0 deletions Src/ILGPU.Algorithms/ILGPU.Algorithms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<FileVersion>$(LibraryFileVersion)</FileVersion>
</PropertyGroup>

<!-- Enable Nullable Reference Types, but only check for warnings on the latest framework -->
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != '$(LibraryLatestTargetFramework)'">
<NoWarn>$(NoWarn);nullable</NoWarn>
</PropertyGroup>

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NeutralLanguage>en-US</NeutralLanguage>
Expand All @@ -23,6 +31,10 @@
<Configurations>Debug;Release</Configurations>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\ILGPU\Util\NullableAttributes.cs" Link="Util\NullableAttributes.cs" />
</ItemGroup>

<ItemGroup>
<None Include="AlgorithmContextMappings.cs">
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private readonly MaskedSparseMatrixMultiplier<T, TPredicate, TStride>
public MaskedMatrixProcessor(
Accelerator accelerator,
int maxNumConcurrentStreams = 0,
Func<Accelerator, AcceleratorStream> streamProvider = null)
Func<Accelerator, AcceleratorStream>? streamProvider = null)
: base(accelerator, maxNumConcurrentStreams, streamProvider)
{
matrixMultiplier = accelerator.CreateSparseTransposedMatrixMultiplierMasked<
Expand Down
21 changes: 14 additions & 7 deletions Src/ILGPU.Algorithms/PTX/PTXContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2019-2021 ILGPU Project
// Copyright (c) 2019-2023 ILGPU Project
// www.ilgpu.net
//
// File: PTXContext.cs
Expand All @@ -12,6 +12,7 @@
using ILGPU.Backends.PTX;
using ILGPU.IR.Intrinsics;
using ILGPU.Runtime.Cuda;
using ILGPU.Util;
using System;
using System.Reflection;

Expand All @@ -35,7 +36,8 @@ static partial class PTXContext
private static readonly MethodInfo MathCodeGenerator =
PTXMathType.GetMethod(
nameof(PTXMath.GenerateMathIntrinsic),
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();

/// <summary>
/// Represents the intrinsic representation of the
Expand All @@ -44,7 +46,8 @@ static partial class PTXContext
private static readonly PTXIntrinsic MathCodeGeneratorIntrinsic =
new PTXIntrinsic(
MathCodeGenerator,
IntrinsicImplementationMode.GenerateCode);
IntrinsicImplementationMode.GenerateCode)
.ThrowIfNull();

/// <summary>
/// The <see cref="PTXGroupExtensions"/> type.
Expand Down Expand Up @@ -82,7 +85,8 @@ private static PTXIntrinsic GetMathIntrinsic(string name, params Type[] types)
AlgorithmContext.IntrinsicBindingFlags,
null,
types,
null);
null)
.ThrowIfNull();
return new PTXIntrinsic(targetMethod, IntrinsicImplementationMode.Redirect);
}

Expand All @@ -101,7 +105,8 @@ private static void RegisterIntrinsicMapping(
{
var sourceMethod = sourceType.GetMethod(
name,
AlgorithmContext.IntrinsicBindingFlags);
AlgorithmContext.IntrinsicBindingFlags)
.ThrowIfNull();
manager.RegisterMethod(
sourceMethod,
new PTXIntrinsic(targetType, name, IntrinsicImplementationMode.Redirect));
Expand Down Expand Up @@ -130,14 +135,16 @@ private static void RegisterXMathRedirect(
AlgorithmContext.IntrinsicBindingFlags,
null,
types,
null),
null)
.ThrowIfNull(),
new PTXIntrinsic(
targetType.GetMethod(
replacementName,
AlgorithmContext.IntrinsicBindingFlags,
null,
types,
null),
null)
.ThrowIfNull(),
IntrinsicImplementationMode.Redirect));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Algorithms/PTX/PTXMath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2019-2021 ILGPU Project
// Copyright (c) 2019-2023 ILGPU Project
// www.ilgpu.net
//
// File: PTXMath.cs
Expand Down Expand Up @@ -39,7 +39,7 @@ public static void GenerateMathIntrinsic(
PTXCodeGenerator codeGenerator,
Value value)
{
var arithmeticValue = value as UnaryArithmeticValue;
var arithmeticValue = value.AsNotNullCast<UnaryArithmeticValue>();
var instruction = PTXInstructions.GetArithmeticOperation(
arithmeticValue.Kind,
arithmeticValue.ArithmeticBasicValueType,
Expand Down
Loading

0 comments on commit 42b4935

Please sign in to comment.