Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InterleaveFields source generator. #982

Merged
merged 10 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/Scripts/UpdateSampleReferences.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## ---------------------------------------------------------------------------------------
## ILGPU
## Copyright (c) 2021-2022 ILGPU Project
## Copyright (c) 2021-2024 ILGPU Project
## www.ilgpu.net
##
## File: UpdateSampleReferences.ps1
Expand Down Expand Up @@ -38,6 +38,12 @@ ForEach ($projectPath in $projectPaths) {
$found = $referencePath -match '\\([^\\]*).csproj$'
if ($found) {
$libraryName = $matches[1]

# Skip ILGPU.Analyzers because we bundle it with ILGPU.
if ($libraryName -eq "ILGPU.Analyzers") {
continue
}

dotnet add $projectPath package $libraryName -v $version --no-restore
}
}
Expand Down
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,18 @@ jobs:
strategy:
matrix:
os: ${{ fromJson(needs.setup-os-matrix.outputs.os) }}
library: [ILGPU, ILGPU.Algorithms]
library: [ILGPU, ILGPU.Algorithms, ILGPU.Analyzers]
framework: [net6.0, net7.0, net8.0]
flavor: [CPU, Velocity]
exclude:
- library: ILGPU.Algorithms
flavor: Velocity
- library: ILGPU.Analyzers
flavor: Velocity
- os: cuda
flavor: Velocity
- os: cuda
library: ILGPU.Analyzers
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -186,10 +190,14 @@ jobs:
- name: Set test flavor
id: test-flavor
shell: bash
run: echo "flavor=$([[ "${{ matrix.os }}" == cuda ]] && echo "Cuda" || echo "${{ matrix.flavor }}")" >> $GITHUB_OUTPUT
run: |
flavor="Tests.${{ matrix.flavor }}"
[[ "${{ matrix.os }}" == cuda ]] && flavor="Tests.Cuda"
[[ "${{ matrix.library }}" == "ILGPU.Analyzers" ]] && flavor="Tests"
echo "flavor=$flavor" >> $GITHUB_OUTPUT

- name: Build and test
run: dotnet test Src/${{ matrix.library }}.Tests.${{ steps.test-flavor.outputs.flavor }} --configuration=Release --framework=${{ matrix.framework }} -p:TreatWarningsAsErrors=true
run: dotnet test Src/${{ matrix.library }}.${{ steps.test-flavor.outputs.flavor }} --configuration=Release --framework=${{ matrix.framework }} -p:TreatWarningsAsErrors=true
env:
ILGPU_CLEAN_TESTS: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || (github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v'))) }}

Expand Down
14 changes: 14 additions & 0 deletions Samples/ILGPU.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AlgorithmsSparseMatrix", "A
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AlgorithmsOptimization", "AlgorithmsOptimization\AlgorithmsOptimization.csproj", "{70B69CE3-24A9-463C-B14C-E2934988BBEE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILGPU.Analyzers", "..\Src\ILGPU.Analyzers\ILGPU.Analyzers.csproj", "{1C5E9E39-3C14-4B52-8D97-04555D5F6331}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterleaveFields", "InterleaveFields\InterleaveFields.csproj", "{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -369,6 +373,14 @@ Global
{70B69CE3-24A9-463C-B14C-E2934988BBEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70B69CE3-24A9-463C-B14C-E2934988BBEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70B69CE3-24A9-463C-B14C-E2934988BBEE}.Release|Any CPU.Build.0 = Release|Any CPU
{1C5E9E39-3C14-4B52-8D97-04555D5F6331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C5E9E39-3C14-4B52-8D97-04555D5F6331}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C5E9E39-3C14-4B52-8D97-04555D5F6331}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C5E9E39-3C14-4B52-8D97-04555D5F6331}.Release|Any CPU.Build.0 = Release|Any CPU
{1E6D0BC6-CFA1-4F52-9EB9-CAA62DD2F33A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -435,6 +447,8 @@ Global
{28FD07DE-7B7D-46C3-9EE1-5D50C0E4F126} = {C1D99632-ED4A-4B08-A14D-4C8DB375934F}
{FBC7F8FA-8EB1-44EA-969E-B3DD365627ED} = {25BA2234-5778-40BC-9386-9CE87AB87D1F}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {30E502BD-3826-417F-888F-1CE19CF5C6DA}
Expand Down
21 changes: 21 additions & 0 deletions Samples/InterleaveFields/InterleaveFields.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(LibrarySamplesTargetFrameworks)</TargetFrameworks>
<OutputType>Exe</OutputType>
<LangVersion>9.0</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
</PropertyGroup>

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

</Project>
83 changes: 83 additions & 0 deletions Samples/InterleaveFields/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// 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.CodeGeneration;
using ILGPU.Runtime;
using System;

#pragma warning disable CA1034 // Nested types should not be visible
#pragma warning disable CA1051 // Do not declare visible instance fields

namespace InterleaveFields
{
public struct MyPoint
{
public int X;
public int Y;
}

public static partial class Program
{
[InterleaveFields(typeof(MyPoint), 4)]
public partial struct MyPoint4
{ }

static unsafe void MyKernel(Index1D index, ArrayView<MyPoint4> dataView)
{
dataView[index].X[0] = index;
dataView[index].X[1] = index + 1;
dataView[index].X[2] = index + 2;
dataView[index].X[3] = index + 3;
dataView[index].Y[0] = index + 4;
dataView[index].Y[1] = index + 5;
dataView[index].Y[2] = index + 6;
dataView[index].Y[3] = index + 7;
}

static unsafe void Main()
{
// Create main context
using var context = Context.CreateDefault();

// For each available device...
foreach (var device in context)
{
// Create accelerator for the given device
using var accelerator = device.CreateAccelerator(context);
Console.WriteLine($"Performing operations on {accelerator}");

var kernel = accelerator.LoadAutoGroupedStreamKernel<
Index1D, ArrayView<MyPoint4>>(MyKernel);
using var buffer = accelerator.Allocate1D<MyPoint4>(1024);

kernel((int)buffer.Length, buffer.View);

var data = buffer.GetAsArray1D();
for (int i = 0, e = data.Length; i < e; ++i)
{
if (data[i].X[0] != i
|| data[i].X[1] != i + 1
|| data[i].X[2] != i + 2
|| data[i].X[3] != i + 3
|| data[i].Y[0] != i + 4
|| data[i].Y[1] != i + 5
|| data[i].Y[2] != i + 6
|| data[i].Y[3] != i + 7)
Console.WriteLine($"Error at element location {i}");
}
}
}
}
}

#pragma warning restore CA1034 // Nested types should not be visible
#pragma warning restore CA1051 // Do not declare visible instance fields
68 changes: 68 additions & 0 deletions Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023-2024 ILGPU Project
// www.ilgpu.net
//
// File: IncrementalGeneratorVerifier.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.CodeGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VerifyTests;
using VerifyXunit;

namespace ILGPU.Analyzers.Tests
{
public static class IncrementalGeneratorVerifier<TIncrementalGenerator>
where TIncrementalGenerator : IIncrementalGenerator, new()
{
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 =
new[]
{
typeof(InterleaveFieldsAttribute),
typeof(TIncrementalGenerator)
}
.Select(x => MetadataReference.CreateFromFile(x.Assembly.Location))
.ToArray();

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

// Create an instance of the incremental source generator.
var generator = new TIncrementalGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

// Run generator and compare to snapshot.
driver = driver.RunGenerators(compilation);

var settings = new VerifySettings();
settings.UseDirectory(Path.Combine("..", "Snapshots"));
return Verifier.Verify(driver, settings);
}
}
}
23 changes: 23 additions & 0 deletions Src/ILGPU.Analyzers.Tests/Generic/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023-2024 ILGPU Project
// www.ilgpu.net
//
// File: ModuleInitializer.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.Runtime.CompilerServices;
using VerifyTests;

namespace ILGPU.Analyzers.Tests.Generic
{
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifySourceGenerators.Initialize();
}
}
31 changes: 31 additions & 0 deletions Src/ILGPU.Analyzers.Tests/ILGPU.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(LibraryUnitTestTargetFrameworks)</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.1.0" />
<PackageReference Include="Verify.Xunit" Version="19.12.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

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

</Project>
Loading
Loading