[Common IR] Do inlining of callable references of inline functions

^KT-52805 Fixed
This commit is contained in:
Alexander Korepanov
2022-07-27 09:33:51 +02:00
committed by Space
parent 3424e756ad
commit bb8da65188
21 changed files with 166 additions and 5 deletions
@@ -1060,6 +1060,12 @@ public class FirBlackBoxInlineCodegenTestGenerated extends AbstractFirBlackBoxIn
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class FirLightTreeBlackBoxInlineCodegenTestGenerated extends AbstractFirL
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class FirSerializeCompileKotlinAgainstInlineKotlinTestGenerated extends A
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -395,7 +395,7 @@ class FunctionInlining(
for (index in 0 until irFunctionReference.typeArgumentsCount)
putTypeArgument(index, irFunctionReference.getTypeArgument(index))
}.implicitCastIfNeededTo(irCall.type)
return this@FunctionInlining.visitExpression(super.visitExpression(immediateCall))
return super.visitExpression(immediateCall).transform(this@FunctionInlining, null)
}
override fun visitElement(element: IrElement) = element.accept(this, null)
@@ -0,0 +1,22 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline public fun <T> composition(x0: T, x1: T, x2: T, fn: (T, T) -> T): T = fn(fn(x0, x1), x2)
// MODULE: main(lib)
// FILE: main.kt
import utils.*
public fun nonInlinableConcat(x: String, y: String): String = "$x$y"
inline fun appendTo(target: String, suffix: String): String = nonInlinableConcat(target, suffix)
// CHECK_CONTAINS_NO_CALLS: test except=nonInlinableConcat
internal fun test(x: String): String = composition("", "O", "K", ::appendTo)
fun box(): String {
return test("O")
}
@@ -1060,6 +1060,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated extends Ab
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -1060,6 +1060,12 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -4622,6 +4622,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/inline/callableReference.kt");
}
@Test
@TestMetadata("callableReferenceClassMethod.kt")
public void testCallableReferenceClassMethod() throws Exception {
runTest("js/js.translator/testData/box/inline/callableReferenceClassMethod.kt");
}
@Test
@TestMetadata("callableReferenceOfLocalFun.kt")
public void testCallableReferenceOfLocalFun() throws Exception {
@@ -868,6 +868,12 @@ public class JsCodegenInlineTestGenerated extends AbstractJsCodegenInlineTest {
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -5070,6 +5070,12 @@ public class FirJsTestGenerated extends AbstractFirJsTest {
runTest("js/js.translator/testData/box/inline/callableReference.kt");
}
@Test
@TestMetadata("callableReferenceClassMethod.kt")
public void testCallableReferenceClassMethod() throws Exception {
runTest("js/js.translator/testData/box/inline/callableReferenceClassMethod.kt");
}
@Test
@TestMetadata("callableReferenceOfLocalFun.kt")
public void testCallableReferenceOfLocalFun() throws Exception {
@@ -5070,6 +5070,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/inline/callableReference.kt");
}
@Test
@TestMetadata("callableReferenceClassMethod.kt")
public void testCallableReferenceClassMethod() throws Exception {
runTest("js/js.translator/testData/box/inline/callableReferenceClassMethod.kt");
}
@Test
@TestMetadata("callableReferenceOfLocalFun.kt")
public void testCallableReferenceOfLocalFun() throws Exception {
@@ -868,6 +868,12 @@ public class IrJsCodegenInlineTestGenerated extends AbstractIrJsCodegenInlineTes
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
@@ -793,6 +793,11 @@ public class IrCodegenBoxInlineWasmTestGenerated extends AbstractIrCodegenBoxInl
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/innerGenericConstuctor.kt");
@@ -0,0 +1,41 @@
typealias Callback = () -> Unit
class CallbackComposer {
inline fun addTo(noinline block: Callback) {
asDynamic().push(block)
}
}
inline fun CallbackComposer.addToExtension(noinline block: Callback) {
asDynamic().push(block)
}
fun createCallbackBuilder(builder: CallbackComposer.() -> Unit): () -> Callback = {
val callbacks = arrayOf<Callback>()
builder(callbacks.unsafeCast<CallbackComposer>())
val composed: Callback = {
for (cb in callbacks) {
cb()
}
}
composed
}
var retVal = ""
fun appendToRetVal(c: String): () -> Unit = {
retVal += c
}
fun box(): String {
val callbackBuilder = createCallbackBuilder {
appendToRetVal("O").also(::addTo)
appendToRetVal("K").also(::addToExtension)
}
val callback = callbackBuilder()
callback()
return retVal
}
@@ -14,10 +14,7 @@ public fun <T, R> apply(x: T, fn: (T)->R): R =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test except=imul TARGET_BACKENDS=JS
// FIXME: Not inlined on IR BE
// CHECK_CONTAINS_NO_CALLS: test except=multiplyBy2 IGNORED_BACKENDS=JS
// CHECK_CONTAINS_NO_CALLS: test except=imul
internal inline fun multiplyBy2(x: Int): Int = x * 2
@@ -40862,6 +40862,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
}
@Test
@TestMetadata("inlineCallableReference.kt")
public void testInlineCallableReference() throws Exception {
runTest("compiler/testData/codegen/boxInline/callableReference/inlineCallableReference.kt");
}
@Test
@TestMetadata("innerGenericConstuctor.kt")
public void testInnerGenericConstuctor() throws Exception {