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

Add support for Arm and Arm64 architectures #816

Merged
merged 1 commit into from
Jun 16, 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
7 changes: 2 additions & 5 deletions Src/ILGPU.Tests/.test.tt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".runsettings" #>
<#@ import namespace="System.Runtime.InteropServices" #>
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<!-- Run on native platform architecture. -->
<# if (Environment.Is64BitOperatingSystem) { #>
<TargetPlatform>x64</TargetPlatform>
<# } else { #>
<TargetPlatform>x86</TargetPlatform>
<# } #>
<TargetPlatform><#= RuntimeInformation.ProcessArchitecture #></TargetPlatform>
</RunConfiguration>
</RunSettings>
50 changes: 47 additions & 3 deletions Src/ILGPU/Backends/Backend.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2017-2021 ILGPU Project
// Copyright (c) 2017-2022 ILGPU Project
// www.ilgpu.net
//
// File: Backend.cs
Expand All @@ -24,6 +24,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace ILGPU.Backends
{
Expand All @@ -41,6 +42,35 @@ public enum TargetPlatform
/// The X64 target platform.
/// </summary>
X64,

/// <summary>
/// The Arm target platform.
/// </summary>
Arm,

/// <summary>
/// The Arm64 target platform.
/// </summary>
Arm64,
}

/// <summary>
/// Extension methods for TargetPlatform related objects.
/// </summary>
public static class TargetPlatformExtensions
{
/// <summary>
/// Returns true if the current runtime platform is 64-bit.
/// </summary>
public static bool Is64Bit(this TargetPlatform targetPlatform) =>
targetPlatform switch
{
TargetPlatform.X86 => false,
TargetPlatform.X64 => true,
TargetPlatform.Arm => false,
TargetPlatform.Arm64 => true,
_ => throw new NotSupportedException(),
};
}

/// <summary>
Expand Down Expand Up @@ -375,13 +405,27 @@ protected delegate void CreateTransformersHandler(
/// Returns the current execution platform.
/// </summary>
public static TargetPlatform RuntimePlatform =>
IntPtr.Size == 8 ? TargetPlatform.X64 : TargetPlatform.X86;
RuntimeInformation.ProcessArchitecture switch
{
Architecture.X86 => TargetPlatform.X86,
Architecture.X64 => TargetPlatform.X64,
Architecture.Arm => TargetPlatform.Arm,
Architecture.Arm64 => TargetPlatform.Arm64,
_ => throw new NotSupportedException(),
};

/// <summary>
/// Returns the native OS platform.
/// </summary>
public static TargetPlatform OSPlatform =>
Environment.Is64BitOperatingSystem ? TargetPlatform.X64 : TargetPlatform.X86;
RuntimeInformation.OSArchitecture switch
{
Architecture.X86 => TargetPlatform.X86,
Architecture.X64 => TargetPlatform.X64,
Architecture.Arm => TargetPlatform.Arm,
Architecture.Arm64 => TargetPlatform.Arm64,
_ => throw new NotSupportedException(),
};

/// <summary>
/// Returns true if the current runtime platform is equal to the OS platform.
Expand Down
9 changes: 2 additions & 7 deletions Src/ILGPU/Frontend/CodeGenerator/Objects.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2021 ILGPU Project
// Copyright (c) 2018-2022 ILGPU Project
// www.ilgpu.net
//
// File: Objects.cs
Expand Down Expand Up @@ -148,12 +148,7 @@ private void LoadSizeOf(Type type)
}
else
{
var pointerSize = TypeContext.TargetPlatform switch
{
TargetPlatform.X86 => 4,
TargetPlatform.X64 => 8,
_ => throw new NotImplementedException()
};
var pointerSize = TypeContext.TargetPlatform.Is64Bit() ? 8 : 4;
Load(pointerSize);
}
}
Expand Down
12 changes: 6 additions & 6 deletions Src/ILGPU/IR/Types/PointerTypes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2021 ILGPU Project
// Copyright (c) 2018-2022 ILGPU Project
// www.ilgpu.net
//
// File: PointerTypes.cs
Expand Down Expand Up @@ -165,15 +165,15 @@ internal PointerType(
MemoryAddressSpace addressSpace)
: base(typeContext, elementType, addressSpace)
{
if (typeContext.TargetPlatform == TargetPlatform.X86)
if (typeContext.TargetPlatform.Is64Bit())
{
Size = Alignment = 4;
BasicValueType = BasicValueType.Int32;
Size = Alignment = 8;
BasicValueType = BasicValueType.Int64;
}
else
{
Size = Alignment = 8;
BasicValueType = BasicValueType.Int64;
Size = Alignment = 4;
BasicValueType = BasicValueType.Int32;
}
AddFlags(TypeFlags.PointerDependent);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU/Resources/RuntimeErrorMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Src/ILGPU/Resources/RuntimeErrorMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<data name="CudaNotSupported" xml:space="preserve">
<value>Cuda is not supported on this platform</value>
</data>
<data name="CudaPlatformX64" xml:space="preserve">
<data name="CudaPlatform64" xml:space="preserve">
<value>Cuda accelerator requires 64-bit application ({0} not supported). Ensure Prefer32Bit is set to 'false'.</value>
</data>
<data name="InvalidCodeGenerationOperation0" xml:space="preserve">
Expand Down
6 changes: 3 additions & 3 deletions Src/ILGPU/Runtime/Cuda/CudaContextExtensions.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: CudaContextExtensions.cs
Expand Down Expand Up @@ -47,10 +47,10 @@ public static Context.Builder Cuda(
this Context.Builder builder,
Predicate<CudaDevice> predicate)
{
if (Backend.RuntimePlatform != TargetPlatform.X64)
if (!Backend.RuntimePlatform.Is64Bit())
{
throw new NotSupportedException(string.Format(
RuntimeErrorMessages.CudaPlatformX64,
RuntimeErrorMessages.CudaPlatform64,
Backend.RuntimePlatform));
}

Expand Down