[IR] Try to tun IrInterpreterKCallableNamePreprocessor only once

#KT-58923 Fixed
This commit is contained in:
Ivan Kylchik
2023-05-30 00:23:33 +02:00
committed by Space Team
parent a6a389933d
commit 0e2b348fa0
3 changed files with 19 additions and 5 deletions
@@ -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.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.transformer.preprocessForConstTransformer import org.jetbrains.kotlin.ir.interpreter.transformer.preprocessForConstTransformer
import org.jetbrains.kotlin.ir.interpreter.transformer.runConstOptimizations import org.jetbrains.kotlin.ir.interpreter.transformer.runConstOptimizations
import org.jetbrains.kotlin.ir.interpreter.transformer.transformConst
class ConstEvaluationLowering( class ConstEvaluationLowering(
val context: CommonBackendContext, val context: CommonBackendContext,
@@ -33,7 +34,9 @@ class ConstEvaluationLowering(
private val mode = EvaluationMode.ONLY_INTRINSIC_CONST private val mode = EvaluationMode.ONLY_INTRINSIC_CONST
override fun lower(irFile: IrFile) { 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 interpreter, mode, evaluatedConstTracker, inlineConstTracker, onWarning, onError, suppressErrors
) )
} }
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.ir.interpreter.preprocessor package org.jetbrains.kotlin.ir.interpreter.preprocessor
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.ir.IrBuiltIns import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass 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.IrGetValue
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.interpreter.property 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.ir.util.isSubclassOf
import org.jetbrains.kotlin.name.SpecialNames 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. // This code will be optimized but not completely turned into "ab" result.
class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor { class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor {
override fun visitCall(expression: IrCall, data: IrInterpreterPreprocessorData): IrElement { 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) if (!expression.isKCallableNameCall(data.irBuiltIns)) return super.visitCall(expression, data)
val callableReference = expression.dispatchReceiver as? IrCallableReference<*> ?: 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 // receiver is needed for bound callable reference
val receiver = callableReference.dispatchReceiver ?: callableReference.extensionReceiver ?: return expression 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.dispatchReceiver = null
callableReference.extensionReceiver = null callableReference.extensionReceiver = null
if (receiver is IrGetValue && receiver.symbol.owner.name == SpecialNames.THIS) return expression if (receiver is IrGetValue && receiver.symbol.owner.name == SpecialNames.THIS) return expression
@@ -61,12 +61,11 @@ fun IrFile.runConstOptimizations(
onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> }, onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
suppressExceptions: Boolean = false, suppressExceptions: Boolean = false,
) { ) {
val preprocessedFile = this.preprocessForConstTransformer(interpreter, mode)
val checker = IrInterpreterCommonChecker() val checker = IrInterpreterCommonChecker()
val irConstExpressionTransformer = IrConstAllTransformer( 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( fun IrFile.preprocessForConstTransformer(