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

Generalize IControlFlowGraph successors #103676

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ public IEnumerable<BlockProxy> Blocks {

public BlockProxy Entry => new BlockProxy (ControlFlowGraph.EntryBlock ());

public static ControlFlowBranch? CreateProxyBranch (Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowBranch? branch)
public static ControlFlowBranch CreateProxyBranch (Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowBranch branch)
{
if (branch == null)
return null;

var finallyRegions = ImmutableArray.CreateBuilder<RegionProxy> ();
foreach (var region in branch.FinallyRegions) {
Debug.Assert (region != null);
Expand All @@ -73,7 +70,12 @@ public IEnumerable<BlockProxy> Blocks {
new BlockProxy (branch.Source),
branch.Destination == null ? null : new BlockProxy (branch.Destination),
finallyRegions.ToImmutable (),
branch.IsConditionalSuccessor);
branch.Source.ConditionKind switch {
ControlFlowConditionKind.None => ConditionKind.Unconditional,
ControlFlowConditionKind.WhenFalse => branch.IsConditionalSuccessor ? ConditionKind.WhenFalse : ConditionKind.WhenTrue,
ControlFlowConditionKind.WhenTrue => branch.IsConditionalSuccessor ? ConditionKind.WhenTrue : ConditionKind.WhenFalse,
sbomer marked this conversation as resolved.
Show resolved Hide resolved
_ => throw new InvalidOperationException ()
});
}

// This is implemented by getting predecessors of the underlying Roslyn BasicBlock.
Expand All @@ -86,9 +88,13 @@ public IEnumerable<ControlFlowBranch> GetPredecessors (BlockProxy block)
}
}

public ControlFlowBranch? GetConditionalSuccessor (BlockProxy block) => CreateProxyBranch (block.Block.ConditionalSuccessor);

public ControlFlowBranch? GetFallThroughSuccessor (BlockProxy block) => CreateProxyBranch (block.Block.FallThroughSuccessor);
public IEnumerable<ControlFlowBranch> GetSuccessors (BlockProxy block)
{
if (block.Block.ConditionalSuccessor is Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowBranch conditionalSuccessor)
yield return CreateProxyBranch (conditionalSuccessor);
if (block.Block.FallThroughSuccessor is Microsoft.CodeAnalysis.FlowAnalysis.ControlFlowBranch fallThroughSuccessor)
yield return CreateProxyBranch (fallThroughSuccessor);
}

public bool TryGetEnclosingTryOrCatchOrFilter (BlockProxy block, out RegionProxy tryOrCatchOrFilterRegion)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

// This is needed due to NativeAOT which doesn't enable nullable globally yet
#nullable enable
Expand Down Expand Up @@ -65,13 +66,12 @@ public ControlFlowGraphState (TControlFlowGraph cfg, TLattice lattice, TValue en
this.cfg = cfg;
this.lattice = lattice;

var entryOut = cfg.GetFallThroughSuccessor (cfg.Entry);
IControlFlowGraph<TBlock, TRegion>.ControlFlowBranch? entryOut = cfg.GetSuccessors (cfg.Entry).SingleOrDefault ();
jkurdek marked this conversation as resolved.
Show resolved Hide resolved
Debug.Assert (entryOut != null);
Debug.Assert (cfg.GetConditionalSuccessor (cfg.Entry) == null);
if (entryOut == null)
return;

branchInput[entryOut!.Value] = new TState () {
branchInput[entryOut.Value] = new TState () {
Lattice = lattice,
Current = entryValue,
Exception = null
Expand Down Expand Up @@ -187,35 +187,20 @@ void TransferOut (TTransfer transfer, TControlFlowGraph cfg, TBlock block, TStat
)
{
TConditionValue? conditionValue = transfer.Transfer (block, state);

if (cfg.GetConditionalSuccessor (block) is IControlFlowGraph<TBlock, TRegion>.ControlFlowBranch conditionalBranch) {
// Duplicate the current state so that it's not shared with fall-through state.
TValue conditionalCurrentState = lattice.Meet (lattice.Top, state.Current);

if (conditionValue != null) {
transfer.ApplyCondition (
// ConditionKind 'WhenTrue' means the condition is true in the conditional branch.
block.ConditionKind is ConditionKind.WhenTrue
? conditionValue.Value
: conditionValue.Value.Negate (),
ref conditionalCurrentState);
}

updateState (conditionalBranch, conditionalCurrentState);
}

if (cfg.GetFallThroughSuccessor (block) is IControlFlowGraph<TBlock, TRegion>.ControlFlowBranch fallThroughBranch) {
TValue fallThroughCurrentState = state.Current;
foreach (var successor in cfg.GetSuccessors (block)) {
// Duplicate the state so that it's not shared between branches.
TValue branchState = lattice.Meet (lattice.Top, state.Current);
if (conditionValue != null) {
Debug.Assert (successor.ConditionKind is ConditionKind.WhenTrue or ConditionKind.WhenFalse);
transfer.ApplyCondition (
// ConditionKind 'WhenFalse' means the condition is true in the fall-through branch (false in the conditional branch).
block.ConditionKind is ConditionKind.WhenFalse
// ConditionKind 'WhenTrue' means the condition is true in this branch.
successor.ConditionKind is ConditionKind.WhenTrue
? conditionValue.Value
: conditionValue.Value.Negate (),
ref fallThroughCurrentState);
ref branchState);
}

updateState (fallThroughBranch, fallThroughCurrentState);
updateState (successor, branchState);
}
}

Expand Down Expand Up @@ -423,11 +408,11 @@ void FlowStateThroughExitedFinallys (
changed = true;

TBlock lastFinallyBlock = cfg.LastBlock (exitedFinally);
Debug.Assert (cfg.GetConditionalSuccessor (lastFinallyBlock) == null);
IControlFlowGraph<TBlock, TRegion>.ControlFlowBranch? finallyExit = cfg.GetFallThroughSuccessor (lastFinallyBlock);
IControlFlowGraph<TBlock, TRegion>.ControlFlowBranch? finallyExit = cfg.GetSuccessors (lastFinallyBlock).SingleOrDefault ();
Debug.Assert (finallyExit != null);
if (finallyExit == null)
continue;
Debug.Assert (finallyExit.Value.ConditionKind == ConditionKind.Unconditional);
predecessorState = cfgState.Get (finallyExit.Value).Current;
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/tools/illink/src/ILLink.Shared/DataFlow/IControlFlowGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public enum RegionKind

public enum ConditionKind
{
None,
Unconditional,
WhenFalse,
WhenTrue,
Unknown
}

public interface IRegion<TRegion> : IEquatable<TRegion>
Expand All @@ -33,7 +34,6 @@ public interface IRegion<TRegion> : IEquatable<TRegion>

public interface IBlock<TBlock> : IEquatable<TBlock>
{
ConditionKind ConditionKind { get; }
}

public interface IControlFlowGraph<TBlock, TRegion>
Expand All @@ -45,8 +45,12 @@ public interface IControlFlowGraph<TBlock, TRegion>
public readonly struct ControlFlowBranch : IEquatable<ControlFlowBranch>
{
public readonly TBlock Source;

// Might be null in a 'throw' branch.
public readonly TBlock? Destination;

public readonly ConditionKind ConditionKind;

// The finally regions exited when control flows through this edge.
// For example:
//
Expand All @@ -62,21 +66,21 @@ public interface IControlFlowGraph<TBlock, TRegion>
// Source() to the block that calls Target(), which exits both
// finally regions.
public readonly ImmutableArray<TRegion> FinallyRegions;
public readonly bool IsConditional;
public ControlFlowBranch (TBlock source, TBlock? destination, ImmutableArray<TRegion> finallyRegions, bool isConditional)

public ControlFlowBranch (TBlock source, TBlock? destination, ImmutableArray<TRegion> finallyRegions, ConditionKind conditionKind)
{
Source = source;
Destination = destination;
FinallyRegions = finallyRegions;
IsConditional = isConditional;
ConditionKind = conditionKind;
}

public bool Equals (ControlFlowBranch other)
{
if (!Source.Equals (other.Source))
return false;

if (IsConditional != other.IsConditional)
if (ConditionKind != other.ConditionKind)
return false;

if (Destination == null)
Expand All @@ -95,7 +99,7 @@ public override int GetHashCode ()
return HashUtils.Combine (
Source.GetHashCode (),
Destination?.GetHashCode () ?? typeof (ControlFlowBranch).GetHashCode (),
IsConditional.GetHashCode ());
ConditionKind.GetHashCode ());
}
}

Expand All @@ -108,9 +112,7 @@ public override int GetHashCode ()
// control flow from try -> finally or from catch -> finally.
IEnumerable<ControlFlowBranch> GetPredecessors (TBlock block);

ControlFlowBranch? GetConditionalSuccessor (TBlock block);

ControlFlowBranch? GetFallThroughSuccessor (TBlock block);
IEnumerable<ControlFlowBranch> GetSuccessors (TBlock block);

bool TryGetEnclosingTryOrCatchOrFilter (TBlock block, [NotNullWhen (true)] out TRegion? tryOrCatchOrFilterRegion);

Expand Down
Loading