Generate CHECKCAST Object inside the markers

otherwise, the unboxing interferes with bytecode analysis.
This commit is contained in:
Ilmir Usmanov
2020-09-03 21:10:47 +02:00
parent e8a451072e
commit 52f9569d33
16 changed files with 246 additions and 7 deletions
@@ -602,11 +602,12 @@ class CoroutineTransformerMethodVisitor(
}
private fun dropUnboxInlineClassMarkers(methodNode: MethodNode, suspensionPoints: List<SuspensionPoint>) {
for (marker in methodNode.instructions.asSequence()
.filter { isBeforeUnboxInlineClassMarker(it) || isAfterUnboxInlineClassMarker(it) }.toList()
) {
for (marker in methodNode.instructions.asSequence().filter { isBeforeUnboxInlineClassMarker(it) }.toList()) {
methodNode.instructions.removeAll(listOf(marker.previous, marker))
}
for (marker in methodNode.instructions.asSequence().filter { isAfterUnboxInlineClassMarker(it) }.toList()) {
methodNode.instructions.removeAll(listOf(marker.previous.previous, marker.previous, marker))
}
for (suspension in suspensionPoints) {
methodNode.instructions.removeAll(suspension.unboxInlineClassInstructions)
}
@@ -1142,7 +1143,7 @@ internal class SuspensionPoint(
if (!isBeforeUnboxInlineClassMarker(beforeMarker)) return emptyList()
val afterMarker = beforeMarker.findNextOrNull { isAfterUnboxInlineClassMarker(it) }
?: error("Before unbox inline class marker without after unbox inline class marker")
return InsnSequence(beforeMarker.next, afterMarker.previous).toList()
return InsnSequence(beforeMarker.next, afterMarker.previous.previous).toList()
}
operator fun contains(insn: AbstractInsnNode): Boolean {
@@ -416,6 +416,11 @@ internal fun addUnboxInlineClassMarkersIfNeeded(v: InstructionAdapter, descripto
if (inlineClass != null) {
addBeforeUnboxInlineClassMarker(v)
StackValue.unboxInlineClass(AsmTypes.OBJECT_TYPE, inlineClass, v)
// Suspend functions always returns Any?, but the unboxing disrupts type analysis of the bytecode.
// For example, if the underlying type is String, CHECKCAST String is removed.
// However, the unboxing is moved to the resume path, the direct path still has Any?, but now, without the CHECKCAST.
// Thus, we add CHECKCAST Object, which we remove, after we copy the unboxing to the resume path.
v.checkcast(AsmTypes.OBJECT_TYPE)
addAfterUnboxInlineClassMarker(v)
}
}
@@ -1,6 +1,5 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND_FIR: JVM_IR
import helpers.*
import kotlin.coroutines.*
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
import helpers.*
import kotlin.coroutines.*
@@ -1,5 +1,6 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS_IR
import helpers.*
import kotlin.coroutines.*
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
import helpers.*
import kotlin.coroutines.*
@@ -1,5 +1,6 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS_IR
import helpers.*
import kotlin.coroutines.*
@@ -0,0 +1,30 @@
// WITH_RUNTIME
// WITH_COROUTINES
// FILE: inline.kt
inline class IC(val s: String)
suspend fun o(): IC = IC("O")
suspend fun k(): IC = IC("K")
inline suspend fun inlineMe(): String {
return o().s + k().s
}
// FILE: box.kt
import helpers.*
import kotlin.coroutines.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var res = "FAIL"
builder {
res = inlineMe()
}
return res
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
// WITH_COROUTINES
// CHECK_STATE_MACHINE
// FILE: inline.kt
import helpers.*
inline class IC(val s: String)
suspend fun o(): IC {
StateMachineChecker.suspendHere()
return IC("O")
}
suspend fun k(): IC {
StateMachineChecker.suspendHere()
return IC("K")
}
inline suspend fun inlineMe(): String {
return o().s + k().s
}
// FILE: box.kt
import helpers.*
import kotlin.coroutines.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
var res = "FAIL"
builder {
res = inlineMe()
}
StateMachineChecker.check(2)
return res
}
@@ -4133,6 +4133,29 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4133,6 +4133,29 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractCompileKotlinAgainstInlineKotlinTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTestWithCustomIgnoreDirective(this::doTest, TargetBackend.JVM, testDataFilePath, "// IGNORE_BACKEND_MULTI_MODULE: ");
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4008,6 +4008,29 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractIrBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4008,6 +4008,29 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractIrCompileKotlinAgainstInlineKotlinTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTestWithCustomIgnoreDirective(this::doTest, TargetBackend.JVM_IR, testDataFilePath, "// IGNORE_BACKEND_MULTI_MODULE: ");
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3568,6 +3568,29 @@ public class IrJsCodegenInlineES6TestGenerated extends AbstractIrJsCodegenInline
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractIrJsCodegenInlineES6Test {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath);
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3568,6 +3568,29 @@ public class IrJsCodegenInlineTestGenerated extends AbstractIrJsCodegenInlineTes
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractIrJsCodegenInlineTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3568,6 +3568,29 @@ public class JsCodegenInlineTestGenerated extends AbstractJsCodegenInlineTest {
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class InlineClass extends AbstractJsCodegenInlineTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInInlineClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/suspend/inlineClass"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
@TestMetadata("returnUnboxedDirect.kt")
public void testReturnUnboxedDirect() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedDirect.kt");
}
@TestMetadata("returnUnboxedResume.kt")
public void testReturnUnboxedResume() throws Exception {
runTest("compiler/testData/codegen/boxInline/suspend/inlineClass/returnUnboxedResume.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)