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 support for LibDevice using Cuda Release v12. #975

Merged
merged 1 commit into from
Mar 17, 2023
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
11 changes: 8 additions & 3 deletions Src/ILGPU/Backends/PTX/PTXBackend.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2022 ILGPU Project
// Copyright (c) 2018-2023 ILGPU Project
// www.ilgpu.net
//
// File: PTXBackend.cs
Expand Down Expand Up @@ -316,15 +316,20 @@ private unsafe void GenerateLibDeviceCode(
if (NvvmAPI == null || backendContext.Count == 0)
return;

// Determine the NVVM IR Version to use.
var result = NvvmAPI.GetIRVersion(out int majorIR, out _, out _, out _);
if (result != NvvmResult.NVVM_SUCCESS)
return;

// Convert the methods in the context into NVVM.
var methods = backendContext.GetEnumerator().AsEnumerable();
var nvvmModule = PTXLibDeviceNvvm.GenerateNvvm(methods);
var nvvmModule = PTXLibDeviceNvvm.GenerateNvvm(majorIR, methods);

if (string.IsNullOrEmpty(nvvmModule))
return;

// Create a new NVVM program.
var result = NvvmAPI.CreateProgram(out var program);
result = NvvmAPI.CreateProgram(out var program);

try
{
Expand Down
10 changes: 8 additions & 2 deletions Src/ILGPU/Backends/PTX/PTXLibDeviceNvvm.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021-2022 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: PTXLibDeviceNvvm.tt/PTXLibDeviceNvvm.cs
Expand Down Expand Up @@ -36,6 +36,10 @@ namespace ILGPU.Backends.PTX
/// </summary>
internal static class PTXLibDeviceNvvm
{
private const string irVersionFormat = @"
!nvvmir.version = !{{!0}}
!0 = !{{i32 {0}, i32 0}}";

private const string prefix = @"
target triple = ""nvptx64-unknown-cuda""
target datalayout = ""e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64""";
Expand Down Expand Up @@ -63,9 +67,10 @@ namespace ILGPU.Backends.PTX
/// <summary>
/// Generates an NVVM module for the Cuda LibDevice functions (if any).
/// </summary>
/// <param name="majorIR">The NVVM IR major version.</param>
/// <param name="methods">The methods to check.</param>
/// <returns>The NVVM module, or an empty string.</returns>
public static string GenerateNvvm(IEnumerable<Method> methods)
public static string GenerateNvvm(int majorIR, IEnumerable<Method> methods)
{
var builder = new StringBuilder();
bool addPrefix = true;
Expand All @@ -77,6 +82,7 @@ namespace ILGPU.Backends.PTX
{
if (addPrefix)
{
builder.AppendLine(string.Format(irVersionFormat, majorIR));
builder.AppendLine(prefix);
addPrefix = false;
}
Expand Down