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 31 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
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();
}
}
56 changes: 56 additions & 0 deletions Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// ---------------------------------------------------------------------------------------
// 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;

public static class DiagnosticAnalyzerVerifier<TDiagnosticAnalyzer>
where TDiagnosticAnalyzer : DiagnosticAnalyzer, new()
{
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);
}
}
36 changes: 9 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,28 @@
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
{
public static class IncrementalGeneratorVerifier<TIncrementalGenerator>
Yey007 marked this conversation as resolved.
Show resolved Hide resolved
where TIncrementalGenerator : IIncrementalGenerator, new()
{
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 +47,4 @@ public static Task Verify(string source)
return Verifier.Verify(driver, settings);
}
}
}
}
53 changes: 53 additions & 0 deletions Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: SourceCompiler.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.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace ILGPU.Analyzers.Tests.Generic;

public static class SourceCompiler
{
public static CSharpCompilation CreateCompilationWithAssemblies(
Yey007 marked this conversation as resolved.
Show resolved Hide resolved
string assemblyName,
string source,
Assembly[] additionalAssemblies)
{
// 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 additionalReferences =
additionalAssemblies
.Select(x => MetadataReference.CreateFromFile(x.Location))
.ToArray();

// Create a roslyn compilation for the syntax tree.
var compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references: systemReferences.Concat(additionalReferences));

return compilation;
}
}
8 changes: 8 additions & 0 deletions Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ILGPU.Algorithms\ILGPU.Algorithms.csproj" />
<ProjectReference Include="..\ILGPU.Analyzers\ILGPU.Analyzers.csproj" />
<ProjectReference Include="..\ILGPU\ILGPU.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Programs\**" />
<Content Include="Programs\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions Src/ILGPU.Analyzers.Tests/InterleaveFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
// ---------------------------------------------------------------------------------------

using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
using VerifyCS =
ILGPU.Analyzers.Tests.IncrementalGeneratorVerifier<
ILGPU.Analyzers.Tests.Generic.IncrementalGeneratorVerifier<
ILGPU.Analyzers.InterleaveFieldsGenerator>;

namespace ILGPU.Analyzers.Tests
Expand Down
40 changes: 40 additions & 0 deletions Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: ManagedTypeAnalyzer.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.IO;
using System.Threading.Tasks;
using Xunit;
using VerifyCS =
ILGPU.Analyzers.Tests.Generic.DiagnosticAnalyzerVerifier<
ILGPU.Analyzers.ManagedTypeAnalyzer>;

namespace ILGPU.Analyzers.Tests;

public class ManagedTypeAnalyzer
{
[Theory]
[InlineData("Simple")]
[InlineData("Complex")]
[InlineData("Arrays")]
[InlineData("Functions")]
[InlineData("Constructors")]
[InlineData("LoadDiscovery")]
[InlineData("ILGPUTypesIntrinsics")]
public async Task FileTests(string file)
{
// In build, we copy all programs to output directory.
// See ILGPU.Analyzers.Tests.csproj
var code = await File.ReadAllTextAsync(
$"Programs/ManagedType/{file}.cs"
);
await VerifyCS.Verify(code, settings => settings.UseParameters(file));
}
}
Loading
Loading