Skip to content

Commit

Permalink
Enable inlining before compiling lists of instructions in VM
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Dec 1, 2024
1 parent f126731 commit 1489cae
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions implement/pine/Pine/PineVM/PineVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,27 +279,45 @@ public ExpressionCompilation ExpressionCompilationLessCache(Expression rootExpre

compilationEnvClasses?.TryGetValue(rootExpression, out specializations);

bool skipInlining(Expression expr, EnvConstraintId? envConstraintId)
{
if (Precompiled.HasPrecompiledForExpression(expr))
{
return true;
}

if (envConstraintId is null && (compilationEnvClasses?.ContainsKey(expr) ?? false))
{
return true;
}

return false;
}

return
CompileExpression(
rootExpression,
specializations ?? [],
parseCache: parseCache,
disableReduction: disableReductionInCompilation);
CompileExpression(
rootExpression,
specializations ?? [],
parseCache: parseCache,
disableReduction: disableReductionInCompilation,
skipInlining: skipInlining);
}

public static ExpressionCompilation CompileExpression(
Expression rootExpression,
IReadOnlyList<EnvConstraintId> specializations,
PineVMParseCache parseCache,
bool disableReduction)
bool disableReduction,
Func<Expression, EnvConstraintId?, bool> skipInlining)
{
var generic =
new StackFrameInstructions(
InstructionsFromExpressionTransitive(
rootExpression,
envConstraintId: null,
parseCache: parseCache,
disableReduction: disableReduction),
disableReduction: disableReduction,
skipInlining: skipInlining),
TrackEnvConstraint: null);

var specialized =
Expand All @@ -316,7 +334,8 @@ public static ExpressionCompilation CompileExpression(
rootExpression,
envConstraintId: specialization,
parseCache: parseCache,
disableReduction: disableReduction),
disableReduction: disableReduction,
skipInlining: skipInlining),
TrackEnvConstraint: specialization)))
.ToImmutableArray();

Expand Down Expand Up @@ -346,7 +365,8 @@ public static IReadOnlyList<StackInstruction> InstructionsFromExpressionTransiti
Expression rootExpression,
EnvConstraintId? envConstraintId,
PineVMParseCache parseCache,
bool disableReduction)
bool disableReduction,
Func<Expression, EnvConstraintId?, bool> skipInlining)
{
var expressionWithEnvConstraint =
envConstraintId is null ?
Expand All @@ -366,14 +386,14 @@ envConstraintId is null ?
expressionWithEnvConstraint
:
ReduceExpressionAndInlineRecursive(
rootExpression: rootExpression,
currentExpression: expressionWithEnvConstraint,
inlinedParents: [],
maxDepth: 7,
maxSubexpressionCount: 4_000,
parseCache: parseCache,
envConstraintId: envConstraintId,
disableRecurseAfterInline: false);
disableRecurseAfterInline: false,
skipInlining: skipInlining);

IReadOnlyList<StackInstruction> allInstructions =
[.. InstructionsFromExpressionTransitive(reducedExpression).Append(StackInstruction.Return)];
Expand All @@ -387,26 +407,27 @@ public static Expression ReduceExpressionAndInlineRecursive(
int maxDepth,
int maxSubexpressionCount,
PineVMParseCache parseCache,
bool disableRecurseAfterInline) =>
bool disableRecurseAfterInline,
Func<Expression, EnvConstraintId?, bool> skipInlining) =>
ReduceExpressionAndInlineRecursive(
rootExpression: rootExpression,
currentExpression: rootExpression,
inlinedParents: [],
envConstraintId: envConstraintId,
maxDepth: maxDepth,
maxSubexpressionCount: maxSubexpressionCount,
parseCache: parseCache,
disableRecurseAfterInline: disableRecurseAfterInline);
disableRecurseAfterInline: disableRecurseAfterInline,
skipInlining: skipInlining);

public static Expression ReduceExpressionAndInlineRecursive(
Expression rootExpression,
Expression currentExpression,
ImmutableStack<Expression> inlinedParents,
EnvConstraintId? envConstraintId,
int maxDepth,
int maxSubexpressionCount,
PineVMParseCache parseCache,
bool disableRecurseAfterInline)
bool disableRecurseAfterInline,
Func<Expression, EnvConstraintId?, bool> skipInlining)
{
var expressionSubstituted =
envConstraintId is null
Expand All @@ -433,7 +454,12 @@ envConstraintId is null
* Stopping recursion here if envConstraintId is null resulted in significantly faster
* completion times in a test compiling all modules of the Elm compiler.
* */

/*
* 2024-11-30: Enable inlining also for cases without environment classes.
*
return expressionReduced;
*/
}


Expand All @@ -452,19 +478,19 @@ envConstraintId is null
{
Expression? ContinueReduceForKnownExprValue(PineValue exprValue)
{
if (parseCache.ParseExpression(exprValue) is not Result<string, Expression>.Ok parseOk)
if (parseCache.ParseExpression(exprValue).IsOkOrNull() is not { } parseOk)
{
return null;
}

if (rootExpression.Equals(parseOk.Value))
if (skipInlining(parseOk, envConstraintId))
{
return null;
}

if (noRecursion)
{
if (inlinedParents.Contains(parseOk.Value))
if (inlinedParents.Contains(parseOk))
{
return null;
}
Expand All @@ -486,7 +512,7 @@ envConstraintId is null
return null;
},
parseOk.Value).expr;
parseOk).expr;

if (disableRecurseAfterInline)
{
Expand All @@ -507,7 +533,7 @@ envConstraintId is null
envConstraintId: envConstraintId);

{
if (300 < inlinedExprReduced.SubexpressionCount)
if (500 < inlinedExprReduced.SubexpressionCount)
{
return null;
}
Expand All @@ -521,7 +547,7 @@ envConstraintId is null
{
++conditionsCount;

if (3 < conditionsCount)
if (5 < conditionsCount)
{
return null;
}
Expand All @@ -533,7 +559,7 @@ envConstraintId is null
{
++invocationsCount;

if (4 < invocationsCount)
if (5 < invocationsCount)
{
return null;
}
Expand All @@ -545,18 +571,18 @@ envConstraintId is null

var inlinedFinal =
ReduceExpressionAndInlineRecursive(
rootExpression: rootExpression,
// currentExpression: inlinedExpr,
currentExpression: inlinedExprReduced,
inlinedParents: inlinedParents.Push(parseOk.Value),
inlinedParents: inlinedParents.Push(parseOk),
envConstraintId: envConstraintId,
maxDepth: maxDepth - 1,
maxSubexpressionCount: maxSubexpressionCount,
parseCache: parseCache,
skipInlining: skipInlining,
disableRecurseAfterInline: disableRecurseAfterInline);

{
if (300 < inlinedFinal.SubexpressionCount)
if (500 < inlinedFinal.SubexpressionCount)
{
return null;
}
Expand All @@ -570,7 +596,7 @@ envConstraintId is null
{
++conditionsCount;

if (3 < conditionsCount)
if (5 < conditionsCount)
{
return null;
}
Expand All @@ -582,7 +608,7 @@ envConstraintId is null
{
++invocationsCount;

if (4 < invocationsCount)
if (5 < invocationsCount)
{
return null;
}
Expand All @@ -597,10 +623,11 @@ envConstraintId is null

if (!parseAndEvalExpr.encoded.ReferencesEnvironment)
{
if (CompilePineToDotNet.ReducePineExpression.TryEvaluateExpressionIndependent(parseAndEvalExpr.encoded) is
Result<string, PineValue>.Ok evalExprOk)
if (CompilePineToDotNet.ReducePineExpression.TryEvaluateExpressionIndependent(
parseAndEvalExpr.encoded).IsOkOrNull() is
{ } evalExprOk)
{
return ContinueReduceForKnownExprValue(evalExprOk.Value);
return ContinueReduceForKnownExprValue(evalExprOk);
}
}

Expand Down

0 comments on commit 1489cae

Please sign in to comment.