summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2015-02-23 20:43:21 +0100
committerLukas Rytz <lukas.rytz@gmail.com>2015-03-11 12:53:28 -0700
commit42054a1bebcc2155f773787ffda781b497d4178b (patch)
treec5cf4f830c1d27cba397e5060a9bd29cc08ad9b4 /src
parent3a32ae3651f69237bde32598674bc135ad9e4064 (diff)
downloadscala-42054a1bebcc2155f773787ffda781b497d4178b.tar.gz
scala-42054a1bebcc2155f773787ffda781b497d4178b.tar.bz2
scala-42054a1bebcc2155f773787ffda781b497d4178b.zip
Emit the ScalaInlineInfo attribute under GenASM
The goal of this commit is to allow the new inliner (in GenBCode, coming soon) to inline methods generated by the GenASM backend of 2.11.6. The ScalaInlineInfo attribute is added to every classfile generated by GenASM. It contains metadata about the class and its methods that will be used by the new inliner. Storing this metadata to the classfile prevents the need to look up a class symbol for a certain class file name, a process that is known to be brittle due to name mangling. Also, some symbols are not exactly the same when originating in a class being compiled or an unpickled one. For example, method symbols for mixed-in members are only added to classes being compiled. The classfile attribute is relatively small, because all strings it references (class internal names, method names, method descriptors) would exist anyway in the constant pool. It just adds a few references and bits for each method in the classfile. Jar sizes before: 480142 scala-actors.jar 15531408 scala-compiler.jar 5543249 scala-library.jar 4663078 scala-reflect.jar 785953 scalap.jar After: 490491 scala-actors.jar (102.1%) 15865500 scala-compiler.jar (102.1%) 5722504 scala-library.jar (103.2%) 4788370 scala-reflect.jar (102.7%) 805890 scalap.jar (102.5%)
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala5
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala97
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala48
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala4
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala147
-rw-r--r--src/compiler/scala/tools/nsc/settings/ScalaSettings.scala2
-rw-r--r--src/compiler/scala/tools/nsc/transform/AddInterfaces.scala2
7 files changed, 301 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
index 75aa0fc984..d3f09217cd 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala
@@ -8,8 +8,9 @@ package scala.tools.nsc.backend.jvm
import scala.tools.asm.tree.{InsnList, AbstractInsnNode, ClassNode, MethodNode}
import java.io.{StringWriter, PrintWriter}
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}
-import scala.tools.asm.ClassReader
+import scala.tools.asm.{Attribute, ClassReader}
import scala.collection.convert.decorateAsScala._
+import scala.tools.nsc.backend.jvm.opt.InlineInfoAttributePrototype
object AsmUtils {
@@ -49,7 +50,7 @@ object AsmUtils {
def readClass(bytes: Array[Byte]): ClassNode = {
val node = new ClassNode()
- new ClassReader(bytes).accept(node, 0)
+ new ClassReader(bytes).accept(node, Array[Attribute](InlineInfoAttributePrototype), 0)
node
}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala
index 27827015c3..2ebf338f5e 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala
@@ -3,9 +3,11 @@
* @author Martin Odersky
*/
-package scala.tools.nsc.backend.jvm
+package scala.tools.nsc
+package backend.jvm
import scala.tools.nsc.Global
+import scala.tools.nsc.backend.jvm.BTypes.{MethodInlineInfo, InlineInfo, InternalName}
/**
* This trait contains code shared between GenBCode and GenASM that depends on types defined in
@@ -300,4 +302,97 @@ final class BCodeAsmCommon[G <: Global](val global: G) {
}
interfaces.map(_.typeSymbol)
}
+
+ /**
+ * This is a hack to work around SI-9111. The completer of `methodSym` may report type errors. We
+ * cannot change the typer context of the completer at this point and make it silent: the context
+ * captured when creating the completer in the namer. However, we can temporarily replace
+ * global.reporter (it's a var) to store errors.
+ */
+ def completeSilentlyAndCheckErroneous(sym: Symbol): Boolean = {
+ if (sym.hasCompleteInfo) false
+ else {
+ val originalReporter = global.reporter
+ val storeReporter = new reporters.StoreReporter()
+ global.reporter = storeReporter
+ try {
+ sym.info
+ } finally {
+ global.reporter = originalReporter
+ }
+ sym.isErroneous
+ }
+ }
+
+ /**
+ * Build the [[InlineInfo]] for a class symbol.
+ */
+ def buildInlineInfoFromClassSymbol(classSym: Symbol, classSymToInternalName: Symbol => InternalName, methodSymToDescriptor: Symbol => String): InlineInfo = {
+ val selfType = {
+ // The mixin phase uses typeOfThis for the self parameter in implementation class methods.
+ val selfSym = classSym.typeOfThis.typeSymbol
+ if (selfSym != classSym) Some(classSymToInternalName(selfSym)) else None
+ }
+
+ val isEffectivelyFinal = classSym.isEffectivelyFinal
+
+ var warning = Option.empty[String]
+
+ // Primitive methods cannot be inlined, so there's no point in building a MethodInlineInfo. Also, some
+ // primitive methods (e.g., `isInstanceOf`) have non-erased types, which confuses [[typeToBType]].
+ val methodInlineInfos = classSym.info.decls.iterator.filter(m => m.isMethod && !scalaPrimitives.isPrimitive(m)).flatMap({
+ case methodSym =>
+ if (completeSilentlyAndCheckErroneous(methodSym)) {
+ // Happens due to SI-9111. Just don't provide any MethodInlineInfo for that method, we don't need fail the compiler.
+ if (!classSym.isJavaDefined) devWarning("SI-9111 should only be possible for Java classes")
+ warning = Some(s"Failed to get the type of a method of class symbol ${classSym.fullName} due to SI-9111")
+ None
+ } else {
+ val name = methodSym.javaSimpleName.toString // same as in genDefDef
+ val signature = name + methodSymToDescriptor(methodSym)
+
+ // Some detours are required here because of changing flags (lateDEFERRED, lateMODULE):
+ // 1. Why the phase travel? Concrete trait methods obtain the lateDEFERRED flag in Mixin.
+ // This makes isEffectivelyFinalOrNotOverridden false, which would prevent non-final
+ // but non-overridden methods of sealed traits from being inlined.
+ // 2. Why the special case for `classSym.isImplClass`? Impl class symbols obtain the
+ // lateMODULE flag during Mixin. During the phase travel to exitingPickler, the late
+ // flag is ignored. The members are therefore not isEffectivelyFinal (their owner
+ // is not a module). Since we know that all impl class members are static, we can
+ // just take the shortcut.
+ val effectivelyFinal = classSym.isImplClass || exitingPickler(methodSym.isEffectivelyFinalOrNotOverridden)
+
+ // Identify trait interface methods that have a static implementation in the implementation
+ // class. Invocations of these methods can be re-wrired directly to the static implementation
+ // if they are final or the receiver is known.
+ //
+ // Using `erasure.needsImplMethod` is not enough: it keeps field accessors, module getters
+ // and super accessors. When AddInterfaces creates the impl class, these methods are
+ // initially added to it.
+ //
+ // The mixin phase later on filters out most of these members from the impl class (see
+ // Mixin.isImplementedStatically). However, accessors for concrete lazy vals remain in the
+ // impl class after mixin. So the filter in mixin is not exactly what we need here (we
+ // want to identify concrete trait methods, not any accessors). So we check some symbol
+ // properties manually.
+ val traitMethodWithStaticImplementation = {
+ import symtab.Flags._
+ classSym.isTrait && !classSym.isImplClass &&
+ erasure.needsImplMethod(methodSym) &&
+ !methodSym.isModule &&
+ !(methodSym hasFlag (ACCESSOR | SUPERACCESSOR))
+ }
+
+ val info = MethodInlineInfo(
+ effectivelyFinal = effectivelyFinal,
+ traitMethodWithStaticImplementation = traitMethodWithStaticImplementation,
+ annotatedInline = methodSym.hasAnnotation(ScalaInlineClass),
+ annotatedNoInline = methodSym.hasAnnotation(ScalaNoInlineClass)
+ )
+ Some((signature, info))
+ }
+ }).toMap
+
+ InlineInfo(selfType, isEffectivelyFinal, methodInlineInfos, warning)
+ }
}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
index a194fe8fe4..ec26ef9b48 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
@@ -936,4 +936,52 @@ object BTypes {
* But that would create overhead in a Collection[InternalName].
*/
type InternalName = String
+
+ /**
+ * Metadata about a ClassBType, used by the inliner.
+ *
+ * More information may be added in the future to enable more elaborate inlinine heuristics.
+ *
+ * @param traitImplClassSelfType `Some(tp)` if this InlineInfo describes a trait, and the `self`
+ * parameter type of the methods in the implementation class is not
+ * the trait itself. Example:
+ * trait T { self: U => def f = 1 }
+ * Generates something like:
+ * class T$class { static def f(self: U) = 1 }
+ *
+ * In order to inline a trat method call, the INVOKEINTERFACE is
+ * rewritten to an INVOKESTATIC of the impl class, so we need the
+ * self type (U) to get the right signature.
+ *
+ * `None` if the self type is the interface type, or if this
+ * InlineInfo does not describe a trait.
+ *
+ * @param isEffectivelyFinal True if the class cannot have subclasses: final classes, module
+ * classes, trait impl classes.
+ *
+ * @param methodInfos The [[MethodInlineInfo]]s for the methods declared in this class.
+ * The map is indexed by the string s"$name$descriptor" (to
+ * disambiguate overloads).
+ */
+ final case class InlineInfo(traitImplClassSelfType: Option[InternalName],
+ isEffectivelyFinal: Boolean,
+ methodInfos: Map[String, MethodInlineInfo],
+ warning: Option[String])
+
+ val EmptyInlineInfo = InlineInfo(None, false, Map.empty, None)
+
+ /**
+ * Metadata about a method, used by the inliner.
+ *
+ * @param effectivelyFinal True if the method cannot be overridden (in Scala)
+ * @param traitMethodWithStaticImplementation True if the method is an interface method method of
+ * a trait method and has a static counterpart in the
+ * implementation class.
+ * @param annotatedInline True if the method is annotated `@inline`
+ * @param annotatedNoInline True if the method is annotated `@noinline`
+ */
+ final case class MethodInlineInfo(effectivelyFinal: Boolean,
+ traitMethodWithStaticImplementation: Boolean,
+ annotatedInline: Boolean,
+ annotatedNoInline: Boolean)
} \ No newline at end of file
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
index 707336e5de..62268e1907 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
@@ -9,6 +9,7 @@ package backend.jvm
import scala.collection.{ mutable, immutable }
import scala.reflect.internal.pickling.{ PickleFormat, PickleBuffer }
+import scala.tools.nsc.backend.jvm.opt.InlineInfoAttribute
import scala.tools.nsc.symtab._
import scala.tools.asm
import asm.Label
@@ -1292,6 +1293,9 @@ abstract class GenASM extends SubComponent with BytecodeWriters { self =>
jclass.visitAttribute(if(ssa.isDefined) pickleMarkerLocal else pickleMarkerForeign)
emitAnnotations(jclass, c.symbol.annotations ++ ssa)
+ if (!settings.YskipInlineInfoAttribute.value)
+ jclass.visitAttribute(InlineInfoAttribute(buildInlineInfoFromClassSymbol(c.symbol, javaName, javaType(_).getDescriptor)))
+
// typestate: entering mode with valid call sequences:
// ( visitInnerClass | visitField | visitMethod )* visitEnd
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala
new file mode 100644
index 0000000000..4812f2290f
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala
@@ -0,0 +1,147 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2014 LAMP/EPFL
+ * @author Martin Odersky
+ */
+
+package scala.tools.nsc
+package backend.jvm
+package opt
+
+import scala.tools.asm._
+import scala.tools.nsc.backend.jvm.BTypes.{InlineInfo, MethodInlineInfo}
+
+/**
+ * This attribute stores the InlineInfo for a ClassBType as an independent classfile attribute.
+ * The compiler does so for every class being compiled.
+ *
+ * The reason is that a precise InlineInfo can only be obtained if the symbol for a class is available.
+ * For example, we need to know if a method is final in Scala's terms, or if it has the @inline annotation.
+ * Looking up a class symbol for a given class filename is brittle (name-mangling).
+ *
+ * The attribute is also helpful for inlining mixin methods. The mixin phase only adds mixin method
+ * symbols to classes that are being compiled. For all other class symbols, there are no mixin members.
+ * However, the inliner requires an InlineInfo for inlining mixin members. That problem is solved by
+ * reading the InlineInfo from this attribute.
+ *
+ * In principle we could encode the InlineInfo into a Java annotation (instead of a classfile attribute).
+ * However, an attribute allows us to save many bits. In particular, note that the strings in an
+ * InlineInfo are serialized as references to constants in the constant pool, and those strings
+ * (traitImplClassSelfType, method names, method signatures) would exist in there anyway. So the
+ * ScalaInlineAttribute remains relatively compact.
+ */
+case class InlineInfoAttribute(inlineInfo: InlineInfo) extends Attribute(InlineInfoAttribute.attributeName) {
+ /**
+ * Not sure what this method is good for, it is not invoked anywhere in the ASM framework. However,
+ * the example in the ASM manual also overrides it to `false` for custom attributes, so it might be
+ * a good idea.
+ */
+ override def isUnknown: Boolean = false
+
+ /**
+ * Serialize the `inlineInfo` into a byte array. Strings are added to the constant pool and serialized
+ * as references.
+ */
+ override def write(cw: ClassWriter, code: Array[Byte], len: Int, maxStack: Int, maxLocals: Int): ByteVector = {
+ val result = new ByteVector()
+
+ result.putByte(InlineInfoAttribute.VERSION)
+
+ var hasSelfIsFinal = 0
+ if (inlineInfo.isEffectivelyFinal) hasSelfIsFinal |= 1
+ if (inlineInfo.traitImplClassSelfType.isDefined) hasSelfIsFinal |= 2
+ result.putByte(hasSelfIsFinal)
+
+ for (selfInternalName <- inlineInfo.traitImplClassSelfType) {
+ result.putShort(cw.newUTF8(selfInternalName))
+ }
+
+ // The method count fits in a short (the methods_count in a classfile is also a short)
+ result.putShort(inlineInfo.methodInfos.size)
+
+ // Sort the methodInfos for stability of classfiles
+ for ((nameAndType, info) <- inlineInfo.methodInfos.toList.sortBy(_._1)) {
+ val (name, desc) = nameAndType.span(_ != '(')
+ // Name and desc are added separately because a NameAndType entry also stores them separately.
+ // This makes sure that we use the existing constant pool entries for the method.
+ result.putShort(cw.newUTF8(name))
+ result.putShort(cw.newUTF8(desc))
+
+ var inlineInfo = 0
+ if (info.effectivelyFinal) inlineInfo |= 1
+ if (info.traitMethodWithStaticImplementation) inlineInfo |= 2
+ if (info.annotatedInline) inlineInfo |= 4
+ if (info.annotatedNoInline) inlineInfo |= 8
+ result.putByte(inlineInfo)
+ }
+
+ result
+ }
+
+ /**
+ * De-serialize the attribute into an InlineInfo. The attribute starts at cr.b(off), but we don't
+ * need to access that array directly, we can use the `read` methods provided by the ClassReader.
+ *
+ * `buf` is a pre-allocated character array that is guaranteed to be long enough to hold any
+ * string of the constant pool. So we can use it to invoke `cr.readUTF8`.
+ */
+ override def read(cr: ClassReader, off: Int, len: Int, buf: Array[Char], codeOff: Int, labels: Array[Label]): InlineInfoAttribute = {
+ var next = off
+
+ def nextByte() = { val r = cr.readByte(next) ; next += 1; r }
+ def nextUTF8() = { val r = cr.readUTF8(next, buf); next += 2; r }
+ def nextShort() = { val r = cr.readShort(next) ; next += 2; r }
+
+ val version = nextByte()
+ if (version == 1) {
+ val hasSelfIsFinal = nextByte()
+ val isFinal = (hasSelfIsFinal & 1) != 0
+ val hasSelf = (hasSelfIsFinal & 2) != 0
+
+ val self = if (hasSelf) {
+ val selfName = nextUTF8()
+ Some(selfName)
+ } else {
+ None
+ }
+
+ val numEntries = nextShort()
+ val infos = (0 until numEntries).map(_ => {
+ val name = nextUTF8()
+ val desc = nextUTF8()
+
+ val inlineInfo = nextByte()
+ val isFinal = (inlineInfo & 1) != 0
+ val traitMethodWithStaticImplementation = (inlineInfo & 2) != 0
+ val isInline = (inlineInfo & 4) != 0
+ val isNoInline = (inlineInfo & 8) != 0
+ (name + desc, MethodInlineInfo(isFinal, traitMethodWithStaticImplementation, isInline, isNoInline))
+ }).toMap
+
+ InlineInfoAttribute(InlineInfo(self, isFinal, infos, None))
+ } else {
+ val msg = s"Cannot read ScalaInlineInfo version $version in classfile ${cr.getClassName}. Use a more recent compiler."
+ InlineInfoAttribute(BTypes.EmptyInlineInfo.copy(warning = Some(msg)))
+ }
+ }
+}
+
+object InlineInfoAttribute {
+ /**
+ * [u1] version
+ * [u1] isEffectivelyFinal (<< 0), hasTraitImplClassSelfType (<< 1)
+ * [u2]? traitImplClassSelfType (reference)
+ * [u2] numMethodEntries
+ * [u2] name (reference)
+ * [u2] descriptor (reference)
+ * [u1] isFinal (<< 0), traitMethodWithStaticImplementation (<< 1), hasInlineAnnotation (<< 2), hasNoInlineAnnotation (<< 3)
+ */
+ final val VERSION: Byte = 1
+
+ final val attributeName = "ScalaInlineInfo"
+}
+
+/**
+ * In order to instruct the ASM framework to de-serialize the ScalaInlineInfo attribute, we need
+ * to pass a prototype instance when running the class reader.
+ */
+object InlineInfoAttributePrototype extends InlineInfoAttribute(InlineInfo(null, false, null, null))
diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala
index a5b722612d..d7f4cca615 100644
--- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala
+++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala
@@ -213,6 +213,8 @@ trait ScalaSettings extends AbsScalaSettings
// the current standard is "inline" but we are moving towards "method"
val Ydelambdafy = ChoiceSetting ("-Ydelambdafy", "strategy", "Strategy used for translating lambdas into JVM code.", List("inline", "method"), "inline")
+ val YskipInlineInfoAttribute = BooleanSetting("-Yskip-inline-info-attribute", "Do not add the ScalaInlineInfo attribute to classfiles generated by -Ybackend:GenASM")
+
object YoptChoices extends MultiChoiceEnumeration {
val unreachableCode = Choice("unreachable-code", "Eliminate unreachable code, exception handlers protecting no instructions, debug information of eliminated variables.")
val simplifyJumps = Choice("simplify-jumps", "Simplify branching instructions, eliminate unnecessary ones.")
diff --git a/src/compiler/scala/tools/nsc/transform/AddInterfaces.scala b/src/compiler/scala/tools/nsc/transform/AddInterfaces.scala
index f786ffb8f3..3591372bbe 100644
--- a/src/compiler/scala/tools/nsc/transform/AddInterfaces.scala
+++ b/src/compiler/scala/tools/nsc/transform/AddInterfaces.scala
@@ -55,7 +55,7 @@ abstract class AddInterfaces extends InfoTransform { self: Erasure =>
)
/** Does symbol need an implementation method? */
- private def needsImplMethod(sym: Symbol) = (
+ def needsImplMethod(sym: Symbol) = (
sym.isMethod
&& isInterfaceMember(sym)
&& (!sym.hasFlag(DEFERRED | SUPERACCESSOR) || (sym hasFlag lateDEFERRED))