Skip to content

Commit

Permalink
Attempts to fix the lost dependencies during monomoprhization.
Browse files Browse the repository at this point in the history
The fix is a draft and not optimal, for instance it sometimes adds
dependencies that were not part of the `uses` clauses of some functions.
  • Loading branch information
zafer-esen committed Aug 3, 2023
1 parent 0b1e684 commit 41d2e86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 44 additions & 1 deletion Source/Core/Monomorphization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ abstract class BinderExprMonomorphizer
*/

protected MonomorphizationVisitor monomorphizationVisitor;
protected Dictionary<List<Type>, Expr> instanceExprs;
public Dictionary<List<Type>, Expr> instanceExprs { get; }

public BinderExprMonomorphizer(MonomorphizationVisitor monomorphizationVisitor)
{
Expand Down Expand Up @@ -615,6 +615,23 @@ class PolymorphicMapAndBinderSubstituter : VarDeclOnceStandardVisitor
// be applied.
private List<Axiom> splitAxioms;

private Dictionary<Expr, List<Type>> binderExprToTypes = null;
public List<Type> lookupTypeForBinderExpr(Expr binderExpr)
{
if (binderExprToTypes == null)
{
binderExprToTypes = new Dictionary<Expr, List<Type>>();
foreach (var binderMonomorphizer in binderExprMonomorphizers.Values)
{
foreach ((var types, var expr) in binderMonomorphizer.instanceExprs)
{
binderExprToTypes.TryAdd(expr, types);
}
}
}
return binderExprToTypes.TryGetValue(binderExpr, out var res) ? res : new List<Type>();
}

public PolymorphicMapAndBinderSubstituter(MonomorphizationVisitor monomorphizationVisitor)
{
this.monomorphizationVisitor = monomorphizationVisitor;
Expand Down Expand Up @@ -647,6 +664,10 @@ public override Axiom VisitAxiom(Axiom node)
stack.Push(nAryExpr.Args[0]);
stack.Push(nAryExpr.Args[1]);
}
else if (expr == axiom.Expr)
{
splitAxioms.Add(axiom);
}
else
{
splitAxioms.Add(new Axiom(Token.NoToken, expr));
Expand Down Expand Up @@ -1379,6 +1400,28 @@ public static MonomorphizationVisitor Initialize(CoreOptions options, Program pr
polymorphicMapInfo.CreateDatatypeTypeCtorDecl(polymorphicMapAndBinderSubstituter)).ToList();
var splitAxioms = polymorphicMapAndBinderSubstituter.Substitute(program);
program.RemoveTopLevelDeclarations(decl => decl is Axiom);
foreach (var axiom in splitAxioms)
{
if (axiom.FunctionDependencies != null)
{
var functionDependenciesCopy = new List<Function>(axiom.FunctionDependencies);
foreach (var function in functionDependenciesCopy)
{
if (monomorphizationVisitor.functionInstantiations.TryGetValue(function, out var instFuncsMap))
{
var types = polymorphicMapAndBinderSubstituter.lookupTypeForBinderExpr(axiom.Expr);
if (types.Any() && instFuncsMap.ContainsKey(types))
{
var newFunction = instFuncsMap[types];
if (!newFunction.DefinitionAxioms.Contains(axiom))
{
newFunction.AddOtherDefinitionAxiom(axiom);
}
}
}
}
}
}
program.AddTopLevelDeclarations(splitAxioms);
program.AddTopLevelDeclarations(polymorphicMapDatatypeCtorDecls);
Contract.Assert(MonomorphismChecker.IsMonomorphic(program));
Expand Down
1 change: 1 addition & 0 deletions Source/ExecutionEngine/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public PipelineOutcome ResolveAndTypecheck(Program program, string bplFileName,
}
else if (Options.TypeEncodingMethod == CoreOptions.TypeEncoding.Monomorphic)
{
DependencyCollector.Collect(Options, program);
var monomorphizableStatus = Monomorphizer.Monomorphize(Options, program);
if (monomorphizableStatus == MonomorphizableStatus.Monomorphizable)
{
Expand Down

0 comments on commit 41d2e86

Please sign in to comment.