Skip to content

Commit

Permalink
More work on deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaMorphic committed Jul 24, 2024
1 parent 50438ca commit 6130b4f
Show file tree
Hide file tree
Showing 8 changed files with 709 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Src/ILGPU/IR/Serialization/IIRWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public partial interface IIRWriter
void Write(string tag, string value);

/// <summary>
/// Serializes a string value to the stream.
/// Serializes an enum value to the stream.
/// </summary>
/// <param name="tag">
/// A tag that describes the purpose of this value.
Expand Down
57 changes: 55 additions & 2 deletions Src/ILGPU/IR/Values/ArrayValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,37 @@ namespace ILGPU.IR.Values
/// Represents an allocation operation of a new array in a particular address space.
/// </summary>
[ValueKind(ValueKind.Array)]
public sealed class NewArray : ControlFlowValue
public sealed class NewArray : ControlFlowValue, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out long typeId))
{
var newArrayBuilder = blockBuilder.CreateNewArray(
Location.Unknown, reader.Context.Types[typeId]
.AsNotNullCast<ArrayType>());

foreach (var node in header.Nodes)
newArrayBuilder.Add(node);

return newArrayBuilder.Seal();
}
else
{
return null;
}
}

#endregion

#region Nested Types

/// <summary>
Expand Down Expand Up @@ -185,8 +214,32 @@ public interface IArrayValueOperation
/// Gets the length of an array value or a particular array dimension.
/// </summary>
[ValueKind(ValueKind.GetArrayLength)]
public sealed class GetArrayLength : Value, IArrayValueOperation
public sealed class GetArrayLength : Value, IArrayValueOperation, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder))
{
return blockBuilder.CreateGetArrayLength(
Location.Unknown,
header.Nodes[0],
header.Nodes[1]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
60 changes: 58 additions & 2 deletions Src/ILGPU/IR/Values/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,36 @@ public enum AtomicKind
/// Represents a generic atomic operation.
/// </summary>
[ValueKind(ValueKind.GenericAtomic)]
public sealed class GenericAtomic : AtomicValue
public sealed class GenericAtomic : AtomicValue, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out AtomicKind kind) &&
reader.Read(out AtomicFlags flags))
{
return blockBuilder.CreateAtomic(
Location.Unknown,
header.Nodes[0],
header.Nodes[1],
kind, flags
);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -227,8 +255,36 @@ protected internal override void Write<T>(T writer)
/// Represents an atomic compare-and-swap operation.
/// </summary>
[ValueKind(ValueKind.AtomicCAS)]
public sealed class AtomicCAS : AtomicValue
public sealed class AtomicCAS : AtomicValue, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out AtomicFlags flags))
{
return blockBuilder.CreateAtomicCAS(
Location.Unknown,
header.Nodes[0],
header.Nodes[1],
header.Nodes[2],
flags
);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
178 changes: 170 additions & 8 deletions Src/ILGPU/IR/Values/Cast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,30 @@ protected PointerIntCast(
/// Casts from an integer to a raw pointer value.
/// </summary>
[ValueKind(ValueKind.IntAsPointerCast)]
public sealed class IntAsPointerCast : PointerIntCast
public sealed class IntAsPointerCast : PointerIntCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder))
{
return blockBuilder.CreateIntAsPointerCast(
Location.Unknown, header.Nodes[0]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -156,8 +178,32 @@ protected internal override void Write<T>(T writer) { }
/// Casts from a pointer value to an integer.
/// </summary>
[ValueKind(ValueKind.PointerAsIntCast)]
public sealed class PointerAsIntCast : PointerIntCast
public sealed class PointerAsIntCast : PointerIntCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out BasicValueType targetBasicValueType))
{
return blockBuilder.CreatePointerAsIntCast(
Location.Unknown, header.Nodes[0],
targetBasicValueType);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -214,7 +260,8 @@ protected internal override Value Rebuild(
TargetBasicValueType);

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

/// <summary cref="Value.Accept"/>
public override void Accept<T>(T visitor) => visitor.Visit(this);
Expand Down Expand Up @@ -269,8 +316,32 @@ internal BaseAddressSpaceCast(
/// Casts the type of a pointer to a different type.
/// </summary>
[ValueKind(ValueKind.PointerCast)]
public sealed class PointerCast : BaseAddressSpaceCast
public sealed class PointerCast : BaseAddressSpaceCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out long targetTypeId))
{
return blockBuilder.CreatePointerCast(
Location.Unknown, header.Nodes[0],
reader.Context.Types[targetTypeId]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -478,8 +549,33 @@ protected internal override void Write<T>(T writer) =>
/// Casts a view from one element type to another.
/// </summary>
[ValueKind(ValueKind.ViewCast)]
public sealed class ViewCast : BaseAddressSpaceCast
public sealed class ViewCast : BaseAddressSpaceCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder) &&
reader.Read(out long targetElementTypeId))
{
return blockBuilder.CreateViewCast(
Location.Unknown, header.Nodes[0],
reader.Context.Types[targetElementTypeId]
);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -561,8 +657,30 @@ protected internal override void Write<T>(T writer) =>
/// Casts an array to a linear array view.
/// </summary>
[ValueKind(ValueKind.ArrayToViewCast)]
public sealed class ArrayToViewCast : CastValue
public sealed class ArrayToViewCast : CastValue, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder))
{
return blockBuilder.CreateArrayToViewCast(
Location.Unknown, header.Nodes[0]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -697,8 +815,30 @@ protected sealed override TypeNode ComputeType(
/// Casts from a float to an int while preserving bits.
/// </summary>
[ValueKind(ValueKind.FloatAsIntCast)]
public sealed class FloatAsIntCast : BitCast
public sealed class FloatAsIntCast : BitCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder))
{
return blockBuilder.CreateFloatAsIntCast(
Location.Unknown, header.Nodes[0]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -766,8 +906,30 @@ protected internal override void Write<T>(T writer) { }
/// Casts from an int to a float while preserving bits.
/// </summary>
[ValueKind(ValueKind.IntAsFloatCast)]
public sealed class IntAsFloatCast : BitCast
public sealed class IntAsFloatCast : BitCast, IValueReader
{
#region Static

/// <summary cref="IValueReader.Read(ValueHeader, IIRReader)"/>
public static Value? Read(ValueHeader header, IIRReader reader)
{
var methodBuilder = header.Method?.MethodBuilder;
if (methodBuilder is not null &&
header.Block is not null &&
header.Block.GetOrCreateBuilder(methodBuilder,
out BasicBlock.Builder? blockBuilder))
{
return blockBuilder.CreateIntAsFloatCast(
Location.Unknown, header.Nodes[0]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
Loading

0 comments on commit 6130b4f

Please sign in to comment.