Skip to content

Commit

Permalink
More .Read implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaMorphic committed Jun 4, 2024
1 parent 6efc84f commit e669083
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Src/ILGPU.Analyzers/ReaderDelegatesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void OnVisitSyntaxNode(SyntaxNode node)
{
AttributeSyntax? attribute;
if (node is ClassDeclarationSyntax cl &&
cl.Parent is NamespaceDeclarationSyntax ns &&
ns.Name.ToString().StartsWith("ILGPU.IR.Values") &&
(attribute = cl.AttributeLists.SelectMany(x => x.Attributes)
.SingleOrDefault(attr => attr.Name.ToString() == "ValueKind"))
is not null)
Expand Down Expand Up @@ -72,7 +74,7 @@ public void Execute(GeneratorExecutionContext context)
builder.AppendLine(" {");
foreach (var kv in valueKindReciever.ValueKinds)
{
builder.AppendLine($" _readerDelegates.Add({kv.Key}, {kv.Value}.Read);");
builder.AppendLine($" _readerDelegates.TryAdd({kv.Key}, {kv.Value}.Read);");
}
builder.AppendLine(" }");
builder.AppendLine(" }");
Expand Down
52 changes: 50 additions & 2 deletions Src/ILGPU/IR/Values/AlignValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,32 @@ protected override string ToArgString() =>
/// Aligns a pointer or a view to a specified alignment in bytes.
/// </summary>
[ValueKind(ValueKind.AlignTo)]
public sealed class AlignTo : BaseAlignOperationValue
public sealed class AlignTo : BaseAlignOperationValue, 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.CreateAlignTo(
Location.Unknown,
header.Nodes[0],
header.Nodes[1]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -187,8 +211,32 @@ protected override string ToPrefixString() =>
/// bytes.
/// </summary>
[ValueKind(ValueKind.AsAligned)]
public sealed class AsAligned : BaseAlignOperationValue
public sealed class AsAligned : BaseAlignOperationValue, 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.CreateAsAligned(
Location.Unknown,
header.Nodes[0],
header.Nodes[1]);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
28 changes: 27 additions & 1 deletion Src/ILGPU/IR/Values/Cast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,34 @@ protected internal override void Write<T>(T writer) =>
/// Cast a pointer from one address space to another.
/// </summary>
[ValueKind(ValueKind.AddressSpaceCast)]
public sealed class AddressSpaceCast : BaseAddressSpaceCast
public sealed class AddressSpaceCast : 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 MemoryAddressSpace targetAddrSpace))
{
return blockBuilder.CreateAddressSpaceCast(
Location.Unknown,
header.Nodes[0],
targetAddrSpace
);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
24 changes: 23 additions & 1 deletion Src/ILGPU/IR/Values/DeviceConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,30 @@ internal DeviceConstantValue(
/// Represents the <see cref="Accelerator.AcceleratorType"/> property.
/// </summary>
[ValueKind(ValueKind.AcceleratorType)]
public sealed class AcceleratorTypeValue : DeviceConstantValue
public sealed class AcceleratorTypeValue : DeviceConstantValue, 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.CreateAcceleratorTypeValue(
Location.Unknown);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
34 changes: 32 additions & 2 deletions Src/ILGPU/IR/Values/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,35 @@ internal MemoryValue(in ValueInitializer initializer, TypeNode staticType)
/// Represents an allocation operation on the stack.
/// </summary>
[ValueKind(ValueKind.Alloca)]
public sealed class Alloca : MemoryValue
public sealed class Alloca : MemoryValue, 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 allocaTypeId) &&
reader.Read(out MemoryAddressSpace addrSpace))
{
return blockBuilder.CreateAlloca(
Location.Unknown,
reader.Context.Types[allocaTypeId],
addrSpace);
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down Expand Up @@ -141,8 +168,11 @@ protected internal override Value Rebuild(
rebuilder.Rebuild(ArrayLength));

/// <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(AllocaType), AllocaType.Id);
writer.Write(nameof(AddressSpace), AddressSpace);
}

/// <summary cref="Value.Accept"/>
public override void Accept<T>(T visitor) => visitor.Visit(this);
Expand Down
23 changes: 22 additions & 1 deletion Src/ILGPU/IR/Values/Undefined.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,29 @@ namespace ILGPU.IR.Values
/// Represents an undefined value.
/// </summary>
[ValueKind(ValueKind.Undefined)]
public sealed class UndefinedValue : Value
public sealed class UndefinedValue : Value, 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.CreateUndefined();
}
else
{
return null;
}
}

#endregion

#region Instance

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Src/ILGPU/IR/Values/ValueKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using ILGPU.IR.Serialization;
using ILGPU.Util;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;

Expand Down Expand Up @@ -396,7 +397,7 @@ public ValueKindAttribute(ValueKind kind)
/// </summary>
public static partial class ValueKinds
{
private readonly static Dictionary<ValueKind,
private readonly static ConcurrentDictionary<ValueKind,
GenericValueReader> _readerDelegates = new();

/// <summary>
Expand Down

0 comments on commit e669083

Please sign in to comment.