Skip to content
This repository
Browse code

Add option to disable optimization

By default we run par test under -optimise. But occasionally we need
to test optimizations in isolation. This commit adds a Ynooptimise
flag that turns the optimize flags off back off after they've been
turned on.

A test is included to ensure that -Ynooptimise turns off optimizations
and an existing test is modified to show that optimizations coming
after -Ynooptimise in command line are enabled.
  • Loading branch information...
commit 3f0224c4de5b04f33e3de523c03d418b818af879 1 parent 32f04db
James Iry authored March 05, 2013
2  src/compiler/scala/tools/nsc/settings/ScalaSettings.scala
@@ -43,6 +43,7 @@ trait ScalaSettings extends AbsScalaSettings
43 43
   /** Internal use - syntax enhancements. */
44 44
   private class EnableSettings[T <: BooleanSetting](val s: T) {
45 45
     def enabling(toEnable: List[BooleanSetting]): s.type = s withPostSetHook (_ => toEnable foreach (_.value = s.value))
  46
+    def disabling(toDisable: List[BooleanSetting]): s.type = s withPostSetHook (_ => toDisable foreach (_.value = !s.value))
46 47
     def andThen(f: s.T => Unit): s.type                  = s withPostSetHook (setting => f(setting.value))
47 48
   }
48 49
   private implicit def installEnableSettings[T <: BooleanSetting](s: T) = new EnableSettings(s)
@@ -194,6 +195,7 @@ trait ScalaSettings extends AbsScalaSettings
194 195
    */
195 196
   val future        = BooleanSetting("-Xfuture", "Turn on future language features.") enabling futureSettings
196 197
   val optimise      = BooleanSetting("-optimise", "Generates faster bytecode by applying optimisations to the program") withAbbreviation "-optimize" enabling optimiseSettings
  198
+  val nooptimise    = BooleanSetting("-Ynooptimise", "Clears all the flags set by -optimise. Useful for testing optimizations in isolation.") withAbbreviation "-Ynooptimize" disabling optimise::optimiseSettings
197 199
   val Xexperimental = BooleanSetting("-Xexperimental", "Enable experimental extensions.") enabling experimentalSettings
198 200
 
199 201
   // Feature extensions
1  test/files/jvm/nooptimise/Foo_1.flags
... ...
@@ -0,0 +1 @@
  1
+-optimise -Ynooptimise
8  test/files/jvm/nooptimise/Foo_1.scala
... ...
@@ -0,0 +1,8 @@
  1
+class Foo_1 {
  2
+  def foo() {
  3
+    // optimization will remove this magic 3 from appearing in the source
  4
+    // so -Ynooptimize should prevent that
  5
+    val x = 3
  6
+     
  7
+  }
  8
+}
23  test/files/jvm/nooptimise/Test.scala
... ...
@@ -0,0 +1,23 @@
  1
+import scala.tools.partest.BytecodeTest
  2
+import scala.tools.asm
  3
+import asm.tree.InsnList
  4
+import scala.collection.JavaConverters._
  5
+
  6
+object Test extends BytecodeTest {
  7
+  def show: Unit = {
  8
+    val classNode = loadClassNode("Foo_1")
  9
+    val methodNode = getMethod(classNode, "foo")
  10
+    // if optimization didn't run then
  11
+    // there should be some useless instructions 
  12
+    // with the magic constant 3
  13
+    val expected = 1
  14
+    val got = countMagicThrees(methodNode.instructions)
  15
+    assert(got == expected, s"expected $expected but got $got magic threes")
  16
+  }
  17
+
  18
+  def countMagicThrees(insnList: InsnList): Int = {
  19
+    def isMagicThree(node: asm.tree.AbstractInsnNode): Boolean =
  20
+      (node.getOpcode == asm.Opcodes.ICONST_3)
  21
+    insnList.iterator.asScala.count(isMagicThree)
  22
+  }
  23
+}
2  test/files/jvm/t7006/Foo_1.flags
... ...
@@ -1 +1 @@
1  
- -Ydead-code -Ydebug -Xfatal-warnings
  1
+-Ynooptimise -Ydead-code -Ydebug -Xfatal-warnings

0 notes on commit 3f0224c

Please sign in to comment.
Something went wrong with that request. Please try again.