From df3e114f317c2534a2ac8f571d99d8bf2c465c59 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Tue, 3 Oct 2017 14:51:53 +0300 Subject: [PATCH] Added phase RETURNS_INSERTION Moved explicit insertion of return statement from code generator to a lowering --- .../kotlin/backend/konan/KonanLower.kt | 3 ++ .../kotlin/backend/konan/KonanPhases.kt | 1 + .../backend/konan/llvm/CodeGenerator.kt | 8 +--- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 2 + .../konan/lower/ReturnsInsertionLowering.kt | 39 +++++++++++++++++++ .../konan/lower/SuspendFunctionsLowering.kt | 4 +- backend.native/tests/build.gradle | 12 ++++++ .../tests/codegen/coroutines/degenerate1.kt | 22 +++++++++++ .../tests/codegen/coroutines/degenerate2.kt | 29 ++++++++++++++ 9 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReturnsInsertionLowering.kt create mode 100644 backend.native/tests/codegen/coroutines/degenerate1.kt create mode 100644 backend.native/tests/codegen/coroutines/degenerate2.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt index 8677eda294f..860e8c8e7c5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -133,6 +133,9 @@ internal class KonanLower(val context: Context) { validateIrFile(context, irFile) Autoboxing(context).lower(irFile) } + phaser.phase(KonanPhase.RETURNS_INSERTION) { + ReturnsInsertionLowering(context).lower(irFile) + } } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt index afb7f1fd079..1ad9e7d71bc 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt @@ -55,6 +55,7 @@ enum class KonanPhase(val description: String, /* ... ... */ LOWER_STRING_CONCAT("String concatenation lowering"), /* ... ... */ LOWER_DATA_CLASSES("Data classes lowering"), /* ... ... */ AUTOBOX("Autoboxing of primitive types", BRIDGES_BUILDING, LOWER_COROUTINES), + /* ... ... */ RETURNS_INSERTION("Returns insertion for Unit functions", AUTOBOX, LOWER_COROUTINES, LOWER_ENUMS), /* ... */ BITCODE("LLVM BitCode Generation"), /* ... ... */ RTTI("RTTI Generation"), /* ... ... */ ESCAPE_ANALYSIS("Escape analysis"), diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 00a90a4fa31..8a7ede5912d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -78,12 +78,8 @@ internal inline fun generateFunction(codegen: CodeGenerator, function: LLVMVa inline private fun generateFunctionBody(functionGenerationContext: FunctionGenerationContext, code: FunctionGenerationContext.(FunctionGenerationContext) -> R) { functionGenerationContext.prologue() functionGenerationContext.code(functionGenerationContext) - if (!functionGenerationContext.isAfterTerminator()) { - if (functionGenerationContext.returnType == voidType) - functionGenerationContext.ret(null) - else - functionGenerationContext.unreachable() - } + if (!functionGenerationContext.isAfterTerminator()) + functionGenerationContext.unreachable() functionGenerationContext.epilogue() functionGenerationContext.resetDebugLocation() } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index dcb801b401d..3102d642468 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -338,6 +338,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1() { + println("s1") +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + builder { + s1() + } +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/degenerate2.kt b/backend.native/tests/codegen/coroutines/degenerate2.kt new file mode 100644 index 00000000000..b5f4d4f91b9 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/degenerate2.kt @@ -0,0 +1,29 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Unit = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(Unit) + COROUTINE_SUSPENDED +} + +suspend fun s2() { + println("s2") + s1() +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + builder { + s2() + } +} \ No newline at end of file