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

Emit less nested target code #4683

Merged
merged 10 commits into from
Nov 1, 2023
Merged

Conversation

RustanLeino
Copy link
Collaborator

@RustanLeino RustanLeino commented Oct 19, 2023

Emit better target code for match-case expressions. In particular, avoid some deeply nested IIFE's that (were hard for a human to read and) caused the Java compiler to choke.

Fixes #3868

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@RustanLeino RustanLeino marked this pull request as ready for review October 19, 2023 01:39
@RustanLeino RustanLeino enabled auto-merge (squash) October 19, 2023 01:40
Copy link
Member

@robin-aws robin-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress, and I love how simple the actual change is. Are you correcting any cases of accidentally failing to provide the right values to recursive calls, as opposed to intentionally being conservative? If so is there anything we can do to the signatures to make that less likely in the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a case where we actually want to assert something about the structure of the code we're emitting. Otherwise we could regress and the test would still pass, but perhaps take 30 mins to pass, which we might not notice in CI. :)

You could add an additional LIT command that uses our support for %OutputCheck to apply a CHECK-NOT regex to the Java code we emit to say the nested structure doesn't appear.

@@ -2740,7 +2740,7 @@ void TrCasePatternOpt<VT>(CasePattern<VT> pat, Expression rhs, Action<ConcreteSy
}
}

void TrExprOpt(Expression expr, Type resultType, ConcreteSyntaxTree wr, IVariable/*?*/ accumulatorVar) {
void TrExprOpt(Expression expr, Type resultType, ConcreteSyntaxTree wr, bool inLetExprBody, [CanBeNull] IVariable accumulatorVar) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and TrCaseExpressionOpt deserve a short comment about what optimizations they are applying and when they are valid. AFAICT it's something like "parallel optimized implementations for when we are translating the return value of a function, and hence can do things like compile chains of LetExprs into sequences of statements"

Comment on lines +2751 to +2760
/// <summary>
/// This method compiles "expr" into a statement context of the target. This typically means that, for example, Dafny let-bound variables can
/// be compiled into local variables in the target code, and that Dafny if-then-else expressions can be compiled into if statements in the
/// target code.
/// In contrast, the "Expr(...)" method compiles its given expression into an expression context of the target. This can result in
/// more complicated constructions in target languages that don't support name bindings in expressions (like most of our target
/// languages) or that don't support if-then-else expressions (like Go).
/// Other than the syntactic differences in the target code, the idea is that "TrExprOpt(...)" and "Expr(...)" generate code with the
/// same semantics.
/// </summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@RustanLeino RustanLeino merged commit 451a9a5 into dafny-lang:master Nov 1, 2023
20 checks passed
@RustanLeino RustanLeino deleted the issue-3868 branch November 1, 2023 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested LetExpr chain compiles to tons of nested Java lambdas, which javac chokes on
2 participants