Skip to content

Commit

Permalink
lambdas via compile-time specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed May 21, 2021
1 parent 28d2613 commit 076f147
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Src/ILGPU.Tests.CPU/ILGPU.Tests.CPU.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 4 additions & 0 deletions Src/ILGPU.Tests.Cuda/ILGPU.Tests.Cuda.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 4 additions & 0 deletions Src/ILGPU.Tests.OpenCL/ILGPU.Tests.OpenCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
37 changes: 37 additions & 0 deletions Src/ILGPU.Tests/ClassValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Xunit;
using Xunit.Abstractions;

namespace ILGPU.Tests
{
public abstract class ClassValues : TestBase
{
protected ClassValues(
ITestOutputHelper output,
TestContext testContext)
: base(output, testContext)
{ }

private static void ThisIndependentLambdaKernel(
Index1D index, ArrayView<int> data, Func<int, int> op)
{
data[index] = op(1);
}

private static int Inc(int v) => v + 1;
[Fact]
public void ThisIndependentLambda()
{
Action<Index1D, ArrayView<int>> kernel =
(i, v) => ThisIndependentLambdaKernel(i, v, Inc);

using var output = Accelerator.Allocate1D<int>(1);
Execute(kernel.Method, new Index1D(1), output.View);

var expected = new [] { 2 };
Verify(output.View, expected);
}
}
}

#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
1 change: 1 addition & 0 deletions Src/ILGPU.Tests/Configurations.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ArrayViews: Debug, Release, O2
Arrays: Debug, Release, O2
ClassValues: Debug, Release, O2
DebugTests: Debug, Release, O2
DisassemblerTests: Debug, Release, O2
EnumValues: Debug, Release, O2
Expand Down
4 changes: 4 additions & 0 deletions Src/ILGPU.Tests/ILGPU.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="T4.Build" Version="0.2.0" PrivateAssets="All" />
Expand Down
2 changes: 1 addition & 1 deletion Src/ILGPU/Frontend/CodeGenerator/Calls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private MethodInfo ResolveVirtualCallTarget(
const BindingFlags ConstraintMethodFlags = BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;

if (!target.IsVirtual)
if (!target.IsVirtual || target.DeclaringType.IsSealed)
return target;
if (constrainedType == null)
{
Expand Down
3 changes: 1 addition & 2 deletions Src/ILGPU/Frontend/CodeGenerator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public CodeGenerator(
private void SetupVariables()
{
var builder = EntryBlock.Builder;
LambdaArgumentOffset = Method.IsNotCapturingLambda() ? 1 : 0;

// Check for SSA variables
for (int i = 0, e = DisassembledMethod.Count; i < e; ++i)
Expand All @@ -101,7 +100,7 @@ private void SetupVariables()
}

// Initialize params
if (!Method.IsStatic && !Method.IsNotCapturingLambda())
if (!Method.IsStatic)
{
var declaringType = builder.CreateType(Method.DeclaringType);
declaringType = builder.CreatePointerType(
Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU/Frontend/CodeGenerator/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private bool TryGenerateCode(ILInstruction instruction)
MakeTrap();
return true;
case ILInstructionType.LdToken:
case ILInstructionType.LdFn:
MakeLoadToken(instruction.Argument);
return true;

Expand Down
11 changes: 11 additions & 0 deletions Src/ILGPU/Frontend/CodeGenerator/Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ private void MakeNewObject(MethodBase method)
throw Location.GetInvalidOperationException();

var type = constructor.DeclaringType;
if (typeof(Delegate).IsAssignableFrom(type))
{
var func = Block.Pop();
var instancePtr = Block.Pop();
if (!(instancePtr is NullValue))
throw Location.GetNotSupportedException("Only delegates pointing to static methods are supported");

Block.Push(func);
return;
}

var typeNode = Builder.CreateType(type);
var alloca = CreateTempAlloca(typeNode);

Expand Down
3 changes: 3 additions & 0 deletions Src/ILGPU/Frontend/DisassemblerDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ private bool TryDisasembleInstruction(ILOpCode opCode)
case ILOpCode.Ldtoken:
AppendInstruction(ILInstructionType.LdToken, 0, 1, AssociatedModule.ResolveMember(ReadIntArg(), TypeGenericArguments, MethodGenericArguments));
return true;
case ILOpCode.Ldftn:
AppendInstruction(ILInstructionType.LdFn, 0, 1, AssociatedModule.ResolveMethod(ReadIntArg(), TypeGenericArguments, MethodGenericArguments));
return true;

default:
return false;
Expand Down
4 changes: 4 additions & 0 deletions Src/ILGPU/Frontend/ILInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ public enum ILInstructionType
/// <summary>LoadToken</summary>
///
LdToken,
///
/// <summary>LoadFunction</summary>
///
LdFn,
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions Src/ILGPU/ILGPU.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
Expand Down

0 comments on commit 076f147

Please sign in to comment.