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

[NativeAOT] Implement IIRWriter API #1228

Merged
merged 16 commits into from
Aug 29, 2024
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
30 changes: 29 additions & 1 deletion Src/ILGPU/IR/IRContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2023 ILGPU Project
// Copyright (c) 2018-2024 ILGPU Project
// www.ilgpu.net
//
// File: IRContext.cs
Expand All @@ -12,6 +12,7 @@
using ILGPU.Frontend;
using ILGPU.IR.Analyses;
using ILGPU.IR.Construction;
using ILGPU.IR.Serialization;
using ILGPU.IR.Transformations;
using ILGPU.IR.Types;
using ILGPU.IR.Values;
Expand Down Expand Up @@ -498,6 +499,33 @@ public void Transform(in Transformer transformer)
Parallel.ForEach(toTransform, gcDelegate);
}

/// <summary>
/// Serializes this instance using the given <see cref="IIRWriter"/>.
/// </summary>
/// <typeparam name="T">
/// The specific type of <see cref="IIRWriter"/>.
/// </typeparam>
/// <param name="writer">
/// The writer to use for serialization.
/// </param>
public void Write<T>(T writer) where T : IIRWriter
{
var allMethods = GetMethodCollection(new MethodCollections.AllMethods());
foreach (var method in allMethods)
{
foreach (var param in method.Parameters)
param.Write(writer);

foreach (var block in method.Blocks)
{
foreach (var entry in block)
entry.Value.Write(writer);

block.Terminator?.Write(writer);
}
}
}

/// <summary>
/// Dumps the IR context to the given text writer.
/// </summary>
Expand Down
67 changes: 67 additions & 0 deletions Src/ILGPU/IR/Serialization/BinaryIRWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: BinaryIRWriter.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Util;
using System;
using System.IO;
using System.Text;

namespace ILGPU.IR.Serialization
{
/// <summary>
/// Wrapper class around <see cref="BinaryWriter"/>
/// for serializing IR types and values.
/// </summary>
public sealed partial class BinaryIRWriter : DisposeBase, IIRWriter
{
private readonly BinaryWriter writer;

/// <summary>
/// Wraps an instance of <see cref="BinaryIRWriter"/>
/// around a given <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="Stream"/> instance to wrap.
/// </param>
/// <param name="encoding">
/// The <see cref="Encoding"/> to use for
/// serializing <see cref="string"/> values.
/// </param>
public BinaryIRWriter(Stream stream, Encoding encoding)
{
writer = new BinaryWriter(stream, encoding);
}

/// <inheritdoc/>
public void Write(string tag, int value) =>
writer.Write(value);

/// <inheritdoc/>
public void Write(string tag, long value) =>
writer.Write(value);

/// <inheritdoc/>
public void Write(string tag, string value) =>
writer.Write(value);

/// <inheritdoc/>
public void Write<T>(string tag, T value) where T : Enum =>
writer.Write(Convert.ToInt32(value));

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
writer.Dispose();
base.Dispose(disposing);
}
}
}
66 changes: 66 additions & 0 deletions Src/ILGPU/IR/Serialization/IIRWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: IIRWriter.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;

namespace ILGPU.IR.Serialization
{
/// <summary>
/// Describes a wrapper that serializes IR values and types
/// to some implementation-specific instance
/// </summary>
public partial interface IIRWriter
{
/// <summary>
/// Serializes a 32-bit integer value to the stream.
/// </summary>
/// <param name="tag">
/// A tag that describes the purpose of this value.
/// </param>
/// <param name="value">
/// The value to serialize.
/// </param>
void Write(string tag, int value);

/// <summary>
/// Serializes a 64-bit integer value to the stream.
/// </summary>
/// <param name="tag">
/// A tag that describes the purpose of this value.
/// </param>
/// <param name="value">
/// The value to serialize.
/// </param>
void Write(string tag, long value);

/// <summary>
/// Serializes a 32-bit integer value to the stream.
/// </summary>
/// <param name="tag">
/// A tag that describes the purpose of this value.
/// </param>
/// <param name="value">
/// The value to serialize.
/// </param>
void Write(string tag, string value);

/// <summary>
/// Serializes a string value to the stream.
/// </summary>
/// <param name="tag">
/// A tag that describes the purpose of this value.
/// </param>
/// <param name="value">
/// The value to serialize.
/// </param>
void Write<T>(string tag, T value) where T : Enum;
}
}
12 changes: 11 additions & 1 deletion Src/ILGPU/IR/Types/ArrayType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2019-2023 ILGPU Project
// Copyright (c) 2019-2024 ILGPU Project
// www.ilgpu.net
//
// File: ArrayType.cs
Expand Down Expand Up @@ -47,6 +47,9 @@ internal ArrayType(
/// <inheritdoc/>
public override bool IsArrayType => true;

/// <inheritdoc/>
public override TypeKind TypeKind => TypeKind.Array;

/// <summary>
/// Returns the underlying element type.
/// </summary>
Expand Down Expand Up @@ -75,6 +78,13 @@ protected override Type GetManagedType<TTypeProvider>(
TTypeProvider typeProvider) =>
typeProvider.GetArrayType(this);

/// <summary cref="TypeNode.Write{T}(T)"/>
protected internal override void Write<T>(T writer)
{
writer.Write(nameof(ElementType), ElementType.Id);
writer.Write(nameof(NumDimensions), NumDimensions);
}

#endregion

#region Object
Expand Down
12 changes: 11 additions & 1 deletion Src/ILGPU/IR/Types/HandleType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2019-2023 ILGPU Project
// Copyright (c) 2019-2024 ILGPU Project
// www.ilgpu.net
//
// File: HandleType.cs
Expand Down Expand Up @@ -30,6 +30,13 @@ internal HandleType(IRTypeContext typeContext)

#endregion

#region Properties

/// <inheritdoc/>
public override TypeKind TypeKind => TypeKind.Handle;

#endregion

#region Methods

/// <summary>
Expand All @@ -39,6 +46,9 @@ protected override Type GetManagedType<TTypeProvider>(
TTypeProvider typeProvider) =>
typeof(object);

/// <summary cref="TypeNode.Write{T}(T)"/>
protected internal override void Write<T>(T writer) { }

#endregion

#region Object
Expand Down
2 changes: 1 addition & 1 deletion Src/ILGPU/IR/Types/IIRTypeContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2023 ILGPU Project
// Copyright (c) 2018-2024 ILGPU Project
// www.ilgpu.net
//
// File: IIRTypeContext.cs
Expand Down
33 changes: 33 additions & 0 deletions Src/ILGPU/IR/Types/IRTypeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// ---------------------------------------------------------------------------------------

using ILGPU.Backends;
using ILGPU.IR.Serialization;
using ILGPU.Resources;
using ILGPU.Util;
using System;
Expand Down Expand Up @@ -579,6 +580,38 @@ public override void ClearCache(ClearCacheMode mode)
PopulateTypeMapping();
}

/// <summary>
/// Acquires a lock on the current <see cref="IRTypeContext"/> and
/// retrieves an <see cref="IEnumerable{TypeNode}"/> containing the
/// full type unification therewithin.
/// </summary>
/// <returns>
/// The retrieved type unification as a generic collection.
/// </returns>
public IEnumerable<TypeNode> GetUnifiedTypeCollection()
{
using var readScope = typeLock.EnterReadScope();
foreach (var (type, _) in unifiedTypes)
{
yield return type;
}
}

/// <summary>
/// Serializes this instance using the given <see cref="IIRWriter"/>.
/// </summary>
/// <typeparam name="T">
/// The specific type of <see cref="IIRWriter"/>.
/// </typeparam>
/// <param name="writer">
/// The writer to use for serialization.
/// </param>
public void Write<T>(T writer) where T : IIRWriter
{
foreach (var type in GetUnifiedTypeCollection())
writer.Write(type);
}

#endregion

#region IDisposable
Expand Down
10 changes: 8 additions & 2 deletions Src/ILGPU/IR/Types/PaddingType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2020-2023 ILGPU Project
// Copyright (c) 2020-2024 ILGPU Project
// www.ilgpu.net
//
// File: PaddingType.cs
Expand All @@ -9,7 +9,6 @@
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Util;
using System;

namespace ILGPU.IR.Types
Expand Down Expand Up @@ -41,6 +40,9 @@ internal PaddingType(IRTypeContext typeContext, PrimitiveType primitiveType)
/// <inheritdoc/>
public override bool IsPaddingType => true;

/// <inheritdoc/>
public override TypeKind TypeKind => TypeKind.Padding;

/// <summary>
/// Returns the associated basic value type.
/// </summary>
Expand All @@ -62,6 +64,10 @@ protected override Type GetManagedType<TTypeProvider>(
TTypeProvider typeProvider) =>
typeProvider.GetPrimitiveType(PrimitiveType);

/// <summary cref="TypeNode.Write{T}(T)"/>
protected internal override void Write<T>(T writer) =>
writer.Write(nameof(PrimitiveType), PrimitiveType.Id);

#endregion

#region Object
Expand Down
19 changes: 18 additions & 1 deletion Src/ILGPU/IR/Types/PointerTypes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2018-2023 ILGPU Project
// Copyright (c) 2018-2024 ILGPU Project
// www.ilgpu.net
//
// File: PointerTypes.cs
Expand Down Expand Up @@ -126,6 +126,17 @@ protected AddressSpaceType(

#endregion

#region Methods

/// <summary cref="TypeNode.Write{T}(T)"/>
protected internal override void Write<T>(T writer)
{
writer.Write(nameof(ElementType), ElementType.Id);
writer.Write(nameof(AddressSpace), AddressSpace);
}

#endregion

#region Object

/// <summary cref="TypeNode.GetHashCode"/>
Expand Down Expand Up @@ -185,6 +196,9 @@ internal PointerType(
/// <inheritdoc/>
public override bool IsPointerType => true;

/// <inheritdoc/>
public override TypeKind TypeKind => TypeKind.Pointer;

/// <summary>
/// Returns the associated basic value type.
/// </summary>
Expand Down Expand Up @@ -257,6 +271,9 @@ internal ViewType(
/// <inheritdoc/>
public override bool IsViewType => true;

/// <inheritdoc/>
public override TypeKind TypeKind => TypeKind.View;

#endregion

#region Methods
Expand Down
Loading
Loading