Skip to content

Commit

Permalink
Added support for LibDevice using Cuda Release v12. (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ authored Mar 17, 2023
1 parent b13e814 commit fbda1a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit fbda1a5

Please sign in to comment.