From faadd39071aeefd0c43cbc0815a44b3a74ddac58 Mon Sep 17 00:00:00 2001 From: Jonathan Giannuzzi Date: Fri, 10 Jun 2022 16:53:00 +0100 Subject: [PATCH] Add support for Arm and Arm64 architectures --- Src/ILGPU.Tests/.test.tt | 7 +-- Src/ILGPU/Backends/Backend.cs | 50 +++++++++++++++++-- Src/ILGPU/Frontend/CodeGenerator/Objects.cs | 9 +--- Src/ILGPU/IR/Types/PointerTypes.cs | 12 ++--- .../RuntimeErrorMessages.Designer.cs | 4 +- Src/ILGPU/Resources/RuntimeErrorMessages.resx | 2 +- .../Runtime/Cuda/CudaContextExtensions.cs | 6 +-- 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/Src/ILGPU.Tests/.test.tt b/Src/ILGPU.Tests/.test.tt index 23ed73619..6021ace5e 100644 --- a/Src/ILGPU.Tests/.test.tt +++ b/Src/ILGPU.Tests/.test.tt @@ -1,14 +1,11 @@ <#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ output extension=".runsettings" #> +<#@ import namespace="System.Runtime.InteropServices" #> -<# if (Environment.Is64BitOperatingSystem) { #> - x64 -<# } else { #> - x86 -<# } #> + <#= RuntimeInformation.ProcessArchitecture #> \ No newline at end of file diff --git a/Src/ILGPU/Backends/Backend.cs b/Src/ILGPU/Backends/Backend.cs index 7ec0176d9..6dbb3ba6b 100644 --- a/Src/ILGPU/Backends/Backend.cs +++ b/Src/ILGPU/Backends/Backend.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2017-2021 ILGPU Project +// Copyright (c) 2017-2022 ILGPU Project // www.ilgpu.net // // File: Backend.cs @@ -24,6 +24,7 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace ILGPU.Backends { @@ -41,6 +42,35 @@ public enum TargetPlatform /// The X64 target platform. /// X64, + + /// + /// The Arm target platform. + /// + Arm, + + /// + /// The Arm64 target platform. + /// + Arm64, + } + + /// + /// Extension methods for TargetPlatform related objects. + /// + public static class TargetPlatformExtensions + { + /// + /// Returns true if the current runtime platform is 64-bit. + /// + public static bool Is64Bit(this TargetPlatform targetPlatform) => + targetPlatform switch + { + TargetPlatform.X86 => false, + TargetPlatform.X64 => true, + TargetPlatform.Arm => false, + TargetPlatform.Arm64 => true, + _ => throw new NotSupportedException(), + }; } /// @@ -375,13 +405,27 @@ protected delegate void CreateTransformersHandler( /// Returns the current execution platform. /// 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(), + }; /// /// Returns the native OS platform. /// 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(), + }; /// /// Returns true if the current runtime platform is equal to the OS platform. diff --git a/Src/ILGPU/Frontend/CodeGenerator/Objects.cs b/Src/ILGPU/Frontend/CodeGenerator/Objects.cs index d75758ffc..595168d24 100644 --- a/Src/ILGPU/Frontend/CodeGenerator/Objects.cs +++ b/Src/ILGPU/Frontend/CodeGenerator/Objects.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2018-2021 ILGPU Project +// Copyright (c) 2018-2022 ILGPU Project // www.ilgpu.net // // File: Objects.cs @@ -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); } } diff --git a/Src/ILGPU/IR/Types/PointerTypes.cs b/Src/ILGPU/IR/Types/PointerTypes.cs index 638ab66b9..1ee624b8c 100644 --- a/Src/ILGPU/IR/Types/PointerTypes.cs +++ b/Src/ILGPU/IR/Types/PointerTypes.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2018-2021 ILGPU Project +// Copyright (c) 2018-2022 ILGPU Project // www.ilgpu.net // // File: PointerTypes.cs @@ -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); } diff --git a/Src/ILGPU/Resources/RuntimeErrorMessages.Designer.cs b/Src/ILGPU/Resources/RuntimeErrorMessages.Designer.cs index f0792fabd..1d5610f81 100644 --- a/Src/ILGPU/Resources/RuntimeErrorMessages.Designer.cs +++ b/Src/ILGPU/Resources/RuntimeErrorMessages.Designer.cs @@ -99,9 +99,9 @@ internal static string CudaNotSupported { /// /// Looks up a localized string similar to Cuda accelerator requires 64-bit application ({0} not supported). Ensure Prefer32Bit is set to 'false'.. /// - internal static string CudaPlatformX64 { + internal static string CudaPlatform64 { get { - return ResourceManager.GetString("CudaPlatformX64", resourceCulture); + return ResourceManager.GetString("CudaPlatform64", resourceCulture); } } diff --git a/Src/ILGPU/Resources/RuntimeErrorMessages.resx b/Src/ILGPU/Resources/RuntimeErrorMessages.resx index 73e97c1aa..eb4c0cfbd 100644 --- a/Src/ILGPU/Resources/RuntimeErrorMessages.resx +++ b/Src/ILGPU/Resources/RuntimeErrorMessages.resx @@ -129,7 +129,7 @@ Cuda is not supported on this platform - + Cuda accelerator requires 64-bit application ({0} not supported). Ensure Prefer32Bit is set to 'false'. diff --git a/Src/ILGPU/Runtime/Cuda/CudaContextExtensions.cs b/Src/ILGPU/Runtime/Cuda/CudaContextExtensions.cs index c9c097a1b..705efddcf 100644 --- a/Src/ILGPU/Runtime/Cuda/CudaContextExtensions.cs +++ b/Src/ILGPU/Runtime/Cuda/CudaContextExtensions.cs @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU -// Copyright (c) 2021 ILGPU Project +// Copyright (c) 2021-2022 ILGPU Project // www.ilgpu.net // // File: CudaContextExtensions.cs @@ -47,10 +47,10 @@ public static Context.Builder Cuda( this Context.Builder builder, Predicate predicate) { - if (Backend.RuntimePlatform != TargetPlatform.X64) + if (!Backend.RuntimePlatform.Is64Bit()) { throw new NotSupportedException(string.Format( - RuntimeErrorMessages.CudaPlatformX64, + RuntimeErrorMessages.CudaPlatform64, Backend.RuntimePlatform)); }