Skip to content

Commit

Permalink
Merge branch 'main' into src-improve-coreclr-debug-spew
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne authored May 11, 2022
2 parents d435e06 + 92eda7f commit 3674bbb
Show file tree
Hide file tree
Showing 58 changed files with 3,426 additions and 3,689 deletions.
2 changes: 2 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
/src/metalkit.cs
/src/MetalPerformanceShaders
/src/metalperformanceshaders.cs
/src/MetalPerformanceShadersGraph @praeclarum
/src/metalperformanceshadersgraph.cs @praeclarum
/src/MobileCoreServices
/src/mobilecoreservices.cs
/src/ModelIO
Expand Down
2 changes: 2 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@
PlatformAssembly=$(_PlatformAssemblyName).dll
RelativeAppBundlePath=$(_RelativeAppBundlePath)
Registrar=$(_BundlerRegistrar)
RequirePInvokeWrappers=$(_RequirePInvokeWrappers)
RuntimeConfigurationFile=$(_RuntimeConfigurationFile)
SdkDevPath=$(_SdkDevPath)
SdkRootDirectory=$(_XamarinSdkRootDirectory)
Expand Down Expand Up @@ -995,6 +996,7 @@
<AOTCompile
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
AotArguments="@(_AotArguments)"
Assemblies="@(_AssembliesToAOT)"
AOTCompilerPath="$(_AOTCompiler)"
InputDirectory="$(_AOTInputDirectory)"
Expand Down
9 changes: 7 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks.Core/Tasks/AOTCompileTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Xamarin.MacDev.Tasks {
public abstract class AOTCompileTaskBase : XamarinTask {
public ITaskItem [] AotArguments { get; set; } = Array.Empty<ITaskItem> ();

[Required]
public string AOTCompilerPath { get; set; } = string.Empty;

Expand Down Expand Up @@ -71,6 +73,7 @@ public override bool Execute ()
{ "MONO_PATH", Path.GetFullPath (InputDirectory) },
};

var globalAotArguments = AotArguments?.Select (v => v.ItemSpec).ToList ();
for (var i = 0; i < Assemblies.Length; i++) {
var asm = Assemblies [i];
var input = inputs [i];
Expand All @@ -87,14 +90,16 @@ public override bool Execute ()

var arguments = new List<string> ();
if (!StringUtils.TryParseArguments (aotArguments, out var parsedArguments, out var ex)) {
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ aotArguments, ex.Message);
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ aotArguments, ex!.Message);
return false;
}
if (!StringUtils.TryParseArguments (processArguments, out var parsedProcessArguments, out var ex2)) {
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ processArguments, ex2.Message);
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ processArguments, ex2!.Message);
return false;
}
arguments.Add ($"{string.Join (",", parsedArguments)}");
if (globalAotArguments is not null)
arguments.Add ($"--aot={string.Join (",", globalAotArguments)}");
arguments.AddRange (parsedProcessArguments);
arguments.Add (input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Xamarin.MacDev.Tasks {
public abstract class ParseBundlerArgumentsTaskBase : XamarinTask {
public string ExtraArgs { get; set; }

[Output]
public ITaskItem [] Aot { get; set; }

[Output]
public ITaskItem [] DlSym { get; set; }

Expand Down Expand Up @@ -43,6 +46,9 @@ public abstract class ParseBundlerArgumentsTaskBase : XamarinTask {
[Output]
public string Registrar { get; set; }

[Output]
public string RequirePInvokeWrappers { get; set; }

// This is input too
[Output]
public string NoStrip { get; set; }
Expand All @@ -65,6 +71,7 @@ public override bool Execute ()
var args = CommandLineArgumentBuilder.Parse (ExtraArgs);
List<string> xml = null;
List<string> customLinkFlags = null;
var aot = new List<ITaskItem> ();
var envVariables = new List<ITaskItem> ();
var dlsyms = new List<ITaskItem> ();

Expand Down Expand Up @@ -100,6 +107,9 @@ public override bool Execute ()
}

switch (name) {
case "aot":
aot.Add (new TaskItem (value));
break;
case "nosymbolstrip":
// There's also a version that takes symbols as arguments:
// --nosymbolstrip:symbol1,symbol2
Expand Down Expand Up @@ -141,6 +151,9 @@ public override bool Execute ()
case "package-debug-symbols":
PackageDebugSymbols = string.IsNullOrEmpty (value) ? "true" : value;
break;
case "require-pinvoke-wrappers":
RequirePInvokeWrappers = string.IsNullOrEmpty (value) ? "true" : value;
break;
case "registrar":
value = hasValue ? value : nextValue; // requires a value, which might be the next option
Registrar = value;
Expand Down Expand Up @@ -207,6 +220,12 @@ public override bool Execute ()
dlsyms.AddRange (DlSym);
DlSym = dlsyms.ToArray ();
}

if (aot.Count > 0) {
if (Aot is not null)
aot.AddRange (Aot);
Aot = aot.ToArray ();
}
}

return !Log.HasLoggedErrors;
Expand Down
3 changes: 3 additions & 0 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1763,12 +1763,14 @@ Copyright (C) 2018 Microsoft. All rights reserved.

<Target Name="_ParseBundlerArguments">
<ParseBundlerArguments
Aot="@(_AotArguments)"
ExtraArgs="$(_BundlerArguments)"
NoSymbolStrip="$(NoSymbolStrip)"
NoDSymUtil="$(NoDSymUtil)"
PackageDebugSymbols="$(PackageDebugSymbols)"
Verbosity="$(_BundlerVerbosity)"
>
<Output TaskParameter="Aot" ItemName="_AotArguments" />
<Output TaskParameter="CustomBundleName" PropertyName="_CustomBundleName" />
<Output TaskParameter="CustomLinkFlags" ItemName="_CustomLinkFlags" />
<Output TaskParameter="DlSym" ItemName="_BundlerDlsym" />
Expand All @@ -1780,6 +1782,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<Output TaskParameter="Optimize" PropertyName="_BundlerOptimize"/>
<Output TaskParameter="PackageDebugSymbols" PropertyName="PackageDebugSymbols" />
<Output TaskParameter="Registrar" PropertyName="_BundlerRegistrar" />
<Output TaskParameter="RequirePInvokeWrappers" PropertyName="_RequirePInvokeWrappers" />
<Output TaskParameter="Verbosity" PropertyName="_BundlerVerbosity" />
<Output TaskParameter="XmlDefinitions" ItemName="_BundlerXmlDefinitions" />
<Output TaskParameter="NoStrip" PropertyName="EnableAssemblyILStripping" />
Expand Down
1 change: 1 addition & 0 deletions src/Foundation/NSObject.mac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public partial class NSObject : INativeObject
static IntPtr ios = Dlfcn.dlopen (Constants.IOSurfaceLibrary, 1);
static IntPtr ex = Dlfcn.dlopen (Constants.ExternalAccessoryLibrary, 1);
static IntPtr ms = Dlfcn.dlopen (Constants.MetalPerformanceShadersLibrary, 1);
static IntPtr msg = Dlfcn.dlopen (Constants.MetalPerformanceShadersGraphLibrary, 1);
static IntPtr bc = Dlfcn.dlopen (Constants.BusinessChatLibrary, 1);
static IntPtr ad = Dlfcn.dlopen (Constants.AdSupportLibrary, 1);
static IntPtr nl = Dlfcn.dlopen (Constants.NaturalLanguageLibrary, 1);
Expand Down
42 changes: 42 additions & 0 deletions src/MetalPerformanceShaders/MPSNDArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
namespace MetalPerformanceShaders {
public partial class MPSNDArray {

public static MPSNDArray Create (IMTLDevice device, ReadOnlySpan<float> values, params int[] shape)
{
var ushape = new nuint [shape.Length];
for (var i = 0; i < shape.Length; i++) {
ushape [i] = (nuint) shape [i];
}
var desc = MPSNDArrayDescriptor.Create (MPSDataType.Float32, ushape);
var ndarray = new MPSNDArray (device, desc);
ndarray.Write (values);
return ndarray;
}

public void ExportData (IMTLCommandBuffer cmdBuf, IMTLBuffer buffer, MPSDataType sourceDataType, nuint offset)
{
ExportData (cmdBuf, buffer, sourceDataType, offset, IntPtr.Zero);
Expand Down Expand Up @@ -37,6 +49,21 @@ public unsafe void WriteBytes (IntPtr buffer, nint[] strideBytesPerDimension)
WriteBytes (buffer, (IntPtr)p);
}
}
public unsafe void Write (ReadOnlySpan<float> values)
{
if (DataType != MPSDataType.Float32)
throw new InvalidOperationException($"Attempted to write array data of type {DataType} to span of Float32s.");
nuint length = 1;
var ndims = NumberOfDimensions;
for (nuint i = 0; i < ndims; i++) {
length *= GetLength (i);
}
if (length != (nuint) values.Length)
throw new ArgumentException ($"The number of values ({values.Length}) does not match the shape length ({length}).");
fixed (float* p = values) {
WriteBytes ((IntPtr) p, strideBytesPerDimension: IntPtr.Zero);
}
}

public void ReadBytes (IntPtr buffer)
{
Expand All @@ -48,5 +75,20 @@ public unsafe void ReadBytes (IntPtr buffer, nint[] strideBytesPerDimension)
ReadBytes (buffer, (IntPtr)p);
}
}
public unsafe void Read (Span<float> values)
{
if (DataType != MPSDataType.Float32)
throw new InvalidOperationException ($"Attempted to read array data of type {DataType} to span of Float32s.");
nuint length = 1;
var ndims = NumberOfDimensions;
for (nuint i = 0; i < ndims; i++) {
length *= GetLength (i);
}
if (length != (nuint) values.Length)
throw new ArgumentException ($"The number of values ({values.Length}) does not match the shape length ({length}).");
fixed (float* p = values) {
ReadBytes ((IntPtr) p, strideBytesPerDimension: IntPtr.Zero);
}
}
}
}
117 changes: 117 additions & 0 deletions src/MetalPerformanceShadersGraph/MPSGraphEnums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Runtime.InteropServices;

using Foundation;
using ObjCRuntime;
using Metal;

namespace MetalPerformanceShadersGraph
{
[Flags]
public enum MPSGraphOptions : ulong
{
None = 0,
SynchronizeResults = 1,
Verbose = 2,
Default = SynchronizeResults,
}

[Native]
public enum MPSGraphTensorNamedDataLayout : ulong
{
Nchw = 0,
Nhwc = 1,
Oihw = 2,
Hwio = 3,
Chw = 4,
Hwc = 5,
Hw = 6,
}

[Native]
public enum MPSGraphPaddingStyle : ulong
{
Explicit = 0,
Valid = 1,
Same = 2,
ExplicitOffset = 3,
}

[Native]
public enum MPSGraphPaddingMode : long
{
Constant = 0,
Reflect = 1,
Symmetric = 2,
ClampToEdge = 3,
Zero = 4,
Periodic = 5,
AntiPeriodic = 6,
}

[Native]
public enum MPSGraphReductionMode : ulong
{
Min = 0,
Max = 1,
Sum = 2,
Product = 3,
ArgumentMin = 4,
ArgumentMax = 5,
}

[Native]
public enum MPSGraphResizeMode : ulong
{
Nearest = 0,
Bilinear = 1,
}

[Native]
public enum MPSGraphScatterMode : long
{
Add = 0,
Sub = 1,
Mul = 2,
Div = 3,
Min = 4,
Max = 5,
Set = 6,
}

public enum MPSGraphDeviceType : uint
{
Metal = 0,
}

public enum MPSGraphLossReductionType : ulong
{
Axis = 0,
Sum = 1,
Mean = 2,
}

// For COO, indexTensor0 is x index and indexTensor1 is y index
// For CSC, indexTensor0 and indexTensor1 correspond to rowIndex and colStarts respectively.
// For CSR, indexTensor0 and indexTensor1 correspond to colIndex and rowStarts respectively.
public enum MPSGraphSparseStorageType : ulong
{
Coo = 0,
Csc = 1,
Csr = 2,
}

public enum MPSGraphRandomDistribution : ulong
{
Uniform = 0,
Normal = 1,
TruncatedNormal = 2,
}

public enum MPSGraphRandomNormalSamplingMethod : ulong
{
InvCdf = 0,
BoxMuller = 1,
}

}
60 changes: 60 additions & 0 deletions src/MetalPerformanceShadersGraph/MPSGraphExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#nullable enable

using System;
using System.Buffers;
using System.Runtime.InteropServices;

using Foundation;
using ObjCRuntime;
using Metal;
using MetalPerformanceShaders;

namespace MetalPerformanceShadersGraph
{
public static partial class MPSGraphMemoryOps_Extensions
{
public static unsafe MPSGraphTensor Constant (this MPSGraph graph, float scalar)
{
return graph.Constant ((double) scalar, new [] { 1 }, MPSDataType.Float32);
}

public static unsafe MPSGraphTensor Constant (this MPSGraph graph, ReadOnlySpan<float> values, int[] shape)
{
var length = 1;
for (var i = 0; i < shape.Length; i++)
length *= shape [i];
if (length != values.Length)
throw new ArgumentException ($"The number of values ({values.Length}) does not match the shape length ({length}).");
fixed (float* p = values) {
using var data = NSData.FromBytesNoCopy ((IntPtr) p, (nuint) (values.Length * 4), freeWhenDone: false);
return graph.Constant (data, shape, MPSDataType.Float32);
}
}

public static MPSGraphTensor Variable (this MPSGraph graph, float initialValue, int[] shape, string? name = null)
{
var length = 1;
for (var i = 0; i < shape.Length; i++)
length *= shape [i];
var pool = ArrayPool<float>.Shared;
var a = pool.Rent (length);
Array.Fill (a, initialValue);
var v = Variable (graph, a, shape, name);
pool.Return (a);
return v;
}

public static unsafe MPSGraphTensor Variable (this MPSGraph graph, ReadOnlySpan<float> initialValues, int[] shape, string? name = null)
{
var length = 1;
for (var i = 0; i < shape.Length; i++)
length *= shape [i];
if (length != initialValues.Length)
throw new ArgumentException ($"The number of initial values ({initialValues.Length}) does not match the shape length ({length}).");
fixed (float* p = initialValues) {
using var data = NSData.FromBytesNoCopy ((IntPtr) p, (nuint) (initialValues.Length * 4), freeWhenDone: false);
return graph.Variable (data, shape, MPSDataType.Float32, name);
}
}
}
}
Loading

0 comments on commit 3674bbb

Please sign in to comment.