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

Add Reference Type Warnings #1142

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5b0ef5d
Fixed analyzer code.
Yey007 Dec 29, 2023
0e0343d
Minor refactors
Yey007 Feb 3, 2024
791d117
Add sample
Yey007 Feb 19, 2024
18addc7
Static main
Yey007 Feb 20, 2024
d667259
Re-added samples project to solution
Yey007 Feb 20, 2024
6b65d8d
Update sample to actually dispose resources
Yey007 Feb 20, 2024
7e4cc85
Update style
Yey007 Feb 20, 2024
202a0e7
Add copyright headers
Yey007 Feb 20, 2024
1ac82d5
Move sample to correct namespace
Yey007 Feb 20, 2024
ab6e95e
Factor out compilation
Yey007 Feb 22, 2024
c4e3926
Add simple test
Yey007 Feb 23, 2024
310521b
Refactored KernelAnalyzer to only find kernels, leaving recursive ana…
Yey007 Feb 25, 2024
c154dc9
Fixed array tests
Yey007 Feb 25, 2024
8107c65
Functions tests
Yey007 Feb 25, 2024
d0dacbc
Constructor tests
Yey007 Feb 25, 2024
2b1e102
Check for unmanaged types instead of value types
Yey007 Mar 7, 2024
b10a162
Kernel discovery tests
Yey007 Mar 19, 2024
0b987a6
Remove TODO in sample
Yey007 Mar 30, 2024
ed021bb
Refactor tests into theory
Yey007 Mar 30, 2024
7eef5d5
Switch to warning severity
Yey007 Mar 30, 2024
61fd179
Factor out resources files for ILA001 and ILA002
Yey007 Mar 30, 2024
e642cd2
Rebrand as ManagedTypeAnalyzer
Yey007 Mar 30, 2024
4c00f4e
Add tests for ILGPU types and intrinsics and finish renaming
Yey007 Mar 30, 2024
b8d73aa
Fix line lengths
Yey007 Mar 30, 2024
5c5a0c3
Update copyright and fix issues with samples and test files
Yey007 Mar 30, 2024
a1f50dd
Fix analyzer release formatting
Yey007 Mar 30, 2024
4c6fe84
Actually run copyright update tool and remove usage of collection exp…
Yey007 Mar 31, 2024
cf4524f
Fix tests
Yey007 Mar 31, 2024
77576f9
Fix samples build
Yey007 Mar 31, 2024
f3bfa05
Merge branch 'master' into feature/reftype-analyzer
Yey007 Mar 31, 2024
c9eb0a3
Allow strings and types from Algorithms
Yey007 Mar 31, 2024
e3346e1
Moved error messages back to a single file.
Yey007 May 18, 2024
c412f64
Improved documentation and refactored helper functions.
Yey007 May 19, 2024
8efa584
Added test for partial methods.
Yey007 May 19, 2024
7c8e4e1
Made ConcurrentDictionary in KernelAnalyzer readonly.
Yey007 May 19, 2024
82268fc
Removed driver-crashing PTX.
Yey007 May 19, 2024
3ad49c6
Update samples to use unmanaged where needed.
Yey007 May 19, 2024
db72095
Require unmanaged in function that launches kernel in StaticAbstractI…
Yey007 May 19, 2024
32a441a
Ignore operations not in the current syntax tree.
Yey007 Jun 20, 2024
3182d5b
Update copyright
Yey007 Jun 20, 2024
45d8c2d
Fix line lengths
Yey007 Jun 20, 2024
c4afc18
Fixed issue with collection expressions
Yey007 Jun 20, 2024
9e499ba
Merge branch 'master' into feature/reftype-analyzer
Yey007 Jul 20, 2024
4b4dca1
Merge branch 'master' into feature/reftype-analyzer
Yey007 Nov 30, 2024
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
8 changes: 4 additions & 4 deletions Samples/AdjustableSharedMemory/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
Expand Down Expand Up @@ -31,7 +31,7 @@ interface ISharedAllocationSize
struct SharedArray32 : ISharedAllocationSize
{
/// <summary>
/// Returns a
/// Returns a
/// </summary>
public int ArraySize => 32;
}
Expand All @@ -49,7 +49,7 @@ struct SharedArray64 : ISharedAllocationSize
/// <param name="sharedArray">Implicit shared-memory parameter that is handled by the runtime.</param>
static void SharedMemoryKernel<TSharedAllocationSize>(
ArrayView<int> outputView) // A view to a chunk of memory (1D in this case)
where TSharedAllocationSize : struct, ISharedAllocationSize
where TSharedAllocationSize : unmanaged, ISharedAllocationSize
{
// Compute the global 1D index for accessing the data view
var globalIndex = Grid.GlobalIndex.X;
Expand All @@ -66,7 +66,7 @@ static void SharedMemoryKernel<TSharedAllocationSize>(
}

static void ExecuteSample<TSharedAllocationSize>(Context context)
where TSharedAllocationSize : struct, ISharedAllocationSize
where TSharedAllocationSize : unmanaged, ISharedAllocationSize
{
// For each available device...
foreach (var device in context)
Expand Down
4 changes: 2 additions & 2 deletions Samples/GenericKernel/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
Expand Down Expand Up @@ -72,7 +72,7 @@ static void Kernel<TKernelFunction, T>(
ArrayView<T> data,
int value,
TKernelFunction function)
where TKernelFunction : struct, IKernelFunction<T>
where TKernelFunction : unmanaged, IKernelFunction<T>
where T : unmanaged
{
// Invoke the custom "lambda function"
Expand Down
9 changes: 9 additions & 0 deletions Samples/ILGPU.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILGPU.Analyzers", "..\Src\I
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterleaveFields", "InterleaveFields\InterleaveFields.csproj", "{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ILGPU.Analyzers", "ILGPU.Analyzers", "{6D111E09-49A1-492E-B4CE-E18CE27B56A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedTypeAnalyzer", "ManagedTypeAnalyzer\ManagedTypeAnalyzer.csproj", "{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -381,6 +385,10 @@ Global
{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}.Release|Any CPU.Build.0 = Release|Any CPU
{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -449,6 +457,7 @@ Global
{70B69CE3-24A9-463C-B14C-E2934988BBEE} = {25BA2234-5778-40BC-9386-9CE87AB87D1F}
{1C5E9E39-3C14-4B52-8D97-04555D5F6331} = {03FCC663-945D-4982-90D8-B14BE52D8FCD}
{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A} = {C1D99632-ED4A-4B08-A14D-4C8DB375934F}
{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3} = {6D111E09-49A1-492E-B4CE-E18CE27B56A8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {30E502BD-3826-417F-888F-1CE19CF5C6DA}
Expand Down
17 changes: 17 additions & 0 deletions Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>$(LibrarySamplesTargetFrameworks)</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ManagedTypeAnalyzer</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\ILGPU\ILGPU.csproj"/>
<ProjectReference Include="..\..\Src\ILGPU.Analyzers\ILGPU.Analyzers.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>

</Project>
88 changes: 88 additions & 0 deletions Samples/ManagedTypeAnalyzer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU;
using ILGPU.Runtime;
using System.Diagnostics.CodeAnalysis;

namespace ManagedTypeAnalyzer;

// Disable warnings for build
[SuppressMessage("Usage", "ILA003:Managed type in kernel")]
[SuppressMessage("Usage", "ILA004:Array of managed types in kernel")]
class Program
{
class RefType
{
public int Hello => 42;
}

struct ValueType
{
public int Hello;

public ValueType()
{
Hello = 42;
}
}

static int AnotherFunction()
{
return new RefType().Hello;
}

static void Kernel(Index1D index, ArrayView<int> input, ArrayView<int> output)
{
// This is disallowed, since MyRefType is a reference type
var refType = new RefType();
output[index] = input[index] + refType.Hello;

// Allocating arrays of unmanaged types is fine
ValueType[] array =
{
new ValueType()
};

int[] ints =
{
0, 1, 2
};

// But arrays of reference types are still disallowed
RefType[] other =
{
new RefType(),
};

// Any functions that may be called are also analyzed
int result = AnotherFunction();
}

static void Main(string[] args)
{
using var context = Context.CreateDefault();
var device = context.GetPreferredDevice(false);
using var accelerator = device.CreateAccelerator(context);

using var input = accelerator.Allocate1D<int>(1024);
using var output = accelerator.Allocate1D<int>(1024);

var kernel =
accelerator
.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ArrayView<int>>(
Kernel);

kernel(input.IntExtent, input.View, output.View);

accelerator.Synchronize();
}
}
10 changes: 5 additions & 5 deletions Samples/StaticAbstractInterfaceMembers/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2023 ILGPU Project
// Copyright (c) 2023-2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
Expand Down Expand Up @@ -66,13 +66,13 @@ public interface ICalculatorOperation<T>
{
T Calculate(T left, T right);
}

// Interface must be implemented by a 'struct'.
public struct AdditionOp : ICalculatorOperation<int>
{
public int Calculate(int left, int right) => left + right;
}

// Interface must be implemented by a 'struct'.
public struct MultiplyOp : ICalculatorOperation<float>
{
Expand All @@ -84,7 +84,7 @@ public static void CalculatorKernel<T, TOp>(
ArrayView1D<T, Stride1D.Dense> input,
ArrayView1D<T, Stride1D.Dense> output)
where T : unmanaged
where TOp : struct, ICalculatorOperation<T>
where TOp : unmanaged, ICalculatorOperation<T>
{
// Creates a new instance of the struct, and calls the method.
output[index] = default(TOp).Calculate(input[index], input[index]);
Expand All @@ -98,7 +98,7 @@ public static void UsingAbstractFunction<T, TOp>(Accelerator accelerator)
where TOp : ICalculatorOperation<T>
#else
where T : unmanaged
where TOp : struct, ICalculatorOperation<T>
where TOp : unmanaged, ICalculatorOperation<T>
#endif
{
var values =
Expand Down
68 changes: 68 additions & 0 deletions Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: DiagnosticAnalyzerVerifier.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
using ILGPU.CodeGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using VerifyTests;
using VerifyXunit;

namespace ILGPU.Analyzers.Tests.Generic;

/// <summary>
/// Verifies the output of running <c>TDiagnosticAnalyzer</c> on C# source.
/// </summary>
/// <typeparam name="TDiagnosticAnalyzer">The diagnostic analyzer to run.</typeparam>
public static class DiagnosticAnalyzerVerifier<TDiagnosticAnalyzer>
where TDiagnosticAnalyzer : DiagnosticAnalyzer, new()
{
/// <summary>
/// Verifies the output of running <c>TDiagnosticAnalyzer</c> on <c>source</c> using
/// snapshots.
/// </summary>
/// <param name="source">The source to run the analyzer on.</param>
/// <param name="configure">
/// Optional action to configure verification settings.
/// </param>
public static async Task Verify(string source,
Action<VerifySettings> configure = null)
{
var ilgpuAssemblies =
new[]
{
typeof(InterleaveFieldsAttribute).Assembly,
typeof(TDiagnosticAnalyzer).Assembly
};

var compilation =
SourceCompiler.CreateCompilationWithAssemblies("Tests", source,
ilgpuAssemblies);

var array = ImmutableArray.Create<DiagnosticAnalyzer>(new TDiagnosticAnalyzer());
var options = new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty);
var analyzerCompilation =
new CompilationWithAnalyzers(compilation, array, options);

var diagnostics = await analyzerCompilation.GetAnalyzerDiagnosticsAsync();

var settings = new VerifySettings();
settings.UseDirectory(Path.Combine("..", "Snapshots"));

if (configure is not null)
configure(settings);

await Verifier.Verify(diagnostics, settings);
}
}
48 changes: 21 additions & 27 deletions Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,40 @@
using ILGPU.CodeGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VerifyTests;
using VerifyXunit;

namespace ILGPU.Analyzers.Tests
namespace ILGPU.Analyzers.Tests.Generic
{
/// <summary>
/// Verifies the output source from the source generator <c>TIncrementalGenerator</c>
/// using snapshots.
/// </summary>
/// <typeparam name="TIncrementalGenerator">
/// The generator to run on source text.
/// </typeparam>
public static class IncrementalGeneratorVerifier<TIncrementalGenerator>
Yey007 marked this conversation as resolved.
Show resolved Hide resolved
where TIncrementalGenerator : IIncrementalGenerator, new()
{
/// <summary>
/// Verifies the C# source generated by <c>TIncrementalGenerator</c>.
/// </summary>
/// <param name="source">The source text to run the generator on.</param>
/// <returns>A task that runs the generator and verifies the output.</returns>
public static Task Verify(string source)
Yey007 marked this conversation as resolved.
Show resolved Hide resolved
{
// Parse syntax tree.
var syntaxTree = CSharpSyntaxTree.ParseText(source);

// Add system references.
var trustedAssembliesPaths =
(string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES");
var systemReferences =
trustedAssembliesPaths
.Split(Path.PathSeparator)
.Select(x => MetadataReference.CreateFromFile(x))
.ToArray();

var ilgpuReferences =
var ilgpuAssemblies =
new[]
{
typeof(InterleaveFieldsAttribute),
typeof(TIncrementalGenerator)
}
.Select(x => MetadataReference.CreateFromFile(x.Assembly.Location))
.ToArray();
typeof(InterleaveFieldsAttribute).Assembly,
typeof(TIncrementalGenerator).Assembly
};

// Create a roslyn compilation for the syntax tree.
var compilation = CSharpCompilation.Create(
"Tests",
new[] { syntaxTree },
references: systemReferences.Concat(ilgpuReferences));
var compilation =
SourceCompiler.CreateCompilationWithAssemblies("Tests", source,
ilgpuAssemblies);

// Create an instance of the incremental source generator.
var generator = new TIncrementalGenerator();
Expand All @@ -65,4 +59,4 @@ public static Task Verify(string source)
return Verifier.Verify(driver, settings);
}
}
}
}
Loading
Loading