Skip to content

Commit

Permalink
Fixed issue with emitting constructor name in OpenCL.
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ committed Oct 8, 2022
1 parent b7ed379 commit 2892e08
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
33 changes: 31 additions & 2 deletions Src/ILGPU.Tests/BasicCalls.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2022 ILGPU Project
// www.ilgpu.net
//
// File: BasicCalls.cs
Expand All @@ -12,6 +12,7 @@
using ILGPU.Runtime;
using ILGPU.Util;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -301,7 +302,7 @@ public void GetSubVariableView(int length)
{
using var buffer = Accelerator.Allocate1D<int>(length);
var sourceData = Enumerable.Range(0, length).Select(t =>
new Int2(t, t + 1 )).ToArray();
new Int2(t, t + 1)).ToArray();
var expected = Enumerable.Range(1, length).ToArray();
using (var source = Accelerator.Allocate1D<Int2>(length))
{
Expand All @@ -311,6 +312,34 @@ public void GetSubVariableView(int length)

Verify(buffer.View, expected);
}

internal static void ConstructorCallKernel(
Index1D index,
ArrayView1D<Vector4, Stride1D.Dense> output)
{
output[index] = index.X switch
{
0 => new Vector4(0),
1 => new Vector4(1),
2 => new Vector4(2),
_ => new Vector4(index.X),
};
}

[Fact]
[KernelMethod(nameof(ConstructorCallKernel))]
public void ConstructorCall()
{
using var output = Accelerator.Allocate1D<Vector4>(Length);
output.MemSetToZero(Accelerator.DefaultStream);
Execute(Length, output.View);

var expected =
Enumerable.Range(0, Length)
.Select(x => new Vector4(x))
.ToArray();
Verify(output.View, expected);
}
}
}

18 changes: 15 additions & 3 deletions Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,21 @@ public bool TryGetPhis(BasicBlock block, out List<Variable> phisToDeclare) =>
protected static string GetMethodName(Method method)
{
var handleName = method.Handle.Name;
return method.HasFlags(MethodFlags.External)
? handleName
: handleName + "_" + method.Id;
if (method.HasFlags(MethodFlags.External))
{
return handleName;
}
else
{
// Constructor names in MSIL start with a dot. This is a reserved
// character that cannot be used in the OpenCL function name. Replace
// the dot with an underscore, assuming that the rest of the name is
// using valid characters. Appending the IR node identifier will ensure
// that there are no conflicts.
return handleName.StartsWith(".", System.StringComparison.Ordinal)
? handleName.Substring(1) + "_" + method.Id
: handleName + "_" + method.Id;
}
}

/// <summary>
Expand Down

0 comments on commit 2892e08

Please sign in to comment.