Fixup end label of local variable if it is before start label

In that case, put end label to next label after start label.
This commit is contained in:
Ilmir Usmanov
2021-09-20 16:12:03 +02:00
committed by Space
parent ade9f5a1e8
commit 91491eef06
9 changed files with 113 additions and 0 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.kotlin.codegen.optimization.boxing.isMethodInsnWith
import org.jetbrains.kotlin.codegen.optimization.common.InsnSequence
import org.jetbrains.kotlin.codegen.optimization.common.findNextOrNull
import org.jetbrains.kotlin.codegen.optimization.common.removeUnusedLocalVariables
import org.jetbrains.kotlin.codegen.optimization.common.updateMaxStack
import org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer
@@ -25,6 +26,8 @@ class InplaceArgumentsMethodTransformer : MethodTransformer() {
transformMethod(methodContext)
methodNode.fixupLVT()
methodNode.removeUnusedLocalVariables()
methodNode.updateMaxStack()
}
@@ -368,4 +371,16 @@ class InplaceArgumentsMethodTransformer : MethodTransformer() {
insn = insn.next
}
}
}
// HACK: if new end label is before start label, change to the next one
private fun MethodNode.fixupLVT() {
for (localVariable in localVariables) {
val startIndex = instructions.indexOf(localVariable.start)
val endIndex = instructions.indexOf(localVariable.end)
if (endIndex < startIndex) {
val newEnd = localVariable.start.findNextOrNull { it is LabelNode } as? LabelNode
localVariable.end = newEnd ?: localVariable.start
}
}
}
@@ -12355,6 +12355,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@Test
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@Test
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
@@ -0,0 +1,60 @@
// WITH_RUNTIME
// FULL_JDK
import kotlin.coroutines.*
fun ticker(
delayMillis: Long,
initialDelayMillis: Long = delayMillis
): ReceiveChannel<Unit> = object : ReceiveChannel<Unit> {
override fun cancel() {}
override fun receive() {}
}
suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend CoroutineScope.() -> T): T? = null
interface ReceiveChannel<out E> {
fun receive(): E
fun cancel()
}
interface CoroutineScope
suspend fun delay(i: Int) {}
suspend fun test() {
val tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) // create ticker channel
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
println("Initial element is available immediately: $nextElement") // no initial delay
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements have 100ms delay
println("Next element is not ready in 50 ms: $nextElement")
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }
println("Next element is ready in 100 ms: $nextElement")
// Emulate large consumption delays
println("Consumer pauses for 150ms")
delay(150)
// Next element is available immediately
nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
println("Next element is available immediately after large consumer delay: $nextElement")
// Note that the pause between `receive` calls is taken into account and next element arrives faster
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }
println("Next element is ready in 50ms after consumer pause in 150ms: $nextElement")
tickerChannel.cancel() // indicate that no more elements are needed
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(Continuation(EmptyCoroutineContext) {
it.getOrThrow()
})
}
fun box(): String {
builder {
test()
}
return "OK"
}
@@ -12277,6 +12277,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@Test
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@Test
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
@@ -12355,6 +12355,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@Test
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@Test
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
@@ -9893,6 +9893,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/nullSpilling.kt");
@@ -8787,6 +8787,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/nullSpilling.kt");
@@ -8193,6 +8193,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/nullSpilling.kt");
@@ -8173,6 +8173,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/kt38925.kt");
}
@TestMetadata("lvtWithInlineOnly.kt")
public void testLvtWithInlineOnly() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/lvtWithInlineOnly.kt");
}
@TestMetadata("nullSpilling.kt")
public void testNullSpilling() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/varSpilling/nullSpilling.kt");