From 41ab4727d46a9a8a36a67c7f677dcb506acde59f Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Wed, 15 Feb 2017 16:46:08 +0700 Subject: [PATCH] Inlining lambda with parameters --- .../kotlin/backend/konan/KonanLower.kt | 2 +- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 7 +- .../backend/konan/lower/FunctionInlining.kt | 206 ++++++++++++------ 3 files changed, 141 insertions(+), 74 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt index f1fdb17c8e7..4e6a17c3ea0 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -22,7 +22,7 @@ internal class KonanLower(val context: Context) { StringConcatenationLowering(context).lower(irFile) } phaser.phase(KonanPhase.LOWER_INLINE) { - //FunctionInlining(context).inline(irFile) + FunctionInlining(context).inline(irFile) } phaser.phase(KonanPhase.LOWER_ENUMS) { EnumClassLowering(context).run(irFile) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 21cb5433359..af6b579641e 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -489,8 +489,11 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid context.log("visitFunction : ${ir2string(declaration)}") val body = declaration.body - if (declaration.descriptor.modality == Modality.ABSTRACT || declaration.descriptor.isExternal || body == null) - return + if (declaration.descriptor.modality == Modality.ABSTRACT) return + if (body == null) return + if (declaration.descriptor.isExternal) return + if (declaration.descriptor.name.toString().contains("foo") && + declaration.descriptor.isInline) return codegen.prologue(declaration.descriptor) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt index 6fc298c39eb..5a9ddfee787 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt @@ -6,6 +6,7 @@ import org.jetbrains.kotlin.backend.konan.ir.IrInlineFunctionBody import org.jetbrains.kotlin.backend.konan.ir.getArguments import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.ParameterDescriptor +import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.builders.Scope @@ -14,10 +15,13 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrContainerExpressionBase import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl +import org.jetbrains.kotlin.ir.expressions.impl.IrMemberAccessExpressionBase +import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl import org.jetbrains.kotlin.ir.util.DeepCopyIrTree import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.types.KotlinType + //-----------------------------------------------------------------------------// internal class FunctionInlining(val context: Context): IrElementTransformerVoid() { @@ -26,9 +30,90 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( //-------------------------------------------------------------------------// - fun isLambda(value: IrExpression) : Boolean { - if (value !is IrContainerExpressionBase) return false - if (value.origin != IrStatementOrigin.LAMBDA) return false + fun isLambdaExpression(expression: IrExpression) : Boolean { + if (expression !is IrContainerExpressionBase) return false + if (expression.origin != IrStatementOrigin.LAMBDA) return false + return true + } + + //-------------------------------------------------------------------------// + + fun evaluateParameters(irCall: IrCall, + statements: MutableList): List> { + + val scope = Scope(irCall.descriptor as FunctionDescriptor) + val parametersOld = irCall.getArguments() // Create map call_site_argument -> inline_function_parameter. + val parametersNew = parametersOld.map { + val argument = it.first + val parameter = it.second + if (parameter is IrGetValue) return@map it // Parameter is already GetValue - nothing to evaluate. + if (parameter is IrConst<*>) return@map it // Parameter is constant - nothing to evaluate. + if (isLambdaExpression(parameter)) return@map it // Parameter is lambda - will be inlined. + + val newVar = scope.createTemporaryVariable(parameter, "inline", false) // Create new variable and init it with the parameter expression. + statements.add(0, newVar) // Add initialization of the new variable in statement list. + + val getVal = IrGetValueImpl(0, 0, newVar.descriptor) // Create new IR element representing access the new variable. + argument to getVal // Parameter will be replaced with the new variable. + } + return parametersNew + } + + //-------------------------------------------------------------------------// + + fun inlineFunction(irCall: IrCall): IrExpression { + + val functionDescriptor = irCall.descriptor as FunctionDescriptor + val functionDeclaration = context.ir.originalModuleIndex + .functions[functionDescriptor.original] // Get FunctionDeclaration by FunctionDescriptor. + + if (functionDeclaration == null) return super.visitCall(irCall) // Function is declared in another module. + + val copyFuncDeclaration = functionDeclaration.accept(DeepCopyIrTree(), // Create copy of the function. + null) as IrFunction + + val startOffset = copyFuncDeclaration.startOffset + val endOffset = copyFuncDeclaration.endOffset + val returnType = copyFuncDeclaration.descriptor.returnType!! + val blockBody = copyFuncDeclaration.body!! as IrBlockBody + val statements = blockBody.statements + val parameters = evaluateParameters(irCall, statements) // Evaluate parameters representing expression. + val inlineBody = IrInlineFunctionBody(startOffset, endOffset, returnType, null, statements) + + val lambdaInliner = LambdaInliner(parameters) + inlineBody.accept(lambdaInliner, null) + val transformer = ParametersTransformer(parameters, irCall) + inlineBody.accept(transformer, null) // Replace parameters with expression. + return inlineBody + } + + //-------------------------------------------------------------------------// + + override fun visitCall(expression: IrCall): IrExpression { + val functionDescriptor = expression.descriptor as FunctionDescriptor + if (!functionDescriptor.name.asString().contains("foo")) return super.visitCall(expression) + + if (functionDescriptor.isInline) return inlineFunction(expression) // Return newly created IrInlineBody instead of IrCall. + return super.visitCall(expression) + } + + //-------------------------------------------------------------------------// + + override fun visitElement(element: IrElement) = element.accept(this, null) +} + +//-----------------------------------------------------------------------------// + +internal class LambdaInliner(val parameterToArgument: + List>): IrElementTransformerVoid() { + + override fun visitElement(element: IrElement) = element.accept(this, null) + + //-------------------------------------------------------------------------// + + fun isLambdaCall(irCall: IrCall) : Boolean { + if (!(irCall.descriptor as FunctionDescriptor).isFunctionInvoke) return false // If it is lambda call. + if (irCall.dispatchReceiver !is IrGetValue) return false // Do not process such dispatch receiver. return true } @@ -51,95 +136,74 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid( //-------------------------------------------------------------------------// - fun evaluateParameters(irCall: IrCall, - statements: MutableList): List> { + fun getLambdaFunction(lambdaArgument: IrBlock): IrFunction { + val statements = (lambdaArgument as IrContainerExpressionBase).statements + val lambdaFunction = statements[0] as IrFunction + return lambdaFunction + } - val scope = Scope(irCall.descriptor as FunctionDescriptor) - val parametersOld = irCall.getArguments() // Create map inline_function_parameter -> containing_function_expression. - val parametersNew = parametersOld.map { - val parameter = it.first - val expression = it.second - if (expression is IrGetValue) return@map it // There is nothing to evaluate. - if (isLambda(expression)) { // The expression is lambda. - val inlineFunctionBody = inlineLambda(expression) // Create IrInlineFunctionBody to replace this parameter. - return@map parameter to inlineFunctionBody - } + //-------------------------------------------------------------------------// - val newVar = scope.createTemporaryVariable(expression, "inline", false) // Create new variable and init it with the expression. - statements.add(0, newVar) // Add initialization of the new variable in statement list. + fun inlineLambda(irCall: IrCall): IrExpression { - val getVal = IrGetValueImpl(0, 0, newVar.descriptor) // Create new IR element representing access the new variable. - parameter to getVal // Parameter will be replaced with the new variable. + val dispatchReceiver = irCall.dispatchReceiver as IrGetValue // + val parameterArgument = parameterToArgument.find { // Find expression to replace this parameter. + it.first == dispatchReceiver.descriptor } - return parametersNew - } + if (parameterArgument == null) return super.visitCall(irCall) // It is not function parameter. - //-------------------------------------------------------------------------// + val lambdaArgument = parameterArgument.second + val lambdaFunction = getLambdaFunction(lambdaArgument as IrBlock) - fun inlineFunction(irCall: IrCall): IrBlock { + val parameters = lambdaFunction.descriptor.valueParameters // Lambda function parameters + val res = parameters.map { + val argument = irCall.getValueArgument(it.index) + val parameter = it + parameter to argument!! + } - val functionDescriptor = irCall.descriptor as FunctionDescriptor - val functionDeclaration = context.ir.originalModuleIndex - .functions[functionDescriptor] // Get FunctionDeclaration by FunctionDescriptor. - val copyFuncDeclaration = functionDeclaration!!.accept(DeepCopyIrTree(), null) as IrFunction // Create copy of the function. + val lambdaStatements = getLambdaStatements(lambdaArgument) + val lambdaReturnType = getLambdaReturnType(lambdaArgument) + val inlineBody = IrInlineFunctionBody(0, 0, lambdaReturnType, null, lambdaStatements) - val startOffset = copyFuncDeclaration.startOffset - val endOffset = copyFuncDeclaration.endOffset - val returnType = copyFuncDeclaration.descriptor.returnType!! - val inlineBody = copyFuncDeclaration.body!! as IrBlockBody - val statements = inlineBody.statements - val parameters = evaluateParameters(irCall, statements) // Evaluate parameters representing expression. - val irBlock = IrInlineFunctionBody(startOffset, endOffset, returnType, null, statements) + val transformer = ParametersTransformer(res, irCall) + inlineBody.accept(transformer, null) // Replace parameters with expression. - val transformer = ParametersTransformer(parameters) - irBlock.accept(transformer, null) // Replace parameters with expression. - return irBlock - } - - //-------------------------------------------------------------------------// - - fun inlineLambda(value: IrExpression): IrBlock { - - val lambdaStatements = getLambdaStatements(value) - val lambdaReturnType = getLambdaReturnType(value) - val irBlock = IrInlineFunctionBody(0, 0, lambdaReturnType, null, lambdaStatements) - - return irBlock + return inlineBody // Replace call site with InlineFunctionBody. } //-------------------------------------------------------------------------// override fun visitCall(expression: IrCall): IrExpression { - val functionDescriptor = expression.descriptor as FunctionDescriptor - if (!functionDescriptor.isInline) return super.visitCall(expression) // Function is not to be inlined - do nothing. - - val functionDeclaration = context.ir.originalModuleIndex.functions[functionDescriptor] // Get FunctionDeclaration by FunctionDescriptor. - if (functionDeclaration == null) return super.visitCall(expression) // Function is declared in another module. - return inlineFunction(expression) // Return newly created IrBlock instead of IrCall. + if (!isLambdaCall(expression)) return super.visitCall(expression) // If call it is not lambda call - do nothing + return inlineLambda(expression) } - - //-------------------------------------------------------------------------// - - override fun visitElement(element: IrElement) = element.accept(this, null) } //-----------------------------------------------------------------------------// -internal class ParametersTransformer(val parameterToExpression: List >): IrElementTransformerVoid() { +internal class ParametersTransformer(val parameterToArgument: List>, + val callSite: IrCall): IrElementTransformerVoid() { override fun visitElement(element: IrElement) = element.accept(this, null) //-------------------------------------------------------------------------// - override fun visitCall(expression: IrCall): IrExpression { - if ((expression.descriptor as FunctionDescriptor).isFunctionInvoke) { // If it is lambda call. - if (expression.dispatchReceiver !is IrGetValue) super.visitCall(expression) // Do not process such dispatch receiver. - val getValue = expression.dispatchReceiver as IrGetValue // - val parameterExpression = parameterToExpression.find { it.first == getValue.descriptor } // Find expression to replace this parameter. - if (parameterExpression == null) super.visitCall(expression) // It is not function parameter. - return parameterExpression!!.second // Replace call site with InlineFunctionBody. - } - return super.visitCall(expression) // Function is declared in another module. + override fun visitTypeOperator(oldExpression: IrTypeOperatorCall): IrExpression { + + val expression = super.visitTypeOperator(oldExpression) as IrTypeOperatorCall + + val typeArgsMap = (callSite as IrMemberAccessExpressionBase).typeArguments!! + val typeConstructor = expression.typeOperand.constructor + val typeOld = typeConstructor.declarationDescriptor as TypeParameterDescriptorImpl + val typeNew = typeArgsMap[typeOld]!! + val startOffset = expression.startOffset + val endOffset = expression.endOffset + val type = expression.type + val operator = expression.operator + val argument = expression.argument + + return IrTypeOperatorCallImpl(startOffset, endOffset, type, operator, typeNew, argument) } //-------------------------------------------------------------------------// @@ -149,9 +213,9 @@ internal class ParametersTransformer(val parameterToExpression: List