Skip to content

Commit 517cdac

Browse files
Export hidden functions (#989)
Export hidden functions, used by dafny-lang/dafny#5929
1 parent e2b326d commit 517cdac

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

Source/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!-- Target framework and package configuration -->
44
<PropertyGroup>
5-
<Version>3.4.2</Version>
5+
<Version>3.4.3</Version>
66
<TargetFramework>net6.0</TargetFramework>
77
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
88
<Authors>Boogie</Authors>

Source/Provers/LeanAuto/LeanAutoGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void EmitPassiveProgramAsLean(VCGenOptions options, Program p, Tex
4848
var liveDeclarations =
4949
!options.Prune
5050
? p.TopLevelDeclarations
51-
: Pruner.GetLiveDeclarations(options, p, allBlocks.ToList()).ToList();
51+
: Pruner.GetLiveDeclarations(options, p, allBlocks.ToList()).LiveDeclarations.ToList();
5252

5353
generator.WriteLine("-- Type constructors");
5454
// Always include all type constructors

Source/VCGeneration/Prune/DataflowAnalysis.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ protected DataflowAnalysis(IReadOnlyList<TNode> roots,
2222
this.roots = roots;
2323
}
2424

25-
public IReadOnlyDictionary<TNode, TState> States => outStates;
25+
public IReadOnlyDictionary<TNode, TState> OutStates => outStates;
26+
public IReadOnlyDictionary<TNode, TState> InStates => inStates;
27+
2628

2729
protected abstract TState Empty { get; }
2830

Source/VCGeneration/Prune/Pruner.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Linq;
44
using System.Collections.Generic;
5+
using System.Collections.Immutable;
56
using Microsoft.Boogie.GraphUtil;
67
using VCGeneration.Prune;
78

@@ -57,11 +58,11 @@ public class Pruner {
5758
* See Checker.Setup for more information.
5859
* Data type constructor declarations are not pruned and they do affect VC generation.
5960
*/
60-
public static IEnumerable<Declaration> GetLiveDeclarations(VCGenOptions options, Program program, IList<Block>? blocks)
61+
public static (IEnumerable<Declaration> LiveDeclarations, ISet<Function> HiddenFunctions) GetLiveDeclarations(VCGenOptions options, Program program, IList<Block>? blocks)
6162
{
6263
if (program.DeclarationDependencies == null || blocks == null || !options.Prune)
6364
{
64-
return program.TopLevelDeclarations;
65+
return (program.TopLevelDeclarations, ImmutableHashSet<Function>.Empty);
6566
}
6667

6768
var revealedState = GetRevealedState(blocks);
@@ -71,15 +72,28 @@ public static IEnumerable<Declaration> GetLiveDeclarations(VCGenOptions options,
7172
blocksVisitor.Visit(block);
7273
}
7374

75+
var hiddenFunctions = new HashSet<Function>();
7476
var keepRoots = program.TopLevelDeclarations.Where(d => d.Attributes.FindBoolAttribute("keep"));
7577
var reachableDeclarations = GraphAlgorithms.FindReachableNodesInGraphWithMergeNodes(program.DeclarationDependencies,
7678
blocksVisitor.Outgoing.Concat(keepRoots).ToHashSet(), TraverseDeclaration).ToHashSet();
77-
return program.TopLevelDeclarations.Where(d =>
79+
var liveDeclarations = program.TopLevelDeclarations.Where(d =>
7880
d is not Constant && d is not Axiom && d is not Function || reachableDeclarations.Contains(d));
81+
return (liveDeclarations, hiddenFunctions);
7982

80-
bool TraverseDeclaration(object parent, object child) {
81-
return parent is not Function function || child is not Axiom axiom || revealedState.IsRevealed(function)
82-
|| !axiom.CanHide;
83+
bool TraverseDeclaration(object parent, object child)
84+
{
85+
if (parent is not Function function || child is not Axiom axiom || !axiom.CanHide)
86+
{
87+
return true;
88+
}
89+
90+
if (revealedState.IsRevealed(function))
91+
{
92+
return true;
93+
}
94+
95+
hiddenFunctions.Add(function);
96+
return false;
8397
}
8498
}
8599

@@ -93,7 +107,7 @@ private static RevealedState GetRevealedState(IList<Block> blocks)
93107
revealedAnalysis.Run();
94108

95109
var assertions = controlFlowGraph.Nodes.OfType<AssertCmd>();
96-
return assertions.Select(assertCmd => revealedAnalysis.States[assertCmd].Peek()).
110+
return assertions.Select(assertCmd => revealedAnalysis.OutStates[assertCmd].Peek()).
97111
Aggregate(RevealedState.AllHidden, RevealedAnalysis.MergeStates);
98112
}
99113

Source/VCGeneration/Prune/RevealedAnalysis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace VCGeneration.Prune;
99

1010
record RevealedState(HideRevealCmd.Modes Mode, IImmutableSet<Function> Offset) {
1111
public bool IsRevealed(Function function) {
12-
return (Mode == HideRevealCmd.Modes.Hide) == Offset.Contains(function) || function.AlwaysRevealed;
12+
return Mode == HideRevealCmd.Modes.Hide == Offset.Contains(function) || function.AlwaysRevealed;
1313
}
14-
14+
1515
public static readonly RevealedState AllRevealed = new(HideRevealCmd.Modes.Reveal, ImmutableHashSet<Function>.Empty);
1616
public static readonly RevealedState AllHidden = new(HideRevealCmd.Modes.Hide, ImmutableHashSet<Function>.Empty);
1717
}

Source/VCGeneration/Splits/Split.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,35 @@ public class Split : ProofRun
2727

2828
readonly List<Block> bigBlocks = new();
2929
public List<AssertCmd> Asserts => Blocks.SelectMany(block => block.Cmds.OfType<AssertCmd>()).ToList();
30-
public IReadOnlyList<Declaration> prunedDeclarations;
31-
30+
private IReadOnlyList<Declaration> prunedDeclarations;
31+
32+
public ISet<Function> HiddenFunctions {
33+
get {
34+
if (hiddenFunctions == null) {
35+
ComputePrunedDeclsAndHiddenFunctions();
36+
}
37+
return hiddenFunctions;
38+
}
39+
}
40+
3241
public IReadOnlyList<Declaration> PrunedDeclarations {
3342
get {
3443
if (prunedDeclarations == null) {
35-
prunedDeclarations = Pruner.GetLiveDeclarations(parent.Options, parent.program, Blocks).ToList();
44+
ComputePrunedDeclsAndHiddenFunctions();
3645
}
3746

3847
return prunedDeclarations;
3948
}
4049
}
4150

51+
private void ComputePrunedDeclsAndHiddenFunctions()
52+
{
53+
var (liveDeclarations, hiddenFunctions) =
54+
Pruner.GetLiveDeclarations(parent.Options, parent.program, Blocks);
55+
this.hiddenFunctions = hiddenFunctions;
56+
prunedDeclarations = liveDeclarations.ToList();
57+
}
58+
4259
readonly Dictionary<Block /*!*/, BlockStats /*!*/> /*!*/
4360
stats = new Dictionary<Block /*!*/, BlockStats /*!*/>();
4461

@@ -199,6 +216,7 @@ public void DumpDot(int splitNum)
199216

200217
int bsid;
201218
private readonly Func<IList<Block>> getBlocks;
219+
private ISet<Function> hiddenFunctions;
202220

203221
BlockStats GetBlockStats(Block b)
204222
{

0 commit comments

Comments
 (0)