JVM_IR: incorporate validation into jvmPhases
This commit is contained in:
+18
-1
@@ -5,6 +5,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.backend.common.phaser
|
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.IrElement
|
||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.util.dump
|
import org.jetbrains.kotlin.ir.util.dump
|
||||||
@@ -117,4 +121,17 @@ fun <Data, Context> dumpToStdout(
|
|||||||
println()
|
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)
|
||||||
|
|||||||
+37
-4
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.backend.common.phaser
|
|||||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||||
import org.jetbrains.kotlin.backend.common.lower
|
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.IrFile
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ fun <Context : CommonBackendContext> namedIrModulePhase(
|
|||||||
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
|
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
|
||||||
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper),
|
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction),
|
||||||
nlevels: Int = 1
|
nlevels: Int = 1
|
||||||
) = SameTypeNamedPhaseWrapper(
|
) = SameTypeNamedPhaseWrapper(
|
||||||
name,
|
name,
|
||||||
@@ -76,7 +77,7 @@ fun <Context : CommonBackendContext> namedIrFilePhase(
|
|||||||
preconditions: Set<Checker<IrFile>> = emptySet(),
|
preconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
postconditions: Set<Checker<IrFile>> = emptySet(),
|
postconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
|
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
|
||||||
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper),
|
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction),
|
||||||
nlevels: Int = 1
|
nlevels: Int = 1
|
||||||
) = SameTypeNamedPhaseWrapper(
|
) = SameTypeNamedPhaseWrapper(
|
||||||
name,
|
name,
|
||||||
@@ -90,6 +91,38 @@ fun <Context : CommonBackendContext> namedIrFilePhase(
|
|||||||
nlevels
|
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(
|
fun <Context : CommonBackendContext> namedUnitPhase(
|
||||||
name: String,
|
name: String,
|
||||||
description: String,
|
description: String,
|
||||||
@@ -160,7 +193,7 @@ fun <Context : CommonBackendContext> makeIrFilePhase(
|
|||||||
preconditions: Set<Checker<IrFile>> = emptySet(),
|
preconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
postconditions: Set<Checker<IrFile>> = emptySet(),
|
postconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
stickyPostconditions: 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(
|
) = namedIrFilePhase(
|
||||||
name, description, prerequisite,
|
name, description, prerequisite,
|
||||||
preconditions = preconditions,
|
preconditions = preconditions,
|
||||||
@@ -184,7 +217,7 @@ fun <Context : CommonBackendContext> makeIrModulePhase(
|
|||||||
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
preconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
stickyPostconditions: 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(
|
) = namedIrModulePhase(
|
||||||
name, description, prerequisite,
|
name, description, prerequisite,
|
||||||
preconditions=preconditions,
|
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 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(
|
private fun makeJsModulePhase(
|
||||||
lowering: (JsIrBackendContext) -> FileLoweringPass,
|
lowering: (JsIrBackendContext) -> FileLoweringPass,
|
||||||
name: String,
|
name: String,
|
||||||
|
|||||||
@@ -37,6 +37,18 @@ private fun makePatchParentsPhase(number: Int) = namedIrFilePhase(
|
|||||||
nlevels = 0
|
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>(
|
private val stripTypeAliasDeclarationsPhase = makeIrFilePhase<CommonBackendContext>(
|
||||||
{ StripTypeAliasDeclarationsLowering() },
|
{ StripTypeAliasDeclarationsLowering() },
|
||||||
name = "StripTypeAliasDeclarations",
|
name = "StripTypeAliasDeclarations",
|
||||||
@@ -231,10 +243,12 @@ private val jvmFilePhases =
|
|||||||
val jvmPhases = namedIrModulePhase(
|
val jvmPhases = namedIrModulePhase(
|
||||||
name = "IrLowering",
|
name = "IrLowering",
|
||||||
description = "IR lowering",
|
description = "IR lowering",
|
||||||
lower = expectDeclarationsRemovingPhase then
|
lower = validateIrBeforeLowering then
|
||||||
|
expectDeclarationsRemovingPhase then
|
||||||
fileClassPhase then
|
fileClassPhase then
|
||||||
performByIrFile(lower = jvmFilePhases) then
|
performByIrFile(lower = jvmFilePhases) then
|
||||||
generateMultifileFacadesPhase
|
generateMultifileFacadesPhase then
|
||||||
|
validateIrAfterLowering
|
||||||
)
|
)
|
||||||
|
|
||||||
class JvmLower(val context: JvmBackendContext) {
|
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 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(
|
private fun makeWasmModulePhase(
|
||||||
lowering: (WasmBackendContext) -> FileLoweringPass,
|
lowering: (WasmBackendContext) -> FileLoweringPass,
|
||||||
name: String,
|
name: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user