From b58de2f8daa1445a825c5740510113a2e1409f1f Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Fri, 3 Feb 2023 14:49:00 +0100 Subject: [PATCH] Move `ConstEvaluationLowering` into folder for common lowerings #KT-56023 --- .../common/lower/ConstEvaluationLowering.kt | 35 +++++++++++++ .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 20 ++++++++ .../jvm/lower/ConstEvaluationLowering.kt | 51 ------------------- .../interpreter/checker/IrConstTransformer.kt | 8 +-- .../AbstractJvmBlackBoxCodegenTestBase.kt | 2 +- 5 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ConstEvaluationLowering.kt delete mode 100644 compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ConstEvaluationLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ConstEvaluationLowering.kt new file mode 100644 index 00000000000..62434ede57a --- /dev/null +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ConstEvaluationLowering.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.backend.common.lower + +import org.jetbrains.kotlin.backend.common.CommonBackendContext +import org.jetbrains.kotlin.backend.common.FileLoweringPass +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.expressions.IrErrorExpression +import org.jetbrains.kotlin.ir.interpreter.IrInterpreter +import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration +import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment +import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode +import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer + +class ConstEvaluationLowering( + val context: CommonBackendContext, + private val suppressErrors: Boolean = false, + private val onWarning: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, + private val onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, +) : FileLoweringPass { + val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true) + val interpreter = IrInterpreter(IrInterpreterEnvironment(context.irBuiltIns, configuration), emptyMap()) + + override fun lower(irFile: IrFile) { + val transformer = IrConstTransformer( + interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST, onWarning, onError, suppressErrors + ) + irFile.transformChildren(transformer, null) + } +} + diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index d903b86028d..467cd464072 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities import org.jetbrains.kotlin.name.NameUtils +import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors private var patchParentPhases = 0 @@ -315,6 +316,25 @@ internal val functionInliningPhase = makeIrModulePhase( ) ) +val constEvaluationPhase = makeIrModulePhase( + { + ConstEvaluationLowering( + it, + it.configuration.getBoolean(JVMConfigurationKeys.IGNORE_CONST_OPTIMIZATION_ERRORS), + { irFile, element, warning -> + it.ktDiagnosticReporter.at(element, irFile) + .report(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, warning.description) + }, + { irFile, element, error -> + it.ktDiagnosticReporter.at(element, irFile) + .report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description) + } + ) + }, + name = "ConstEvaluationLowering", + description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`" +) + private val jvmFilePhases = listOf( typeAliasAnnotationMethodsPhase, provisionalFunctionExpressionPhase, diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt deleted file mode 100644 index e5ab7ad9f89..00000000000 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/ConstEvaluationLowering.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.backend.jvm.lower - -import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase -import org.jetbrains.kotlin.backend.jvm.JvmBackendContext -import org.jetbrains.kotlin.config.JVMConfigurationKeys -import org.jetbrains.kotlin.ir.IrElement -import org.jetbrains.kotlin.ir.declarations.IrFile -import org.jetbrains.kotlin.ir.expressions.IrErrorExpression -import org.jetbrains.kotlin.ir.interpreter.IrInterpreter -import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration -import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment -import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode -import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer -import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors - -val constEvaluationPhase = makeIrModulePhase( - ::ConstEvaluationLowering, - name = "ConstEvaluationLowering", - description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`" -) - -// TODO make context common -class ConstEvaluationLowering(val context: JvmBackendContext) : FileLoweringPass { - val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true) - val interpreter = IrInterpreter(IrInterpreterEnvironment(context.irBuiltIns, configuration), emptyMap()) - - override fun lower(irFile: IrFile) { - fun onError(element: IrElement, error: IrErrorExpression) { - context.ktDiagnosticReporter.at(element, irFile) - .report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description) - } - - fun onWarning(element: IrElement, warning: IrErrorExpression) { - context.ktDiagnosticReporter.at(element, irFile) - .report(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, warning.description) - } - - val suppressErrors = context.configuration.getBoolean(JVMConfigurationKeys.IGNORE_CONST_OPTIMIZATION_ERRORS) - val transformer = IrConstTransformer( - interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST, ::onWarning, ::onError, suppressErrors - ) - irFile.transformChildren(transformer, null) - } -} - diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt index 25cef772342..dabcdf1979f 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt @@ -25,13 +25,13 @@ class IrConstTransformer( private val interpreter: IrInterpreter, private val irFile: IrFile, private val mode: EvaluationMode, - private val onWarning: (IrElement, IrErrorExpression) -> Unit = { _, _ -> }, - private val onError: (IrElement, IrErrorExpression) -> Unit = { _, _ -> }, + private val onWarning: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, + private val onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, private val suppressExceptions: Boolean = false, ) : IrElementTransformerVoid() { private fun IrExpression.warningIfError(original: IrExpression): IrExpression { if (this is IrErrorExpression) { - onWarning(original, this) + onWarning(irFile, original, this) return original } return this @@ -39,7 +39,7 @@ class IrConstTransformer( private fun IrExpression.reportIfError(original: IrExpression): IrExpression { if (this is IrErrorExpression) { - onError(original, this) + onError(irFile, original, this) return when (mode) { // need to pass any const value to be able to get some bytecode and then report error EvaluationMode.ONLY_INTRINSIC_CONST -> IrConstImpl.constNull(startOffset, endOffset, type) diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/codegen/AbstractJvmBlackBoxCodegenTestBase.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/codegen/AbstractJvmBlackBoxCodegenTestBase.kt index e9a6dee3ed4..49636d28d91 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/codegen/AbstractJvmBlackBoxCodegenTestBase.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/codegen/AbstractJvmBlackBoxCodegenTestBase.kt @@ -5,7 +5,7 @@ package org.jetbrains.kotlin.test.runners.codegen -import org.jetbrains.kotlin.backend.jvm.lower.constEvaluationPhase +import org.jetbrains.kotlin.backend.jvm.constEvaluationPhase import org.jetbrains.kotlin.config.JvmTarget import org.jetbrains.kotlin.test.Constructor import org.jetbrains.kotlin.test.TargetBackend