From 342a166ab8a9d401d814b6eba03377686fe45f40 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 7 Mar 2017 16:07:51 +0300 Subject: [PATCH] Add explicit phase dependency mechanism. (#313) --- .../kotlin/backend/konan/KonanDriver.kt | 6 +++ .../kotlin/backend/konan/KonanPhases.kt | 52 +++++++++++-------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanDriver.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanDriver.kt index 186bfcbec86..71ea110f775 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanDriver.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanDriver.kt @@ -76,6 +76,12 @@ public fun runTopLevelPhases(konanConfig: KonanConfig, environment: KotlinCoreEn phaser.phase(KonanPhase.BITCODE) { emitLLVM(context) } + // We always verify bitcode to prevent hard to debug bugs. + context.verifyBitCode() + + if (context.shouldPrintBitCode()) { + context.printBitCode() + } } phaser.phase(KonanPhase.LINKER) { 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 9e05245e540..fe8232f0a9c 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 @@ -1,37 +1,40 @@ package org.jetbrains.kotlin.backend.konan -import org.jetbrains.kotlin.backend.konan.Context import org.jetbrains.kotlin.backend.konan.util.* enum class KonanPhase(val description: String, + val prerequisite: Set = setOf(), var enabled: Boolean = true, var verbose: Boolean = false) { /* */ FRONTEND("Frontend builds AST"), /* */ PSI_TO_IR("Psi to IR conversion"), /* */ BACKEND("All backend"), - /* ... */ LOWER("IR Lowering"), - /* ... ... */ LOWER_BUILTIN_OPERATORS("BuiltIn Operators Lowering"), - /* ... ... */ LOWER_VARARG("Vararg lowering"), - /* ... ... */ LOWER_DEFAULT_PARAMETER_EXTENT("Default Parameter Extent Lowering"), - /* ... ... */ LOWER_TYPE_OPERATORS("Type operators lowering"), - /* ... ... */ LOWER_SHARED_VARIABLES("Shared Variable Lowering"), - /* ... ... */ LOWER_LOCAL_FUNCTIONS("Local Function Lowering"), - /* ... ... */ LOWER_CALLABLES("Callable references Lowering"), + /* ... */ LOWER("IR Lowering"), /* ... ... */ LOWER_INLINE("Functions inlining"), /* ... ... */ LOWER_INTEROP("Interop lowering"), - /* ... ... */ AUTOBOX("Autoboxing of primitive types"), + /* ... ... */ LOWER_SHARED_VARIABLES("Shared Variable Lowering"), /* ... ... */ LOWER_ENUMS("Enum classes lowering"), - /* ... ... */ LOWER_INNER_CLASSES("Inner classes lowering"), - /* ... ... */ LOWER_STRING_CONCAT("String concatenation lowering"), - /* ... ... */ LOWER_INITIALIZERS("Initializers lowering"), - /* ... ... */ BRIDGES_BUILDING("Bridges building"), /* ... ... */ LOWER_DELEGATION("Delegation lowering"), - /* ... ... */ LOWER_TAILREC("tailrec lowering"), + /* ... ... */ LOWER_INITIALIZERS("Initializers lowering", setOf(LOWER_ENUMS)), + /* ... ... */ LOWER_CALLABLES("Callable references Lowering", setOf( + LOWER_INTEROP, LOWER_INITIALIZERS, LOWER_DELEGATION)), + /* ... ... */ LOWER_VARARG("Vararg lowering", setOf(LOWER_CALLABLES)), + /* ... ... */ LOWER_LOCAL_FUNCTIONS("Local Function Lowering", setOf(LOWER_INITIALIZERS)), + /* ... ... */ LOWER_TAILREC("tailrec lowering", setOf(LOWER_LOCAL_FUNCTIONS)), + /* ... ... */ LOWER_DEFAULT_PARAMETER_EXTENT("Default Parameter Extent Lowering", setOf( + LOWER_TAILREC, LOWER_ENUMS)), + /* ... ... */ LOWER_INNER_CLASSES("Inner classes lowering", setOf(LOWER_DEFAULT_PARAMETER_EXTENT)), + /* ... ... */ LOWER_BUILTIN_OPERATORS("BuiltIn Operators Lowering", setOf( + LOWER_DEFAULT_PARAMETER_EXTENT)), + /* ... ... */ LOWER_TYPE_OPERATORS("Type operators lowering"), + /* ... ... */ BRIDGES_BUILDING("Bridges building"), + /* ... ... */ LOWER_STRING_CONCAT("String concatenation lowering"), + /* ... ... */ AUTOBOX("Autoboxing of primitive types", setOf(BRIDGES_BUILDING)), /* ... */ BITCODE("LLVM BitCode Generation"), /* ... ... */ RTTI("RTTI Generation"), /* ... ... */ CODEGEN("Code Generation"), /* ... ... */ METADATOR("Metadata Generation"), - /* */ LINKER("Link Stage"); + /* */ LINKER("Link Stage") } object KonanPhases { @@ -73,9 +76,18 @@ object KonanPhases { internal class PhaseManager(val context: Context) { + val previousPhases = mutableSetOf() + internal fun phase(phase: KonanPhase, body: () -> Unit) { - if (!phase .enabled) return + if (!phase.enabled) return + + phase.prerequisite.forEach { + if (!previousPhases.contains(it)) + throw Error("$phase requires $it") + } + + previousPhases.add(phase) val savePhase = context.phase context.phase = phase @@ -93,18 +105,12 @@ internal class PhaseManager(val context: Context) { verifyIr() } - // We always verify bitcode to prevent hard to debug bugs. - verifyBitCode() - if (shouldPrintDescriptors()) { printDescriptors() } if (shouldPrintIr()) { printIr() } - if (shouldPrintBitCode()) { - printBitCode() - } } context.depth --