Skip to content

Commit

Permalink
Simplified PhiBindings analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt committed Sep 5, 2023
1 parent 00d5290 commit b4288c0
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions Src/ILGPU/Backends/PhiBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,11 @@

namespace ILGPU.Backends
{
/// <summary>
/// An abstract binding allocator for the class <see cref="PhiBindings"/>.
/// </summary>
public interface IPhiBindingAllocator
{
/// <summary>
/// Processes all phis that are declared in the given node.
/// </summary>
/// <param name="block">The current block.</param>
/// <param name="phis">The phi nodes to process.</param>
void Process(BasicBlock block, Phis phis);

/// <summary>
/// Allocates the given phi node.
/// </summary>
/// <param name="block">The current block.</param>
/// <param name="phiValue">The phi node to allocate.</param>
void Allocate(BasicBlock block, PhiValue phiValue);
}

/// <summary>
/// Maps phi nodes to basic blocks in order to emit move command during
/// the final code generation phase.
/// </summary>
public readonly struct PhiBindings
readonly struct PhiBindings
{
#region Nested Types

Expand Down Expand Up @@ -84,7 +64,6 @@ internal Enumerator(in BlockInfo blockInfo)
void IDisposable.Dispose() { }

/// <summary cref="IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => enumerator.MoveNext();

/// <summary cref="IEnumerator.Reset"/>
Expand Down Expand Up @@ -116,7 +95,7 @@ internal IntermediatePhiCollection(in BlockInfo info)
/// <returns>
/// An enumerator to enumerate all entries in this collection.
/// </returns>
public readonly Enumerator GetEnumerator() => new Enumerator(blockInfo);
public Enumerator GetEnumerator() => new(blockInfo);

#endregion
}
Expand Down Expand Up @@ -154,7 +133,6 @@ internal Enumerator(in PhiBindingCollection collection)
void IDisposable.Dispose() { }

/// <summary cref="IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => enumerator.MoveNext();

/// <summary cref="IEnumerator.Reset"/>
Expand Down Expand Up @@ -183,8 +161,7 @@ internal PhiBindingCollection(in BlockInfo info)
/// <summary>
/// Returns all intermediate phi values that must be assigned to temporaries.
/// </summary>
public readonly IntermediatePhiCollection Intermediates =>
new IntermediatePhiCollection(blockInfo);
public IntermediatePhiCollection Intermediates => new(blockInfo);

#endregion

Expand All @@ -195,7 +172,7 @@ internal PhiBindingCollection(in BlockInfo info)
/// a temporary intermediate variable to be assigned to.
/// </summary>
/// <param name="phi">The phi value to test.</param>
public readonly bool IsIntermediate(PhiValue phi) =>
public bool IsIntermediate(PhiValue phi) =>
blockInfo.IntermediatePhis.Contains(phi);

/// <summary>
Expand All @@ -204,7 +181,7 @@ public readonly bool IsIntermediate(PhiValue phi) =>
/// <returns>
/// An enumerator to enumerate all entries in this collection.
/// </returns>
public readonly Enumerator GetEnumerator() => new Enumerator(this);
public Enumerator GetEnumerator() => new(this);

#endregion
}
Expand Down Expand Up @@ -248,8 +225,7 @@ public BlockInfo(int capacity)
/// </summary>
/// <param name="phi">The phi value it has to be bound to.</param>
/// <param name="value">The source value to read from.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Add(PhiValue phi, Value value)
public void Add(PhiValue phi, Value value)
{
LHSPhis.Add(phi);
// Check whether we are assigning the value of a phi value
Expand All @@ -268,8 +244,8 @@ public readonly void Add(PhiValue phi, Value value)
/// Creates a new <see cref="List{T}"/> instance.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly BlockInfo GetValue(BasicBlock block, int traversalIndex) =>
new BlockInfo(block.Count);
public BlockInfo GetValue(BasicBlock block, int traversalIndex) =>
new(block.Count);
}

#endregion
Expand All @@ -281,16 +257,14 @@ public readonly BlockInfo GetValue(BasicBlock block, int traversalIndex) =>
/// </summary>
/// <typeparam name="TOrder">The current order.</typeparam>
/// <typeparam name="TDirection">The control-flow direction.</typeparam>
/// <typeparam name="TAllocator">The custom allocator type.</typeparam>
/// <param name="collection">The source collection.</param>
/// <param name="allocator">The allocator to use.</param>
/// <returns>The created phi bindings.</returns>
public static PhiBindings Create<TOrder, TDirection, TAllocator>(
public static PhiBindings Create<TOrder, TDirection>(
in BasicBlockCollection<TOrder, TDirection> collection,
TAllocator allocator)
Action<BasicBlock, PhiValue> allocator)
where TOrder : struct, ITraversalOrder
where TDirection : struct, IControlFlowDirection
where TAllocator : struct, IPhiBindingAllocator
{
var mapping = collection.CreateMap(new InfoProvider());

Expand All @@ -300,7 +274,6 @@ public static PhiBindings Create<TOrder, TDirection, TAllocator>(
{
// Resolve phis
var phis = Phis.Create(block);
allocator.Process(block, phis);

// Map all phi arguments
foreach (var phi in phis)
Expand All @@ -309,7 +282,7 @@ public static PhiBindings Create<TOrder, TDirection, TAllocator>(
allPhiValues.Add(phi);

// Allocate phi for further processing
allocator.Allocate(block, phi);
allocator(block, phi);

// Determine predecessor mapping
phi.Assert(block.Predecessors.Length == phi.Nodes.Length);
Expand Down Expand Up @@ -390,10 +363,7 @@ private PhiBindings(
/// <param name="block">The block.</param>
/// <param name="bindings">The resolved bindings (if any)</param>
/// <returns>True, if phi bindings could be resolved.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetBindings(
BasicBlock block,
out PhiBindingCollection bindings)
public bool TryGetBindings(BasicBlock block, out PhiBindingCollection bindings)
{
bindings = default;
if (!phiMapping.TryGetValue(block, out var info))
Expand Down

0 comments on commit b4288c0

Please sign in to comment.