Skip to content

Allow Return arg to be a void if the target Labeled is a void. #5074

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

Merged
merged 1 commit into from
Nov 17, 2024

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Nov 13, 2024

Previously, even if the target label of a Return declared a void result type, the argument to the Return had to be an expression, with a non-void type. Because of that rule, we generated fake Undefined() arguments to the Returns.

In this commit, we remove that restriction. This simplifies the IR specification and removes some ad hoc code paths to generate the fake Undefined(). We do have to add some handling in the JS function emitter. The Wasm function emitter requires no change.

The changes in doReturnToLabel() are necessary to maintain the status quo in terms of the emitted JS. My understanding is that before, we would get a lot of Block(stats, Return(Undefined)) which are now Return(Block(stats)). If the stats contain Ifs and other branches, the new Return can be pushed down a lot more. When it wasn't pushed down, we would have regular fall-through behavior without js.Breaks. Now with the Returns pushed down, we get a lot of js.Breaks that are not actually necessary. The new optimization in doReturnToLabel() removes them.

@sjrd sjrd requested a review from gzm0 November 13, 2024 14:45
Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

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

Only questions :P

case Lhs.ReturnFromFunction =>
js.Block(
transformStat(rhs, tailPosLabels = Set.empty),
js.Return(js.Undefined()))
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels odd that we do not have tail label optimization here, but IIUC that's on par with the status quo.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't have tail label optimization here. Indeed, there is control-flow-disrupting code after the transformed rhs, namely this js.Return(js.Undefined()). The labels that are in tail position after the js.Return(js.Undefined()) are definitely not in tail position of the rhs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry, I wasn't clear. I understand why tailPosLabels = Set.empty is necessary.

What I didn't get is why there is no attempt to eliminate the return we are emitting itself here.

But I think now I understand: We only get here if an actual label is transformed to an Lhs.ReturnFromFunction. If the function we are translating has void return type (and no top-level label), we invoke transformStat immediately.

Maybe that warrants a comment :P

@sjrd sjrd force-pushed the ir-allow-return-void branch from 037e161 to 5a9dd4e Compare November 17, 2024 11:30
@sjrd
Copy link
Member Author

sjrd commented Nov 17, 2024

Updated with the requested comment + commit message explanation.

@sjrd sjrd requested a review from gzm0 November 17, 2024 11:31
Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

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

Just a suggested clarification of a comment.

/* If we get here, it is because desugarToFunctionInternal()
* found a top-level Labeled and eliminated it. Therefore, unless
* we're mistaken, by construction we cannot be in tail position
* of the whole function. That means there is no point trying to
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* of the whole function. That means there is no point trying to
* of the whole function (otherwise doReturnToLabel would have
* eliminated the lhs). That means there is no point trying to

Previously, even if the target label of a `Return` declared a
`void` result type, the argument to the `Return` had to be an
expression, with a non-`void` type. Because of that rule, we
generated fake `Undefined()` arguments to the `Return`s.

In this commit, we remove that restriction. This simplifies the IR
specification and removes some ad hoc code paths to generate the
fake `Undefined()`. We do have to add some handling in the JS
function emitter. The Wasm function emitter requires no change.

The changes in `doReturnToLabel()` are necessary to maintain the
status quo in terms of the emitted JS. My understanding is that
before, we would get a lot of `Block(stats, Return(Undefined))`
which are now `Return(Block(stats))`. If the `stats` contain `If`s
and other branches, the new `Return` can be pushed down a lot more.
When it wasn't pushed down, we would have regular fall-through
behavior without `js.Break`s. Now with the `Return`s pushed down,
we get a lot of `js.Break`s that are not actually necessary. The
new optimization in `doReturnToLabel()` removes them.
@sjrd sjrd force-pushed the ir-allow-return-void branch from 5a9dd4e to bb4f6da Compare November 17, 2024 12:15
@sjrd sjrd merged commit ab619d7 into scala-js:main Nov 17, 2024
3 checks passed
@sjrd sjrd deleted the ir-allow-return-void branch November 17, 2024 17:02
sjrd added a commit to dotty-staging/dotty that referenced this pull request Jan 17, 2025
And port the changes that were made to the compiler backend.
Most nobably, the changes from the following upstream PRs:

* Introduce non-nullable reference types in the IR.
  scala-js/scala-js#5018
* Opt: Remove useless *Ident indirections in the IR model.
  scala-js/scala-js#5092
* Merge the IR node This into VarRef, with a magic LocalName.
  scala-js/scala-js#5090
* Merge several IR nodes into UnaryOp.
  scala-js/scala-js#5088
* Restrict usages of StoreModule even further.
  scala-js/scala-js#5059
* Allow Return arg to be a void if the target Labeled is a void.
  scala-js/scala-js#5074
* Rename NoType to VoidType and print it as "void".
  scala-js/scala-js#5061
sjrd added a commit to dotty-staging/dotty that referenced this pull request Jan 17, 2025
And port the changes that were made to the compiler backend.
Most notably, the changes from the following upstream PRs:

* Introduce non-nullable reference types in the IR.
  scala-js/scala-js#5018
* Opt: Remove useless *Ident indirections in the IR model.
  scala-js/scala-js#5092
* Merge the IR node This into VarRef, with a magic LocalName.
  scala-js/scala-js#5090
* Merge several IR nodes into UnaryOp.
  scala-js/scala-js#5088
* Restrict usages of StoreModule even further.
  scala-js/scala-js#5059
* Allow Return arg to be a void if the target Labeled is a void.
  scala-js/scala-js#5074
* Rename NoType to VoidType and print it as "void".
  scala-js/scala-js#5061
sjrd added a commit to scala/scala3 that referenced this pull request Jan 22, 2025
And port the changes that were made to the compiler backend.
Most notably, the changes from the following upstream PRs:

* scala-js/scala-js#5018
* scala-js/scala-js#5092
* scala-js/scala-js#5090
* scala-js/scala-js#5088
* scala-js/scala-js#5059
* scala-js/scala-js#5074
* scala-js/scala-js#5061
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.

2 participants