From 5b0ef5d9eda2e297d51c036c14d3053442559bf5 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 29 Dec 2023 18:21:03 +0300 Subject: [PATCH 01/41] Fixed analyzer code. --- Samples/ILGPU.Samples.sln | 2 + Src/ILGPU.Analyzers/DiagnosticCategory.cs | 7 ++ Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj | 18 +++ Src/ILGPU.Analyzers/KernelAnalyzer.cs | 113 ++++++++++++++++++ Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 69 +++++++++++ .../ILA003_ReferenceTypeInKernel.Designer.cs | 66 ++++++++++ .../ILA003_ReferenceTypeInKernel.resx | 30 +++++ ...004_ReferenceTypeArrayInKernel.Designer.cs | 66 ++++++++++ .../ILA004_ReferenceTypeArrayInKernel.resx | 30 +++++ 9 files changed, 401 insertions(+) create mode 100644 Src/ILGPU.Analyzers/DiagnosticCategory.cs create mode 100644 Src/ILGPU.Analyzers/KernelAnalyzer.cs create mode 100644 Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs create mode 100644 Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs create mode 100644 Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx create mode 100644 Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs create mode 100644 Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx diff --git a/Samples/ILGPU.Samples.sln b/Samples/ILGPU.Samples.sln index ce9098eb9..c53441272 100644 --- a/Samples/ILGPU.Samples.sln +++ b/Samples/ILGPU.Samples.sln @@ -135,6 +135,8 @@ 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 Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Src/ILGPU.Analyzers/DiagnosticCategory.cs b/Src/ILGPU.Analyzers/DiagnosticCategory.cs new file mode 100644 index 000000000..4091a03be --- /dev/null +++ b/Src/ILGPU.Analyzers/DiagnosticCategory.cs @@ -0,0 +1,7 @@ +namespace ILGPU.Analyzers +{ + public static class DiagnosticCategory + { + public static string Usage => "Usage"; + } +} diff --git a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj index 8efc56628..8765e1618 100644 --- a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj +++ b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj @@ -41,6 +41,16 @@ True ErrorMessages.resx + + True + True + ILA004_ReferenceTypeArrayInKernel.resx + + + True + True + ILA003_ReferenceTypeInKernel.resx + @@ -48,6 +58,14 @@ ResXFileCodeGenerator ErrorMessages.Designer.cs + + ResXFileCodeGenerator + ILA004_ReferenceTypeArrayInKernel.Designer.cs + + + ResXFileCodeGenerator + ILA003_ReferenceTypeInKernel.Designer.cs + diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs new file mode 100644 index 000000000..7eb2604e2 --- /dev/null +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -0,0 +1,113 @@ +using System.Collections.Generic; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Operations; +using System.Collections.Immutable; +using System.Linq; + +namespace ILGPU.Analyzers +{ + public abstract class KernelAnalyzer : DiagnosticAnalyzer + { + private readonly ImmutableHashSet kernelLoadNames = ImmutableHashSet.Create( + "LoadKernel", + "LoadAutoGroupedKernel", + "LoadImplicitlyGroupedKernel", + "LoadStreamKernel", + "LoadAutoGroupedStreamKernel", + "LoadImplicitlyGroupedStreamKernel" + ); + + /// + /// Called for every operation potentially reachable from a kernel. + /// + /// + /// The analysis context used to report diagnostics. + /// + /// + /// The operation. Operations for subsequent calls may be parents or descendants of + /// an operation for a previous call. + /// + protected abstract void AnalyzeKernelOperation( + OperationAnalysisContext context, + IOperation operation); + + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + + // Subscribe to semantic (compile time) action invocation, e.g. method invocation. + context.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation); + } + + private void AnalyzeOperation(OperationAnalysisContext context) + { + if (context.Operation is not IInvocationOperation invocationOperation || + context.Operation.Syntax is not InvocationExpressionSyntax) + return; + + var methodSymbol = invocationOperation.TargetMethod; + + if (methodSymbol.MethodKind != MethodKind.Ordinary + || !kernelLoadNames.Contains(methodSymbol.Name)) + return; + + var kernelArg = invocationOperation.Arguments.FirstOrDefault(x => + x.Parameter?.Type.TypeKind == TypeKind.Delegate); + + if (kernelArg?.Value is + IDelegateCreationOperation + delegateOp) // TODO: support expressions that return delegate (probably requires dataflow) + { + var semanticModel = context.Operation.SemanticModel; + + var bodyOp = delegateOp.Target switch + { + IMethodReferenceOperation refOp => GetMethodBody(semanticModel, refOp.Method), + IAnonymousFunctionOperation anonymousOp => anonymousOp.Body, + _ => null + }; + + RecursivelyAnalyzeKernelOperations(context, bodyOp); + } + } + + // TODO: iterative using stack + private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context, IOperation? bodyOp, + HashSet? seen = null) + { + seen ??= new HashSet(); + + if (bodyOp is null) return; + if (seen.Contains(bodyOp)) return; + + var semanticModel = context.Operation.SemanticModel; + + foreach (var descendant in bodyOp.DescendantsAndSelf()) + { + seen.Add(descendant); + AnalyzeKernelOperation(context, descendant); + + // Okay, so this doesn't cover every possible way other code can be called through a kernel. + // But this is in general quite difficult to do. We can improve things over time as necessary, perhaps. + // Thankfully, if we accept a degree of false positives, we probably don't need to solve the halting problem :) + if (descendant is IInvocationOperation kernelMethodCallOperation) + { + var innerBodyOp = GetMethodBody(semanticModel, kernelMethodCallOperation.TargetMethod); + RecursivelyAnalyzeKernelOperations(context, innerBodyOp, seen); + } + } + } + + private IOperation? GetMethodBody(SemanticModel? model, IMethodSymbol symbol) => symbol switch + { + { IsPartialDefinition: false } => model?.GetOperation( + symbol.DeclaringSyntaxReferences[0].GetSyntax()), + { PartialImplementationPart: not null } => model?.GetOperation( + symbol.PartialImplementationPart.DeclaringSyntaxReferences[0].GetSyntax()), + _ => null + }; + } +} diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs new file mode 100644 index 000000000..3eabe8845 --- /dev/null +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -0,0 +1,69 @@ +using ILGPU.Analyzers.Resources; +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ILGPU.Analyzers +{ + [DiagnosticAnalyzer(LanguageNames.CSharp)] + public class ReferenceTypeAnalyzer : KernelAnalyzer + { + private static readonly DiagnosticDescriptor GeneralDiagnosticRule = new( + id: "ILA003", + title: ILA003_ReferenceTypeInKernel.Title, + messageFormat: ILA003_ReferenceTypeInKernel.MessageFormat, + category: DiagnosticCategory.Usage, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: ILA003_ReferenceTypeInKernel.Description + ); + + private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( + id: "ILA004", + title: ILA004_ReferenceTypeArrayInKernel.Title, + messageFormat: ILA004_ReferenceTypeArrayInKernel.MessageFormat, + category: DiagnosticCategory.Usage, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: ILA004_ReferenceTypeArrayInKernel.Description + ); + + public override ImmutableArray SupportedDiagnostics { get; } = + ImmutableArray.Create(GeneralDiagnosticRule, ArrayDiagnosticRule); + + protected override void AnalyzeKernelOperation(OperationAnalysisContext context, + IOperation operation) + { + if (operation.Type is null) + { + return; + } + + if (operation.Type.IsValueType) + { + return; + } + + if (operation.Type is IArrayTypeSymbol arrayTypeSymbol) + { + if (!arrayTypeSymbol.ElementType.IsValueType) + { + var arrayDiagnostic = + Diagnostic.Create(ArrayDiagnosticRule, + operation.Syntax.GetLocation(), + operation.Type.ToDisplayString(), + arrayTypeSymbol.ElementType.ToDisplayString()); + context.ReportDiagnostic(arrayDiagnostic); + } + + return; + } + + var generalDiagnostic = + Diagnostic.Create(GeneralDiagnosticRule, + operation.Syntax.GetLocation(), + operation.Type.ToDisplayString()); + context.ReportDiagnostic(generalDiagnostic); + } + } +} diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs new file mode 100644 index 000000000..466eb1b02 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ILGPU.Analyzers.Resources { + using System; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class ILA003_ReferenceTypeInKernel { + + private static System.Resources.ResourceManager resourceMan; + + private static System.Globalization.CultureInfo resourceCulture; + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal ILA003_ReferenceTypeInKernel() { + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { + get { + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA003_ReferenceTypeInKernel", typeof(ILA003_ReferenceTypeInKernel).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + internal static string Title { + get { + return ResourceManager.GetString("Title", resourceCulture); + } + } + + internal static string MessageFormat { + get { + return ResourceManager.GetString("MessageFormat", resourceCulture); + } + } + + internal static string Description { + get { + return ResourceManager.GetString("Description", resourceCulture); + } + } + } +} diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx new file mode 100644 index 000000000..f61b774d4 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx @@ -0,0 +1,30 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Reference type in kernel + + + Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types. + + + Reference types like classes cannot be used in kernels. + + \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs new file mode 100644 index 000000000..80fe60ce3 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ILGPU.Analyzers.Resources { + using System; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class ILA004_ReferenceTypeArrayInKernel { + + private static System.Resources.ResourceManager resourceMan; + + private static System.Globalization.CultureInfo resourceCulture; + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal ILA004_ReferenceTypeArrayInKernel() { + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { + get { + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA004_ReferenceTypeArrayInKernel", typeof(ILA004_ReferenceTypeArrayInKernel).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + internal static string Description { + get { + return ResourceManager.GetString("Description", resourceCulture); + } + } + + internal static string MessageFormat { + get { + return ResourceManager.GetString("MessageFormat", resourceCulture); + } + } + + internal static string Title { + get { + return ResourceManager.GetString("Title", resourceCulture); + } + } + } +} diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx new file mode 100644 index 000000000..b0f044bb0 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx @@ -0,0 +1,30 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Arrays of reference types cannot be used in kernels. + + + Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels. + + + Array of reference types in kernel + + \ No newline at end of file From 0e0343d804c3cfd880b2deed7d55a741cf916c73 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 3 Feb 2024 01:02:08 -0500 Subject: [PATCH 02/41] Minor refactors --- .../RefTypeAnalyzer/RefTypeAnalyzer.csproj | 11 +++ .../AnalyzerReleases.Unshipped.md | 4 +- Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj | 17 +---- .../InterleaveFieldsGenerator.cs | 4 +- .../Properties/launchSettings.json | 6 +- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 14 ++-- .../Resources/ErrorMessages.Designer.cs | 76 ++++++++----------- .../Resources/ErrorMessages.resx | 13 +++- 8 files changed, 74 insertions(+), 71 deletions(-) create mode 100644 Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj diff --git a/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj b/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj new file mode 100644 index 000000000..4e3a1c7e6 --- /dev/null +++ b/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj @@ -0,0 +1,11 @@ + + + + Exe + net7.0 + enable + enable + RefTypeAnalyzers + + + diff --git a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md index ddc574b58..9ac28829e 100644 --- a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md +++ b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md @@ -6,4 +6,6 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- ILA001 | CodeGeneration | Error | SourceGenerator -ILA002 | CodeGeneration | Error | SourceGenerator \ No newline at end of file +ILA002 | CodeGeneration | Error | SourceGenerator +ILA003 | Usage | Error | Analyzer +ILA004 | Usage | Error | Analyzer \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj index 8765e1618..c085bc038 100644 --- a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj +++ b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj @@ -41,15 +41,10 @@ True ErrorMessages.resx - + True True - ILA004_ReferenceTypeArrayInKernel.resx - - - True - True - ILA003_ReferenceTypeInKernel.resx + DiagnosticCategory.resx @@ -58,13 +53,9 @@ ResXFileCodeGenerator ErrorMessages.Designer.cs - - ResXFileCodeGenerator - ILA004_ReferenceTypeArrayInKernel.Designer.cs - - + ResXFileCodeGenerator - ILA003_ReferenceTypeInKernel.Designer.cs + DiagnosticCategory.Designer.cs diff --git a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs index 0506a69a4..9ed076aa9 100644 --- a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs +++ b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs @@ -34,7 +34,7 @@ public class InterleaveFieldsGenerator : IIncrementalGenerator id: "ILA001", title: ErrorMessages.StructMustBePartial_Title, messageFormat: ErrorMessages.StructMustBePartial_Message, - category: ErrorMessages.Usage_Category, + category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); @@ -42,7 +42,7 @@ public class InterleaveFieldsGenerator : IIncrementalGenerator id: "ILA002", title: ErrorMessages.ContainingTypeMustBePartial_Title, messageFormat: ErrorMessages.ContainingTypeMustBePartial_Message, - category: ErrorMessages.Usage_Category, + category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); diff --git a/Src/ILGPU.Analyzers/Properties/launchSettings.json b/Src/ILGPU.Analyzers/Properties/launchSettings.json index 54dcc924f..d924de27b 100644 --- a/Src/ILGPU.Analyzers/Properties/launchSettings.json +++ b/Src/ILGPU.Analyzers/Properties/launchSettings.json @@ -1,8 +1,12 @@ { "profiles": { - "ILGPU.Analyzers": { + "InterleaveFields": { "commandName": "DebugRoslynComponent", "targetProject": "..\\..\\Samples\\InterleaveFields\\InterleaveFields.csproj" + }, + "RefType": { + "commandName": "DebugRoslynComponent", + "targetProject": "..\\..\\Samples\\\\RefType.csproj" } } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index 3eabe8845..58e83ee9d 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -10,22 +10,20 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer { private static readonly DiagnosticDescriptor GeneralDiagnosticRule = new( id: "ILA003", - title: ILA003_ReferenceTypeInKernel.Title, - messageFormat: ILA003_ReferenceTypeInKernel.MessageFormat, + title: ErrorMessages.RefTypeInKernel_Title, + messageFormat: ErrorMessages.RefTypeInKernel_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true, - description: ILA003_ReferenceTypeInKernel.Description + isEnabledByDefault: true ); private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", - title: ILA004_ReferenceTypeArrayInKernel.Title, - messageFormat: ILA004_ReferenceTypeArrayInKernel.MessageFormat, + title: ErrorMessages.RefTypeArrInKernel_Title, + messageFormat: ErrorMessages.RefTypeArrInKernel_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true, - description: ILA004_ReferenceTypeArrayInKernel.Description + isEnabledByDefault: true ); public override ImmutableArray SupportedDiagnostics { get; } = diff --git a/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs b/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs index fcf31f0c6..fc606a004 100644 --- a/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs +++ b/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs @@ -1,7 +1,6 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,46 +11,32 @@ namespace ILGPU.Analyzers.Resources { using System; - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class ErrorMessages { - private static global::System.Resources.ResourceManager resourceMan; + private static System.Resources.ResourceManager resourceMan; - private static global::System.Globalization.CultureInfo resourceCulture; + private static System.Globalization.CultureInfo resourceCulture; - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal ErrorMessages() { } - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ErrorMessages", typeof(ErrorMessages).Assembly); + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ErrorMessages", typeof(ErrorMessages).Assembly); resourceMan = temp; } return resourceMan; } } - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { get { return resourceCulture; } @@ -60,48 +45,51 @@ internal ErrorMessages() { } } - /// - /// Looks up a localized string similar to The type '{0}' containing '{1}' must be partial. - /// internal static string ContainingTypeMustBePartial_Message { get { return ResourceManager.GetString("ContainingTypeMustBePartial_Message", resourceCulture); } } - /// - /// Looks up a localized string similar to Containing type must be partial. - /// internal static string ContainingTypeMustBePartial_Title { get { return ResourceManager.GetString("ContainingTypeMustBePartial_Title", resourceCulture); } } - /// - /// Looks up a localized string similar to The struct '{0}' must be partial. - /// internal static string StructMustBePartial_Message { get { return ResourceManager.GetString("StructMustBePartial_Message", resourceCulture); } } - /// - /// Looks up a localized string similar to Struct must be partial. - /// internal static string StructMustBePartial_Title { get { return ResourceManager.GetString("StructMustBePartial_Title", resourceCulture); } } - /// - /// Looks up a localized string similar to Usage. - /// - internal static string Usage_Category { + internal static string RefTypeInKernel_Message { get { - return ResourceManager.GetString("Usage_Category", resourceCulture); + return ResourceManager.GetString("RefTypeInKernel_Message", resourceCulture); + } + } + + internal static string RefTypeInKernel_Title { + get { + return ResourceManager.GetString("RefTypeInKernel_Title", resourceCulture); + } + } + + internal static string RefTypeArrInKernel_Message { + get { + return ResourceManager.GetString("RefTypeArrInKernel_Message", resourceCulture); + } + } + + internal static string RefTypeArrInKernel_Title { + get { + return ResourceManager.GetString("RefTypeArrInKernel_Title", resourceCulture); } } } diff --git a/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx b/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx index 6603f4ca7..b37a247ca 100644 --- a/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx +++ b/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx @@ -129,7 +129,16 @@ Struct must be partial - - Usage + + Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types. + + + Reference type in kernel + + + Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels. + + + Array of reference types in kernel \ No newline at end of file From 791d1171d39eeddcb94dc8ee99320ef2073697f6 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Mon, 19 Feb 2024 12:04:25 -0500 Subject: [PATCH 03/41] Add sample --- Samples/RefTypeAnalyzer/Program.cs | 50 +++++++++++++++++++ .../RefTypeAnalyzer/RefTypeAnalyzer.csproj | 20 +++++--- 2 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 Samples/RefTypeAnalyzer/Program.cs diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs new file mode 100644 index 000000000..a7f360653 --- /dev/null +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -0,0 +1,50 @@ +using ILGPU; +using ILGPU.Runtime; + +class Program +{ + class MyRefType + { + public int Hello => 42; + } + + struct MyUnmanagedType + { + public int Hello; + + public MyUnmanagedType() + { + Hello = 42; + } + } + + void Kernel(Index1D index, ArrayView input, ArrayView output) + { + // This is disallowed, since MyRefType is a reference type + var refType = new MyRefType(); + output[index] = input[index] + refType.Hello; + + // Allocating arrays of unmanaged types is fine + MyUnmanagedType[] array = [new MyUnmanagedType()]; + int[] ints = [0, 1, 2]; + + // But arrays of reference types are still disallowed + MyRefType[] refs = [new MyRefType()]; + } + + void Main(string[] args) + { + var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + var accelerator = device.CreateAccelerator(context); + + var input = accelerator.Allocate1D(1024); + var output = accelerator.Allocate1D(1024); + + var kernel = accelerator.LoadAutoGroupedStreamKernel, ArrayView>(Kernel); + + kernel(input.IntExtent, input.View, output.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj b/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj index 4e3a1c7e6..8cc34e0d4 100644 --- a/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj +++ b/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj @@ -1,11 +1,17 @@  + + Exe + $(LibrarySamplesTargetFrameworks) + enable + enable + RefTypeAnalyzers + - - Exe - net7.0 - enable - enable - RefTypeAnalyzers - + + + + From 18addc7cc8f3a4b810edfd2b239709dc6f991fad Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 14:10:29 -0500 Subject: [PATCH 04/41] Static main --- Samples/RefTypeAnalyzer/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index a7f360653..ed18fbb2d 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -18,7 +18,7 @@ public MyUnmanagedType() } } - void Kernel(Index1D index, ArrayView input, ArrayView output) + static void Kernel(Index1D index, ArrayView input, ArrayView output) { // This is disallowed, since MyRefType is a reference type var refType = new MyRefType(); @@ -32,7 +32,7 @@ void Kernel(Index1D index, ArrayView input, ArrayView output) MyRefType[] refs = [new MyRefType()]; } - void Main(string[] args) + static void Main(string[] args) { var context = Context.CreateDefault(); var device = context.GetPreferredDevice(false); From d667259cb42fad40bc0e36a95101a2e825960655 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 14:40:16 -0500 Subject: [PATCH 05/41] Re-added samples project to solution --- Samples/ILGPU.Samples.sln | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Samples/ILGPU.Samples.sln b/Samples/ILGPU.Samples.sln index c53441272..2fb92dc7f 100644 --- a/Samples/ILGPU.Samples.sln +++ b/Samples/ILGPU.Samples.sln @@ -137,6 +137,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterleaveFields", "Interle EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ILGPU.Analyzers", "ILGPU.Analyzers", "{6D111E09-49A1-492E-B4CE-E18CE27B56A8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefTypeAnalyzer", "RefTypeAnalyzer\RefTypeAnalyzer.csproj", "{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -383,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 @@ -451,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} From 6b65d8dd9981e9639ad9dd759a57e5403a4773e8 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 14:47:21 -0500 Subject: [PATCH 06/41] Update sample to actually dispose resources --- Samples/RefTypeAnalyzer/Program.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index ed18fbb2d..cdc11c154 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -17,6 +17,11 @@ public MyUnmanagedType() Hello = 42; } } + + static int AnotherFunction() + { + return new MyRefType().Hello; + } static void Kernel(Index1D index, ArrayView input, ArrayView output) { @@ -30,16 +35,19 @@ static void Kernel(Index1D index, ArrayView input, ArrayView output) // But arrays of reference types are still disallowed MyRefType[] refs = [new MyRefType()]; + + // Any functions that may be called are also analyzed + int result = AnotherFunction(); } static void Main(string[] args) { - var context = Context.CreateDefault(); + using var context = Context.CreateDefault(); var device = context.GetPreferredDevice(false); - var accelerator = device.CreateAccelerator(context); + using var accelerator = device.CreateAccelerator(context); - var input = accelerator.Allocate1D(1024); - var output = accelerator.Allocate1D(1024); + using var input = accelerator.Allocate1D(1024); + using var output = accelerator.Allocate1D(1024); var kernel = accelerator.LoadAutoGroupedStreamKernel, ArrayView>(Kernel); From 7e4cc85e7d16a613cf08abc4bbea76b07f95339e Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 15:30:22 -0500 Subject: [PATCH 07/41] Update style --- Samples/RefTypeAnalyzer/Program.cs | 23 ++++++---- Src/ILGPU.Analyzers/KernelAnalyzer.cs | 61 +++++++++++++++------------ 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index cdc11c154..70c2ed41c 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -22,24 +22,26 @@ static int AnotherFunction() { return new MyRefType().Hello; } - + + // TODO: tests needed: normal, using stuff from the class context, arrays, arrays of + // ref types, constructor calls, method/function calls static void Kernel(Index1D index, ArrayView input, ArrayView output) { // This is disallowed, since MyRefType is a reference type var refType = new MyRefType(); output[index] = input[index] + refType.Hello; - + // Allocating arrays of unmanaged types is fine MyUnmanagedType[] array = [new MyUnmanagedType()]; int[] ints = [0, 1, 2]; - + // But arrays of reference types are still disallowed MyRefType[] refs = [new MyRefType()]; - + // Any functions that may be called are also analyzed int result = AnotherFunction(); } - + static void Main(string[] args) { using var context = Context.CreateDefault(); @@ -48,11 +50,14 @@ static void Main(string[] args) using var input = accelerator.Allocate1D(1024); using var output = accelerator.Allocate1D(1024); - - var kernel = accelerator.LoadAutoGroupedStreamKernel, ArrayView>(Kernel); + + var kernel = + accelerator + .LoadAutoGroupedStreamKernel, ArrayView>( + Kernel); kernel(input.IntExtent, input.View, output.View); - + accelerator.Synchronize(); } -} \ No newline at end of file +} diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index 7eb2604e2..cfa9f7fcf 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -10,14 +10,15 @@ namespace ILGPU.Analyzers { public abstract class KernelAnalyzer : DiagnosticAnalyzer { - private readonly ImmutableHashSet kernelLoadNames = ImmutableHashSet.Create( - "LoadKernel", - "LoadAutoGroupedKernel", - "LoadImplicitlyGroupedKernel", - "LoadStreamKernel", - "LoadAutoGroupedStreamKernel", - "LoadImplicitlyGroupedStreamKernel" - ); + private readonly ImmutableHashSet _kernelLoadNames = + ImmutableHashSet.Create( + "LoadKernel", + "LoadAutoGroupedKernel", + "LoadImplicitlyGroupedKernel", + "LoadStreamKernel", + "LoadAutoGroupedStreamKernel", + "LoadImplicitlyGroupedStreamKernel" + ); /// /// Called for every operation potentially reachable from a kernel. @@ -26,8 +27,8 @@ public abstract class KernelAnalyzer : DiagnosticAnalyzer /// The analysis context used to report diagnostics. /// /// - /// The operation. Operations for subsequent calls may be parents or descendants of - /// an operation for a previous call. + /// The operation. Operations for subsequent calls may be parents or descendants + /// of an operation for a previous call. /// protected abstract void AnalyzeKernelOperation( OperationAnalysisContext context, @@ -51,21 +52,23 @@ private void AnalyzeOperation(OperationAnalysisContext context) var methodSymbol = invocationOperation.TargetMethod; if (methodSymbol.MethodKind != MethodKind.Ordinary - || !kernelLoadNames.Contains(methodSymbol.Name)) + || !_kernelLoadNames.Contains(methodSymbol.Name)) return; var kernelArg = invocationOperation.Arguments.FirstOrDefault(x => x.Parameter?.Type.TypeKind == TypeKind.Delegate); + // TODO: support expressions that return delegate (probably requires dataflow) if (kernelArg?.Value is IDelegateCreationOperation - delegateOp) // TODO: support expressions that return delegate (probably requires dataflow) + delegateOp) { var semanticModel = context.Operation.SemanticModel; var bodyOp = delegateOp.Target switch { - IMethodReferenceOperation refOp => GetMethodBody(semanticModel, refOp.Method), + IMethodReferenceOperation refOp => GetMethodBody(semanticModel, + refOp.Method), IAnonymousFunctionOperation anonymousOp => anonymousOp.Body, _ => null }; @@ -75,7 +78,8 @@ private void AnalyzeOperation(OperationAnalysisContext context) } // TODO: iterative using stack - private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context, IOperation? bodyOp, + private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context, + IOperation? bodyOp, HashSet? seen = null) { seen ??= new HashSet(); @@ -90,24 +94,29 @@ private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context seen.Add(descendant); AnalyzeKernelOperation(context, descendant); - // Okay, so this doesn't cover every possible way other code can be called through a kernel. - // But this is in general quite difficult to do. We can improve things over time as necessary, perhaps. - // Thankfully, if we accept a degree of false positives, we probably don't need to solve the halting problem :) + // Okay, so this doesn't cover every possible way other code can be called + // through a kernel. But this is in general quite difficult to do. We can + // improve things over time as necessary, perhaps. Thankfully, if we + // accept a degree of false positives, we probably don't need to solve the + // halting problem :) if (descendant is IInvocationOperation kernelMethodCallOperation) { - var innerBodyOp = GetMethodBody(semanticModel, kernelMethodCallOperation.TargetMethod); + var innerBodyOp = GetMethodBody(semanticModel, + kernelMethodCallOperation.TargetMethod); RecursivelyAnalyzeKernelOperations(context, innerBodyOp, seen); } } } - private IOperation? GetMethodBody(SemanticModel? model, IMethodSymbol symbol) => symbol switch - { - { IsPartialDefinition: false } => model?.GetOperation( - symbol.DeclaringSyntaxReferences[0].GetSyntax()), - { PartialImplementationPart: not null } => model?.GetOperation( - symbol.PartialImplementationPart.DeclaringSyntaxReferences[0].GetSyntax()), - _ => null - }; + private IOperation? GetMethodBody(SemanticModel? model, IMethodSymbol symbol) => + symbol switch + { + { IsPartialDefinition: false } => model?.GetOperation( + symbol.DeclaringSyntaxReferences[0].GetSyntax()), + { PartialImplementationPart: not null } => model?.GetOperation( + symbol.PartialImplementationPart.DeclaringSyntaxReferences[0] + .GetSyntax()), + _ => null + }; } } From 202a0e74574a6ada295d646165f764450077a0b3 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 15:31:57 -0500 Subject: [PATCH 08/41] Add copyright headers --- Samples/RefTypeAnalyzer/Program.cs | 13 ++++++++++++- Src/ILGPU.Analyzers/DiagnosticCategory.cs | 11 +++++++++++ Src/ILGPU.Analyzers/KernelAnalyzer.cs | 11 +++++++++++ Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 11 +++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index 70c2ed41c..40eebce93 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -1,4 +1,15 @@ -using ILGPU; +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2023-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; class Program diff --git a/Src/ILGPU.Analyzers/DiagnosticCategory.cs b/Src/ILGPU.Analyzers/DiagnosticCategory.cs index 4091a03be..d89d0099b 100644 --- a/Src/ILGPU.Analyzers/DiagnosticCategory.cs +++ b/Src/ILGPU.Analyzers/DiagnosticCategory.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2023-2024 ILGPU Project +// www.ilgpu.net +// +// File: DiagnosticCategory.cs +// +// This file is part of ILGPU and is distributed under the University of Illinois Open +// Source License. See LICENSE.txt for details. +// --------------------------------------------------------------------------------------- + namespace ILGPU.Analyzers { public static class DiagnosticCategory diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index cfa9f7fcf..3ba16952f 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2023-2024 ILGPU Project +// www.ilgpu.net +// +// File: KernelAnalyzer.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.Collections.Generic; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index 58e83ee9d..d3e4b6b79 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2023-2024 ILGPU Project +// www.ilgpu.net +// +// File: ReferenceTypeAnalyzer.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.Analyzers.Resources; using System.Collections.Immutable; using Microsoft.CodeAnalysis; From 1ac82d5c94abb754d93909f6a796ac613d2f416b Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 20 Feb 2024 15:32:56 -0500 Subject: [PATCH 09/41] Move sample to correct namespace --- Samples/RefTypeAnalyzer/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index 40eebce93..14838c6a6 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -11,6 +11,7 @@ using ILGPU; using ILGPU.Runtime; +namespace RefTypeAnalyzers; class Program { From ab6e95e2c36421140ba77820222bd132afbca83b Mon Sep 17 00:00:00 2001 From: Yey007 Date: Thu, 22 Feb 2024 12:52:07 -0500 Subject: [PATCH 10/41] Factor out compilation --- Samples/RefTypeAnalyzer/Program.cs | 17 +++-- .../Generic/IncrementalGeneratorVerifier.cs | 33 +++------ .../Generic/SourceCompiler.cs | 42 +++++++++++ Src/ILGPU.Analyzers.Tests/Programs/Simple.cs | 36 +++++++++ Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 13 ++++ Src/ILGPU.Analyzers/KernelAnalyzer.cs | 73 +++++++++++-------- 6 files changed, 153 insertions(+), 61 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/Simple.cs create mode 100644 Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index 14838c6a6..549970913 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -15,16 +15,16 @@ namespace RefTypeAnalyzers; class Program { - class MyRefType + class RefType { public int Hello => 42; } - struct MyUnmanagedType + struct ValueType { public int Hello; - public MyUnmanagedType() + public ValueType() { Hello = 42; } @@ -32,23 +32,24 @@ public MyUnmanagedType() static int AnotherFunction() { - return new MyRefType().Hello; + return new RefType().Hello; } // TODO: tests needed: normal, using stuff from the class context, arrays, arrays of - // ref types, constructor calls, method/function calls + // ref types, constructor calls, method/function calls, structs with ref types in + // them, structs that use ref types in constructors/methods, partial definitions static void Kernel(Index1D index, ArrayView input, ArrayView output) { // This is disallowed, since MyRefType is a reference type - var refType = new MyRefType(); + var refType = new RefType(); output[index] = input[index] + refType.Hello; // Allocating arrays of unmanaged types is fine - MyUnmanagedType[] array = [new MyUnmanagedType()]; + ValueType[] array = [new ValueType()]; int[] ints = [0, 1, 2]; // But arrays of reference types are still disallowed - MyRefType[] refs = [new MyRefType()]; + RefType[] refs = [new RefType()]; // Any functions that may be called are also analyzed int result = AnotherFunction(); diff --git a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs index 9b4771ee0..bfffc52d8 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs @@ -16,6 +16,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using ILGPU.Analyzers.Tests.Generic; using VerifyTests; using VerifyXunit; @@ -26,32 +27,16 @@ public static class IncrementalGeneratorVerifier { public static Task Verify(string source) { - // 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(); @@ -65,4 +50,4 @@ public static Task Verify(string source) return Verifier.Verify(driver, settings); } } -} +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs new file mode 100644 index 000000000..e2f91d6a2 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; + +namespace ILGPU.Analyzers.Tests.Generic; + +public class SourceCompiler +{ + public static CSharpCompilation CreateCompilationWithAssemblies( + 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; + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/Simple.cs new file mode 100644 index 000000000..c157ab9ac --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/Simple.cs @@ -0,0 +1,36 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs; + +public class Simple +{ + class RefType + { + public int Hello => 42; + } + + static void Kernel(Index1D index, ArrayView input, ArrayView output) + { + var refType = new RefType(); + output[index] = input[index] + refType.Hello; + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + using var output = accelerator.Allocate1D(1024); + + var kernel = + accelerator + .LoadAutoGroupedStreamKernel, ArrayView>( + Kernel); + + kernel(input.IntExtent, input.View, output.View); + + accelerator.Synchronize(); + } +} diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs new file mode 100644 index 000000000..2dc6eed73 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Xunit; + +namespace ILGPU.Analyzers.Tests; + +public class RefTypeAnalyzer +{ + [Fact] + public async Task Simple() + { + + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index 3ba16952f..c0b6f36f1 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -21,7 +21,7 @@ namespace ILGPU.Analyzers { public abstract class KernelAnalyzer : DiagnosticAnalyzer { - private readonly ImmutableHashSet _kernelLoadNames = + private readonly ImmutableHashSet kernelLoadNames = ImmutableHashSet.Create( "LoadKernel", "LoadAutoGroupedKernel", @@ -50,7 +50,8 @@ public override void Initialize(AnalysisContext context) context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.EnableConcurrentExecution(); - // Subscribe to semantic (compile time) action invocation, e.g. method invocation. + // Subscribe to semantic (compile time) action invocation + // Subscribe only to method invocations (we want to find the kernel load call) context.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation); } @@ -63,7 +64,7 @@ private void AnalyzeOperation(OperationAnalysisContext context) var methodSymbol = invocationOperation.TargetMethod; if (methodSymbol.MethodKind != MethodKind.Ordinary - || !_kernelLoadNames.Contains(methodSymbol.Name)) + || !kernelLoadNames.Contains(methodSymbol.Name)) return; var kernelArg = invocationOperation.Arguments.FirstOrDefault(x => @@ -74,7 +75,9 @@ private void AnalyzeOperation(OperationAnalysisContext context) IDelegateCreationOperation delegateOp) { - var semanticModel = context.Operation.SemanticModel; + // We should always have a semantic model since we subscribed + // to semantic analysis + var semanticModel = context.Operation.SemanticModel!; var bodyOp = delegateOp.Target switch { @@ -84,47 +87,59 @@ private void AnalyzeOperation(OperationAnalysisContext context) _ => null }; - RecursivelyAnalyzeKernelOperations(context, bodyOp); + if (bodyOp is not null) + { + RecursivelyAnalyzeKernelOperations(context, bodyOp); + } } } - // TODO: iterative using stack private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context, - IOperation? bodyOp, - HashSet? seen = null) + IOperation bodyOp) { - seen ??= new HashSet(); - - if (bodyOp is null) return; - if (seen.Contains(bodyOp)) return; + HashSet seen = new HashSet(); + Stack stack = new Stack(); + stack.Push(bodyOp); - var semanticModel = context.Operation.SemanticModel; + var semanticModel = context.Operation.SemanticModel!; - foreach (var descendant in bodyOp.DescendantsAndSelf()) + while (stack.Count != 0) { - seen.Add(descendant); - AnalyzeKernelOperation(context, descendant); - - // Okay, so this doesn't cover every possible way other code can be called - // through a kernel. But this is in general quite difficult to do. We can - // improve things over time as necessary, perhaps. Thankfully, if we - // accept a degree of false positives, we probably don't need to solve the - // halting problem :) - if (descendant is IInvocationOperation kernelMethodCallOperation) + var op = stack.Pop(); + if (seen.Contains(op)) continue; + + foreach (var descendant in op.DescendantsAndSelf()) { - var innerBodyOp = GetMethodBody(semanticModel, - kernelMethodCallOperation.TargetMethod); - RecursivelyAnalyzeKernelOperations(context, innerBodyOp, seen); + seen.Add(descendant); + AnalyzeKernelOperation(context, descendant); + + var innerBodyOp = GetInvokedOp(semanticModel, descendant); + + if (innerBodyOp is not null) + { + stack.Push(innerBodyOp); + } } } } - private IOperation? GetMethodBody(SemanticModel? model, IMethodSymbol symbol) => + private IOperation? GetInvokedOp(SemanticModel model, IOperation op) + { + // TODO: Are there more ways code can be called? + if (op is IInvocationOperation invocationOperation) + { + return GetMethodBody(model, invocationOperation.TargetMethod); + } + + return null; + } + + private IOperation? GetMethodBody(SemanticModel model, IMethodSymbol symbol) => symbol switch { - { IsPartialDefinition: false } => model?.GetOperation( + { IsPartialDefinition: false } => model.GetOperation( symbol.DeclaringSyntaxReferences[0].GetSyntax()), - { PartialImplementationPart: not null } => model?.GetOperation( + { PartialImplementationPart: not null } => model.GetOperation( symbol.PartialImplementationPart.DeclaringSyntaxReferences[0] .GetSyntax()), _ => null From c4e3926253c71cd7abb8adb7438790b1780eddb9 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 23 Feb 2024 12:38:59 -0500 Subject: [PATCH 11/41] Add simple test --- .../Generic/DiagnosticAnalyzerVerifier.cs | 38 +++++++++++++++++++ .../Generic/IncrementalGeneratorVerifier.cs | 5 +-- .../ILGPU.Analyzers.Tests.csproj | 7 ++++ Src/ILGPU.Analyzers.Tests/InterleaveFields.cs | 3 +- .../Programs/{Simple.cs => RefType.Simple.cs} | 11 ++---- Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 9 ++++- .../RefTypeAnalyzer.Simple.verified.txt | 22 +++++++++++ 7 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs rename Src/ILGPU.Analyzers.Tests/Programs/{Simple.cs => RefType.Simple.cs} (57%) create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs new file mode 100644 index 000000000..3bd96091f --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -0,0 +1,38 @@ +using System.IO; +using System.Threading.Tasks; +using ILGPU.CodeGeneration; +using Microsoft.CodeAnalysis.Diagnostics; +using VerifyTests; +using VerifyXunit; + +namespace ILGPU.Analyzers.Tests.Generic; + +public static class DiagnosticAnalyzerVerifier + where TDiagnosticAnalyzer : DiagnosticAnalyzer, new() +{ + public static async Task Verify(string source) + { + var ilgpuAssemblies = + new[] + { + typeof(InterleaveFieldsAttribute).Assembly, + typeof(TDiagnosticAnalyzer).Assembly + }; + + var compilation = + SourceCompiler.CreateCompilationWithAssemblies("Tests", source, + ilgpuAssemblies); + + var analyzer = new TDiagnosticAnalyzer(); + var options = new AnalyzerOptions([]); + CompilationWithAnalyzers analyzerCompilation = new CompilationWithAnalyzers( + compilation, + [analyzer], options); + + var diagnostics = await analyzerCompilation.GetAnalyzerDiagnosticsAsync(); + + var settings = new VerifySettings(); + settings.UseDirectory(Path.Combine("..", "Snapshots")); + await Verifier.Verify(diagnostics, settings); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs index bfffc52d8..2aaa1ec16 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs @@ -12,15 +12,12 @@ using ILGPU.CodeGeneration; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -using System; using System.IO; -using System.Linq; using System.Threading.Tasks; -using ILGPU.Analyzers.Tests.Generic; using VerifyTests; using VerifyXunit; -namespace ILGPU.Analyzers.Tests +namespace ILGPU.Analyzers.Tests.Generic { public static class IncrementalGeneratorVerifier where TIncrementalGenerator : IIncrementalGenerator, new() diff --git a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj index 67193d893..e25232344 100644 --- a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj +++ b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj @@ -27,4 +27,11 @@ + + + + Always + + + diff --git a/Src/ILGPU.Analyzers.Tests/InterleaveFields.cs b/Src/ILGPU.Analyzers.Tests/InterleaveFields.cs index 5cc1b135a..36d9a3bda 100644 --- a/Src/ILGPU.Analyzers.Tests/InterleaveFields.cs +++ b/Src/ILGPU.Analyzers.Tests/InterleaveFields.cs @@ -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 diff --git a/Src/ILGPU.Analyzers.Tests/Programs/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs similarity index 57% rename from Src/ILGPU.Analyzers.Tests/Programs/Simple.cs rename to Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs index c157ab9ac..a7df045df 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs @@ -9,10 +9,10 @@ class RefType public int Hello => 42; } - static void Kernel(Index1D index, ArrayView input, ArrayView output) + static void Kernel(Index1D index, ArrayView input) { var refType = new RefType(); - output[index] = input[index] + refType.Hello; + input[index] = input[index] + refType.Hello; } static void Run() @@ -22,14 +22,11 @@ static void Run() using var accelerator = device.CreateAccelerator(context); using var input = accelerator.Allocate1D(1024); - using var output = accelerator.Allocate1D(1024); var kernel = - accelerator - .LoadAutoGroupedStreamKernel, ArrayView>( - Kernel); + accelerator.LoadAutoGroupedStreamKernel>(Kernel); - kernel(input.IntExtent, input.View, output.View); + kernel(input.IntExtent, input.View); accelerator.Synchronize(); } diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 2dc6eed73..728d7116d 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -1,5 +1,10 @@ +using System.IO; +using System.Reflection; using System.Threading.Tasks; using Xunit; +using VerifyCS = + ILGPU.Analyzers.Tests.Generic.DiagnosticAnalyzerVerifier< + ILGPU.Analyzers.ReferenceTypeAnalyzer>; namespace ILGPU.Analyzers.Tests; @@ -8,6 +13,8 @@ public class RefTypeAnalyzer [Fact] public async Task Simple() { - + // In build, we copy all programs to output directory. See ILGPU.Analyzers.Tests.csproj + var code = await File.ReadAllTextAsync("Programs/RefType.Simple.cs"); + await VerifyCS.Verify(code); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt new file mode 100644 index 000000000..7218c90aa --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt @@ -0,0 +1,22 @@ +[ + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (13,22)-(13,35), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (14,38)-(14,45), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file From 310521ba250a9ac6e870410ebea79cf110914f20 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 25 Feb 2024 14:50:19 -0500 Subject: [PATCH 12/41] Refactored KernelAnalyzer to only find kernels, leaving recursive analysis up to implementers. --- Samples/RefTypeAnalyzer/Program.cs | 5 +- .../ILGPU.Analyzers.Tests.csproj | 1 + .../Programs/RefType.Arrays.cs | 45 +++++++++ .../Programs/RefType.Simple.cs | 2 +- Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 7 ++ .../RefTypeAnalyzer.Arrays.received.txt | 32 +++++++ .../RefTypeAnalyzer.Arrays.verified.txt | 1 + Src/ILGPU.Analyzers/KernelAnalyzer.cs | 73 +++------------ Src/ILGPU.Analyzers/MethodUtil.cs | 20 ++++ .../Properties/launchSettings.json | 2 +- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 93 ++++++++++++++----- 11 files changed, 191 insertions(+), 90 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt create mode 100644 Src/ILGPU.Analyzers/MethodUtil.cs diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index 549970913..b81ab3c5f 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -49,7 +49,10 @@ static void Kernel(Index1D index, ArrayView input, ArrayView output) int[] ints = [0, 1, 2]; // But arrays of reference types are still disallowed - RefType[] refs = [new RefType()]; + RefType[] other = + [ + new RefType(), + ]; // Any functions that may be called are also analyzed int result = AnotherFunction(); diff --git a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj index e25232344..8c7339e8e 100644 --- a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj +++ b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj @@ -29,6 +29,7 @@ + Always diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs new file mode 100644 index 000000000..4b75c5615 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs @@ -0,0 +1,45 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs; + +class Arrays +{ + class RefType + { + public int Hello => 42; + } + + struct ValueType + { + public int Hello; + + public ValueType() + { + Hello = 42; + } + } + + static void Kernel(Index1D index, ArrayView input) + { + ValueType[] array = [new ValueType()]; + int[] ints = [0, 1, 2]; + + RefType[] refs = [new RefType()]; + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs index a7df045df..463c14a51 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs @@ -2,7 +2,7 @@ namespace ILGPU.Analyzers.Tests.Programs; -public class Simple +class Simple { class RefType { diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 728d7116d..065f28d32 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -17,4 +17,11 @@ public async Task Simple() var code = await File.ReadAllTextAsync("Programs/RefType.Simple.cs"); await VerifyCS.Verify(code); } + + [Fact] + public async Task Arrays() + { + var code = await File.ReadAllTextAsync("Programs/RefType.Arrays.cs"); + await VerifyCS.Verify(code); + } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt new file mode 100644 index 000000000..deceae46e --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt @@ -0,0 +1,32 @@ +[ + { + Id: ILA004, + Title: Array of reference types in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (26,25)-(26,40), + MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., + Category: Usage + }, + { + Id: ILA004, + Title: Array of reference types in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (26,25)-(26,40), + MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (26,26)-(26,39), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index c0b6f36f1..03ebef268 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -29,21 +29,20 @@ public abstract class KernelAnalyzer : DiagnosticAnalyzer "LoadStreamKernel", "LoadAutoGroupedStreamKernel", "LoadImplicitlyGroupedStreamKernel" - ); + ); /// - /// Called for every operation potentially reachable from a kernel. + /// Called for every kernel body. /// /// /// The analysis context used to report diagnostics. /// - /// - /// The operation. Operations for subsequent calls may be parents or descendants - /// of an operation for a previous call. + /// + /// The operation. /// - protected abstract void AnalyzeKernelOperation( + protected abstract void AnalyzeKernelBody( OperationAnalysisContext context, - IOperation operation); + IOperation bodyOp); public override void Initialize(AnalysisContext context) { @@ -81,68 +80,18 @@ private void AnalyzeOperation(OperationAnalysisContext context) var bodyOp = delegateOp.Target switch { - IMethodReferenceOperation refOp => GetMethodBody(semanticModel, + IMethodReferenceOperation refOp => MethodUtil.GetMethodBody( + semanticModel, refOp.Method), IAnonymousFunctionOperation anonymousOp => anonymousOp.Body, - _ => null + _ => null, }; if (bodyOp is not null) { - RecursivelyAnalyzeKernelOperations(context, bodyOp); + AnalyzeKernelBody(context, bodyOp); } } } - - private void RecursivelyAnalyzeKernelOperations(OperationAnalysisContext context, - IOperation bodyOp) - { - HashSet seen = new HashSet(); - Stack stack = new Stack(); - stack.Push(bodyOp); - - var semanticModel = context.Operation.SemanticModel!; - - while (stack.Count != 0) - { - var op = stack.Pop(); - if (seen.Contains(op)) continue; - - foreach (var descendant in op.DescendantsAndSelf()) - { - seen.Add(descendant); - AnalyzeKernelOperation(context, descendant); - - var innerBodyOp = GetInvokedOp(semanticModel, descendant); - - if (innerBodyOp is not null) - { - stack.Push(innerBodyOp); - } - } - } - } - - private IOperation? GetInvokedOp(SemanticModel model, IOperation op) - { - // TODO: Are there more ways code can be called? - if (op is IInvocationOperation invocationOperation) - { - return GetMethodBody(model, invocationOperation.TargetMethod); - } - - return null; - } - - private IOperation? GetMethodBody(SemanticModel model, IMethodSymbol symbol) => - symbol switch - { - { IsPartialDefinition: false } => model.GetOperation( - symbol.DeclaringSyntaxReferences[0].GetSyntax()), - { PartialImplementationPart: not null } => model.GetOperation( - symbol.PartialImplementationPart.DeclaringSyntaxReferences[0] - .GetSyntax()), - _ => null - }; } -} +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs new file mode 100644 index 000000000..ecb92d95d --- /dev/null +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -0,0 +1,20 @@ +using Microsoft.CodeAnalysis; + +namespace ILGPU.Analyzers +{ + public static class MethodUtil + { + public static IOperation? GetMethodBody(SemanticModel model, IMethodSymbol symbol) + { + return symbol switch + { + { IsPartialDefinition: false } => model.GetOperation( + symbol.DeclaringSyntaxReferences[0].GetSyntax()), + { PartialImplementationPart: not null } => model.GetOperation( + symbol.PartialImplementationPart.DeclaringSyntaxReferences[0] + .GetSyntax()), + _ => null + }; + } + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Properties/launchSettings.json b/Src/ILGPU.Analyzers/Properties/launchSettings.json index d924de27b..0b83fbc0c 100644 --- a/Src/ILGPU.Analyzers/Properties/launchSettings.json +++ b/Src/ILGPU.Analyzers/Properties/launchSettings.json @@ -6,7 +6,7 @@ }, "RefType": { "commandName": "DebugRoslynComponent", - "targetProject": "..\\..\\Samples\\\\RefType.csproj" + "targetProject": "..\\..\\Samples\\RefTypeAnalyzer\\RefTypeAnalyzer.csproj" } } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index d3e4b6b79..d3e9de627 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -13,6 +13,8 @@ using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; +using System.Collections.Generic; +using Microsoft.CodeAnalysis.Operations; namespace ILGPU.Analyzers { @@ -37,42 +39,83 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer isEnabledByDefault: true ); - public override ImmutableArray SupportedDiagnostics { get; } = + public override ImmutableArray + SupportedDiagnostics { get; } = ImmutableArray.Create(GeneralDiagnosticRule, ArrayDiagnosticRule); - protected override void AnalyzeKernelOperation(OperationAnalysisContext context, - IOperation operation) + protected override void AnalyzeKernelBody(OperationAnalysisContext context, + IOperation bodyOp) { - if (operation.Type is null) - { - return; - } + Stack bodies = new Stack(); + // To catch mutual recursion + HashSet seenBodies = new HashSet(); + bodies.Push(bodyOp); - if (operation.Type.IsValueType) - { - return; - } + // We will always have a semantic model because KernelAnalyzer subscribes + // to semantic analysis + var semanticModel = context.Operation.SemanticModel!; - if (operation.Type is IArrayTypeSymbol arrayTypeSymbol) + while (bodies.Count != 0) { - if (!arrayTypeSymbol.ElementType.IsValueType) + var op = bodies.Pop(); + + foreach (var descendant in op.DescendantsAndSelf()) { - var arrayDiagnostic = - Diagnostic.Create(ArrayDiagnosticRule, - operation.Syntax.GetLocation(), - operation.Type.ToDisplayString(), - arrayTypeSymbol.ElementType.ToDisplayString()); - context.ReportDiagnostic(arrayDiagnostic); + AnalyzeKernelOperation(context, descendant); + + var innerBodyOp = GetInvokedOp(semanticModel, descendant); + if (innerBodyOp is null) continue; + + if (!seenBodies.Contains(innerBodyOp)) + { + bodies.Push(innerBodyOp); + seenBodies.Add(innerBodyOp); + } } + } + } + private void AnalyzeKernelOperation(OperationAnalysisContext context, + IOperation op) + { + if (op.Type is null) return; + + if (op.Type.IsValueType) + return; + + if (op.Type is IArrayTypeSymbol arrayTypeSymbol) + { + if (arrayTypeSymbol.ElementType.IsValueType) + return; + + string first = arrayTypeSymbol.ToDisplayString(); + string second = arrayTypeSymbol.ElementType.ToDisplayString(); + + var arrayDiagnostic = + Diagnostic.Create(ArrayDiagnosticRule, + op.Syntax.GetLocation(), first, second); + context.ReportDiagnostic(arrayDiagnostic); + } + else + { + var generalDiagnostic = + Diagnostic.Create(GeneralDiagnosticRule, + op.Syntax.GetLocation(), + op.Type.ToDisplayString()); + context.ReportDiagnostic(generalDiagnostic); + } + } + + private IOperation? GetInvokedOp(SemanticModel model, IOperation op) + { + // TODO: Are there more ways code can be called? + if (op is IInvocationOperation invocationOperation) + { + return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); } - var generalDiagnostic = - Diagnostic.Create(GeneralDiagnosticRule, - operation.Syntax.GetLocation(), - operation.Type.ToDisplayString()); - context.ReportDiagnostic(generalDiagnostic); + return null; } } -} +} \ No newline at end of file From c154dc9163435486fe42988abd3e44adec256ae6 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 25 Feb 2024 14:53:38 -0500 Subject: [PATCH 13/41] Fixed array tests --- .../Programs/RefType.Arrays.cs | 5 ++- .../RefTypeAnalyzer.Arrays.received.txt | 32 ------------------- .../RefTypeAnalyzer.Arrays.verified.txt | 23 ++++++++++++- 3 files changed, 26 insertions(+), 34 deletions(-) delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs index 4b75c5615..1524a99de 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs @@ -24,7 +24,10 @@ static void Kernel(Index1D index, ArrayView input) ValueType[] array = [new ValueType()]; int[] ints = [0, 1, 2]; - RefType[] refs = [new RefType()]; + // TODO: the new collection expressions seem to have an issue where analyses + // will be produced twice. If anyone has any information on this, please + // let me know. + RefType[] refs = { new RefType() }; } static void Run() diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt deleted file mode 100644 index deceae46e..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.received.txt +++ /dev/null @@ -1,32 +0,0 @@ -[ - { - Id: ILA004, - Title: Array of reference types in kernel, - Severity: Error, - WarningLevel: 0, - Location: : (26,25)-(26,40), - MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., - Category: Usage - }, - { - Id: ILA004, - Title: Array of reference types in kernel, - Severity: Error, - WarningLevel: 0, - Location: : (26,25)-(26,40), - MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Error, - WarningLevel: 0, - Location: : (26,26)-(26,39), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt index 5f282702b..2b8a93697 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt @@ -1 +1,22 @@ - \ No newline at end of file +[ + { + Id: ILA004, + Title: Array of reference types in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (29,25)-(29,42), + MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (29,27)-(29,40), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file From 8107c65a0fbf87ea269344fd575325544b7126f1 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 25 Feb 2024 15:13:05 -0500 Subject: [PATCH 14/41] Functions tests --- .../Generic/DiagnosticAnalyzerVerifier.cs | 2 +- .../Programs/RefType.Functions.cs | 58 +++++++++++++++++++ Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 8 ++- .../RefTypeAnalyzer.Functions.verified.txt | 32 ++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index 3bd96091f..8c75bc44e 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -25,7 +25,7 @@ public static async Task Verify(string source) var analyzer = new TDiagnosticAnalyzer(); var options = new AnalyzerOptions([]); - CompilationWithAnalyzers analyzerCompilation = new CompilationWithAnalyzers( + var analyzerCompilation = new CompilationWithAnalyzers( compilation, [analyzer], options); diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs new file mode 100644 index 000000000..ee37a3a3b --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs @@ -0,0 +1,58 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs; + +class Functions +{ + class RefType + { + public int Hello => 42; + } + + // This would be disallowed anyways, but the analyzer shouldn't hang + static int Recursion(int i) + { + if (i == 0) return new RefType().Hello; + return Recursion(i - 1); + } + + static int MutualRecursion1(int i) + { + if (i == 0) return new RefType().Hello; + return MutualRecursion2(i - 1); + } + + static int MutualRecursion2(int i) + { + if (i == 0) return 0; // TODO: add allocation here + return MutualRecursion1(i - 1); + } + + static int AnotherFunction() + { + return new RefType().Hello; + } + + static void Kernel(Index1D index, ArrayView input) + { + int result = AnotherFunction(); + int rec1 = Recursion(10); + int rec2 = MutualRecursion1(10); + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 065f28d32..47569da27 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -1,5 +1,4 @@ using System.IO; -using System.Reflection; using System.Threading.Tasks; using Xunit; using VerifyCS = @@ -24,4 +23,11 @@ public async Task Arrays() var code = await File.ReadAllTextAsync("Programs/RefType.Arrays.cs"); await VerifyCS.Verify(code); } + + [Fact] + public async Task Functions() + { + var code = await File.ReadAllTextAsync("Programs/RefType.Functions.cs"); + await VerifyCS.Verify(code); + } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt new file mode 100644 index 000000000..d16e3e508 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt @@ -0,0 +1,32 @@ +[ + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (20,27)-(20,40), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (14,27)-(14,40), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (32,15)-(32,28), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file From d0dacbc74362ab779da630b64f221c67048a69c2 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 25 Feb 2024 15:44:06 -0500 Subject: [PATCH 15/41] Constructor tests --- .../Generic/DiagnosticAnalyzerVerifier.cs | 7 ++-- .../ILGPU.Analyzers.Tests.csproj | 1 + .../Programs/RefType.Constructors.cs | 42 +++++++++++++++++++ Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 7 ++++ .../RefTypeAnalyzer.Constructors.verified.txt | 12 ++++++ Src/ILGPU.Analyzers/MethodUtil.cs | 14 ++++--- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 14 +++++-- 7 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index 8c75bc44e..5ef79c8da 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -25,12 +25,11 @@ public static async Task Verify(string source) var analyzer = new TDiagnosticAnalyzer(); var options = new AnalyzerOptions([]); - var analyzerCompilation = new CompilationWithAnalyzers( - compilation, - [analyzer], options); + var analyzerCompilation = + new CompilationWithAnalyzers(compilation, [analyzer], options); var diagnostics = await analyzerCompilation.GetAnalyzerDiagnosticsAsync(); - + var settings = new VerifySettings(); settings.UseDirectory(Path.Combine("..", "Snapshots")); await Verifier.Verify(diagnostics, settings); diff --git a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj index 8c7339e8e..4c5dbf08d 100644 --- a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj +++ b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj @@ -30,6 +30,7 @@ + Always diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs new file mode 100644 index 000000000..a66712889 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs @@ -0,0 +1,42 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs; + +class Constructors +{ + class RefType + { + public int Hello => 42; + } + + struct ValueType + { + public int Hello; + + public ValueType() + { + Hello = new RefType().Hello; + } + } + + static void Kernel(Index1D index, ArrayView input) + { + ValueType value = new ValueType(); + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 47569da27..ab538c78b 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -30,4 +30,11 @@ public async Task Functions() var code = await File.ReadAllTextAsync("Programs/RefType.Functions.cs"); await VerifyCS.Verify(code); } + + [Fact] + public async Task Constructors() + { + var code = await File.ReadAllTextAsync("Programs/RefType.Constructors.cs"); + await VerifyCS.Verify(code); + } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt new file mode 100644 index 000000000..65e212dfc --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt @@ -0,0 +1,12 @@ +[ + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (17,20)-(17,33), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.Constructors.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index ecb92d95d..19a2f9160 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -8,13 +8,15 @@ public static class MethodUtil { return symbol switch { - { IsPartialDefinition: false } => model.GetOperation( - symbol.DeclaringSyntaxReferences[0].GetSyntax()), - { PartialImplementationPart: not null } => model.GetOperation( - symbol.PartialImplementationPart.DeclaringSyntaxReferences[0] - .GetSyntax()), + { + IsPartialDefinition: false, + DeclaringSyntaxReferences: var refs, + } => refs.Length > 0 ? model.GetOperation(refs[0].GetSyntax()) : null, + { + PartialImplementationPart: { DeclaringSyntaxReferences: var refs }, + } => refs.Length > 0 ? model.GetOperation(refs[0].GetSyntax()) : null, _ => null }; } } -} \ No newline at end of file +} diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index d3e9de627..5b632bee2 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -28,7 +28,7 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true - ); + ); private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", @@ -37,7 +37,7 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true - ); + ); public override ImmutableArray SupportedDiagnostics { get; } = @@ -115,7 +115,15 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); } + if (op is IObjectCreationOperation creationOperation) + { + if (creationOperation.Constructor is not null) + { + return MethodUtil.GetMethodBody(model, creationOperation.Constructor); + } + } + return null; } } -} \ No newline at end of file +} From 2b1e10233627d6b11619790ade178c92bc7f37a3 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Thu, 7 Mar 2024 12:02:03 -0500 Subject: [PATCH 16/41] Check for unmanaged types instead of value types - Analyzer now ignores only unmanaged types, not value types - Analyzer now ignores ILGPU types - Additional tests for this new behavior - Change test folder structure --- .../ILGPU.Analyzers.Tests.csproj | 2 - .../{RefType.Arrays.cs => RefType/Arrays.cs} | 2 +- .../Constructors.cs} | 2 +- .../Functions.cs} | 2 +- .../Programs/RefType/ManagedUnmanaged.cs | 59 +++++++++++++++++++ .../{RefType.Simple.cs => RefType/Simple.cs} | 2 +- Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 15 +++-- .../RefTypeAnalyzer.Arrays.verified.txt | 4 +- .../RefTypeAnalyzer.Constructors.verified.txt | 2 +- .../RefTypeAnalyzer.Functions.verified.txt | 6 +- ...TypeAnalyzer.ManagedUnmanaged.verified.txt | 12 ++++ .../RefTypeAnalyzer.Simple.verified.txt | 4 +- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 18 +++--- 13 files changed, 105 insertions(+), 25 deletions(-) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType.Arrays.cs => RefType/Arrays.cs} (95%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType.Constructors.cs => RefType/Constructors.cs} (94%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType.Functions.cs => RefType/Functions.cs} (96%) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs rename Src/ILGPU.Analyzers.Tests/Programs/{RefType.Simple.cs => RefType/Simple.cs} (93%) create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj index 4c5dbf08d..e25232344 100644 --- a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj +++ b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj @@ -29,8 +29,6 @@ - - Always diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Arrays.cs similarity index 95% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs rename to Src/ILGPU.Analyzers.Tests/Programs/RefType/Arrays.cs index 1524a99de..15e5c6bab 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Arrays.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs; +namespace ILGPU.Analyzers.Tests.Programs.RefType; class Arrays { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Constructors.cs similarity index 94% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs rename to Src/ILGPU.Analyzers.Tests/Programs/RefType/Constructors.cs index a66712889..0e249f4f4 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Constructors.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Constructors.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs; +namespace ILGPU.Analyzers.Tests.Programs.RefType; class Constructors { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs similarity index 96% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs rename to Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs index ee37a3a3b..1f9c405df 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Functions.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs; +namespace ILGPU.Analyzers.Tests.Programs.RefType; class Functions { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs new file mode 100644 index 000000000..90f1f6ad1 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs @@ -0,0 +1,59 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs.RefType; + +class ManagedUnmanaged +{ + class RefType + { + } + + struct Unmanaged + { + private int a; + private int b; + } + + struct Managed + { + private int a; + private RefType r; + } + + class RefType + { + public int Hello => 42; + } + + struct ValueType + { + public int Hello; + + public ValueType() + { + Hello = 42; + } + } + + static void Kernel(Index1D index, ArrayView input) + { + var unmanaged = new Unmanaged(); + var managed = new Managed(); + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Simple.cs similarity index 93% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs rename to Src/ILGPU.Analyzers.Tests/Programs/RefType/Simple.cs index 463c14a51..c150aeacf 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType.Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Simple.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs; +namespace ILGPU.Analyzers.Tests.Programs.RefType; class Simple { diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index ab538c78b..10091cfcd 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -13,28 +13,35 @@ public class RefTypeAnalyzer public async Task Simple() { // In build, we copy all programs to output directory. See ILGPU.Analyzers.Tests.csproj - var code = await File.ReadAllTextAsync("Programs/RefType.Simple.cs"); + var code = await File.ReadAllTextAsync("Programs/RefType/Simple.cs"); await VerifyCS.Verify(code); } [Fact] public async Task Arrays() { - var code = await File.ReadAllTextAsync("Programs/RefType.Arrays.cs"); + var code = await File.ReadAllTextAsync("Programs/RefType/Arrays.cs"); await VerifyCS.Verify(code); } [Fact] public async Task Functions() { - var code = await File.ReadAllTextAsync("Programs/RefType.Functions.cs"); + var code = await File.ReadAllTextAsync("Programs/RefType/Functions.cs"); await VerifyCS.Verify(code); } [Fact] public async Task Constructors() { - var code = await File.ReadAllTextAsync("Programs/RefType.Constructors.cs"); + var code = await File.ReadAllTextAsync("Programs/RefType/Constructors.cs"); + await VerifyCS.Verify(code); + } + + [Fact] + public async Task ManagedUnmanaged() + { + var code = await File.ReadAllTextAsync("Programs/RefType/ManagedUnmanaged.cs"); await VerifyCS.Verify(code); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt index 2b8a93697..aa0640cc7 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 0, Location: : (29,25)-(29,42), MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 0, Location: : (29,27)-(29,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt index 65e212dfc..d255351b8 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 0, Location: : (17,20)-(17,33), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Constructors.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Constructors.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt index d16e3e508..de1598cbc 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 0, Location: : (20,27)-(20,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 0, Location: : (14,27)-(14,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage }, { @@ -26,7 +26,7 @@ WarningLevel: 0, Location: : (32,15)-(32,28), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt new file mode 100644 index 000000000..c1cb4d6ae --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt @@ -0,0 +1,12 @@ +[ + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (40,22)-(40,35), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.ManagedUnmanaged.Managed' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt index 7218c90aa..dfa94aa89 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 0, Location: : (13,22)-(13,35), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 0, Location: : (14,38)-(14,45), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index 5b632bee2..711bfcf1d 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -28,7 +28,7 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true - ); + ); private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", @@ -37,7 +37,9 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true - ); + ); + + private const string ILGPUAssemblyName = "ILGPU"; public override ImmutableArray SupportedDiagnostics { get; } = @@ -47,7 +49,7 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, IOperation bodyOp) { Stack bodies = new Stack(); - // To catch mutual recursion + // To catch recursion HashSet seenBodies = new HashSet(); bodies.Push(bodyOp); @@ -81,12 +83,12 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, if (op.Type is null) return; - if (op.Type.IsValueType) + if (op.Type.IsUnmanagedType) return; if (op.Type is IArrayTypeSymbol arrayTypeSymbol) { - if (arrayTypeSymbol.ElementType.IsValueType) + if (arrayTypeSymbol.ElementType.IsUnmanagedType) return; string first = arrayTypeSymbol.ToDisplayString(); @@ -99,6 +101,9 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, } else { + if (op.Type.ContainingAssembly.Name == ILGPUAssemblyName) + return; + var generalDiagnostic = Diagnostic.Create(GeneralDiagnosticRule, op.Syntax.GetLocation(), @@ -109,7 +114,6 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, private IOperation? GetInvokedOp(SemanticModel model, IOperation op) { - // TODO: Are there more ways code can be called? if (op is IInvocationOperation invocationOperation) { return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); @@ -126,4 +130,4 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return null; } } -} +} \ No newline at end of file From b10a162645797cbc8034144dea381d482ce066c4 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Tue, 19 Mar 2024 00:02:20 -0400 Subject: [PATCH 17/41] Kernel discovery tests --- .../Programs/RefType/Functions.cs | 4 +- .../Programs/RefType/LoadDiscovery.cs | 64 +++++++++++++++++++ Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 7 ++ ...RefTypeAnalyzer.LoadDiscovery.verified.txt | 62 ++++++++++++++++++ Src/ILGPU.Analyzers/KernelAnalyzer.cs | 15 ++++- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 16 +++-- 6 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs index 1f9c405df..b68c2ac47 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs @@ -9,7 +9,7 @@ class RefType public int Hello => 42; } - // This would be disallowed anyways, but the analyzer shouldn't hang + // This wouldn't compile anyways, but the analyzer shouldn't hang static int Recursion(int i) { if (i == 0) return new RefType().Hello; @@ -24,7 +24,7 @@ static int MutualRecursion1(int i) static int MutualRecursion2(int i) { - if (i == 0) return 0; // TODO: add allocation here + if (i == 0) return 0; return MutualRecursion1(i - 1); } diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs new file mode 100644 index 000000000..04ba3bbc9 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs @@ -0,0 +1,64 @@ +using ILGPU.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs.RefType; + +class Functions +{ + class RefType + { + public int Hello => 42; + } + + // Separate kernels so we can observe multiple analyses + static void Kernel1(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Kernel2(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Kernel3(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Kernel4(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Kernel5(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Kernel6(Index1D index, ArrayView input) + { + int result = new RefType().Hello; + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + // Also make sure kernels loaded twice aren't analyzed twice + var kernel1 = accelerator.LoadStreamKernel>(Kernel1); + var twice = accelerator.LoadStreamKernel>(Kernel1); + + var kernel2 = accelerator.LoadAutoGroupedStreamKernel>(Kernel2); + var kernel3 = + accelerator.LoadImplicitlyGroupedStreamKernel>( + Kernel3, + 32); + + var kernel4 = accelerator.LoadKernel>(Kernel4); + var kernel5 = accelerator.LoadAutoGroupedKernel>(Kernel5); + var kernel6 = + accelerator.LoadImplicitlyGroupedKernel>(Kernel6, 32); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 10091cfcd..61b35e07c 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -44,4 +44,11 @@ public async Task ManagedUnmanaged() var code = await File.ReadAllTextAsync("Programs/RefType/ManagedUnmanaged.cs"); await VerifyCS.Verify(code); } + + [Fact] + public async Task LoadDiscovery() + { + var code = await File.ReadAllTextAsync("Programs/RefType/LoadDiscovery.cs"); + await VerifyCS.Verify(code); + } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt new file mode 100644 index 000000000..39ce79130 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt @@ -0,0 +1,62 @@ +[ + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (14,21)-(14,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (19,21)-(19,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (24,21)-(24,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (29,21)-(29,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (34,21)-(34,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + }, + { + Id: ILA003, + Title: Reference type in kernel, + Severity: Error, + WarningLevel: 0, + Location: : (39,21)-(39,34), + MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index 03ebef268..577242593 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -9,7 +9,7 @@ // Source License. See LICENSE.txt for details. // --------------------------------------------------------------------------------------- -using System.Collections.Generic; +using System.Collections.Concurrent; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; @@ -31,6 +31,8 @@ public abstract class KernelAnalyzer : DiagnosticAnalyzer "LoadImplicitlyGroupedStreamKernel" ); + private ConcurrentDictionary seen = new(); + /// /// Called for every kernel body. /// @@ -49,6 +51,8 @@ public override void Initialize(AnalysisContext context) context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.EnableConcurrentExecution(); + seen.Clear(); + // Subscribe to semantic (compile time) action invocation // Subscribe only to method invocations (we want to find the kernel load call) context.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation); @@ -56,6 +60,8 @@ public override void Initialize(AnalysisContext context) private void AnalyzeOperation(OperationAnalysisContext context) { + if (seen.ContainsKey(context.Operation)) return; + if (context.Operation is not IInvocationOperation invocationOperation || context.Operation.Syntax is not InvocationExpressionSyntax) return; @@ -89,7 +95,12 @@ private void AnalyzeOperation(OperationAnalysisContext context) if (bodyOp is not null) { - AnalyzeKernelBody(context, bodyOp); + // If we couldn't add it, another thread got here first. + bool added = seen.TryAdd(bodyOp, true); + if (added) + { + AnalyzeKernelBody(context, bodyOp); + } } } } diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index 711bfcf1d..eacee21d6 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -101,7 +101,7 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, } else { - if (op.Type.ContainingAssembly.Name == ILGPUAssemblyName) + if (IsILGPUSymbol(op.Type)) return; var generalDiagnostic = @@ -116,18 +116,22 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, { if (op is IInvocationOperation invocationOperation) { + if (IsILGPUSymbol(invocationOperation.TargetMethod)) return null; return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); } - if (op is IObjectCreationOperation creationOperation) + if (op is IObjectCreationOperation { Constructor: not null } creationOperation) { - if (creationOperation.Constructor is not null) - { - return MethodUtil.GetMethodBody(model, creationOperation.Constructor); - } + if (IsILGPUSymbol(creationOperation.Constructor)) return null; + return MethodUtil.GetMethodBody(model, creationOperation.Constructor); } return null; } + + private bool IsILGPUSymbol(ISymbol symbol) + { + return symbol.ContainingAssembly.Name == ILGPUAssemblyName; + } } } \ No newline at end of file From 0b987a6ece7406f6e59474225dc490b3bf99c074 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 29 Mar 2024 23:02:37 -0400 Subject: [PATCH 18/41] Remove TODO in sample --- Samples/RefTypeAnalyzer/Program.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/RefTypeAnalyzer/Program.cs index b81ab3c5f..8ca0d4643 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/RefTypeAnalyzer/Program.cs @@ -35,9 +35,6 @@ static int AnotherFunction() return new RefType().Hello; } - // TODO: tests needed: normal, using stuff from the class context, arrays, arrays of - // ref types, constructor calls, method/function calls, structs with ref types in - // them, structs that use ref types in constructors/methods, partial definitions static void Kernel(Index1D index, ArrayView input, ArrayView output) { // This is disallowed, since MyRefType is a reference type From ed021bb8d9fc68055138cb028609cb55c4330889 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 29 Mar 2024 23:16:26 -0400 Subject: [PATCH 19/41] Refactor tests into theory --- .../Generic/DiagnosticAnalyzerVerifier.cs | 8 ++- Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs | 49 ++++--------------- ...alyzer.FileTests_file=Arrays.verified.txt} | 0 ....FileTests_file=Constructors.verified.txt} | 0 ...zer.FileTests_file=Functions.verified.txt} | 0 ...FileTests_file=LoadDiscovery.verified.txt} | 0 ...eTests_file=ManagedUnmanaged.verified.txt} | 0 ...alyzer.FileTests_file=Simple.verified.txt} | 0 8 files changed, 17 insertions(+), 40 deletions(-) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.Arrays.verified.txt => RefTypeAnalyzer.FileTests_file=Arrays.verified.txt} (100%) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.Constructors.verified.txt => RefTypeAnalyzer.FileTests_file=Constructors.verified.txt} (100%) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.Functions.verified.txt => RefTypeAnalyzer.FileTests_file=Functions.verified.txt} (100%) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.LoadDiscovery.verified.txt => RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt} (100%) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.ManagedUnmanaged.verified.txt => RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt} (100%) rename Src/ILGPU.Analyzers.Tests/Snapshots/{RefTypeAnalyzer.Simple.verified.txt => RefTypeAnalyzer.FileTests_file=Simple.verified.txt} (100%) diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index 5ef79c8da..ed4836e91 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Threading.Tasks; using ILGPU.CodeGeneration; @@ -10,7 +11,8 @@ namespace ILGPU.Analyzers.Tests.Generic; public static class DiagnosticAnalyzerVerifier where TDiagnosticAnalyzer : DiagnosticAnalyzer, new() { - public static async Task Verify(string source) + public static async Task Verify(string source, + Action configure = null) { var ilgpuAssemblies = new[] @@ -32,6 +34,10 @@ public static async Task Verify(string source) var settings = new VerifySettings(); settings.UseDirectory(Path.Combine("..", "Snapshots")); + + if (configure is not null) + configure(settings); + await Verifier.Verify(diagnostics, settings); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs index 61b35e07c..26ef41d4e 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs @@ -9,46 +9,17 @@ namespace ILGPU.Analyzers.Tests; public class RefTypeAnalyzer { - [Fact] - public async Task Simple() + [Theory] + [InlineData("Simple")] + [InlineData("Arrays")] + [InlineData("Functions")] + [InlineData("Constructors")] + [InlineData("ManagedUnmanaged")] + [InlineData("LoadDiscovery")] + 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/RefType/Simple.cs"); - await VerifyCS.Verify(code); - } - - [Fact] - public async Task Arrays() - { - var code = await File.ReadAllTextAsync("Programs/RefType/Arrays.cs"); - await VerifyCS.Verify(code); - } - - [Fact] - public async Task Functions() - { - var code = await File.ReadAllTextAsync("Programs/RefType/Functions.cs"); - await VerifyCS.Verify(code); - } - - [Fact] - public async Task Constructors() - { - var code = await File.ReadAllTextAsync("Programs/RefType/Constructors.cs"); - await VerifyCS.Verify(code); - } - - [Fact] - public async Task ManagedUnmanaged() - { - var code = await File.ReadAllTextAsync("Programs/RefType/ManagedUnmanaged.cs"); - await VerifyCS.Verify(code); - } - - [Fact] - public async Task LoadDiscovery() - { - var code = await File.ReadAllTextAsync("Programs/RefType/LoadDiscovery.cs"); - await VerifyCS.Verify(code); + var code = await File.ReadAllTextAsync($"Programs/RefType/{file}.cs"); + await VerifyCS.Verify(code, settings => settings.UseParameters(file)); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Arrays.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Constructors.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Functions.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.LoadDiscovery.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.ManagedUnmanaged.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.Simple.verified.txt rename to Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt From 7eef5d5bced91cebd171e644659b149fee9c93cd Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 29 Mar 2024 23:25:24 -0400 Subject: [PATCH 20/41] Switch to warning severity --- ...efTypeAnalyzer.FileTests_file=Arrays.verified.txt | 4 ++-- ...Analyzer.FileTests_file=Constructors.verified.txt | 2 +- ...ypeAnalyzer.FileTests_file=Functions.verified.txt | 6 +++--- ...nalyzer.FileTests_file=LoadDiscovery.verified.txt | 12 ++++++------ ...yzer.FileTests_file=ManagedUnmanaged.verified.txt | 2 +- ...efTypeAnalyzer.FileTests_file=Simple.verified.txt | 4 ++-- Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md | 12 ++++++------ Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 5 +++-- 8 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt index aa0640cc7..0d1f7bb53 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -2,7 +2,7 @@ { Id: ILA004, Title: Array of reference types in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (29,25)-(29,42), MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., @@ -12,7 +12,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (29,27)-(29,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt index d255351b8..ed67b5629 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt @@ -2,7 +2,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (17,20)-(17,33), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt index de1598cbc..4800773fc 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt @@ -2,7 +2,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (20,27)-(20,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -12,7 +12,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (14,27)-(14,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -22,7 +22,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (32,15)-(32,28), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt index 39ce79130..b0d816910 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt @@ -2,7 +2,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (14,21)-(14,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -12,7 +12,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (19,21)-(19,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -22,7 +22,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (24,21)-(24,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -32,7 +32,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (29,21)-(29,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -42,7 +42,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (34,21)-(34,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -52,7 +52,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (39,21)-(39,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt index c1cb4d6ae..5717322a2 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt @@ -2,7 +2,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (40,22)-(40,35), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt index dfa94aa89..1f6a342e6 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt @@ -2,7 +2,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (13,22)-(13,35), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -12,7 +12,7 @@ { Id: ILA003, Title: Reference type in kernel, - Severity: Error, + Severity: Warning, WarningLevel: 0, Location: : (14,38)-(14,45), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md index 9ac28829e..0a9ffa5c6 100644 --- a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md +++ b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md @@ -3,9 +3,9 @@ ### New Rules -Rule ID | Category | Severity | Notes ---------|----------|----------|------- -ILA001 | CodeGeneration | Error | SourceGenerator -ILA002 | CodeGeneration | Error | SourceGenerator -ILA003 | Usage | Error | Analyzer -ILA004 | Usage | Error | Analyzer \ No newline at end of file +| Rule ID | Category | Severity | Notes | +|---------|----------------|----------|-----------------| +| ILA001 | CodeGeneration | Error | SourceGenerator | +| ILA002 | CodeGeneration | Error | SourceGenerator | +| ILA003 | Usage | Warning | Analyzer | +| ILA004 | Usage | Warning | Analyzer | \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index eacee21d6..8f9326ec0 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -26,7 +26,7 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer title: ErrorMessages.RefTypeInKernel_Title, messageFormat: ErrorMessages.RefTypeInKernel_Message, category: DiagnosticCategory.Usage, - defaultSeverity: DiagnosticSeverity.Error, + defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true ); @@ -35,7 +35,7 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer title: ErrorMessages.RefTypeArrInKernel_Title, messageFormat: ErrorMessages.RefTypeArrInKernel_Message, category: DiagnosticCategory.Usage, - defaultSeverity: DiagnosticSeverity.Error, + defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true ); @@ -129,6 +129,7 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return null; } + // TODO: test that ILGPU symbols are skipped private bool IsILGPUSymbol(ISymbol symbol) { return symbol.ContainingAssembly.Name == ILGPUAssemblyName; From 61fd1798c7cf9fcba403c28687e519de06b8f2b7 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Fri, 29 Mar 2024 23:34:52 -0400 Subject: [PATCH 21/41] Factor out resources files for ILA001 and ILA002 --- ...nalyzer.FileTests_file=Arrays.verified.txt | 4 +- ...r.FileTests_file=Constructors.verified.txt | 2 +- ...yzer.FileTests_file=Functions.verified.txt | 6 +- ....FileTests_file=LoadDiscovery.verified.txt | 12 +- ...leTests_file=ManagedUnmanaged.verified.txt | 2 +- ...nalyzer.FileTests_file=Simple.verified.txt | 4 +- Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj | 25 ++- .../InterleaveFieldsGenerator.cs | 8 +- Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs | 8 +- .../Resources/ErrorMessages.resx | 144 ------------------ ...=> ILA001_StructMustBePartial.Designer.cs} | 52 +------ .../Resources/ILA001_StructMustBePartial.resx | 27 ++++ ...02_ContainingTypeMustBePartial.Designer.cs | 60 ++++++++ .../ILA002_ContainingTypeMustBePartial.resx | 27 ++++ .../ILA003_ReferenceTypeInKernel.resx | 3 - .../ILA004_ReferenceTypeArrayInKernel.resx | 3 - 16 files changed, 162 insertions(+), 225 deletions(-) delete mode 100644 Src/ILGPU.Analyzers/Resources/ErrorMessages.resx rename Src/ILGPU.Analyzers/Resources/{ErrorMessages.Designer.cs => ILA001_StructMustBePartial.Designer.cs} (50%) create mode 100644 Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx create mode 100644 Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs create mode 100644 Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt index 0d1f7bb53..d449bbc7b 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -3,7 +3,7 @@ Id: ILA004, Title: Array of reference types in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (29,25)-(29,42), MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., @@ -13,7 +13,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (29,27)-(29,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt index ed67b5629..d78af2e34 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt @@ -3,7 +3,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (17,20)-(17,33), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Constructors.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt index 4800773fc..4de525844 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt @@ -3,7 +3,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (20,27)-(20,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -13,7 +13,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (14,27)-(14,40), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -23,7 +23,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (32,15)-(32,28), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt index b0d816910..269b4d89c 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt @@ -3,7 +3,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (14,21)-(14,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -13,7 +13,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (19,21)-(19,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -23,7 +23,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (24,21)-(24,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -33,7 +33,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (29,21)-(29,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -43,7 +43,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (34,21)-(34,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -53,7 +53,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (39,21)-(39,34), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt index 5717322a2..374c6bdbf 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt @@ -3,7 +3,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (40,22)-(40,35), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.ManagedUnmanaged.Managed' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt index 1f6a342e6..f0b1f7062 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt @@ -3,7 +3,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (13,22)-(13,35), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., @@ -13,7 +13,7 @@ Id: ILA003, Title: Reference type in kernel, Severity: Warning, - WarningLevel: 0, + WarningLevel: 1, Location: : (14,38)-(14,45), MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., diff --git a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj index c085bc038..1ae27bb95 100644 --- a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj +++ b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj @@ -36,27 +36,36 @@ - + True True - ErrorMessages.resx + DiagnosticCategory.resx - + True True - DiagnosticCategory.resx + ILA001_StructMustBePartial.resx + + + True + True + ILA002_ContainingTypeMustBePartial.resx - - ResXFileCodeGenerator - ErrorMessages.Designer.cs - ResXFileCodeGenerator DiagnosticCategory.Designer.cs + + ResXFileCodeGenerator + ILA001_StructMustBePartial.Designer.cs + + + ResXFileCodeGenerator + ILA002_ContainingTypeMustBePartial.Designer.cs + diff --git a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs index 9ed076aa9..0a3ef96ba 100644 --- a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs +++ b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs @@ -32,16 +32,16 @@ public class InterleaveFieldsGenerator : IIncrementalGenerator private static readonly DiagnosticDescriptor TargetNotPartial = new( id: "ILA001", - title: ErrorMessages.StructMustBePartial_Title, - messageFormat: ErrorMessages.StructMustBePartial_Message, + title: ILA001_StructMustBePartial.Title, + messageFormat: ILA001_StructMustBePartial.Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); private static readonly DiagnosticDescriptor ContainingTypeNotPartial = new( id: "ILA002", - title: ErrorMessages.ContainingTypeMustBePartial_Title, - messageFormat: ErrorMessages.ContainingTypeMustBePartial_Message, + title: ILA002_ContainingTypeMustBePartial.Title, + messageFormat: ILA002_ContainingTypeMustBePartial.Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs index 8f9326ec0..79fbffde0 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs @@ -23,8 +23,8 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer { private static readonly DiagnosticDescriptor GeneralDiagnosticRule = new( id: "ILA003", - title: ErrorMessages.RefTypeInKernel_Title, - messageFormat: ErrorMessages.RefTypeInKernel_Message, + title: ILA003_ReferenceTypeInKernel.Title, + messageFormat: ILA003_ReferenceTypeInKernel.MessageFormat, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -32,8 +32,8 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", - title: ErrorMessages.RefTypeArrInKernel_Title, - messageFormat: ErrorMessages.RefTypeArrInKernel_Message, + title: ILA004_ReferenceTypeArrayInKernel.Title, + messageFormat: ILA004_ReferenceTypeArrayInKernel.MessageFormat, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true diff --git a/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx b/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx deleted file mode 100644 index b37a247ca..000000000 --- a/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx +++ /dev/null @@ -1,144 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - The type '{0}' containing '{1}' must be partial - - - Containing type must be partial - - - The struct '{0}' must be partial - - - Struct must be partial - - - Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types. - - - Reference type in kernel - - - Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels. - - - Array of reference types in kernel - - \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs similarity index 50% rename from Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs rename to Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs index fc606a004..8695769cc 100644 --- a/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs +++ b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs @@ -11,24 +11,24 @@ namespace ILGPU.Analyzers.Resources { using System; - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ErrorMessages { + internal class ILA001_StructMustBePartial { private static System.Resources.ResourceManager resourceMan; private static System.Globalization.CultureInfo resourceCulture; [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ErrorMessages() { + internal ILA001_StructMustBePartial() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] internal static System.Resources.ResourceManager ResourceManager { get { if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ErrorMessages", typeof(ErrorMessages).Assembly); + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA001_StructMustBePartial", typeof(ILA001_StructMustBePartial).Assembly); resourceMan = temp; } return resourceMan; @@ -45,51 +45,15 @@ internal static System.Globalization.CultureInfo Culture { } } - internal static string ContainingTypeMustBePartial_Message { + internal static string Message { get { - return ResourceManager.GetString("ContainingTypeMustBePartial_Message", resourceCulture); + return ResourceManager.GetString("Message", resourceCulture); } } - internal static string ContainingTypeMustBePartial_Title { + internal static string Title { get { - return ResourceManager.GetString("ContainingTypeMustBePartial_Title", resourceCulture); - } - } - - internal static string StructMustBePartial_Message { - get { - return ResourceManager.GetString("StructMustBePartial_Message", resourceCulture); - } - } - - internal static string StructMustBePartial_Title { - get { - return ResourceManager.GetString("StructMustBePartial_Title", resourceCulture); - } - } - - internal static string RefTypeInKernel_Message { - get { - return ResourceManager.GetString("RefTypeInKernel_Message", resourceCulture); - } - } - - internal static string RefTypeInKernel_Title { - get { - return ResourceManager.GetString("RefTypeInKernel_Title", resourceCulture); - } - } - - internal static string RefTypeArrInKernel_Message { - get { - return ResourceManager.GetString("RefTypeArrInKernel_Message", resourceCulture); - } - } - - internal static string RefTypeArrInKernel_Title { - get { - return ResourceManager.GetString("RefTypeArrInKernel_Title", resourceCulture); + return ResourceManager.GetString("Title", resourceCulture); } } } diff --git a/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx new file mode 100644 index 000000000..09c6d7b6b --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + The struct '{0}' must be partial + + + Struct must be partial + + \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs new file mode 100644 index 000000000..ed2a50901 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ILGPU.Analyzers.Resources { + using System; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class ILA002_ContainingTypeMustBePartial { + + private static System.Resources.ResourceManager resourceMan; + + private static System.Globalization.CultureInfo resourceCulture; + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal ILA002_ContainingTypeMustBePartial() { + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { + get { + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA002_ContainingTypeMustBePartial", typeof(ILA002_ContainingTypeMustBePartial).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + internal static string Message { + get { + return ResourceManager.GetString("Message", resourceCulture); + } + } + + internal static string Title { + get { + return ResourceManager.GetString("Title", resourceCulture); + } + } + } +} diff --git a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx new file mode 100644 index 000000000..bfd3e05b3 --- /dev/null +++ b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + The type '{0}' containing '{1}' must be partial + + + Containing type must be partial + + \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx index f61b774d4..b5969d9d2 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx +++ b/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx @@ -24,7 +24,4 @@ Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types. - - Reference types like classes cannot be used in kernels. - \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx index b0f044bb0..cd121f953 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx +++ b/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx @@ -18,9 +18,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - Arrays of reference types cannot be used in kernels. - Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels. From e642cd20c9a8f489a0fee73a31eeb42442244495 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 00:07:20 -0400 Subject: [PATCH 22/41] Rebrand as ManagedTypeAnalyzer --- ...TypeAnalyzer.cs => ManagedTypeAnalyzer.cs} | 4 +- .../{RefType => ManagedType}/Arrays.cs | 0 .../Complex.cs} | 0 .../{RefType => ManagedType}/Constructors.cs | 0 .../{RefType => ManagedType}/Functions.cs | 0 .../{RefType => ManagedType}/LoadDiscovery.cs | 0 .../{RefType => ManagedType}/Simple.cs | 0 ...nalyzer.FileTests_file=Arrays.verified.txt | 22 +++++++ ...alyzer.FileTests_file=Complex.verified.txt | 12 ++++ ...r.FileTests_file=Constructors.verified.txt | 12 ++++ ...yzer.FileTests_file=Functions.verified.txt | 32 ++++++++++ ....FileTests_file=LoadDiscovery.verified.txt | 62 +++++++++++++++++++ ...nalyzer.FileTests_file=Simple.verified.txt | 22 +++++++ ...nalyzer.FileTests_file=Arrays.verified.txt | 22 ------- ...r.FileTests_file=Constructors.verified.txt | 12 ---- ...yzer.FileTests_file=Functions.verified.txt | 32 ---------- ....FileTests_file=LoadDiscovery.verified.txt | 62 ------------------- ...leTests_file=ManagedUnmanaged.verified.txt | 12 ---- ...nalyzer.FileTests_file=Simple.verified.txt | 22 ------- ...TypeAnalyzer.cs => ManagedTypeAnalyzer.cs} | 13 ++-- ...=> ILA003_ManagedTypeInKernel.Designer.cs} | 6 +- ...l.resx => ILA003_ManagedTypeInKernel.resx} | 4 +- ...A004_ManagedTypeArrayInKernel.Designer.cs} | 6 +- ...x => ILA004_ManagedTypeArrayInKernel.resx} | 4 +- 24 files changed, 180 insertions(+), 181 deletions(-) rename Src/ILGPU.Analyzers.Tests/{RefTypeAnalyzer.cs => ManagedTypeAnalyzer.cs} (89%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType => ManagedType}/Arrays.cs (100%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType/ManagedUnmanaged.cs => ManagedType/Complex.cs} (100%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType => ManagedType}/Constructors.cs (100%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType => ManagedType}/Functions.cs (100%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType => ManagedType}/LoadDiscovery.cs (100%) rename Src/ILGPU.Analyzers.Tests/Programs/{RefType => ManagedType}/Simple.cs (100%) create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt delete mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt rename Src/ILGPU.Analyzers/{ReferenceTypeAnalyzer.cs => ManagedTypeAnalyzer.cs} (92%) rename Src/ILGPU.Analyzers/Resources/{ILA003_ReferenceTypeInKernel.Designer.cs => ILA003_ManagedTypeInKernel.Designer.cs} (93%) rename Src/ILGPU.Analyzers/Resources/{ILA003_ReferenceTypeInKernel.resx => ILA003_ManagedTypeInKernel.resx} (83%) rename Src/ILGPU.Analyzers/Resources/{ILA004_ReferenceTypeArrayInKernel.Designer.cs => ILA004_ManagedTypeArrayInKernel.Designer.cs} (92%) rename Src/ILGPU.Analyzers/Resources/{ILA004_ReferenceTypeArrayInKernel.resx => ILA004_ManagedTypeArrayInKernel.resx} (88%) diff --git a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs similarity index 89% rename from Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs rename to Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 26ef41d4e..977d2b9c7 100644 --- a/Src/ILGPU.Analyzers.Tests/RefTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -3,11 +3,11 @@ using Xunit; using VerifyCS = ILGPU.Analyzers.Tests.Generic.DiagnosticAnalyzerVerifier< - ILGPU.Analyzers.ReferenceTypeAnalyzer>; + ILGPU.Analyzers.ManagedTypeAnalyzer>; namespace ILGPU.Analyzers.Tests; -public class RefTypeAnalyzer +public class ManagedTypeAnalyzer { [Theory] [InlineData("Simple")] diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/Arrays.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/ManagedUnmanaged.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/Constructors.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/Functions.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/LoadDiscovery.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/RefType/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs similarity index 100% rename from Src/ILGPU.Analyzers.Tests/Programs/RefType/Simple.cs rename to Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt new file mode 100644 index 000000000..544316393 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -0,0 +1,22 @@ +[ + { + Id: ILA004, + Title: Array of managed types in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (29,25)-(29,42), + MessageFormat: Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType', which is a managed type. Arrays of managed types cannot be used in kernels., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (29,27)-(29,40), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt new file mode 100644 index 000000000..e331756b5 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt @@ -0,0 +1,12 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (40,22)-(40,35), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.ManagedUnmanaged.Managed' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt new file mode 100644 index 000000000..2aece1749 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt @@ -0,0 +1,12 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (17,20)-(17,33), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Constructors.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt new file mode 100644 index 000000000..d58d74490 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt @@ -0,0 +1,32 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (20,27)-(20,40), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (14,27)-(14,40), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (32,15)-(32,28), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt new file mode 100644 index 000000000..4ddeb8897 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt @@ -0,0 +1,62 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (14,21)-(14,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (19,21)-(19,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (24,21)-(24,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (29,21)-(29,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (34,21)-(34,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (39,21)-(39,34), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt new file mode 100644 index 000000000..9def35655 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt @@ -0,0 +1,22 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (13,22)-(13,35), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (14,38)-(14,45), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt deleted file mode 100644 index d449bbc7b..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - Id: ILA004, - Title: Array of reference types in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (29,25)-(29,42), - MessageFormat: Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType', which is a reference type. Arrays of reference types cannot be used in kernels., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (29,27)-(29,40), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt deleted file mode 100644 index d78af2e34..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Constructors.verified.txt +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (17,20)-(17,33), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Constructors.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt deleted file mode 100644 index 4de525844..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Functions.verified.txt +++ /dev/null @@ -1,32 +0,0 @@ -[ - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (20,27)-(20,40), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (14,27)-(14,40), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (32,15)-(32,28), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt deleted file mode 100644 index 269b4d89c..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt +++ /dev/null @@ -1,62 +0,0 @@ -[ - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (14,21)-(14,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (19,21)-(19,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (24,21)-(24,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (29,21)-(29,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (34,21)-(34,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (39,21)-(39,34), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt deleted file mode 100644 index 374c6bdbf..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=ManagedUnmanaged.verified.txt +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (40,22)-(40,35), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.ManagedUnmanaged.Managed' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt deleted file mode 100644 index f0b1f7062..000000000 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/RefTypeAnalyzer.FileTests_file=Simple.verified.txt +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (13,22)-(13,35), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - }, - { - Id: ILA003, - Title: Reference type in kernel, - Severity: Warning, - WarningLevel: 1, - Location: : (14,38)-(14,45), - MessageFormat: Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types., - Category: Usage - } -] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs similarity index 92% rename from Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs rename to Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 79fbffde0..3cefd70f3 100644 --- a/Src/ILGPU.Analyzers/ReferenceTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -9,22 +9,22 @@ // Source License. See LICENSE.txt for details. // --------------------------------------------------------------------------------------- -using ILGPU.Analyzers.Resources; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using System.Collections.Generic; using Microsoft.CodeAnalysis.Operations; +using ILGPU.Analyzers.Resources; namespace ILGPU.Analyzers { [DiagnosticAnalyzer(LanguageNames.CSharp)] - public class ReferenceTypeAnalyzer : KernelAnalyzer + public class ManagedTypeAnalyzer : KernelAnalyzer { private static readonly DiagnosticDescriptor GeneralDiagnosticRule = new( id: "ILA003", - title: ILA003_ReferenceTypeInKernel.Title, - messageFormat: ILA003_ReferenceTypeInKernel.MessageFormat, + title: ILA003_ManagedTypeInKernel.Title, + messageFormat: ILA003_ManagedTypeInKernel.MessageFormat, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -32,8 +32,8 @@ public class ReferenceTypeAnalyzer : KernelAnalyzer private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", - title: ILA004_ReferenceTypeArrayInKernel.Title, - messageFormat: ILA004_ReferenceTypeArrayInKernel.MessageFormat, + title: ILA004_ManagedTypeArrayInKernel.Title, + messageFormat: ILA004_ManagedTypeArrayInKernel.MessageFormat, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -129,7 +129,6 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return null; } - // TODO: test that ILGPU symbols are skipped private bool IsILGPUSymbol(ISymbol symbol) { return symbol.ContainingAssembly.Name == ILGPUAssemblyName; diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs similarity index 93% rename from Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs rename to Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs index 466eb1b02..582c11a61 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.Designer.cs +++ b/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs @@ -14,21 +14,21 @@ namespace ILGPU.Analyzers.Resources { [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA003_ReferenceTypeInKernel { + internal class ILA003_ManagedTypeInKernel { private static System.Resources.ResourceManager resourceMan; private static System.Globalization.CultureInfo resourceCulture; [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA003_ReferenceTypeInKernel() { + internal ILA003_ManagedTypeInKernel() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] internal static System.Resources.ResourceManager ResourceManager { get { if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA003_ReferenceTypeInKernel", typeof(ILA003_ReferenceTypeInKernel).Assembly); + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA003_ManagedTypeInKernel", typeof(ILA003_ManagedTypeInKernel).Assembly); resourceMan = temp; } return resourceMan; diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx similarity index 83% rename from Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx rename to Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx index b5969d9d2..861a700de 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA003_ReferenceTypeInKernel.resx +++ b/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx @@ -19,9 +19,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Reference type in kernel + Managed type in kernel - Type '{0}' is a reference type. Reference types like classes cannot be used in kernels, except for arrays of value types. + Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types. \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs similarity index 92% rename from Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs rename to Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs index 80fe60ce3..7b6cc57d2 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.Designer.cs +++ b/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs @@ -14,21 +14,21 @@ namespace ILGPU.Analyzers.Resources { [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA004_ReferenceTypeArrayInKernel { + internal class ILA004_ManagedTypeArrayInKernel { private static System.Resources.ResourceManager resourceMan; private static System.Globalization.CultureInfo resourceCulture; [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA004_ReferenceTypeArrayInKernel() { + internal ILA004_ManagedTypeArrayInKernel() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] internal static System.Resources.ResourceManager ResourceManager { get { if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA004_ReferenceTypeArrayInKernel", typeof(ILA004_ReferenceTypeArrayInKernel).Assembly); + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA004_ManagedTypeArrayInKernel", typeof(ILA004_ManagedTypeArrayInKernel).Assembly); resourceMan = temp; } return resourceMan; diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx similarity index 88% rename from Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx rename to Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx index cd121f953..3da7075bd 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA004_ReferenceTypeArrayInKernel.resx +++ b/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx @@ -19,9 +19,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Type '{0}' is an array of type '{1}', which is a reference type. Arrays of reference types cannot be used in kernels. + Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels. - Array of reference types in kernel + Array of managed types in kernel \ No newline at end of file From 4c00f4e6b18a33e401e1fcbae50ea38103d62678 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 00:25:06 -0400 Subject: [PATCH 23/41] Add tests for ILGPU types and intrinsics and finish renaming --- .../ManagedTypeAnalyzer.cs | 5 ++-- .../Programs/ManagedType/Arrays.cs | 2 +- .../Programs/ManagedType/Complex.cs | 2 +- .../Programs/ManagedType/Constructors.cs | 2 +- .../Programs/ManagedType/Functions.cs | 2 +- .../ManagedType/ILGPUTypesIntrinsics.cs | 30 +++++++++++++++++++ .../Programs/ManagedType/LoadDiscovery.cs | 2 +- .../Programs/ManagedType/Simple.cs | 2 +- ...nalyzer.FileTests_file=Arrays.verified.txt | 4 +-- ...alyzer.FileTests_file=Complex.verified.txt | 2 +- ...r.FileTests_file=Constructors.verified.txt | 2 +- ...yzer.FileTests_file=Functions.verified.txt | 6 ++-- ...sts_file=ILGPUTypesIntrinsics.verified.txt | 1 + ....FileTests_file=LoadDiscovery.verified.txt | 12 ++++---- ...nalyzer.FileTests_file=Simple.verified.txt | 4 +-- Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj | 27 +++++++++++------ 16 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=ILGPUTypesIntrinsics.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 977d2b9c7..367fed98b 100644 --- a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -11,15 +11,16 @@ public class ManagedTypeAnalyzer { [Theory] [InlineData("Simple")] + [InlineData("Complex")] [InlineData("Arrays")] [InlineData("Functions")] [InlineData("Constructors")] - [InlineData("ManagedUnmanaged")] [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/RefType/{file}.cs"); + var code = await File.ReadAllTextAsync($"Programs/ManagedType/{file}.cs"); await VerifyCS.Verify(code, settings => settings.UseParameters(file)); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs index 15e5c6bab..2f98eaf7e 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class Arrays { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs index 90f1f6ad1..4b5879bf6 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class ManagedUnmanaged { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs index 0e249f4f4..632617e8d 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class Constructors { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs index b68c2ac47..059011158 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class Functions { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs new file mode 100644 index 000000000..895915b7c --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs @@ -0,0 +1,30 @@ +using System; + +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; + +class ILGPUTypesIntrinsics +{ + static void Kernel(Index1D index, ArrayView input) + { + var a = input.SubView(0, 10); + int b = a[index]; + int c = Warp.WarpIdx; + Group.Barrier(); + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs index 04ba3bbc9..b775ea2f9 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class Functions { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs index c150aeacf..b6295fd52 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs @@ -1,6 +1,6 @@ using ILGPU.Runtime; -namespace ILGPU.Analyzers.Tests.Programs.RefType; +namespace ILGPU.Analyzers.Tests.Programs.ManagedType; class Simple { diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt index 544316393..29e435bc3 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (29,25)-(29,42), MessageFormat: Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType', which is a managed type. Arrays of managed types cannot be used in kernels., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType', which is a managed type. Arrays of managed types cannot be used in kernels., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 1, Location: : (29,27)-(29,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Arrays.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt index e331756b5..28d3fa652 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (40,22)-(40,35), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.ManagedUnmanaged.Managed' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.ManagedUnmanaged.Managed' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt index 2aece1749..59a1cdd66 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (17,20)-(17,33), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Constructors.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Constructors.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt index d58d74490..19644cb45 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (20,27)-(20,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 1, Location: : (14,27)-(14,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -26,7 +26,7 @@ WarningLevel: 1, Location: : (32,15)-(32,28), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=ILGPUTypesIntrinsics.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=ILGPUTypesIntrinsics.verified.txt new file mode 100644 index 000000000..ad47dbb93 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=ILGPUTypesIntrinsics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt index 4ddeb8897..01d45d2cb 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (14,21)-(14,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 1, Location: : (19,21)-(19,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -26,7 +26,7 @@ WarningLevel: 1, Location: : (24,21)-(24,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -36,7 +36,7 @@ WarningLevel: 1, Location: : (29,21)-(29,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -46,7 +46,7 @@ WarningLevel: 1, Location: : (34,21)-(34,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -56,7 +56,7 @@ WarningLevel: 1, Location: : (39,21)-(39,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt index 9def35655..44a52e3b2 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt @@ -6,7 +6,7 @@ WarningLevel: 1, Location: : (13,22)-(13,35), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -16,7 +16,7 @@ WarningLevel: 1, Location: : (14,38)-(14,45), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.RefType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj index 1ae27bb95..92bfbcad2 100644 --- a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj +++ b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj @@ -36,11 +36,6 @@ - - True - True - DiagnosticCategory.resx - True True @@ -51,13 +46,19 @@ True ILA002_ContainingTypeMustBePartial.resx + + True + True + ILA003_ManagedTypeInKernel.Designer.cs + + + True + True + ILA004_ManagedTypeArrayInKernel.Designer.cs + - - ResXFileCodeGenerator - DiagnosticCategory.Designer.cs - ResXFileCodeGenerator ILA001_StructMustBePartial.Designer.cs @@ -66,6 +67,14 @@ ResXFileCodeGenerator ILA002_ContainingTypeMustBePartial.Designer.cs + + ResXFileCodeGenerator + ILA003_ManagedTypeInKernel.Designer.cs + + + ResXFileCodeGenerator + ILA004_ManagedTypeArrayInKernel.Designer.cs + From b8d73aa4acec7235bc38d89ac486c1a31460cc98 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 00:33:12 -0400 Subject: [PATCH 24/41] Fix line lengths --- Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs | 7 +++++-- .../Programs/ManagedType/LoadDiscovery.cs | 6 +++--- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 5 ++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 367fed98b..448bf43e7 100644 --- a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -19,8 +19,11 @@ public class ManagedTypeAnalyzer [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"); + // 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)); } } \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs index b775ea2f9..1257e027c 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs @@ -50,11 +50,11 @@ static void Run() var kernel1 = accelerator.LoadStreamKernel>(Kernel1); var twice = accelerator.LoadStreamKernel>(Kernel1); - var kernel2 = accelerator.LoadAutoGroupedStreamKernel>(Kernel2); + var kernel2 = + accelerator.LoadAutoGroupedStreamKernel>(Kernel2); var kernel3 = accelerator.LoadImplicitlyGroupedStreamKernel>( - Kernel3, - 32); + Kernel3, 32); var kernel4 = accelerator.LoadKernel>(Kernel4); var kernel5 = accelerator.LoadAutoGroupedKernel>(Kernel5); diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 3cefd70f3..1fc28bfde 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -120,7 +120,10 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); } - if (op is IObjectCreationOperation { Constructor: not null } creationOperation) + if (op is IObjectCreationOperation + { + Constructor: not null + } creationOperation) { if (IsILGPUSymbol(creationOperation.Constructor)) return null; return MethodUtil.GetMethodBody(model, creationOperation.Constructor); From 5c5a0c3121e3a93d64e3310354d54dbfe29dc651 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 00:49:32 -0400 Subject: [PATCH 25/41] Update copyright and fix issues with samples and test files --- Samples/ILGPU.Samples.sln | 2 +- .../ManagedTypeAnalyzer.csproj} | 0 .../Program.cs | 4 +-- .../Generic/DiagnosticAnalyzerVerifier.cs | 11 +++++++ .../Generic/SourceCompiler.cs | 13 ++++++++- .../ManagedTypeAnalyzer.cs | 11 +++++++ .../Programs/ManagedType/Arrays.cs | 11 +++++++ .../Programs/ManagedType/Complex.cs | 26 ++++++++++------- .../Programs/ManagedType/Constructors.cs | 11 +++++++ .../Programs/ManagedType/Functions.cs | 13 ++++++++- .../ManagedType/ILGPUTypesIntrinsics.cs | 13 ++++++++- .../Programs/ManagedType/LoadDiscovery.cs | 29 +++++++++++++------ .../Programs/ManagedType/Simple.cs | 13 ++++++++- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 4 +-- Src/ILGPU.Analyzers/MethodUtil.cs | 11 +++++++ 15 files changed, 144 insertions(+), 28 deletions(-) rename Samples/{RefTypeAnalyzer/RefTypeAnalyzer.csproj => ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj} (100%) rename Samples/{RefTypeAnalyzer => ManagedTypeAnalyzer}/Program.cs (95%) diff --git a/Samples/ILGPU.Samples.sln b/Samples/ILGPU.Samples.sln index 2fb92dc7f..ba07fe3fd 100644 --- a/Samples/ILGPU.Samples.sln +++ b/Samples/ILGPU.Samples.sln @@ -137,7 +137,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterleaveFields", "Interle EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ILGPU.Analyzers", "ILGPU.Analyzers", "{6D111E09-49A1-492E-B4CE-E18CE27B56A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefTypeAnalyzer", "RefTypeAnalyzer\RefTypeAnalyzer.csproj", "{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedTypeAnalyzer", "ManagedTypeAnalyzer\ManagedTypeAnalyzer.csproj", "{6176F0D0-4A34-42DB-BDFB-2D8ED80868C3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj b/Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj similarity index 100% rename from Samples/RefTypeAnalyzer/RefTypeAnalyzer.csproj rename to Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj diff --git a/Samples/RefTypeAnalyzer/Program.cs b/Samples/ManagedTypeAnalyzer/Program.cs similarity index 95% rename from Samples/RefTypeAnalyzer/Program.cs rename to Samples/ManagedTypeAnalyzer/Program.cs index 8ca0d4643..42d1c6357 100644 --- a/Samples/RefTypeAnalyzer/Program.cs +++ b/Samples/ManagedTypeAnalyzer/Program.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2023-2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Program.cs @@ -11,7 +11,7 @@ using ILGPU; using ILGPU.Runtime; -namespace RefTypeAnalyzers; +namespace ManagedTypeAnalyzer; class Program { diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index ed4836e91..2ff511308 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// 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.IO; using System.Threading.Tasks; diff --git a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs index e2f91d6a2..3e63a6b8d 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// 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; @@ -7,7 +18,7 @@ namespace ILGPU.Analyzers.Tests.Generic; -public class SourceCompiler +public static class SourceCompiler { public static CSharpCompilation CreateCompilationWithAssemblies( string assemblyName, diff --git a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 448bf43e7..827ce4e1e 100644 --- a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// 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; diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs index 2f98eaf7e..46fda4f14 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: Arrays.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs index 4b5879bf6..82eaad138 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs @@ -1,13 +1,24 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: Complex.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; -class ManagedUnmanaged +class Complex { - class RefType + class RefTypeEmpty { - } - + } + struct Unmanaged { private int a; @@ -17,12 +28,7 @@ struct Unmanaged struct Managed { private int a; - private RefType r; - } - - class RefType - { - public int Hello => 42; + private RefTypeEmpty r; } struct ValueType diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs index 632617e8d..cff6e2698 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: Constructors.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs index 059011158..f1db1b712 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: Functions.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; @@ -8,7 +19,7 @@ class RefType { public int Hello => 42; } - + // This wouldn't compile anyways, but the analyzer shouldn't hang static int Recursion(int i) { diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs index 895915b7c..ec08b187e 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs @@ -1,4 +1,15 @@ -using System; +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: ILGPUTypesIntrinsics.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs index 1257e027c..958622cda 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs @@ -1,40 +1,51 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: LoadDiscovery.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; -class Functions +class LoadDiscovery { class RefType { public int Hello => 42; } - + // Separate kernels so we can observe multiple analyses static void Kernel1(Index1D index, ArrayView input) { int result = new RefType().Hello; } - + static void Kernel2(Index1D index, ArrayView input) { int result = new RefType().Hello; } - + static void Kernel3(Index1D index, ArrayView input) { int result = new RefType().Hello; } - + static void Kernel4(Index1D index, ArrayView input) { int result = new RefType().Hello; } - + static void Kernel5(Index1D index, ArrayView input) { int result = new RefType().Hello; } - + static void Kernel6(Index1D index, ArrayView input) { int result = new RefType().Hello; @@ -49,8 +60,8 @@ static void Run() // Also make sure kernels loaded twice aren't analyzed twice var kernel1 = accelerator.LoadStreamKernel>(Kernel1); var twice = accelerator.LoadStreamKernel>(Kernel1); - - var kernel2 = + + var kernel2 = accelerator.LoadAutoGroupedStreamKernel>(Kernel2); var kernel3 = accelerator.LoadImplicitlyGroupedStreamKernel>( diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs index b6295fd52..9e426b3b3 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: Simple.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.Runtime; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; @@ -30,4 +41,4 @@ static void Run() accelerator.Synchronize(); } -} +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 1fc28bfde..20d926bc7 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -1,9 +1,9 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2023-2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // -// File: ReferenceTypeAnalyzer.cs +// 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. diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index 19a2f9160..c1a7f632b 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -1,3 +1,14 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: MethodUtil.cs +// +// This file is part of ILGPU and is distributed under the University of Illinois Open +// Source License. See LICENSE.txt for details. +// --------------------------------------------------------------------------------------- + using Microsoft.CodeAnalysis; namespace ILGPU.Analyzers From a1f50dde2a699a1f627bffc8d8dc3387591eed1c Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 01:02:02 -0400 Subject: [PATCH 26/41] Fix analyzer release formatting --- Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md index 0a9ffa5c6..3d29a0ce6 100644 --- a/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md +++ b/Src/ILGPU.Analyzers/AnalyzerReleases.Unshipped.md @@ -3,9 +3,9 @@ ### New Rules -| Rule ID | Category | Severity | Notes | -|---------|----------------|----------|-----------------| -| ILA001 | CodeGeneration | Error | SourceGenerator | -| ILA002 | CodeGeneration | Error | SourceGenerator | -| ILA003 | Usage | Warning | Analyzer | -| ILA004 | Usage | Warning | Analyzer | \ No newline at end of file +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +ILA001 | CodeGeneration | Error | SourceGenerator +ILA002 | CodeGeneration | Error | SourceGenerator +ILA003 | Usage | Warning | Analyzer +ILA004 | Usage | Warning | Analyzer From 4c6fe84924e186a05f2e7f9b26530036d67ce437 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 23:14:00 -0400 Subject: [PATCH 27/41] Actually run copyright update tool and remove usage of collection expressions --- Samples/ManagedTypeAnalyzer/Program.cs | 4 ++-- .../Generic/DiagnosticAnalyzerVerifier.cs | 10 ++++++---- Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs | 2 +- Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs | 2 +- .../Programs/ManagedType/Arrays.cs | 2 +- .../Programs/ManagedType/Complex.cs | 2 +- .../Programs/ManagedType/Constructors.cs | 2 +- .../Programs/ManagedType/Functions.cs | 2 +- .../Programs/ManagedType/ILGPUTypesIntrinsics.cs | 2 +- .../Programs/ManagedType/LoadDiscovery.cs | 2 +- .../Programs/ManagedType/Simple.cs | 2 +- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 2 +- Src/ILGPU.Analyzers/MethodUtil.cs | 2 +- 13 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Samples/ManagedTypeAnalyzer/Program.cs b/Samples/ManagedTypeAnalyzer/Program.cs index 42d1c6357..1f61e8509 100644 --- a/Samples/ManagedTypeAnalyzer/Program.cs +++ b/Samples/ManagedTypeAnalyzer/Program.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- -// ILGPU -// Copyright (c) 2024 ILGPU Project +// ILGPU Samples +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Program.cs diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index 2ff511308..727c40790 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: DiagnosticAnalyzerVerifier.cs @@ -10,9 +10,11 @@ // --------------------------------------------------------------------------------------- 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; @@ -36,10 +38,10 @@ public static async Task Verify(string source, SourceCompiler.CreateCompilationWithAssemblies("Tests", source, ilgpuAssemblies); - var analyzer = new TDiagnosticAnalyzer(); - var options = new AnalyzerOptions([]); + var array = new ImmutableArray { new TDiagnosticAnalyzer() }; + var options = new AnalyzerOptions(ImmutableArray.Empty); var analyzerCompilation = - new CompilationWithAnalyzers(compilation, [analyzer], options); + new CompilationWithAnalyzers(compilation, array, options); var diagnostics = await analyzerCompilation.GetAnalyzerDiagnosticsAsync(); diff --git a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs index 3e63a6b8d..75f19e3e5 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: SourceCompiler.cs diff --git a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 827ce4e1e..48080c42c 100644 --- a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: ManagedTypeAnalyzer.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs index 46fda4f14..8d8b38e8c 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Arrays.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs index 82eaad138..d0193d2d5 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Complex.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Complex.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs index cff6e2698..e64bfe1f7 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Constructors.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Constructors.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs index f1db1b712..a1f1abc9f 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Functions.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Functions.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs index ec08b187e..4f9f723af 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: ILGPUTypesIntrinsics.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs index 958622cda..4edab3c20 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/LoadDiscovery.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: LoadDiscovery.cs diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs index 9e426b3b3..ec225ac00 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Simple.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: Simple.cs diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 20d926bc7..ead6516b3 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: ManagedTypeAnalyzer.cs diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index c1a7f632b..ca27fed39 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2024 ILGPU Project +// Copyright (c) 2024 ILGPU Project // www.ilgpu.net // // File: MethodUtil.cs From cf4524f667f428e15af9171ca4c7656f0105279f Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 23:31:58 -0400 Subject: [PATCH 28/41] Fix tests --- .../Generic/DiagnosticAnalyzerVerifier.cs | 2 +- ...nalyzer.FileTests_file=Arrays.verified.txt | 4 ++-- ...alyzer.FileTests_file=Complex.verified.txt | 4 ++-- ...r.FileTests_file=Constructors.verified.txt | 2 +- ...yzer.FileTests_file=Functions.verified.txt | 6 ++--- ....FileTests_file=LoadDiscovery.verified.txt | 24 +++++++++---------- ...nalyzer.FileTests_file=Simple.verified.txt | 4 ++-- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index 727c40790..b99668d09 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -38,7 +38,7 @@ public static async Task Verify(string source, SourceCompiler.CreateCompilationWithAssemblies("Tests", source, ilgpuAssemblies); - var array = new ImmutableArray { new TDiagnosticAnalyzer() }; + var array = ImmutableArray.Create(new TDiagnosticAnalyzer()); var options = new AnalyzerOptions(ImmutableArray.Empty); var analyzerCompilation = new CompilationWithAnalyzers(compilation, array, options); diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt index 29e435bc3..e82209516 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -4,7 +4,7 @@ Title: Array of managed types in kernel, Severity: Warning, WarningLevel: 1, - Location: : (29,25)-(29,42), + Location: : (40,25)-(40,42), MessageFormat: Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType', which is a managed type. Arrays of managed types cannot be used in kernels., Category: Usage @@ -14,7 +14,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (29,27)-(29,40), + Location: : (40,27)-(40,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt index 28d3fa652..bad99fa9b 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Complex.verified.txt @@ -4,9 +4,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (40,22)-(40,35), + Location: : (46,22)-(46,35), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.ManagedUnmanaged.Managed' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Complex.Managed' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt index 59a1cdd66..70fe334d2 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Constructors.verified.txt @@ -4,7 +4,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (17,20)-(17,33), + Location: : (28,20)-(28,33), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Constructors.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt index 19644cb45..25d483611 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Functions.verified.txt @@ -4,7 +4,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (20,27)-(20,40), + Location: : (31,27)-(31,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage @@ -14,7 +14,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (14,27)-(14,40), + Location: : (25,27)-(25,40), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage @@ -24,7 +24,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (32,15)-(32,28), + Location: : (43,15)-(43,28), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt index 01d45d2cb..09eeec3d6 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=LoadDiscovery.verified.txt @@ -4,9 +4,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (14,21)-(14,34), + Location: : (25,21)-(25,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -14,9 +14,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (19,21)-(19,34), + Location: : (30,21)-(30,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -24,9 +24,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (24,21)-(24,34), + Location: : (35,21)-(35,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -34,9 +34,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (29,21)-(29,34), + Location: : (40,21)-(40,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -44,9 +44,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (34,21)-(34,34), + Location: : (45,21)-(45,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage }, { @@ -54,9 +54,9 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (39,21)-(39,34), + Location: : (50,21)-(50,34), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., - Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Functions.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.LoadDiscovery.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage } ] \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt index 44a52e3b2..adf2a8458 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Simple.verified.txt @@ -4,7 +4,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (13,22)-(13,35), + Location: : (24,22)-(24,35), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage @@ -14,7 +14,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (14,38)-(14,45), + Location: : (25,38)-(25,45), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Simple.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage From 77576f925eabfb861795ab54de89ce5ee322ecc7 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 30 Mar 2024 23:42:32 -0400 Subject: [PATCH 29/41] Fix samples build --- .../ManagedTypeAnalyzer.csproj | 2 +- Samples/ManagedTypeAnalyzer/Program.cs | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj b/Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj index 8cc34e0d4..4e6d0c8c8 100644 --- a/Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj +++ b/Samples/ManagedTypeAnalyzer/ManagedTypeAnalyzer.csproj @@ -4,7 +4,7 @@ $(LibrarySamplesTargetFrameworks) enable enable - RefTypeAnalyzers + ManagedTypeAnalyzer diff --git a/Samples/ManagedTypeAnalyzer/Program.cs b/Samples/ManagedTypeAnalyzer/Program.cs index 1f61e8509..b717799ba 100644 --- a/Samples/ManagedTypeAnalyzer/Program.cs +++ b/Samples/ManagedTypeAnalyzer/Program.cs @@ -11,8 +11,13 @@ 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 @@ -42,14 +47,21 @@ static void Kernel(Index1D index, ArrayView input, ArrayView output) output[index] = input[index] + refType.Hello; // Allocating arrays of unmanaged types is fine - ValueType[] array = [new ValueType()]; - int[] ints = [0, 1, 2]; + 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(); From c9eb0a3c039e6832fb8d369fb1e0dec8973fb927 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 31 Mar 2024 00:30:26 -0400 Subject: [PATCH 30/41] Allow strings and types from Algorithms --- .../ILGPU.Analyzers.Tests.csproj | 1 + .../ManagedType/ILGPUTypesIntrinsics.cs | 11 ++++++++++ Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 20 ++++++++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj index e25232344..26a061a5f 100644 --- a/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj +++ b/Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj @@ -23,6 +23,7 @@ + diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs index 4f9f723af..bd6e1e9cb 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs @@ -10,6 +10,8 @@ // --------------------------------------------------------------------------------------- using ILGPU.Runtime; +using ILGPU.Runtime.Cuda; +using ILGPU.Algorithms.Random; namespace ILGPU.Analyzers.Tests.Programs.ManagedType; @@ -21,6 +23,15 @@ static void Kernel(Index1D index, ArrayView input) int b = a[index]; int c = Warp.WarpIdx; Group.Barrier(); + + // Strings should be allowed for this and debug. + // We will be conservative in our analysis and ignore all strings. + CudaAsm.Emit("instruction here"); + + // Algorithms should also be allowed. + // RNGView is just an example of a managed type in Algorithms. + var rngView = new RNGView(); + rngView.Next(); } static void Run() diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index ead6516b3..4aec0f9ef 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -40,6 +40,7 @@ public class ManagedTypeAnalyzer : KernelAnalyzer ); private const string ILGPUAssemblyName = "ILGPU"; + private const string AlgorithmsAssemblyName = "ILGPU.Algorithms"; public override ImmutableArray SupportedDiagnostics { get; } = @@ -65,7 +66,8 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, { AnalyzeKernelOperation(context, descendant); - var innerBodyOp = GetInvokedOp(semanticModel, descendant); + var innerBodyOp = + GetInvokedNonLibOpIfExists(semanticModel, descendant); if (innerBodyOp is null) continue; if (!seenBodies.Contains(innerBodyOp)) @@ -86,6 +88,12 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, if (op.Type.IsUnmanagedType) return; + if (IsILGPUSymbol(op.Type)) + return; + + if (op.Type.SpecialType == SpecialType.System_String) + return; + if (op.Type is IArrayTypeSymbol arrayTypeSymbol) { if (arrayTypeSymbol.ElementType.IsUnmanagedType) @@ -101,9 +109,6 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, } else { - if (IsILGPUSymbol(op.Type)) - return; - var generalDiagnostic = Diagnostic.Create(GeneralDiagnosticRule, op.Syntax.GetLocation(), @@ -112,7 +117,7 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, } } - private IOperation? GetInvokedOp(SemanticModel model, IOperation op) + private IOperation? GetInvokedNonLibOpIfExists(SemanticModel model, IOperation op) { if (op is IInvocationOperation invocationOperation) { @@ -132,9 +137,10 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, return null; } - private bool IsILGPUSymbol(ISymbol symbol) + private static bool IsILGPUSymbol(ISymbol symbol) { - return symbol.ContainingAssembly.Name == ILGPUAssemblyName; + return symbol.ContainingAssembly?.Name is ILGPUAssemblyName + or AlgorithmsAssemblyName; } } } \ No newline at end of file From e3346e1220f40a0e9b9d9d16a8bad45d88c96132 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 19:41:32 -0400 Subject: [PATCH 31/41] Moved error messages back to a single file. --- Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj | 31 ++------- .../InterleaveFieldsGenerator.cs | 8 +-- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 8 +-- ....Designer.cs => ErrorMessages.Designer.cs} | 48 +++++++++++--- ...eArrayInKernel.resx => ErrorMessages.resx} | 22 ++++++- .../ILA001_StructMustBePartial.Designer.cs | 60 ----------------- .../Resources/ILA001_StructMustBePartial.resx | 27 -------- ...02_ContainingTypeMustBePartial.Designer.cs | 60 ----------------- .../ILA002_ContainingTypeMustBePartial.resx | 27 -------- .../Resources/ILA003_ManagedTypeInKernel.resx | 27 -------- ...LA004_ManagedTypeArrayInKernel.Designer.cs | 66 ------------------- 11 files changed, 74 insertions(+), 310 deletions(-) rename Src/ILGPU.Analyzers/Resources/{ILA003_ManagedTypeInKernel.Designer.cs => ErrorMessages.Designer.cs} (52%) rename Src/ILGPU.Analyzers/Resources/{ILA004_ManagedTypeArrayInKernel.resx => ErrorMessages.resx} (50%) delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx delete mode 100644 Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs diff --git a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj index 92bfbcad2..c13a48077 100644 --- a/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj +++ b/Src/ILGPU.Analyzers/ILGPU.Analyzers.csproj @@ -36,16 +36,6 @@ - - True - True - ILA001_StructMustBePartial.resx - - - True - True - ILA002_ContainingTypeMustBePartial.resx - True True @@ -56,24 +46,17 @@ True ILA004_ManagedTypeArrayInKernel.Designer.cs + + True + True + ErrorMessages.resx + - - ResXFileCodeGenerator - ILA001_StructMustBePartial.Designer.cs - - - ResXFileCodeGenerator - ILA002_ContainingTypeMustBePartial.Designer.cs - - - ResXFileCodeGenerator - ILA003_ManagedTypeInKernel.Designer.cs - - + ResXFileCodeGenerator - ILA004_ManagedTypeArrayInKernel.Designer.cs + ErrorMessages.Designer.cs diff --git a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs index 0a3ef96ba..9ed076aa9 100644 --- a/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs +++ b/Src/ILGPU.Analyzers/InterleaveFieldsGenerator.cs @@ -32,16 +32,16 @@ public class InterleaveFieldsGenerator : IIncrementalGenerator private static readonly DiagnosticDescriptor TargetNotPartial = new( id: "ILA001", - title: ILA001_StructMustBePartial.Title, - messageFormat: ILA001_StructMustBePartial.Message, + title: ErrorMessages.StructMustBePartial_Title, + messageFormat: ErrorMessages.StructMustBePartial_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); private static readonly DiagnosticDescriptor ContainingTypeNotPartial = new( id: "ILA002", - title: ILA002_ContainingTypeMustBePartial.Title, - messageFormat: ILA002_ContainingTypeMustBePartial.Message, + title: ErrorMessages.ContainingTypeMustBePartial_Title, + messageFormat: ErrorMessages.ContainingTypeMustBePartial_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 4aec0f9ef..4e75fbc8a 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -23,8 +23,8 @@ public class ManagedTypeAnalyzer : KernelAnalyzer { private static readonly DiagnosticDescriptor GeneralDiagnosticRule = new( id: "ILA003", - title: ILA003_ManagedTypeInKernel.Title, - messageFormat: ILA003_ManagedTypeInKernel.MessageFormat, + title: ErrorMessages.ManagedTypeInKernel_Title, + messageFormat: ErrorMessages.ManagedTypeInKernel_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -32,8 +32,8 @@ public class ManagedTypeAnalyzer : KernelAnalyzer private static readonly DiagnosticDescriptor ArrayDiagnosticRule = new( id: "ILA004", - title: ILA004_ManagedTypeArrayInKernel.Title, - messageFormat: ILA004_ManagedTypeArrayInKernel.MessageFormat, + title: ErrorMessages.ManagedTypeArrayInKernel_Title, + messageFormat: ErrorMessages.ManagedTypeArrayInKernel_Message, category: DiagnosticCategory.Usage, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs similarity index 52% rename from Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs rename to Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs index 582c11a61..d182e6d95 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.Designer.cs +++ b/Src/ILGPU.Analyzers/Resources/ErrorMessages.Designer.cs @@ -14,21 +14,21 @@ namespace ILGPU.Analyzers.Resources { [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA003_ManagedTypeInKernel { + internal class ErrorMessages { private static System.Resources.ResourceManager resourceMan; private static System.Globalization.CultureInfo resourceCulture; [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA003_ManagedTypeInKernel() { + internal ErrorMessages() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] internal static System.Resources.ResourceManager ResourceManager { get { if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA003_ManagedTypeInKernel", typeof(ILA003_ManagedTypeInKernel).Assembly); + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ErrorMessages", typeof(ErrorMessages).Assembly); resourceMan = temp; } return resourceMan; @@ -45,21 +45,51 @@ internal static System.Globalization.CultureInfo Culture { } } - internal static string Title { + internal static string StructMustBePartial_Message { get { - return ResourceManager.GetString("Title", resourceCulture); + return ResourceManager.GetString("StructMustBePartial_Message", resourceCulture); } } - internal static string MessageFormat { + internal static string StructMustBePartial_Title { get { - return ResourceManager.GetString("MessageFormat", resourceCulture); + return ResourceManager.GetString("StructMustBePartial_Title", resourceCulture); } } - internal static string Description { + internal static string ContainingTypeMustBePartial_Message { get { - return ResourceManager.GetString("Description", resourceCulture); + return ResourceManager.GetString("ContainingTypeMustBePartial_Message", resourceCulture); + } + } + + internal static string ContainingTypeMustBePartial_Title { + get { + return ResourceManager.GetString("ContainingTypeMustBePartial_Title", resourceCulture); + } + } + + internal static string ManagedTypeInKernel_Message { + get { + return ResourceManager.GetString("ManagedTypeInKernel_Message", resourceCulture); + } + } + + internal static string ManagedTypeInKernel_Title { + get { + return ResourceManager.GetString("ManagedTypeInKernel_Title", resourceCulture); + } + } + + internal static string ManagedTypeArrayInKernel_Message { + get { + return ResourceManager.GetString("ManagedTypeArrayInKernel_Message", resourceCulture); + } + } + + internal static string ManagedTypeArrayInKernel_Title { + get { + return ResourceManager.GetString("ManagedTypeArrayInKernel_Title", resourceCulture); } } } diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx b/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx similarity index 50% rename from Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx rename to Src/ILGPU.Analyzers/Resources/ErrorMessages.resx index 3da7075bd..f6f460cc5 100644 --- a/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.resx +++ b/Src/ILGPU.Analyzers/Resources/ErrorMessages.resx @@ -18,10 +18,28 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + The struct '{0}' must be partial + + + Struct must be partial + + + The type '{0}' containing '{1}' must be partial + + + Containing type must be partial + + + Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types. + + + Managed type in kernel + + Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels. - + Array of managed types in kernel \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs deleted file mode 100644 index 8695769cc..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.Designer.cs +++ /dev/null @@ -1,60 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ILGPU.Analyzers.Resources { - using System; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA001_StructMustBePartial { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA001_StructMustBePartial() { - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA001_StructMustBePartial", typeof(ILA001_StructMustBePartial).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - internal static string Message { - get { - return ResourceManager.GetString("Message", resourceCulture); - } - } - - internal static string Title { - get { - return ResourceManager.GetString("Title", resourceCulture); - } - } - } -} diff --git a/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx b/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx deleted file mode 100644 index 09c6d7b6b..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA001_StructMustBePartial.resx +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - The struct '{0}' must be partial - - - Struct must be partial - - \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs deleted file mode 100644 index ed2a50901..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.Designer.cs +++ /dev/null @@ -1,60 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ILGPU.Analyzers.Resources { - using System; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA002_ContainingTypeMustBePartial { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA002_ContainingTypeMustBePartial() { - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA002_ContainingTypeMustBePartial", typeof(ILA002_ContainingTypeMustBePartial).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - internal static string Message { - get { - return ResourceManager.GetString("Message", resourceCulture); - } - } - - internal static string Title { - get { - return ResourceManager.GetString("Title", resourceCulture); - } - } - } -} diff --git a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx b/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx deleted file mode 100644 index bfd3e05b3..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA002_ContainingTypeMustBePartial.resx +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - The type '{0}' containing '{1}' must be partial - - - Containing type must be partial - - \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx b/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx deleted file mode 100644 index 861a700de..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA003_ManagedTypeInKernel.resx +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Managed type in kernel - - - Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types. - - \ No newline at end of file diff --git a/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs b/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs deleted file mode 100644 index 7b6cc57d2..000000000 --- a/Src/ILGPU.Analyzers/Resources/ILA004_ManagedTypeArrayInKernel.Designer.cs +++ /dev/null @@ -1,66 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ILGPU.Analyzers.Resources { - using System; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [System.Diagnostics.DebuggerNonUserCodeAttribute()] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class ILA004_ManagedTypeArrayInKernel { - - private static System.Resources.ResourceManager resourceMan; - - private static System.Globalization.CultureInfo resourceCulture; - - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal ILA004_ManagedTypeArrayInKernel() { - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Resources.ResourceManager ResourceManager { - get { - if (object.Equals(null, resourceMan)) { - System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ILGPU.Analyzers.Resources.ILA004_ManagedTypeArrayInKernel", typeof(ILA004_ManagedTypeArrayInKernel).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] - internal static System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - internal static string Description { - get { - return ResourceManager.GetString("Description", resourceCulture); - } - } - - internal static string MessageFormat { - get { - return ResourceManager.GetString("MessageFormat", resourceCulture); - } - } - - internal static string Title { - get { - return ResourceManager.GetString("Title", resourceCulture); - } - } - } -} From c412f64d479b3a85b9e401f35797fb8f72c009a8 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 21:58:47 -0400 Subject: [PATCH 32/41] Improved documentation and refactored helper functions. --- .../Generic/DiagnosticAnalyzerVerifier.cs | 12 ++++ .../Generic/IncrementalGeneratorVerifier.cs | 12 ++++ .../Generic/SourceCompiler.cs | 15 ++++- Src/ILGPU.Analyzers/KernelAnalyzer.cs | 8 ++- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 63 ++++++++++++------- Src/ILGPU.Analyzers/MethodUtil.cs | 13 ++++ 6 files changed, 95 insertions(+), 28 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs index b99668d09..99da0ff8d 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/DiagnosticAnalyzerVerifier.cs @@ -21,9 +21,21 @@ namespace ILGPU.Analyzers.Tests.Generic; +/// +/// Verifies the output of running TDiagnosticAnalyzer on C# source. +/// +/// The diagnostic analyzer to run. public static class DiagnosticAnalyzerVerifier where TDiagnosticAnalyzer : DiagnosticAnalyzer, new() { + /// + /// Verifies the output of running TDiagnosticAnalyzer on source using + /// snapshots. + /// + /// The source to run the analyzer on. + /// + /// Optional action to configure verification settings. + /// public static async Task Verify(string source, Action configure = null) { diff --git a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs index 2aaa1ec16..88bec1a6c 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs @@ -19,9 +19,21 @@ namespace ILGPU.Analyzers.Tests.Generic { + /// + /// Verifies the output source from the source generator TIncrementalGenerator + /// using snapshots. + /// + /// + /// The generator to run on source text. + /// public static class IncrementalGeneratorVerifier where TIncrementalGenerator : IIncrementalGenerator, new() { + /// + /// Verifies the C# source generated by TIncrementalGenerator. + /// + /// The source text to run the generator on. + /// A task that runs the generator and verifies the output. public static Task Verify(string source) { var ilgpuAssemblies = diff --git a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs index 75f19e3e5..816dff341 100644 --- a/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs +++ b/Src/ILGPU.Analyzers.Tests/Generic/SourceCompiler.cs @@ -18,8 +18,21 @@ namespace ILGPU.Analyzers.Tests.Generic; +/// +/// Contains utility functions for compiling C# source files for analyzer testing. +/// public static class SourceCompiler { + /// + /// Compiles the source text source into an assembly with the name + /// assemblyName and includes the given additionalAssemblies. + /// + /// The name of the output assembly. + /// The source text to compile. + /// + /// The additional assembly references to include in the compilation. + /// + /// The resulting compilation. public static CSharpCompilation CreateCompilationWithAssemblies( string assemblyName, string source, @@ -32,7 +45,7 @@ public static CSharpCompilation CreateCompilationWithAssemblies( var trustedAssembliesPaths = (string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"); var systemReferences = - trustedAssembliesPaths + trustedAssembliesPaths! .Split(Path.PathSeparator) .Select(x => MetadataReference.CreateFromFile(x)) .ToArray(); diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index 577242593..37c3f4a06 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -19,6 +19,10 @@ namespace ILGPU.Analyzers { + /// + /// Base analyzer that can be used to implement diagnostic analyzers that operate on + /// kernel method bodies. + /// public abstract class KernelAnalyzer : DiagnosticAnalyzer { private readonly ImmutableHashSet kernelLoadNames = @@ -39,9 +43,7 @@ public abstract class KernelAnalyzer : DiagnosticAnalyzer /// /// The analysis context used to report diagnostics. /// - /// - /// The operation. - /// + /// The operation. protected abstract void AnalyzeKernelBody( OperationAnalysisContext context, IOperation bodyOp); diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index 4e75fbc8a..e8e6f9335 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -66,14 +66,19 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, { AnalyzeKernelOperation(context, descendant); - var innerBodyOp = - GetInvokedNonLibOpIfExists(semanticModel, descendant); - if (innerBodyOp is null) continue; + var invokedSymbol = GetInvokedSymbolIfExists(descendant); + if (invokedSymbol is null) continue; - if (!seenBodies.Contains(innerBodyOp)) + if (IsILGPUSymbol(invokedSymbol)) continue; + + var methodBodyOp = + MethodUtil.GetMethodBody(semanticModel, invokedSymbol); + if (methodBodyOp is null) continue; + + if (!seenBodies.Contains(methodBodyOp)) { - bodies.Push(innerBodyOp); - seenBodies.Add(innerBodyOp); + bodies.Push(methodBodyOp); + seenBodies.Add(methodBodyOp); } } } @@ -90,7 +95,7 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, if (IsILGPUSymbol(op.Type)) return; - + if (op.Type.SpecialType == SpecialType.System_String) return; @@ -117,26 +122,36 @@ private void AnalyzeKernelOperation(OperationAnalysisContext context, } } - private IOperation? GetInvokedNonLibOpIfExists(SemanticModel model, IOperation op) - { - if (op is IInvocationOperation invocationOperation) + /// + /// Gets the symbol for a method a given operation invokes, if the operation + /// invokes anything at all. Only looks at direct invocations, meaning descendants + /// will not be explored. + /// + /// The operation to analyze. + /// + /// The method symbol representing the method op invokes. This could be a + /// regular method or a constructor. Null if op doesn't invoke anything + /// directly. + /// + private IMethodSymbol? GetInvokedSymbolIfExists(IOperation op) => + op switch { - if (IsILGPUSymbol(invocationOperation.TargetMethod)) return null; - return MethodUtil.GetMethodBody(model, invocationOperation.TargetMethod); - } - - if (op is IObjectCreationOperation + IInvocationOperation invocationOperation => invocationOperation + .TargetMethod, + IObjectCreationOperation { Constructor: not null - } creationOperation) - { - if (IsILGPUSymbol(creationOperation.Constructor)) return null; - return MethodUtil.GetMethodBody(model, creationOperation.Constructor); - } - - return null; - } - + } creationOperation => creationOperation.Constructor, + _ => null + }; + + /// + /// Whether a given symbol is a symbol from an ILGPU assembly. + /// + /// The symbol to check. + /// + /// Whether symbol is in the ILGPU or ILGPU.Algorithms assemblies. + /// private static bool IsILGPUSymbol(ISymbol symbol) { return symbol.ContainingAssembly?.Name is ILGPUAssemblyName diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index ca27fed39..d2b232dd7 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -15,6 +15,19 @@ namespace ILGPU.Analyzers { public static class MethodUtil { + /// + /// Gets the body operation of a given method. + /// + /// + /// The semantic model for the compilation that includes symbol. + /// + /// The method symbol to get the body of. + /// + /// The body of the method represented by symbol. If symbol is a + /// partial method, the operation representing the implementation is returned. + /// Null if the operation could not be resolved, for example, if symbol + /// is partial and there is no implementation part. + /// public static IOperation? GetMethodBody(SemanticModel model, IMethodSymbol symbol) { return symbol switch From 8efa5843d15fd5ff4a6625dddde5a81a929bb715 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 22:37:02 -0400 Subject: [PATCH 33/41] Added test for partial methods. --- .../ManagedTypeAnalyzer.cs | 1 + .../Programs/ManagedType/PartialMethods.cs | 57 +++++++++++++++++++ ...FileTests_file=PartialMethods.verified.txt | 22 +++++++ 3 files changed, 80 insertions(+) create mode 100644 Src/ILGPU.Analyzers.Tests/Programs/ManagedType/PartialMethods.cs create mode 100644 Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=PartialMethods.verified.txt diff --git a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs index 48080c42c..4714b945c 100644 --- a/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers.Tests/ManagedTypeAnalyzer.cs @@ -25,6 +25,7 @@ public class ManagedTypeAnalyzer [InlineData("Complex")] [InlineData("Arrays")] [InlineData("Functions")] + [InlineData("PartialMethods")] [InlineData("Constructors")] [InlineData("LoadDiscovery")] [InlineData("ILGPUTypesIntrinsics")] diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/PartialMethods.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/PartialMethods.cs new file mode 100644 index 000000000..d7d895305 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/PartialMethods.cs @@ -0,0 +1,57 @@ +// --------------------------------------------------------------------------------------- +// ILGPU +// Copyright (c) 2024 ILGPU Project +// www.ilgpu.net +// +// File: PartialMethods.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.Runtime; + +namespace ILGPU.Analyzers.Tests.Programs.ManagedType.PartialMethods; + +class RefType +{ + public int Hello => 42; +} + +partial class Foo +{ + public static partial int Bar(int i); +} + +partial class Foo +{ + public static partial int Bar(int i) + { + RefType r = new RefType(); + return r.Hello + i; + } +} + +class PartialMethods +{ + static void Kernel(Index1D index, ArrayView view) + { + view[index] = Foo.Bar(10); + } + + static void Run() + { + using var context = Context.CreateDefault(); + var device = context.GetPreferredDevice(false); + using var accelerator = device.CreateAccelerator(context); + + using var input = accelerator.Allocate1D(1024); + + var kernel = + accelerator.LoadAutoGroupedStreamKernel>(Kernel); + + kernel(input.IntExtent, input.View); + + accelerator.Synchronize(); + } +} \ No newline at end of file diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=PartialMethods.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=PartialMethods.verified.txt new file mode 100644 index 000000000..e7a029638 --- /dev/null +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=PartialMethods.verified.txt @@ -0,0 +1,22 @@ +[ + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (29,20)-(29,33), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.PartialMethods.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + }, + { + Id: ILA003, + Title: Managed type in kernel, + Severity: Warning, + WarningLevel: 1, + Location: : (30,15)-(30,16), + MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.PartialMethods.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., + Category: Usage + } +] \ No newline at end of file From 7c8e4e1a00e027c6a1c1b8a32d9bb893ce0111f0 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 22:38:04 -0400 Subject: [PATCH 34/41] Made ConcurrentDictionary in KernelAnalyzer readonly. --- Src/ILGPU.Analyzers/KernelAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/ILGPU.Analyzers/KernelAnalyzer.cs b/Src/ILGPU.Analyzers/KernelAnalyzer.cs index 37c3f4a06..a306e75ed 100644 --- a/Src/ILGPU.Analyzers/KernelAnalyzer.cs +++ b/Src/ILGPU.Analyzers/KernelAnalyzer.cs @@ -35,7 +35,7 @@ public abstract class KernelAnalyzer : DiagnosticAnalyzer "LoadImplicitlyGroupedStreamKernel" ); - private ConcurrentDictionary seen = new(); + private readonly ConcurrentDictionary seen = new(); /// /// Called for every kernel body. From 82268fc883a55adb206de3f0fd504e0c47db0f0f Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 22:53:47 -0400 Subject: [PATCH 35/41] Removed driver-crashing PTX. --- .../Programs/ManagedType/ILGPUTypesIntrinsics.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs index bd6e1e9cb..357aaaa37 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/ILGPUTypesIntrinsics.cs @@ -26,7 +26,7 @@ static void Kernel(Index1D index, ArrayView input) // Strings should be allowed for this and debug. // We will be conservative in our analysis and ignore all strings. - CudaAsm.Emit("instruction here"); + CudaAsm.Emit(""); // Algorithms should also be allowed. // RNGView is just an example of a managed type in Algorithms. From 3ad49c6a5193835cb40e8f1e5a4eb7b5fa2f3bed Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sat, 18 May 2024 23:56:56 -0400 Subject: [PATCH 36/41] Update samples to use unmanaged where needed. --- Samples/AdjustableSharedMemory/Program.cs | 6 +++--- Samples/GenericKernel/Program.cs | 2 +- Samples/StaticAbstractInterfaceMembers/Program.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Samples/AdjustableSharedMemory/Program.cs b/Samples/AdjustableSharedMemory/Program.cs index 5e669ccf4..9fa55259a 100644 --- a/Samples/AdjustableSharedMemory/Program.cs +++ b/Samples/AdjustableSharedMemory/Program.cs @@ -31,7 +31,7 @@ interface ISharedAllocationSize struct SharedArray32 : ISharedAllocationSize { /// - /// Returns a + /// Returns a /// public int ArraySize => 32; } @@ -49,7 +49,7 @@ struct SharedArray64 : ISharedAllocationSize /// Implicit shared-memory parameter that is handled by the runtime. static void SharedMemoryKernel( ArrayView 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; @@ -66,7 +66,7 @@ static void SharedMemoryKernel( } static void ExecuteSample(Context context) - where TSharedAllocationSize : struct, ISharedAllocationSize + where TSharedAllocationSize : unmanaged, ISharedAllocationSize { // For each available device... foreach (var device in context) diff --git a/Samples/GenericKernel/Program.cs b/Samples/GenericKernel/Program.cs index 9e66aed14..25b5dd379 100644 --- a/Samples/GenericKernel/Program.cs +++ b/Samples/GenericKernel/Program.cs @@ -72,7 +72,7 @@ static void Kernel( ArrayView data, int value, TKernelFunction function) - where TKernelFunction : struct, IKernelFunction + where TKernelFunction : unmanaged, IKernelFunction where T : unmanaged { // Invoke the custom "lambda function" diff --git a/Samples/StaticAbstractInterfaceMembers/Program.cs b/Samples/StaticAbstractInterfaceMembers/Program.cs index f11a3e8a8..713ecfef9 100644 --- a/Samples/StaticAbstractInterfaceMembers/Program.cs +++ b/Samples/StaticAbstractInterfaceMembers/Program.cs @@ -66,13 +66,13 @@ public interface ICalculatorOperation { T Calculate(T left, T right); } - + // Interface must be implemented by a 'struct'. public struct AdditionOp : ICalculatorOperation { public int Calculate(int left, int right) => left + right; } - + // Interface must be implemented by a 'struct'. public struct MultiplyOp : ICalculatorOperation { @@ -84,7 +84,7 @@ public static void CalculatorKernel( ArrayView1D input, ArrayView1D output) where T : unmanaged - where TOp : struct, ICalculatorOperation + where TOp : unmanaged, ICalculatorOperation { // Creates a new instance of the struct, and calls the method. output[index] = default(TOp).Calculate(input[index], input[index]); From db720959d89d5283dcd0ea58f1ba56f4126a33f3 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Sun, 19 May 2024 00:03:15 -0400 Subject: [PATCH 37/41] Require unmanaged in function that launches kernel in StaticAbstractInterfaceMembers sample. --- Samples/StaticAbstractInterfaceMembers/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Samples/StaticAbstractInterfaceMembers/Program.cs b/Samples/StaticAbstractInterfaceMembers/Program.cs index 713ecfef9..1130f0767 100644 --- a/Samples/StaticAbstractInterfaceMembers/Program.cs +++ b/Samples/StaticAbstractInterfaceMembers/Program.cs @@ -98,7 +98,7 @@ public static void UsingAbstractFunction(Accelerator accelerator) where TOp : ICalculatorOperation #else where T : unmanaged - where TOp : struct, ICalculatorOperation + where TOp : unmanaged, ICalculatorOperation #endif { var values = From 32a441a5dd8c0a716d80b1937dbee5b28bc76754 Mon Sep 17 00:00:00 2001 From: Yey007 Date: Wed, 19 Jun 2024 22:10:08 -0400 Subject: [PATCH 38/41] Ignore operations not in the current syntax tree. --- Src/ILGPU.Analyzers/MethodUtil.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index d2b232dd7..4e260e846 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -35,12 +35,18 @@ public static class MethodUtil { IsPartialDefinition: false, DeclaringSyntaxReferences: var refs, - } => refs.Length > 0 ? model.GetOperation(refs[0].GetSyntax()) : null, + } => refs.Length > 0 ? GetOperationIfInTree(model, refs[0].GetSyntax()) : null, { PartialImplementationPart: { DeclaringSyntaxReferences: var refs }, - } => refs.Length > 0 ? model.GetOperation(refs[0].GetSyntax()) : null, + } => refs.Length > 0 ? GetOperationIfInTree(model, refs[0].GetSyntax()) : null, _ => null }; } + + private static IOperation? GetOperationIfInTree(SemanticModel model, SyntaxNode node) + { + var root = model.SyntaxTree.GetRoot(); + return root.Contains(node) ? model.GetOperation(node) : null; + } } -} +} \ No newline at end of file From 3182d5b1997ff8fb9f2710953274fe7161d457fb Mon Sep 17 00:00:00 2001 From: Yey007 Date: Wed, 19 Jun 2024 22:25:08 -0400 Subject: [PATCH 39/41] Update copyright --- Samples/AdjustableSharedMemory/Program.cs | 2 +- Samples/GenericKernel/Program.cs | 2 +- Samples/StaticAbstractInterfaceMembers/Program.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Samples/AdjustableSharedMemory/Program.cs b/Samples/AdjustableSharedMemory/Program.cs index 9fa55259a..f070ea5e3 100644 --- a/Samples/AdjustableSharedMemory/Program.cs +++ b/Samples/AdjustableSharedMemory/Program.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Samples -// Copyright (c) 2021 ILGPU Project +// Copyright (c) 2021-2024 ILGPU Project // www.ilgpu.net // // File: Program.cs diff --git a/Samples/GenericKernel/Program.cs b/Samples/GenericKernel/Program.cs index 25b5dd379..f3fe7313a 100644 --- a/Samples/GenericKernel/Program.cs +++ b/Samples/GenericKernel/Program.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Samples -// Copyright (c) 2021 ILGPU Project +// Copyright (c) 2021-2024 ILGPU Project // www.ilgpu.net // // File: Program.cs diff --git a/Samples/StaticAbstractInterfaceMembers/Program.cs b/Samples/StaticAbstractInterfaceMembers/Program.cs index 1130f0767..6980eafb7 100644 --- a/Samples/StaticAbstractInterfaceMembers/Program.cs +++ b/Samples/StaticAbstractInterfaceMembers/Program.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Samples -// Copyright (c) 2023 ILGPU Project +// Copyright (c) 2023-2024 ILGPU Project // www.ilgpu.net // // File: Program.cs From 45d8c2d1cb491d18d3ff36b8279d8d4ed4f81f0a Mon Sep 17 00:00:00 2001 From: Yey007 Date: Wed, 19 Jun 2024 22:33:07 -0400 Subject: [PATCH 40/41] Fix line lengths --- Src/ILGPU.Analyzers/MethodUtil.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Src/ILGPU.Analyzers/MethodUtil.cs b/Src/ILGPU.Analyzers/MethodUtil.cs index 4e260e846..16d111c1a 100644 --- a/Src/ILGPU.Analyzers/MethodUtil.cs +++ b/Src/ILGPU.Analyzers/MethodUtil.cs @@ -34,16 +34,20 @@ public static class MethodUtil { { IsPartialDefinition: false, - DeclaringSyntaxReferences: var refs, - } => refs.Length > 0 ? GetOperationIfInTree(model, refs[0].GetSyntax()) : null, + DeclaringSyntaxReferences: { Length: > 0 } refs + } => GetOperationIfInTree(model, refs[0].GetSyntax()), { - PartialImplementationPart: { DeclaringSyntaxReferences: var refs }, - } => refs.Length > 0 ? GetOperationIfInTree(model, refs[0].GetSyntax()) : null, + PartialImplementationPart: + { + DeclaringSyntaxReferences: { Length: > 0 } refs + }, + } => GetOperationIfInTree(model, refs[0].GetSyntax()), _ => null }; } - private static IOperation? GetOperationIfInTree(SemanticModel model, SyntaxNode node) + private static IOperation? GetOperationIfInTree(SemanticModel model, + SyntaxNode node) { var root = model.SyntaxTree.GetRoot(); return root.Contains(node) ? model.GetOperation(node) : null; From c4afc18625b8265c5921604041ef13d22017a7fb Mon Sep 17 00:00:00 2001 From: Yey007 Date: Wed, 19 Jun 2024 23:18:26 -0400 Subject: [PATCH 41/41] Fixed issue with collection expressions --- .../Programs/ManagedType/Arrays.cs | 5 +---- ...peAnalyzer.FileTests_file=Arrays.verified.txt | 4 ++-- Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs index 8d8b38e8c..26618b9ba 100644 --- a/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs +++ b/Src/ILGPU.Analyzers.Tests/Programs/ManagedType/Arrays.cs @@ -35,10 +35,7 @@ static void Kernel(Index1D index, ArrayView input) ValueType[] array = [new ValueType()]; int[] ints = [0, 1, 2]; - // TODO: the new collection expressions seem to have an issue where analyses - // will be produced twice. If anyone has any information on this, please - // let me know. - RefType[] refs = { new RefType() }; + RefType[] refs = [new RefType()]; } static void Run() diff --git a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt index e82209516..fb4ff8b6e 100644 --- a/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt +++ b/Src/ILGPU.Analyzers.Tests/Snapshots/ManagedTypeAnalyzer.FileTests_file=Arrays.verified.txt @@ -4,7 +4,7 @@ Title: Array of managed types in kernel, Severity: Warning, WarningLevel: 1, - Location: : (40,25)-(40,42), + Location: : (37,25)-(37,40), MessageFormat: Type '{0}' is an array of type '{1}', which is a managed type. Arrays of managed types cannot be used in kernels., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType[]' is an array of type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType', which is a managed type. Arrays of managed types cannot be used in kernels., Category: Usage @@ -14,7 +14,7 @@ Title: Managed type in kernel, Severity: Warning, WarningLevel: 1, - Location: : (40,27)-(40,40), + Location: : (37,26)-(37,39), MessageFormat: Type '{0}' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Message: Type 'ILGPU.Analyzers.Tests.Programs.ManagedType.Arrays.RefType' is a managed type. Managed types like classes cannot be used in kernels, except for arrays of unmanaged types., Category: Usage diff --git a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs index e8e6f9335..8f999de1a 100644 --- a/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs +++ b/Src/ILGPU.Analyzers/ManagedTypeAnalyzer.cs @@ -50,8 +50,13 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, IOperation bodyOp) { Stack bodies = new Stack(); - // To catch recursion - HashSet seenBodies = new HashSet(); + + // Prevents infinite looping when functions are recursive + // Also prevents analyzing syntax (like collection expressions) that appear + // twice in descendants for some unknown reason. + // Costs us more memory than I would like, but not sure if we have a choice. + HashSet seenLocations = new HashSet(); + bodies.Push(bodyOp); // We will always have a semantic model because KernelAnalyzer subscribes @@ -64,6 +69,9 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, foreach (var descendant in op.DescendantsAndSelf()) { + var descendantLocation = descendant.Syntax.GetLocation(); + if (!seenLocations.Add(descendantLocation)) continue; + AnalyzeKernelOperation(context, descendant); var invokedSymbol = GetInvokedSymbolIfExists(descendant); @@ -75,10 +83,10 @@ protected override void AnalyzeKernelBody(OperationAnalysisContext context, MethodUtil.GetMethodBody(semanticModel, invokedSymbol); if (methodBodyOp is null) continue; - if (!seenBodies.Contains(methodBodyOp)) + var methodBodyLocation = methodBodyOp.Syntax.GetLocation(); + if (!seenLocations.Contains(methodBodyLocation)) { bodies.Push(methodBodyOp); - seenBodies.Add(methodBodyOp); } } }