IR: create shared variables for val-variables when needed

This is possible when a lambda's contract guarantees initialization of a
variable.
This commit is contained in:
Alexander Udalov
2019-11-04 20:13:25 +01:00
parent 5c4b47c336
commit 66e19b13ce
18 changed files with 116 additions and 48 deletions
@@ -1,6 +1,6 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR, NATIVE, JS_IR
// IGNORE_BACKEND: NATIVE
import kotlin.contracts.*
@@ -18,4 +18,4 @@ fun box(): String {
x = 1L
}
return if (x != 1L) "FAIL" else "OK"
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -16,8 +16,8 @@ public inline fun <R> myrun(block: () -> R): R {
return block()
}
// FILE: 2.kt
import test.*
fun test(b: Boolean): Int {
@@ -34,8 +34,7 @@ fun test(b: Boolean): Int {
}
fun box(): String {
if (test(true) != 1) return "Fail 1"
if (test(false) != -1) return "Fail 2"
return "OK"
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -29,4 +29,4 @@ fun box(): String {
x = if (cond()) test("OK") else test("fail")
}
return x
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -28,4 +28,4 @@ fun box(): String {
x = try { test("OK") } catch (e: Exception) { test("fail") }
}
return x
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -17,6 +17,7 @@ public inline fun <R> myrun(block: () -> R): R {
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -25,4 +26,4 @@ fun box(): String {
x = 42L
}
return if (x.inc() == 43L) "OK" else "Fail: ${x.inc()}"
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -16,8 +16,8 @@ public inline fun <R> myrun(block: () -> R): R {
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -28,4 +28,4 @@ fun box(): String {
}
}
return if (x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}
}
@@ -1,10 +1,10 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// NO_CHECK_LAMBDA_INLINING
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -17,8 +17,8 @@ public inline fun <R> myrun(block: () -> R): R {
return block()
}
// FILE: 2.kt
import test.*
class A {
@@ -36,4 +36,4 @@ class A {
}
}
fun box() = A().x
fun box() = A().x
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -16,8 +16,8 @@ public inline fun <R> myrun(block: () -> R): R {
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -26,4 +26,4 @@ fun box(): String {
x = 42
}
return if (x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}
}
@@ -1,8 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR, NATIVE, JS_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -17,6 +18,7 @@ public inline fun myrun(crossinline block: () -> Unit): Unit {
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -25,4 +27,4 @@ fun box(): String {
x = 42L
}
return if (x != 42L) "FAIL" else "OK"
}
}
@@ -1,9 +1,10 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR, NATIVE, JS_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// NO_CHECK_LAMBDA_INLINING
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -17,6 +18,7 @@ public inline fun myrun(noinline block: () -> Unit): Unit {
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -25,4 +27,4 @@ fun box(): String {
x = 42L
}
return if (x != 42L) "FAIL" else "OK"
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -17,6 +17,7 @@ public inline fun <R> myrun(block: () -> R): R {
}
// FILE: 2.kt
import test.*
fun box(): String {
@@ -26,4 +27,4 @@ fun box(): String {
if (x == 42L) return "OK"
}
return "fail"
}
}
@@ -1,11 +1,11 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -19,6 +19,7 @@ public inline fun <R> myrun(block: () -> R): R {
}
// FILE: 2.kt
import test.*
fun test(xs: List<String>): String {
@@ -38,7 +39,6 @@ fun test(xs: List<String>): String {
return result
}
fun box(): String {
return test(listOf("O", "K", "fail"))
}
}
@@ -1,9 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@@ -16,9 +16,9 @@ public inline fun <R> myrun(block: () -> R): R {
return block()
}
// FILE: 2.kt
// NO_CHECK_LAMBDA_INLINING
import test.*
fun box(): String {
@@ -30,4 +30,4 @@ fun box(): String {
}()
}
return if (res == 42 && x.inc() == 43) "OK" else "Fail: ${x.inc()}"
}
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JVM_IR
// In JVM IR, SharedVariablesLowering transforms `x` into a shared variable to be able to update it from a lambda,
// which is a separate function (...$lambda-0).
// If we keep the existing representation of lambda bodies as separate functions in JVM IR, the only viable option to fix this test
// seems to support this case in the bytecode optimization pass CapturedVarsOptimizationMethodTransformer.
fun box(): String {
val x: String
run {
x = "OK"
val y = x
}
return x
}
// 0 ObjectRef
@@ -0,0 +1,10 @@
fun box(): String {
val x: String
x = "OK"
{
val y = x
}()
return x
}
// 0 ObjectRef