-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added InterleaveFields source generator. (#982)
- Loading branch information
Showing
31 changed files
with
1,512 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
Src/ILGPU.Analyzers.Tests/Generic/IncrementalGeneratorVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.