-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support tuple specialisation #15060
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
Support tuple specialisation #15060
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c87bd05
Formatting: Auto-show lists
dwijnand 20edebe
Add Show[ParamInfo]
dwijnand eeca5f1
Fix incorrect use of phase.prev, introduce prevMega
dwijnand ad0c52b
Implement Tuple specialisation
dwijnand e6042a5
Workaround TreeChecker & specialised tuples
dwijnand 49e9d67
Avoid crashing when compiling stdLib213
dwijnand 23e4361
Dedupe/refactor in SpecializeTuples
dwijnand 1d2c641
Add a "no-boxing" test for specialised tuples
dwijnand 434f7a7
Match the original apply method name in tuple specialisation
dwijnand 573a0c5
Expose, tweak & use isElideableExpr in tuple specialisation
dwijnand 599b8f3
Switch isSpecializableTuple to classSymbol
dwijnand fb11113
Fix defn.isSpecializedTuple/SpecialisedTuple with Context
dwijnand 02ff089
Fix bootstrapped SpecializeTuplesTests
dwijnand 1124199
Add back isSpecializableTuple exclusion
dwijnand 4c0ab7b
Optimise isSpecializedNameOf & misc docs/renames
dwijnand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
compiler/src/dotty/tools/dotc/transform/SpecializeTuples.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dotty.tools | ||
package dotc | ||
package transform | ||
|
||
import ast.Trees.*, ast.tpd, core.* | ||
import Contexts.*, Types.*, Decorators.*, Symbols.*, DenotTransformers.* | ||
import SymDenotations.*, Scopes.*, StdNames.*, NameOps.*, Names.* | ||
import MegaPhase.MiniPhase | ||
import typer.Inliner.isElideableExpr | ||
|
||
/** Specializes Tuples by replacing tuple construction and selection trees. | ||
* | ||
* Specifically: | ||
* 1. Replaces `(1, 1)` (which is `Tuple2.apply[Int, Int](1, 1)`) and | ||
* `new Tuple2[Int, Int](1, 1)` with `new Tuple2$mcII$sp(1, 1)`. | ||
* 2. Replaces `(_: Tuple2[Int, Int])._1` with `(_: Tuple2[Int, Int])._1$mcI$sp` | ||
*/ | ||
class SpecializeTuples extends MiniPhase: | ||
import tpd.* | ||
|
||
override def phaseName: String = SpecializeTuples.name | ||
override def description: String = SpecializeTuples.description | ||
override def isEnabled(using Context): Boolean = !ctx.settings.scalajs.value | ||
|
||
override def transformApply(tree: Apply)(using Context): Tree = tree match | ||
case Apply(TypeApply(fun: NameTree, targs), args) | ||
if fun.symbol.name == nme.apply && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)) | ||
&& isElideableExpr(tree) | ||
=> | ||
cpy.Apply(tree)(Select(New(defn.SpecialisedTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe) | ||
case Apply(TypeApply(fun: NameTree, targs), args) | ||
if fun.symbol.name == nme.CONSTRUCTOR && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner, targs.map(_.tpe)) | ||
&& isElideableExpr(tree) | ||
=> | ||
cpy.Apply(tree)(Select(New(defn.SpecialisedTuple(fun.symbol.owner, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe) | ||
case _ => tree | ||
end transformApply | ||
|
||
override def transformSelect(tree: Select)(using Context): Tree = tree match | ||
case Select(qual, nme._1) if isAppliedSpecializableTuple(qual.tpe.widen) => | ||
Select(qual, nme._1.specializedName(qual.tpe.widen.argInfos.slice(0, 1))) | ||
case Select(qual, nme._2) if isAppliedSpecializableTuple(qual.tpe.widen) => | ||
Select(qual, nme._2.specializedName(qual.tpe.widen.argInfos.slice(1, 2))) | ||
case _ => tree | ||
|
||
private def isAppliedSpecializableTuple(tp: Type)(using Context) = tp match | ||
case AppliedType(tycon, args) => defn.isSpecializableTuple(tycon.classSymbol, args) | ||
case _ => false | ||
end SpecializeTuples | ||
|
||
object SpecializeTuples: | ||
val name: String = "specializeTuples" | ||
val description: String = "replaces tuple construction and selection trees" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.