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

Fixed issues with LibDevice integration. #784

Merged
merged 2 commits into from
Apr 4, 2022
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
5 changes: 3 additions & 2 deletions Src/ILGPU/Backends/PTX/PTXLibDeviceMethods.tt
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: PTXLibDeviceMethods.tt/PTXLibDeviceMethods.cs
Expand Down Expand Up @@ -112,7 +112,8 @@ void WriteLibDeviceFunction(LibDeviceFunction func)

void WriteBinaryType(string type)
{
if (type == "float")
// Map .NET types to the .NET type used by PTX to represent the register.
if (type == "Half" || type == "float")
Write("uint");
else if (type == "double")
Write("ulong");
Expand Down
9 changes: 8 additions & 1 deletion Src/ILGPU/Backends/PTX/PTXLibDeviceNvvm.tt
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: PTXLibDeviceNvvm.tt/PTXLibDeviceNvvm.cs
Expand Down Expand Up @@ -161,8 +161,15 @@ void WriteLibDeviceFunctionNvvm(LibDeviceFunction func)

void WriteNvvmType(string type)
{
// Map .NET types to the equivalent NNVM types.
if (type == "string")
Write("i8*");
else if (type == "Half")
Write("i16");
else if (type == "int" || type == "uint")
Write("i32");
else if (type == "long" || type == "ulong")
Write("i64");
else
Write(type);
}
Expand Down
22 changes: 15 additions & 7 deletions Src/ILGPU/Runtime/Cuda/LibDevice.tt
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: LibDevice.tt/LibDevice.cs
Expand Down Expand Up @@ -37,8 +37,10 @@ namespace ILGPU.Runtime.Cuda
/// Provides bindings for Cuda LibDevice functions.
/// </summary>
/// <remarks>
/// Deals with thunking the Cuda LibDevice functions, because the compiled PTX uses
/// u32/u64 registers rather than f32/f64 registers.
/// Deals with thunking the Cuda LibDevice functions, because the compiled PTX uses:
/// - b32 registers rather than f16 registers (Half type).
/// - b32 registers rather than f32 registers (float type).
/// - b64 registers rather than f64 registers (double type).
/// </remarks>
public static class LibDevice
{
Expand Down Expand Up @@ -98,14 +100,16 @@ void WriteLibDeviceInterop(LibDeviceFunction func)
var param = func.Parameters[i];
if (string.IsNullOrEmpty(param.Flags))
{
if (param.Type == "float" || param.Type == "double")
if (param.Type == "Half")
WriteLine($"var arg{i} = (uint){param.Name};");
else if (param.Type == "float" || param.Type == "double")
WriteLine($"var arg{i} = Interop.FloatAsInt({param.Name});");
else
WriteLine($"var arg{i} = {param.Name};");
}
else
{
if (param.Type == "float")
if (param.Type == "Half" || param.Type == "float")
WriteLine($"uint arg{i};");
else if (param.Type == "double")
WriteLine($"ulong arg{i};");
Expand Down Expand Up @@ -140,14 +144,18 @@ void WriteLibDeviceInterop(LibDeviceFunction func)
var param = func.Parameters[i];
if (!string.IsNullOrEmpty(param.Flags))
{
if (param.Type == "float" || param.Type == "double")
if (param.Type == "Half")
WriteLine($"{param.Name} = (Half)arg{i};");
else if (param.Type == "float" || param.Type == "double")
WriteLine($"{param.Name} = Interop.IntAsFloat(arg{i});");
else
WriteLine($"{param.Name} = arg{i};");
}
}

if (func.ReturnType == "float" || func.ReturnType == "double")
if (func.ReturnType == "Half")
WriteLine("return (Half)result;");
else if (func.ReturnType == "float" || func.ReturnType == "double")
WriteLine("return Interop.IntAsFloat(result);");
else if (func.ReturnType != "void")
WriteLine("return result;");
Expand Down