JVM_IR: default-initialize variables visible in the LVT

See KT-36812. Aside from the problem stated there, D8 will throw out the
entire LVT if it sees a variable that has not been written to (and will
generate incorrect SSA if the slot is reused with a different type).

Note: this only fixes a FIR test because it's missing an `else -> throw`
branch, and default initialization satisfies the verifier and masks the
incorrect control flow.
This commit is contained in:
pyos
2020-03-02 12:12:56 +01:00
committed by Dmitry Petrov
parent ecb7478794
commit 7e6d080123
18 changed files with 20 additions and 57 deletions
@@ -322,18 +322,14 @@ class ExpressionCodegen(
}
}
private val IrVariable.isVisibleInLVT: Boolean
get() = origin != IrDeclarationOrigin.IR_TEMPORARY_VARIABLE &&
origin != IrDeclarationOrigin.FOR_LOOP_ITERATOR
private fun writeLocalVariablesInTable(info: BlockInfo, endLabel: Label) {
info.variables.forEach {
when (it.declaration.origin) {
IrDeclarationOrigin.IR_TEMPORARY_VARIABLE,
IrDeclarationOrigin.FOR_LOOP_ITERATOR -> {
// Ignore implicitly created variables
}
else -> {
mv.visitLocalVariable(
it.declaration.name.asString(), it.type.descriptor, null, it.startLabel, endLabel, it.index
)
}
if (it.declaration.isVisibleInLVT) {
mv.visitLocalVariable(it.declaration.name.asString(), it.type.descriptor, null, it.startLabel, endLabel, it.index)
}
}
@@ -485,9 +481,13 @@ class ExpressionCodegen(
declaration.markLineNumber(startOffset = true)
declaration.initializer?.let {
it.accept(this, data).coerce(varType, declaration.type).materialize()
it.markLineNumber(startOffset = true)
val initializer = declaration.initializer
if (initializer != null) {
initializer.accept(this, data).coerce(varType, declaration.type).materialize()
initializer.markLineNumber(startOffset = true)
mv.store(index, varType)
} else if (declaration.isVisibleInLVT) {
pushDefaultValueOnStack(varType, mv)
mv.store(index, varType)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
sealed class A {
object B : A()
@@ -1,6 +1,3 @@
// This test checks, that different variables occupy the same slot
// In JVM_IR, however, loop variable's lifetime goes beyond the loop itself, thus the test has no sense in JVM_IR
// IGNORE_BACKEND: JVM_IR
// WITH_COROUTINES
import helpers.*
@@ -1,6 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36648 Captured variables not optimized in JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): java.lang.Integer {
val c: java.lang.Integer
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -22,4 +20,4 @@ fun doIt(block: () -> Unit) {
}
// 0 ISTORE 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L3 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef;
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -22,8 +20,8 @@ fun doIt(block: () -> Unit) {
}
// 0 ISTORE 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L3 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef;
// JVM_IR_TEMPLATES
// 0 ISTORE 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L4 0
// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef;
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.random.Random
fun test(): Char {
@@ -14,4 +11,4 @@ fun test(): Char {
}
// 3 ISTORE 0
// 1 LOCALVARIABLE c C L1 L7 0
// 1 LOCALVARIABLE c C
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.random.Random
fun test(): Char {
@@ -14,4 +11,4 @@ fun test(): Char {
}
// 3 ISTORE 0
// 1 LOCALVARIABLE c C L1 L7 0
// 1 LOCALVARIABLE c C
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.random.Random
fun test(): Char {
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
import kotlin.random.Random
fun test(): Char {
@@ -1,6 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36648 Captured variables not optimized in JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): UInt {
val c: UInt
@@ -1,6 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36648 Captured variables not optimized in JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): UInt {
var c: UInt
@@ -1,6 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36648 Captured variables not optimized in JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): Char {
lateinit var c: Any
@@ -1,6 +1,5 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36648 Captured variables not optimized in JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): Char {
val c: Char
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): Char {
val c: Char
val l = Any()
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(): Char {
var c: Char
val l = Any()
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(i: Int): Char {
val c: Char
when (i) {
@@ -20,4 +17,4 @@ fun test(i: Int): Char {
}
// 12 ISTORE 1
// 1 LOCALVARIABLE c C L1 L16 1
// 1 LOCALVARIABLE c C
@@ -1,6 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR
fun test(i: Int): Char {
var c: Char
when (i) {
@@ -20,4 +17,4 @@ fun test(i: Int): Char {
}
// 12 ISTORE 1
// 1 LOCALVARIABLE c C L1 L16 1
// 1 LOCALVARIABLE c C