From 0e2b348fa0f4caa5fb2beb8a5e2862fa31133898 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Tue, 30 May 2023 00:23:33 +0200 Subject: [PATCH] [IR] Try to tun `IrInterpreterKCallableNamePreprocessor` only once #KT-58923 Fixed --- .../common/lower/ConstEvaluationLowering.kt | 5 ++++- .../IrInterpreterKCallableNamePreprocessor.kt | 14 +++++++++++++- .../interpreter/transformer/IrConstTransformer.kt | 5 ++--- 3 files changed, 19 insertions(+), 5 deletions(-) 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 index 81f1427b81d..09b1eec7688 100644 --- 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 @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode import org.jetbrains.kotlin.ir.interpreter.transformer.preprocessForConstTransformer import org.jetbrains.kotlin.ir.interpreter.transformer.runConstOptimizations +import org.jetbrains.kotlin.ir.interpreter.transformer.transformConst class ConstEvaluationLowering( val context: CommonBackendContext, @@ -33,7 +34,9 @@ class ConstEvaluationLowering( private val mode = EvaluationMode.ONLY_INTRINSIC_CONST override fun lower(irFile: IrFile) { - irFile.runConstOptimizations( + val useFir = context.configuration[CommonConfigurationKeys.USE_FIR] == true + val preprocessedFile = if (useFir) irFile else irFile.preprocessForConstTransformer(interpreter, mode) + preprocessedFile.runConstOptimizations( interpreter, mode, evaluatedConstTracker, inlineConstTracker, onWarning, onError, suppressErrors ) } diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt index 876c7977583..3a528e8d93a 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/preprocessor/IrInterpreterKCallableNamePreprocessor.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.ir.interpreter.preprocessor +import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.ir.IrBuiltIns import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.IrClass @@ -16,6 +17,11 @@ import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue import org.jetbrains.kotlin.ir.expressions.IrGetValue import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl import org.jetbrains.kotlin.ir.interpreter.property +import org.jetbrains.kotlin.ir.types.IrSimpleType +import org.jetbrains.kotlin.ir.types.classFqName +import org.jetbrains.kotlin.ir.types.typeOrNull +import org.jetbrains.kotlin.ir.types.typeWith +import org.jetbrains.kotlin.ir.util.isKFunction import org.jetbrains.kotlin.ir.util.isSubclassOf import org.jetbrains.kotlin.name.SpecialNames @@ -23,7 +29,6 @@ import org.jetbrains.kotlin.name.SpecialNames // This code will be optimized but not completely turned into "ab" result. class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor { override fun visitCall(expression: IrCall, data: IrInterpreterPreprocessorData): IrElement { - if (!data.mode.canEvaluateFunction(expression.symbol.owner)) return super.visitCall(expression, data) if (!expression.isKCallableNameCall(data.irBuiltIns)) return super.visitCall(expression, data) val callableReference = expression.dispatchReceiver as? IrCallableReference<*> ?: return super.visitCall(expression, data) @@ -31,6 +36,13 @@ class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor { // receiver is needed for bound callable reference val receiver = callableReference.dispatchReceiver ?: callableReference.extensionReceiver ?: return expression + val typeArguments = (callableReference.type as IrSimpleType).arguments.map { it.typeOrNull!! } + if (callableReference.type.isKFunction()) { + val kFunction = data.irBuiltIns.kFunctionN(typeArguments.size) + val newType = kFunction.typeWith(receiver.type, *typeArguments.toTypedArray()) + callableReference.type = newType + } + callableReference.dispatchReceiver = null callableReference.extensionReceiver = null if (receiver is IrGetValue && receiver.symbol.owner.name == SpecialNames.THIS) return expression diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstTransformer.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstTransformer.kt index 0a432f9d53e..0cfd8f21171 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstTransformer.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/transformer/IrConstTransformer.kt @@ -61,12 +61,11 @@ fun IrFile.runConstOptimizations( onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, suppressExceptions: Boolean = false, ) { - val preprocessedFile = this.preprocessForConstTransformer(interpreter, mode) val checker = IrInterpreterCommonChecker() val irConstExpressionTransformer = IrConstAllTransformer( - interpreter, preprocessedFile, mode, checker, evaluatedConstTracker, inlineConstTracker, onWarning, onError, suppressExceptions + interpreter, this, mode, checker, evaluatedConstTracker, inlineConstTracker, onWarning, onError, suppressExceptions ) - preprocessedFile.transform(irConstExpressionTransformer, null) + this.transform(irConstExpressionTransformer, null) } fun IrFile.preprocessForConstTransformer(