JVM_IR: incorporate validation into jvmPhases

This commit is contained in:
Georgy Bronnikov
2019-10-01 16:02:10 +03:00
parent 05527fbc9a
commit 2e3428bbe7
5 changed files with 71 additions and 33 deletions
@@ -5,6 +5,10 @@
package org.jetbrains.kotlin.backend.common.phaser
import org.jetbrains.kotlin.backend.common.CheckDeclarationParentsVisitor
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.IrValidator
import org.jetbrains.kotlin.backend.common.IrValidatorConfig
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.dump
@@ -117,4 +121,17 @@ fun <Data, Context> dumpToStdout(
println()
}
val defaultDumper = makeDumpAction(dumpToStdout(::dumpIrElement) + dumpToFile("ir", ::dumpIrElement))
val defaultDumper = makeDumpAction(dumpToStdout(::dumpIrElement) + dumpToFile("ir", ::dumpIrElement))
fun <Fragment : IrElement> validationCallback(context: CommonBackendContext, fragment: Fragment) {
val validatorConfig = IrValidatorConfig(
abortOnError = true,
ensureAllNodesAreDifferent = true,
checkTypes = false,
checkDescriptors = false
)
fragment.accept(IrValidator(context, validatorConfig), null)
fragment.accept(CheckDeclarationParentsVisitor, null)
}
val validationAction = makeVerifyAction(::validationCallback)
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.backend.common.phaser
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
@@ -54,7 +55,7 @@ fun <Context : CommonBackendContext> namedIrModulePhase(
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper),
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
) = SameTypeNamedPhaseWrapper(
name,
@@ -76,7 +77,7 @@ fun <Context : CommonBackendContext> namedIrFilePhase(
preconditions: Set<Checker<IrFile>> = emptySet(),
postconditions: Set<Checker<IrFile>> = emptySet(),
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper),
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
) = SameTypeNamedPhaseWrapper(
name,
@@ -90,6 +91,38 @@ fun <Context : CommonBackendContext> namedIrFilePhase(
nlevels
)
fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
op: (Context, Element) -> Unit,
description: String,
name: String,
prerequisite: Set<AnyNamedPhase> = emptySet(),
preconditions: Set<Checker<Element>> = emptySet(),
postconditions: Set<Checker<Element>> = emptySet(),
stickyPostconditions: Set<Checker<Element>> = emptySet(),
actions: Set<Action<Element, Context>> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
) = SameTypeNamedPhaseWrapper(
name,
description,
prerequisite,
preconditions = preconditions,
postconditions = postconditions,
stickyPostconditions = stickyPostconditions,
actions = actions,
nlevels = nlevels,
lower = object : SameTypeCompilerPhase<Context, Element> {
override fun invoke(
phaseConfig: PhaseConfig,
phaserState: PhaserState<Element>,
context: Context,
input: Element
): Element {
op(context, input)
return input
}
}
)
fun <Context : CommonBackendContext> namedUnitPhase(
name: String,
description: String,
@@ -160,7 +193,7 @@ fun <Context : CommonBackendContext> makeIrFilePhase(
preconditions: Set<Checker<IrFile>> = emptySet(),
postconditions: Set<Checker<IrFile>> = emptySet(),
stickyPostconditions: Set<Checker<IrFile>> = emptySet(),
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper)
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction)
) = namedIrFilePhase(
name, description, prerequisite,
preconditions = preconditions,
@@ -184,7 +217,7 @@ fun <Context : CommonBackendContext> makeIrModulePhase(
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper)
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction)
) = namedIrModulePhase(
name, description, prerequisite,
preconditions=preconditions,
@@ -21,19 +21,6 @@ private fun DeclarationContainerLoweringPass.runOnFilesPostfix(files: Iterable<I
private fun ClassLoweringPass.runOnFilesPostfix(moduleFragment: IrModuleFragment) = moduleFragment.files.forEach { runOnFilePostfix(it) }
private fun validationCallback(context: JsIrBackendContext, module: IrModuleFragment) {
val validatorConfig = IrValidatorConfig(
abortOnError = true,
ensureAllNodesAreDifferent = true,
checkTypes = false,
checkDescriptors = false
)
module.accept(IrValidator(context, validatorConfig), null)
module.accept(CheckDeclarationParentsVisitor, null)
}
val validationAction = makeVerifyAction(::validationCallback)
private fun makeJsModulePhase(
lowering: (JsIrBackendContext) -> FileLoweringPass,
name: String,
@@ -37,6 +37,18 @@ private fun makePatchParentsPhase(number: Int) = namedIrFilePhase(
nlevels = 0
)
private val validateIrBeforeLowering = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
{ context, module -> validationCallback(context, module) },
name = "ValidateIrBeforeLowering",
description = "Validate IR before lowering"
)
private val validateIrAfterLowering = makeCustomPhase<JvmBackendContext, IrModuleFragment>(
{ context, module -> validationCallback(context, module) },
name = "ValidateIrAfterLowering",
description = "Validate IR after lowering"
)
private val stripTypeAliasDeclarationsPhase = makeIrFilePhase<CommonBackendContext>(
{ StripTypeAliasDeclarationsLowering() },
name = "StripTypeAliasDeclarations",
@@ -231,10 +243,12 @@ private val jvmFilePhases =
val jvmPhases = namedIrModulePhase(
name = "IrLowering",
description = "IR lowering",
lower = expectDeclarationsRemovingPhase then
lower = validateIrBeforeLowering then
expectDeclarationsRemovingPhase then
fileClassPhase then
performByIrFile(lower = jvmFilePhases) then
generateMultifileFacadesPhase
generateMultifileFacadesPhase then
validateIrAfterLowering
)
class JvmLower(val context: JvmBackendContext) {
@@ -20,19 +20,6 @@ import org.jetbrains.kotlin.ir.util.patchDeclarationParents
private fun ClassLoweringPass.runOnFilesPostfix(moduleFragment: IrModuleFragment) = moduleFragment.files.forEach { runOnFilePostfix(it) }
private fun validationCallback(context: WasmBackendContext, module: IrModuleFragment) {
val validatorConfig = IrValidatorConfig(
abortOnError = true,
ensureAllNodesAreDifferent = true,
checkTypes = false,
checkDescriptors = false
)
module.accept(IrValidator(context, validatorConfig), null)
module.accept(CheckDeclarationParentsVisitor, null)
}
val validationAction = makeVerifyAction(::validationCallback)
private fun makeWasmModulePhase(
lowering: (WasmBackendContext) -> FileLoweringPass,
name: String,