Skip to content

Commit 0e8a1cd

Browse files
committed
Enabled nullable annotations on ILGPU.
1 parent 79f95b9 commit 0e8a1cd

File tree

256 files changed

+1968
-1266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+1968
-1266
lines changed

LICENSE-3RD-PARTY.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ILGPU Dependencies:
99
(https://www.nuget.org/packages/System.Reflection.Metadata)
1010
- System.Runtime.CompilerServices.Unsafe
1111
(https://www.nuget.org/packages/system.runtime.CompilerServices.Unsafe/)
12+
- .NET Runtime
13+
(https://github.com/dotnet/runtime/)
1214
********************************************************************************
1315

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

3941
System.Runtime.CompilerServices.Unsafe license can be found via:
4042
http://go.microsoft.com/fwlink/?LinkId=329770
43+
44+
********************************************************************************
45+
ILGPU Compiler Dependency: .NET Runtime
46+
47+
For compatibility with older frameworks, parts of ILGPU makes use of source
48+
code for Nullable Attributes from the .NET runtime. This code is licensed
49+
under the MIT license, and can be found via:
50+
https://raw.githubusercontent.com/dotnet/runtime/v7.0.0/LICENSE.TXT

Src/Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<!-- Library Project Configuration -->
33
<PropertyGroup>
44
<LibraryTargetFrameworks>net7.0</LibraryTargetFrameworks>
5+
<LibraryLatestTargetFramework>$(LibraryTargetFrameworks)</LibraryLatestTargetFramework>
56
</PropertyGroup>
67
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
78
<LibraryTargetFrameworks>$(LibraryTargetFrameworks);net471;netstandard2.1;net5.0;net6.0</LibraryTargetFrameworks>
@@ -18,6 +19,7 @@
1819
<!-- Unit Test Project Configuration -->
1920
<PropertyGroup>
2021
<LibraryUnitTestTargetFrameworks>net7.0</LibraryUnitTestTargetFrameworks>
22+
<LibraryLatestUnitTestTargetFramework>$(LibraryUnitTestTargetFrameworks)</LibraryLatestUnitTestTargetFramework>
2123
</PropertyGroup>
2224
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2325
<LibraryUnitTestTargetFrameworks>$(LibraryUnitTestTargetFrameworks);net471;netcoreapp3.1;net5.0;net6.0</LibraryUnitTestTargetFrameworks>

Src/ILGPU.Algorithms.Tests/Generic/AlgorithmsTestData.tt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ---------------------------------------------------------------------------------------
22
// ILGPU Algorithms
3-
// Copyright (c) 2021 ILGPU Project
3+
// Copyright (c) 2021-2023 ILGPU Project
44
// www.ilgpu.net
55
//
66
// File: AlgorithmsTestData.tt/AlgorithmsTestData.cs
@@ -16,6 +16,7 @@
1616

1717
using ILGPU.Algorithms.RadixSortOperations;
1818
using ILGPU.Algorithms.ScanReduceOperations;
19+
using ILGPU.Util;
1920
using System.Numerics;
2021
using System.Runtime.CompilerServices;
2122
using Xunit.Abstractions;
@@ -30,7 +31,10 @@ namespace ILGPU.Algorithms.Tests
3031

3132
public class AlgorithmsTestData<T> : IXunitSerializable
3233
{
33-
public AlgorithmsTestData() { }
34+
public AlgorithmsTestData()
35+
{
36+
Value = Utilities.InitNotNullable<T>();
37+
}
3438

3539
public AlgorithmsTestData(T value)
3640
{
@@ -48,7 +52,7 @@ namespace ILGPU.Algorithms.Tests
4852
info.AddValue(nameof(Value), Value);
4953
}
5054

51-
public override string ToString() => Value.ToString();
55+
public override string ToString() => $"{Value}";
5256
}
5357

5458
#region Xunit Sequencer Structures

Src/ILGPU.Algorithms.Tests/ILGPU.Algorithms.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
<LangVersion>latest</LangVersion>
77
</PropertyGroup>
88

9+
<!-- Enable Nullable Reference Types, but only check for warnings on the latest framework -->
10+
<PropertyGroup>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
<PropertyGroup Condition="'$(TargetFramework)' != '$(LibraryLatestUnitTestTargetFramework)'">
14+
<NoWarn>$(NoWarn);nullable</NoWarn>
15+
</PropertyGroup>
16+
917
<PropertyGroup>
1018
<RunSettingsFilePath>$(MSBuildProjectDirectory)\..\ILGPU.Tests\.test.runsettings</RunSettingsFilePath>
1119
</PropertyGroup>

Src/ILGPU.Algorithms/CL/CLContext.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ---------------------------------------------------------------------------------------
22
// ILGPU Algorithms
3-
// Copyright (c) 2019-2021 ILGPU Project
3+
// Copyright (c) 2019-2023 ILGPU Project
44
// www.ilgpu.net
55
//
66
// File: CLContext.cs
@@ -40,7 +40,8 @@ static partial class CLContext
4040
private static readonly MethodInfo MathCodeGenerator =
4141
CLMathType.GetMethod(
4242
nameof(CLMath.GenerateMathIntrinsic),
43-
AlgorithmContext.IntrinsicBindingFlags);
43+
AlgorithmContext.IntrinsicBindingFlags)
44+
.ThrowIfNull();
4445

4546
/// <summary>
4647
/// Represents the intrinsic representation of the
@@ -49,7 +50,8 @@ static partial class CLContext
4950
private static readonly CLIntrinsic MathCodeGeneratorIntrinsic =
5051
new CLIntrinsic(
5152
MathCodeGenerator,
52-
IntrinsicImplementationMode.GenerateCode);
53+
IntrinsicImplementationMode.GenerateCode)
54+
.ThrowIfNull();
5355

5456
/// <summary>
5557
/// The <see cref="CLGroupExtensions"/> type.
@@ -74,7 +76,8 @@ private static CLIntrinsic GetMathIntrinsic(string name, params Type[] types)
7476
AlgorithmContext.IntrinsicBindingFlags,
7577
null,
7678
types,
77-
null);
79+
null)
80+
.ThrowIfNull();
7881
return new CLIntrinsic(targetMethod, IntrinsicImplementationMode.Redirect);
7982
}
8083

@@ -93,7 +96,8 @@ private static void RegisterIntrinsicMapping(
9396
{
9497
var sourceMethod = sourceType.GetMethod(
9598
name,
96-
AlgorithmContext.IntrinsicBindingFlags);
99+
AlgorithmContext.IntrinsicBindingFlags)
100+
.ThrowIfNull();
97101
manager.RegisterMethod(
98102
sourceMethod,
99103
new CLIntrinsic(targetType, name, IntrinsicImplementationMode.Redirect));
@@ -118,7 +122,8 @@ private static void RegisterIntrinsicCodeGenerator(
118122
{
119123
var sourceMethod = sourceType.GetMethod(
120124
name,
121-
AlgorithmContext.IntrinsicBindingFlags);
125+
AlgorithmContext.IntrinsicBindingFlags)
126+
.ThrowIfNull();
122127
manager.RegisterMethod(
123128
sourceMethod,
124129
new CLIntrinsic(
@@ -150,7 +155,8 @@ private static void RegisterXMathCodeGenerator(
150155
AlgorithmContext.IntrinsicBindingFlags,
151156
null,
152157
types,
153-
null),
158+
null)
159+
.ThrowIfNull(),
154160
new CLIntrinsic(
155161
targetType,
156162
codeGeneratorName,
@@ -176,7 +182,7 @@ internal static void GenerateScanReduce<T, TScanReduce>(
176182
where TScanReduce : struct, IScanReduceOperation<T>
177183
{
178184
// Allocate target and load source argument
179-
var reduce = value as MethodCall;
185+
var reduce = value.AsNotNullCast<MethodCall>();
180186
var sourceValue = codeGenerator.Load(reduce[0]);
181187
var target = codeGenerator.Allocate(value);
182188

Src/ILGPU.Algorithms/CL/CLMath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ---------------------------------------------------------------------------------------
22
// ILGPU Algorithms
3-
// Copyright (c) 2020-2021 ILGPU Project
3+
// Copyright (c) 2020-2023 ILGPU Project
44
// www.ilgpu.net
55
//
66
// File: CLMath.cs
@@ -35,7 +35,7 @@ public static void GenerateMathIntrinsic(
3535
Value value)
3636
{
3737
// Manually generate code for "1.0 / argument"
38-
var arithmeticValue = value as UnaryArithmeticValue;
38+
var arithmeticValue = value.AsNotNullCast<UnaryArithmeticValue>();
3939
var argument = codeGenerator.Load(arithmeticValue.Value);
4040
var target = codeGenerator.Allocate(arithmeticValue);
4141
var operation = CLInstructions.GetArithmeticOperation(

Src/ILGPU.Algorithms/ConcurrentStreamProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ConcurrentStreamProcessor : DisposeBase
3838
public ConcurrentStreamProcessor(
3939
Accelerator accelerator,
4040
int maxNumConcurrentStreams = 0,
41-
Func<Accelerator, AcceleratorStream> streamProvider = null)
41+
Func<Accelerator, AcceleratorStream>? streamProvider = null)
4242
{
4343
maxNumConcurrentStreams = Math.Max(
4444
Math.Max(maxNumConcurrentStreams, 1),
@@ -93,7 +93,7 @@ public void ProcessConcurrently(
9393
/// The action to invoke on each stream to submit work.
9494
/// </param>
9595
public void ProcessConcurrently(
96-
AcceleratorStream stream,
96+
AcceleratorStream? stream,
9797
int numActions,
9898
Action<AcceleratorStream, int> action)
9999
{

Src/ILGPU.Algorithms/HistogramExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using ILGPU.Algorithms.HistogramOperations;
1313
using ILGPU.Algorithms.Resources;
1414
using ILGPU.Runtime;
15+
using ILGPU.Util;
1516
using System;
1617
using System.Reflection;
1718

@@ -145,12 +146,14 @@ private delegate void HistogramUncheckedDelegate<
145146
private static readonly MethodInfo HistogramKernelMethod =
146147
typeof(HistogramExtensions).GetMethod(
147148
nameof(HistogramKernel),
148-
BindingFlags.NonPublic | BindingFlags.Static);
149+
BindingFlags.NonPublic | BindingFlags.Static)
150+
.ThrowIfNull();
149151

150152
private static readonly MethodInfo HistogramUncheckedKernelMethod =
151153
typeof(HistogramExtensions).GetMethod(
152154
nameof(HistogramUncheckedKernel),
153-
BindingFlags.NonPublic | BindingFlags.Static);
155+
BindingFlags.NonPublic | BindingFlags.Static)
156+
.ThrowIfNull();
154157

155158
/// <summary>
156159
/// The actual histogram kernel implementation.

Src/ILGPU.Algorithms/IL/ILContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ---------------------------------------------------------------------------------------
22
// ILGPU Algorithms
3-
// Copyright (c) 2019-2021 ILGPU Project
3+
// Copyright (c) 2019-2023 ILGPU Project
44
// www.ilgpu.net
55
//
66
// File: ILContext.cs
@@ -12,6 +12,7 @@
1212
using ILGPU.Backends;
1313
using ILGPU.Backends.IL;
1414
using ILGPU.IR.Intrinsics;
15+
using ILGPU.Util;
1516
using System;
1617

1718
namespace ILGPU.Algorithms.IL
@@ -63,7 +64,8 @@ private static void RegisterIntrinsicMapping(
6364
{
6465
var sourceMethod = sourceType.GetMethod(
6566
name,
66-
AlgorithmContext.IntrinsicBindingFlags);
67+
AlgorithmContext.IntrinsicBindingFlags)
68+
.ThrowIfNull();
6769
manager.RegisterMethod(
6870
sourceMethod,
6971
new ILIntrinsic(targetType, name, IntrinsicImplementationMode.Redirect));

Src/ILGPU.Algorithms/ILGPU.Algorithms.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
<FileVersion>$(LibraryFileVersion)</FileVersion>
1111
</PropertyGroup>
1212

13+
<!-- Enable Nullable Reference Types, but only check for warnings on the latest framework -->
14+
<PropertyGroup>
15+
<Nullable>enable</Nullable>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(TargetFramework)' != '$(LibraryLatestTargetFramework)'">
18+
<NoWarn>$(NoWarn);nullable</NoWarn>
19+
</PropertyGroup>
20+
1321
<PropertyGroup>
1422
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1523
<NeutralLanguage>en-US</NeutralLanguage>
@@ -23,6 +31,10 @@
2331
<Configurations>Debug;Release</Configurations>
2432
</PropertyGroup>
2533

34+
<ItemGroup>
35+
<Compile Include="..\ILGPU\Util\NullableAttributes.cs" Link="Util\NullableAttributes.cs" />
36+
</ItemGroup>
37+
2638
<ItemGroup>
2739
<None Include="AlgorithmContextMappings.cs">
2840
<DesignTime>True</DesignTime>

0 commit comments

Comments
 (0)