Skip to content

Commit

Permalink
Fixed ControlFlowVerifier on MacOS. (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rs-mt authored May 2, 2024
1 parent 3fa3a85 commit 7aabb14
Showing 1 changed file with 27 additions and 47 deletions.
74 changes: 27 additions & 47 deletions Src/ILGPU/IR/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.IR.Analyses.ControlFlowDirection;
using ILGPU.IR.Analyses.TraversalOrders;
using ILGPU.IR.Values;
using ILGPU.Util;
using System;
Expand Down Expand Up @@ -181,10 +183,6 @@ private sealed class ControlFlowVerifier : VerifierBase
{
#region Instance

private readonly List<BasicBlock> blocksInRpo =
new List<BasicBlock>();
private readonly HashSet<BasicBlock> containedBlocks =
new(new BasicBlock.Comparer());
private readonly Dictionary<BasicBlock, HashSet<BasicBlock>> predecessors =
new(new BasicBlock.Comparer());
private readonly Dictionary<BasicBlock, HashSet<BasicBlock>> successors =
Expand All @@ -203,6 +201,20 @@ public ControlFlowVerifier(Method method, VerificationResult result)

#region Methods

/// <summary>
/// Visits the given block to make sure all sets are properly updated.
/// </summary>
/// <param name="block">The current block.</param>
private void Visit(BasicBlock block)
{
CreateLinkSets(block);

// Iterate over all successors to enumerate their children
block.AssertNoControlFlowUpdate();
foreach (var successor in block.Successors)
AddSuccessor(block, successor);
}

/// <summary>
/// Creates new predecessor and successor link sets.
/// </summary>
Expand Down Expand Up @@ -230,48 +242,13 @@ private void AddSuccessor(BasicBlock block, BasicBlock successor)
predecessors[successor].Add(block);
}

/// <summary>
/// Computes the post order of the given block recursively.
/// </summary>
/// <param name="block">The current block.</param>
private void ComputePostOrder(BasicBlock block)
{
// Skip visited blocks
if (!containedBlocks.Add(block))
return;
CreateLinkSets(block);

// Iterate over all successors to enumerate their children
block.AssertNoControlFlowUpdate();
foreach (var successor in block.Successors)
{
AddSuccessor(block, successor);
ComputePostOrder(successor);
}

// Register our block in the post-order list
blocksInRpo.Add(block);
}

/// <summary>
/// Verifies the existence and the order of all blocks.
/// </summary>
private void VerifyBlocks()
{
var blocks = Method.Blocks;
Assert(Method, blocks.Count == blocksInRpo.Count);

int index = 0;
foreach (var block in blocks)
Assert(Method, block == blocksInRpo[index++]);
}

/// <summary>
/// Verifies all links.
/// </summary>
private void VerifyLinks()
/// <param name="rpo">The input block collection.</param>
private void VerifyLinks(BasicBlockCollection<ReversePostOrder, Forwards> rpo)
{
foreach (var block in blocksInRpo)
foreach (var block in rpo)
{
var successorSet = successors[block];
foreach (var successor in block.CurrentSuccessors)
Expand Down Expand Up @@ -305,11 +282,14 @@ private void VerifyExitBlock()
/// </summary>
public override void Verify()
{
ComputePostOrder(Method.EntryBlock);
blocksInRpo.Reverse();

VerifyBlocks();
VerifyLinks();
var reversePostOrder = Method.Blocks.Traverse<
ReversePostOrder,
Forwards,
BasicBlock.SuccessorsProvider<Forwards>>(default);
foreach (var block in reversePostOrder)
Visit(block);

VerifyLinks(reversePostOrder);
VerifyExitBlock();
}

Expand Down

0 comments on commit 7aabb14

Please sign in to comment.