Skip to content

Commit c7014d9

Browse files
committed
Drop irreducible exceptions
1 parent 39e295a commit c7014d9

File tree

3 files changed

+19
-62
lines changed

3 files changed

+19
-62
lines changed

Source/Core/AST/Implementation.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,14 @@ public override void Typecheck(TypecheckingContext tc)
659659
graph.ComputeLoops();
660660
if (!graph.Reducible)
661661
{
662-
tc.Error(this, "irreducible control flow graph not allowed");
663-
}
664-
else
665-
{
666-
TypecheckLoopAnnotations(tc, graph);
662+
this.ConvertToReducible(null); // TODO: should change the null
663+
tc.Impl = this;
664+
graph = Program.GraphFromImpl(this);
667665
}
666+
667+
// the graph was either reducible from the begining or was converted to a reducible one
668+
Contract.Assert(graph.Reducible);
669+
TypecheckLoopAnnotations(tc, graph);
668670
}
669671
}
670672
}
@@ -1130,7 +1132,7 @@ private QKeyValue FreshenCaptureState(QKeyValue Attributes, int FreshCounter)
11301132

11311133
return result;
11321134
}
1133-
public Graph<Block> ConvertToReducible(CoreOptions options)
1135+
public Graph<Block> ConvertToReducible(CoreOptions /*?*/ options)
11341136
{
11351137
Dictionary<Block, int> nextLabels = this.Blocks.ToDictionary(b => b, _ => 0);
11361138
Graph<Block> g = Program.GraphFromImpl(this);
@@ -1186,7 +1188,10 @@ public Graph<Block> ConvertToReducible(CoreOptions options)
11861188
duplicatesDict.Values.ForEach(b => b.SubstituteBranchTargets(duplicatesDict));
11871189
}
11881190

1189-
PruneUnreachableBlocks(options);
1191+
if (options != null)
1192+
{
1193+
PruneUnreachableBlocks(options);
1194+
}
11901195
ComputePredecessorsForBlocks();
11911196
g = Program.GraphFromImpl(this);
11921197
g.ComputeLoops();

Source/Core/AST/Program.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,21 +541,19 @@ public static Graph<Block> GraphFromImpl(Implementation impl, bool forward = tru
541541
return GraphFromBlocks(impl.Blocks, forward);
542542
}
543543

544-
public class IrreducibleLoopException : Exception
545-
{
546-
}
547-
548544
public Graph<Block> ProcessLoops(CoreOptions options, Implementation impl)
549545
{
550546
impl.PruneUnreachableBlocks(options);
551547
impl.ComputePredecessorsForBlocks();
552548
Graph<Block> g = GraphFromImpl(impl);
553549
g.ComputeLoops();
554-
if (g.Reducible)
550+
if (!g.Reducible)
555551
{
556-
return g;
552+
impl.ConvertToReducible(options);
553+
g = GraphFromImpl(impl);
557554
}
558-
throw new IrreducibleLoopException();
555+
Contract.Assert(g.Reducible);
556+
return g;
559557
}
560558

561559

Source/VCGeneration/LoopExtractor.cs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,8 @@ public static (Dictionary<string, Dictionary<string, Block>> loops, HashSet<Impl
2323
{
2424
if (impl.Blocks != null && impl.Blocks.Count > 0)
2525
{
26-
try
27-
{
28-
Graph<Block> g = program.ProcessLoops(options, impl);
29-
CreateProceduresForLoops(options, impl, g, loopImpls, fullMap);
30-
}
31-
catch (Program.IrreducibleLoopException)
32-
{
33-
System.Diagnostics.Debug.Assert(!fullMap.ContainsKey(impl.Name));
34-
fullMap[impl.Name] = null;
35-
hasIrreducibleLoops.Add(impl);
36-
37-
if (options.LoopUnrollCount == -1)
38-
{
39-
continue;
40-
}
41-
42-
// statically unroll loops in this procedure
43-
44-
// First, build a map of the current blocks
45-
var origBlocks = new Dictionary<string, Block>();
46-
foreach (var blk in impl.Blocks)
47-
{
48-
origBlocks.Add(blk.Label, blk);
49-
}
50-
51-
// unroll
52-
Block start = impl.Blocks[0];
53-
impl.Blocks = LoopUnroll.UnrollLoops(start, options.LoopUnrollCount, false);
54-
55-
// Now construct the "map back" information
56-
// Resulting block label -> original block
57-
var blockMap = new Dictionary<string, Block>();
58-
foreach (var blk in impl.Blocks)
59-
{
60-
var sl = LoopUnroll.sanitizeLabel(blk.Label);
61-
if (sl == blk.Label)
62-
{
63-
blockMap.Add(blk.Label, blk);
64-
}
65-
else
66-
{
67-
Contract.Assert(origBlocks.ContainsKey(sl));
68-
blockMap.Add(blk.Label, origBlocks[sl]);
69-
}
70-
}
71-
72-
fullMap[impl.Name] = blockMap;
73-
}
26+
Graph<Block> g = program.ProcessLoops(options, impl);
27+
CreateProceduresForLoops(options, impl, g, loopImpls, fullMap);
7428
}
7529
}
7630

0 commit comments

Comments
 (0)