Add test for merging coroutine continuations with different field types Change-Id: Ifdcb1b18944188b7f59a5a8a7f6358eaafe39215
diff --git a/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/CoroutineContinuationMergingTest.java b/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/CoroutineContinuationMergingTest.java index 91d82f2..2fb7c7f 100644 --- a/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/CoroutineContinuationMergingTest.java +++ b/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/CoroutineContinuationMergingTest.java
@@ -37,10 +37,18 @@ // ContinuationImpl for a top-level suspend function (no final `this$0` receiver field). private static final ClassReference TOP_LEVEL_CONTINUATION = Reference.classFromTypeName(PKG + ".MainKt$topLevelSuspend$1"); - // ContinuationImpls for member suspend functions on Worker (both have `final Worker this$0`, - // with different non-final spill fields). + // ContinuationImpls for member suspend functions on Worker (all have `final Worker this$0`, + // with different non-final spill field counts and reference types). private static final ClassReference ONE_SUSPEND_CONTINUATION = Reference.classFromTypeName(PKG + ".Worker$oneSuspend$1"); + private static final ClassReference PAYLOAD_FIRST_A_CONTINUATION = + Reference.classFromTypeName(PKG + ".Worker$payloadFirstA$1"); + private static final ClassReference PAYLOAD_FIRST_B_CONTINUATION = + Reference.classFromTypeName(PKG + ".Worker$payloadFirstB$1"); + private static final ClassReference PAYLOAD_SECOND_A_CONTINUATION = + Reference.classFromTypeName(PKG + ".Worker$payloadSecondA$1"); + private static final ClassReference PAYLOAD_SECOND_B_CONTINUATION = + Reference.classFromTypeName(PKG + ".Worker$payloadSecondB$1"); private static final ClassReference TWO_SUSPENDS_CONTINUATION = Reference.classFromTypeName(PKG + ".Worker$twoSuspends$1"); @@ -52,13 +60,30 @@ Reference.classFromTypeName(PKG + ".MainKt$main$oneSuspendLambda$1"); private static final ClassReference TWO_SUSPENDS_LAMBDA = Reference.classFromTypeName(PKG + ".MainKt$main$twoSuspendsLambda$1"); + // SuspendLambdas each capturing one final field of type FirstPayload or SecondPayload. + private static final ClassReference FIRST_PAYLOAD_LAMBDA_A = + Reference.classFromTypeName(PKG + ".MainKt$main$firstPayloadLambdaA$1"); + private static final ClassReference FIRST_PAYLOAD_LAMBDA_B = + Reference.classFromTypeName(PKG + ".MainKt$main$firstPayloadLambdaB$1"); + private static final ClassReference SECOND_PAYLOAD_LAMBDA_A = + Reference.classFromTypeName(PKG + ".MainKt$main$secondPayloadLambdaA$1"); + private static final ClassReference SECOND_PAYLOAD_LAMBDA_B = + Reference.classFromTypeName(PKG + ".MainKt$main$secondPayloadLambdaB$1"); private static final String[] EXPECTED_OUTPUT = { "twoSuspends: worker, 6, v1, v2", "oneSuspend: worker", - "topLevel: tag, v3", - "oneSuspendLambda: tag, v4", - "twoSuspendsLambda: tag, 2, v5, v6" + "payloadFirstA: worker, first:tag-a, v3", + "payloadFirstB: worker, first:tag-b, v4", + "payloadSecondA: worker, second:3, v5", + "payloadSecondB: worker, second:4, v6", + "topLevel: tag, v7", + "oneSuspendLambda: tag, v8", + "twoSuspendsLambda: tag, 2, v9, v10", + "firstPayloadLambdaA: first:tag, v11", + "firstPayloadLambdaB: first:tag, v12", + "secondPayloadLambdaA: second:3, v13", + "secondPayloadLambdaB: second:3, v14" }; private static KotlinCompileMemoizer compiledJars; @@ -95,12 +120,22 @@ inspector -> inspector .assertIsCompleteMergeGroup(ONE_SUSPEND_CONTINUATION, TWO_SUSPENDS_CONTINUATION) + .assertIsCompleteMergeGroup( + PAYLOAD_FIRST_A_CONTINUATION, PAYLOAD_FIRST_B_CONTINUATION) + .assertIsCompleteMergeGroup( + PAYLOAD_SECOND_A_CONTINUATION, PAYLOAD_SECOND_B_CONTINUATION) .assertIsCompleteMergeGroup(ONE_SUSPEND_LAMBDA, TWO_SUSPENDS_LAMBDA) + .assertIsCompleteMergeGroup(FIRST_PAYLOAD_LAMBDA_A, FIRST_PAYLOAD_LAMBDA_B) + .assertIsCompleteMergeGroup(SECOND_PAYLOAD_LAMBDA_A, SECOND_PAYLOAD_LAMBDA_B) .assertClassReferencesNotMerged(TOP_LEVEL_CONTINUATION, TWO_CAPTURES_LAMBDA)) .compile() .inspect( inspector -> { assertThat(inspector.clazz(ONE_SUSPEND_CONTINUATION), isPresent()); + assertThat(inspector.clazz(PAYLOAD_FIRST_A_CONTINUATION), isPresent()); + assertThat(inspector.clazz(PAYLOAD_FIRST_B_CONTINUATION), isAbsent()); + assertThat(inspector.clazz(PAYLOAD_SECOND_A_CONTINUATION), isPresent()); + assertThat(inspector.clazz(PAYLOAD_SECOND_B_CONTINUATION), isAbsent()); assertThat(inspector.clazz(TWO_SUSPENDS_CONTINUATION), isAbsent()); assertThat(inspector.clazz(TOP_LEVEL_CONTINUATION), isPresent()); assertTrue( @@ -136,10 +171,18 @@ inspector.assertClassReferencesNotMerged( TOP_LEVEL_CONTINUATION, ONE_SUSPEND_CONTINUATION, + PAYLOAD_FIRST_A_CONTINUATION, + PAYLOAD_FIRST_B_CONTINUATION, + PAYLOAD_SECOND_A_CONTINUATION, + PAYLOAD_SECOND_B_CONTINUATION, TWO_SUSPENDS_CONTINUATION, TWO_CAPTURES_LAMBDA, ONE_SUSPEND_LAMBDA, - TWO_SUSPENDS_LAMBDA)) + TWO_SUSPENDS_LAMBDA, + FIRST_PAYLOAD_LAMBDA_A, + FIRST_PAYLOAD_LAMBDA_B, + SECOND_PAYLOAD_LAMBDA_A, + SECOND_PAYLOAD_LAMBDA_B)) .run(parameters.getRuntime(), MAIN_CLASS) .assertSuccessWithOutputLines(EXPECTED_OUTPUT); }
diff --git a/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/merging/Main.kt b/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/merging/Main.kt index 01bb674..0f6949b 100644 --- a/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/merging/Main.kt +++ b/src/test/java8/kotlin/com/android/tools/r8/kotlin/coroutines/merging/Main.kt
@@ -49,6 +49,16 @@ } @NeverClassInline +class FirstPayload(val text: String) { + @NeverInline fun describe(): String = "first:$text" +} + +@NeverClassInline +class SecondPayload(val number: Int) { + @NeverInline fun describe(): String = "second:$number" +} + +@NeverClassInline class Worker(val name: String) { // Compiles to a ContinuationImpl with `final Worker this$0` and one suspension point. // Calls twoSuspends() passing its own continuation as the caller continuation; when merged with @@ -60,6 +70,33 @@ println("oneSuspend: $name") } + // Pair of ContinuationImpls with the same non-trivial field-type multiset (`FirstPayload` spill). + @NeverInline + suspend fun payloadFirstA(payload: FirstPayload) { + val step = suspendWithNextValue() + println("payloadFirstA: $name, ${payload.describe()}, $step") + } + + @NeverInline + suspend fun payloadFirstB(payload: FirstPayload) { + val step = suspendWithNextValue() + println("payloadFirstB: $name, ${payload.describe()}, $step") + } + + // Pair of ContinuationImpls with a different non-trivial field-type multiset (`SecondPayload` + // spill); merging with payloadFirstA/B widens the shared reference spill field to Object. + @NeverInline + suspend fun payloadSecondA(payload: SecondPayload) { + val step = suspendWithNextValue() + println("payloadSecondA: $name, ${payload.describe()}, $step") + } + + @NeverInline + suspend fun payloadSecondB(payload: SecondPayload) { + val step = suspendWithNextValue() + println("payloadSecondB: $name, ${payload.describe()}, $step") + } + // Compiles to a ContinuationImpl with the same `final Worker this$0` field as oneSuspend(), so // the two classes can merge even though twoSuspends() spills extra non-final locals (`count: Int` // and `first: String`) across a second suspension point. @@ -79,6 +116,10 @@ // single-capture SuspendLambdas below. val twoCapturesLambda: suspend () -> Unit = { worker.oneSuspend() + worker.payloadFirstA(FirstPayload("$tag-a")) + worker.payloadFirstB(FirstPayload("$tag-b")) + worker.payloadSecondA(SecondPayload(tag.length)) + worker.payloadSecondB(SecondPayload(tag.length + 1)) topLevelSuspend(tag) } runCoroutine(twoCapturesLambda) @@ -101,4 +142,33 @@ println("twoSuspendsLambda: $tag, $count, $first, $second") } runCoroutine(twoSuspendsLambda) + + // Pairs of SuspendLambdas that each capture one final field of a different reference type + // (`$firstPayload: FirstPayload` and `$secondPayload: SecondPayload`). Merging them widens the + // captured final field to a common supertype. + val firstPayload = FirstPayload(tag) + val firstPayloadLambdaA: suspend () -> Unit = { + val step = suspendWithNextValue() + println("firstPayloadLambdaA: ${firstPayload.describe()}, $step") + } + runCoroutine(firstPayloadLambdaA) + + val firstPayloadLambdaB: suspend () -> Unit = { + val step = suspendWithNextValue() + println("firstPayloadLambdaB: ${firstPayload.describe()}, $step") + } + runCoroutine(firstPayloadLambdaB) + + val secondPayload = SecondPayload(tag.length) + val secondPayloadLambdaA: suspend () -> Unit = { + val step = suspendWithNextValue() + println("secondPayloadLambdaA: ${secondPayload.describe()}, $step") + } + runCoroutine(secondPayloadLambdaA) + + val secondPayloadLambdaB: suspend () -> Unit = { + val step = suspendWithNextValue() + println("secondPayloadLambdaB: ${secondPayload.describe()}, $step") + } + runCoroutine(secondPayloadLambdaB) }