Skip to content

Commit 7e9dbfc

Browse files
authored
Merge pull request #5188 from gzm0/opt-long-cleanup
Optimizer: Fail if we cannot inline RuntimeLong
2 parents 289a5e1 + 3a253e3 commit 7e9dbfc

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

linker/shared/src/main/scala/org/scalajs/linker/frontend/optimizer/OptimizerCore.scala

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,36 +3504,27 @@ private[optimizer] abstract class OptimizerCore(
35043504
withBinding(rtLongBinding) { (scope1, cont1) =>
35053505
implicit val scope = scope1
35063506
val tRef = VarRef(tName)(rtLongClassType)
3507-
val newTree = New(LongImpl.RuntimeLongClass,
3508-
MethodIdent(LongImpl.initFromParts),
3509-
List(Apply(ApplyFlags.empty, tRef, MethodIdent(LongImpl.lo), Nil)(IntType),
3510-
Apply(ApplyFlags.empty, tRef, MethodIdent(LongImpl.hi), Nil)(IntType)))
3511-
pretransformExpr(newTree)(cont1)
3507+
3508+
val lo = Apply(ApplyFlags.empty, tRef, MethodIdent(LongImpl.lo), Nil)(IntType)
3509+
val hi = Apply(ApplyFlags.empty, tRef, MethodIdent(LongImpl.hi), Nil)(IntType)
3510+
3511+
pretransformExprs(lo, hi) { (tlo, thi) =>
3512+
inlineClassConstructor(AllocationSite.Anonymous, LongImpl.RuntimeLongClass,
3513+
inlinedRTLongStructure, MethodIdent(LongImpl.initFromParts), List(tlo, thi),
3514+
() => throw new AssertionError(s"rolled-back RuntimeLong inlining at $pos"))(cont1)
3515+
}
35123516
} (cont)
35133517
}
35143518

35153519
private def expandLongOps(pretrans: PreTransform)(cont: PreTransCont)(
35163520
implicit scope: Scope): TailRec[Tree] = {
35173521
implicit val pos = pretrans.pos
35183522

3519-
// unfortunately nullable for the result types of methods
3520-
def rtLongClassType = ClassType(LongImpl.RuntimeLongClass, nullable = true)
3521-
3522-
def expandUnaryOp(methodName: MethodName, arg: PreTransform,
3523-
resultType: Type = rtLongClassType): TailRec[Tree] = {
3524-
pretransformApplyStatic(ApplyFlags.empty, LongImpl.RuntimeLongClass,
3525-
MethodIdent(methodName), arg :: Nil, resultType,
3526-
isStat = false, usePreTransform = true)(
3527-
cont)
3528-
}
3529-
3530-
def expandBinaryOp(methodName: MethodName, lhs: PreTransform,
3531-
rhs: PreTransform,
3532-
resultType: Type = rtLongClassType): TailRec[Tree] = {
3533-
pretransformApplyStatic(ApplyFlags.empty, LongImpl.RuntimeLongClass,
3534-
MethodIdent(methodName), lhs :: rhs :: Nil, resultType,
3535-
isStat = false, usePreTransform = true)(
3536-
cont)
3523+
def expand(methodName: MethodName, targs: PreTransform*): TailRec[Tree] = {
3524+
val impl = staticCall(LongImpl.RuntimeLongClass, MemberNamespace.PublicStatic, methodName)
3525+
pretransformSingleDispatch(ApplyFlags.empty, impl, None, targs.toList,
3526+
isStat = false, usePreTransform = true)(cont)(
3527+
throw new AssertionError(s"failed to inline RuntimeLong method $methodName at $pos"))
35373528
}
35383529

35393530
pretrans match {
@@ -3542,30 +3533,30 @@ private[optimizer] abstract class OptimizerCore(
35423533

35433534
(op: @switch) match {
35443535
case IntToLong =>
3545-
expandUnaryOp(LongImpl.fromInt, arg)
3536+
expand(LongImpl.fromInt, arg)
35463537

35473538
case LongToInt =>
3548-
expandUnaryOp(LongImpl.toInt, arg, IntType)
3539+
expand(LongImpl.toInt, arg)
35493540

35503541
case LongToDouble =>
3551-
expandUnaryOp(LongImpl.toDouble, arg, DoubleType)
3542+
expand(LongImpl.toDouble, arg)
35523543

35533544
case DoubleToLong =>
3554-
expandUnaryOp(LongImpl.fromDouble, arg)
3545+
expand(LongImpl.fromDouble, arg)
35553546

35563547
case LongToFloat =>
3557-
expandUnaryOp(LongImpl.toFloat, arg, FloatType)
3548+
expand(LongImpl.toFloat, arg)
35583549

35593550
case Double_toBits if config.coreSpec.esFeatures.esVersion >= ESVersion.ES2015 =>
3560-
expandBinaryOp(LongImpl.fromDoubleBits,
3551+
expand(LongImpl.fromDoubleBits,
35613552
arg, PreTransTree(Transient(GetFPBitsDataView)))
35623553

35633554
case Double_fromBits if config.coreSpec.esFeatures.esVersion >= ESVersion.ES2015 =>
3564-
expandBinaryOp(LongImpl.bitsToDouble,
3555+
expand(LongImpl.bitsToDouble,
35653556
arg, PreTransTree(Transient(GetFPBitsDataView)))
35663557

35673558
case Long_clz =>
3568-
expandUnaryOp(LongImpl.clz, arg, IntType)
3559+
expand(LongImpl.clz, arg)
35693560

35703561
case _ =>
35713562
cont(pretrans)
@@ -3575,37 +3566,37 @@ private[optimizer] abstract class OptimizerCore(
35753566
import BinaryOp._
35763567

35773568
(op: @switch) match {
3578-
case Long_+ => expandBinaryOp(LongImpl.add, lhs, rhs)
3569+
case Long_+ => expand(LongImpl.add, lhs, rhs)
35793570

35803571
case Long_- =>
35813572
lhs match {
35823573
case PreTransLit(LongLiteral(0L)) =>
3583-
expandUnaryOp(LongImpl.neg, rhs)
3574+
expand(LongImpl.neg, rhs)
35843575
case _ =>
3585-
expandBinaryOp(LongImpl.sub, lhs, rhs)
3576+
expand(LongImpl.sub, lhs, rhs)
35863577
}
35873578

3588-
case Long_* => expandBinaryOp(LongImpl.mul, lhs, rhs)
3589-
case Long_/ => expandBinaryOp(LongImpl.divide, lhs, rhs)
3590-
case Long_% => expandBinaryOp(LongImpl.remainder, lhs, rhs)
3579+
case Long_* => expand(LongImpl.mul, lhs, rhs)
3580+
case Long_/ => expand(LongImpl.divide, lhs, rhs)
3581+
case Long_% => expand(LongImpl.remainder, lhs, rhs)
35913582

3592-
case Long_& => expandBinaryOp(LongImpl.and, lhs, rhs)
3593-
case Long_| => expandBinaryOp(LongImpl.or, lhs, rhs)
3594-
case Long_^ => expandBinaryOp(LongImpl.xor, lhs, rhs)
3583+
case Long_& => expand(LongImpl.and, lhs, rhs)
3584+
case Long_| => expand(LongImpl.or, lhs, rhs)
3585+
case Long_^ => expand(LongImpl.xor, lhs, rhs)
35953586

3596-
case Long_<< => expandBinaryOp(LongImpl.shl, lhs, rhs)
3597-
case Long_>>> => expandBinaryOp(LongImpl.shr, lhs, rhs)
3598-
case Long_>> => expandBinaryOp(LongImpl.sar, lhs, rhs)
3587+
case Long_<< => expand(LongImpl.shl, lhs, rhs)
3588+
case Long_>>> => expand(LongImpl.shr, lhs, rhs)
3589+
case Long_>> => expand(LongImpl.sar, lhs, rhs)
35993590

3600-
case Long_== => expandBinaryOp(LongImpl.equals_, lhs, rhs)
3601-
case Long_!= => expandBinaryOp(LongImpl.notEquals, lhs, rhs)
3602-
case Long_< => expandBinaryOp(LongImpl.lt, lhs, rhs)
3603-
case Long_<= => expandBinaryOp(LongImpl.le, lhs, rhs)
3604-
case Long_> => expandBinaryOp(LongImpl.gt, lhs, rhs)
3605-
case Long_>= => expandBinaryOp(LongImpl.ge, lhs, rhs)
3591+
case Long_== => expand(LongImpl.equals_, lhs, rhs)
3592+
case Long_!= => expand(LongImpl.notEquals, lhs, rhs)
3593+
case Long_< => expand(LongImpl.lt, lhs, rhs)
3594+
case Long_<= => expand(LongImpl.le, lhs, rhs)
3595+
case Long_> => expand(LongImpl.gt, lhs, rhs)
3596+
case Long_>= => expand(LongImpl.ge, lhs, rhs)
36063597

3607-
case Long_unsigned_/ => expandBinaryOp(LongImpl.divideUnsigned, lhs, rhs)
3608-
case Long_unsigned_% => expandBinaryOp(LongImpl.remainderUnsigned, lhs, rhs)
3598+
case Long_unsigned_/ => expand(LongImpl.divideUnsigned, lhs, rhs)
3599+
case Long_unsigned_% => expand(LongImpl.remainderUnsigned, lhs, rhs)
36093600

36103601
case _ =>
36113602
cont(pretrans)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy