From 11a3085659aee6ffad8a3cdc340434f8e0dca6bc Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 10 Sep 2020 12:35:49 +0200 Subject: [PATCH] Adapt to IR change to allow setting of parameters for default stubs. (cherry picked from commit 68034d3c816387b4000c1b298dbea5b800643fe7) --- .../common/AbstractValueUsageTransformer.kt | 7 +++-- .../KonanSharedVariablesManager.kt | 4 +-- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 20 +++++++------- .../backend/konan/llvm/VariableManager.kt | 2 +- .../kotlin/backend/konan/lower/Autoboxing.kt | 7 ++++- .../backend/konan/lower/BridgesBuilding.kt | 3 ++- .../konan/lower/EnumConstructorsLowering.kt | 24 +++++++++++++++++ .../lower/NativeSuspendFunctionLowering.kt | 15 ++++++++--- .../backend/konan/lower/VarargLowering.kt | 2 +- .../backend/konan/optimizations/DFGBuilder.kt | 26 +++++++++---------- .../org/jetbrains/kotlin/ir/util/IrUtils2.kt | 2 +- 11 files changed, 76 insertions(+), 36 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt index 5eba6352049..86a39a7f153 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/AbstractValueUsageTransformer.kt @@ -61,6 +61,9 @@ internal abstract class AbstractValueUsageTransformer( private fun IrExpression.useForVariable(variable: IrVariable): IrExpression = this.useAsValue(variable) + private fun IrExpression.useForValue(value: IrValueDeclaration) = + this.useAsValue(value) + private fun IrExpression.useForField(field: IrField): IrExpression = this.useAs(field.type) @@ -144,10 +147,10 @@ internal abstract class AbstractValueUsageTransformer( return expression } - override fun visitSetVariable(expression: IrSetVariable): IrExpression { + override fun visitSetValue(expression: IrSetValue): IrExpression { expression.transformChildrenVoid(this) - expression.value = expression.value.useForVariable(expression.symbol.owner) + expression.value = expression.value.useForValue(expression.symbol.owner) return expression } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/KonanSharedVariablesManager.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/KonanSharedVariablesManager.kt index 6f5a0b47427..629269b8d49 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/KonanSharedVariablesManager.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/KonanSharedVariablesManager.kt @@ -14,7 +14,7 @@ import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl import org.jetbrains.kotlin.ir.descriptors.WrappedVariableDescriptor import org.jetbrains.kotlin.ir.expressions.IrGetValue -import org.jetbrains.kotlin.ir.expressions.IrSetVariable +import org.jetbrains.kotlin.ir.expressions.IrSetValue import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl @@ -87,7 +87,7 @@ internal class KonanSharedVariablesManager(val context: KonanBackendContext) : S ) } - override fun setSharedValue(sharedVariableSymbol: IrVariableSymbol, originalSet: IrSetVariable) = + override fun setSharedValue(sharedVariableSymbol: IrVariableSymbol, originalSet: IrSetValue) = IrCallImpl(originalSet.startOffset, originalSet.endOffset, context.irBuiltIns.unitType, elementProperty.setter!!.symbol).apply { dispatchReceiver = IrGetValueImpl( 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 fb0ff55d942..b0a179edebc 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 @@ -130,9 +130,9 @@ internal interface CodeContext { fun genDeclareVariable(variable: IrVariable, value: LLVMValueRef?, variableLocation: VariableDebugLocation?): Int /** - * @return index of variable declared before, or -1 if no such variable has been declared yet. + * @return index of value declared before, or -1 if no such variable has been declared yet. */ - fun getDeclaredVariable(variable: IrVariable): Int + fun getDeclaredValue(value: IrValueDeclaration): Int /** * Generates the code to obtain a value available in this context. @@ -244,7 +244,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map return evaluateInstanceInitializerCall(value) is IrGetValue -> return evaluateGetValue (value) - is IrSetVariable -> return evaluateSetVariable (value) + is IrSetValue -> return evaluateSetValue (value) is IrGetField -> return evaluateGetField (value) is IrSetField -> return evaluateSetField (value) is IrConst<*> -> return evaluateConst (value) @@ -1267,10 +1267,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map this.type } - return this.adaptIfNecessary(actualType, type) } @@ -469,6 +468,12 @@ private class InlineClassTransformer(private val context: Context) : IrBuildingT return expression } + override fun visitSetValue(expression: IrSetValue): IrExpression { + expression.transformChildrenVoid() + parameterMapping[expression.symbol]?.let { return irSet(it.symbol, expression.value) } + return expression + } + override fun visitReturn(expression: IrReturn): IrExpression { expression.transformChildrenVoid() if (expression.returnTargetSymbol == irConstructor.symbol) { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/BridgesBuilding.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/BridgesBuilding.kt index 3970d534a30..2ce7554f9f8 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/BridgesBuilding.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/BridgesBuilding.kt @@ -98,7 +98,8 @@ internal class WorkersBridgesBuilding(val context: Context) : DeclarationContain type = context.irBuiltIns.anyNType, varargElementType = null, isCrossinline = arg.isCrossinline, - isNoinline = arg.isNoinline + isNoinline = arg.isNoinline, + isAssignable = arg.isAssignable ).apply { it.bind(this) } } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumConstructorsLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumConstructorsLowering.kt index b659159ab00..3933b624098 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumConstructorsLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/EnumConstructorsLowering.kt @@ -351,6 +351,14 @@ internal class EnumConstructorsLowering(val context: Context) : ClassLoweringPas loweredParameter.symbol, expression.origin) } } + + override fun visitSetValue(expression: IrSetValue): IrExpression { + expression.transformChildrenVoid() + return loweredEnumConstructorParameters[expression.symbol.owner]?.let { + IrSetValueImpl(expression.startOffset, expression.endOffset, it.type, + it.symbol, expression.value, expression.origin) + } ?: expression + } } } } @@ -375,4 +383,20 @@ private class ParameterMapper(superConstructor: IrConstructor, } return expression } + + override fun visitSetValue(expression: IrSetValue): IrExpression { + expression.transformChildrenVoid() + val superParameter = expression.symbol.owner as? IrValueParameter ?: return expression + if (valueParameters.contains(superParameter)) { + val index = if (useLoweredIndex) superParameter.loweredIndex else superParameter.index + val parameter = constructor.valueParameters[index] + return IrSetValueImpl( + expression.startOffset, expression.endOffset, + parameter.type, + parameter.symbol, + expression.value, + expression.origin) + } + return expression + } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt index 26bc65bda48..0e352602ff5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt @@ -18,7 +18,7 @@ import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedVariableDescriptor import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrSetVariableImpl +import org.jetbrains.kotlin.ir.expressions.impl.IrSetValueImpl import org.jetbrains.kotlin.ir.expressions.impl.IrSuspendableExpressionImpl import org.jetbrains.kotlin.ir.expressions.impl.IrSuspensionPointImpl import org.jetbrains.kotlin.ir.symbols.IrClassSymbol @@ -123,6 +123,13 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc return irGetField(irGet(thisReceiver), capturedValue) } + override fun visitSetValue(expression: IrSetValue): IrExpression { + expression.transformChildrenVoid(this) + val capturedValue = argumentToPropertiesMap[expression.symbol.owner] + ?: return expression + return irSetField(irGet(thisReceiver), capturedValue, expression.value) + } + // Save/restore state at suspension points. override fun visitExpression(expression: IrExpression): IrExpression { expression.transformChildrenVoid(this) @@ -202,13 +209,13 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc origin = expression.origin) } - override fun visitSetVariable(expression: IrSetVariable): IrExpression { + override fun visitSetValue(expression: IrSetValue): IrExpression { expression.transformChildrenVoid(this) val newVariable = variablesMap[expression.symbol.owner] ?: return expression - return IrSetVariableImpl( + return IrSetValueImpl( startOffset = expression.startOffset, endOffset = expression.endOffset, type = context.irBuiltIns.unitType, @@ -384,7 +391,7 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc result = irBlock(startOffset, endOffset) { if (!calledSaveState) +irCall(saveState) - +irSetVar(suspendResult.symbol, suspendCall) + +irSet(suspendResult.symbol, suspendCall) +irReturnIfSuspended(suspendResult) +irGet(suspendResult) }, diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt index e239c429a64..da612c61517 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/VarargLowering.kt @@ -165,7 +165,7 @@ internal class VarargInjectionLowering constructor(val context: KonanBackendCont } private fun IrBuilderWithScope.incrementVariable(variable: IrVariable, value: IrExpression): IrExpression { - return irSetVar(variable.symbol, intPlus().apply { + return irSet(variable.symbol, intPlus().apply { dispatchReceiver = irGet(variable) putValueArgument(0, value) }) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt index 514502c0833..b92fe5948de 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt @@ -56,16 +56,16 @@ private fun IrTypeOperator.callsInstanceOf() = private class VariableValues { data class Variable(val loop: IrLoop?, val values: MutableSet) - val elementData = HashMap() + val elementData = HashMap() - fun addEmpty(variable: IrVariable, loop: IrLoop?) { + fun addEmpty(variable: IrValueDeclaration, loop: IrLoop?) { elementData[variable] = Variable(loop, mutableSetOf()) } - fun add(variable: IrVariable, element: IrExpression) = + fun add(variable: IrValueDeclaration, element: IrExpression) = elementData[variable]?.values?.add(element) - private fun add(variable: IrVariable, elements: Set) = + private fun add(variable: IrValueDeclaration, elements: Set) = elementData[variable]?.values?.addAll(elements) fun computeClosure() { @@ -75,14 +75,14 @@ private class VariableValues { } // Computes closure of all possible values for given variable. - private fun computeValueClosure(value: IrVariable): Set { + private fun computeValueClosure(value: IrValueDeclaration): Set { val result = mutableSetOf() - val seen = mutableSetOf() + val seen = mutableSetOf() dfs(value, seen, result) return result } - private fun dfs(value: IrVariable, seen: MutableSet, result: MutableSet) { + private fun dfs(value: IrValueDeclaration, seen: MutableSet, result: MutableSet) { seen += value val elements = elementData[value]?.values ?: return for (element in elements) { @@ -313,7 +313,7 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag element.acceptChildrenVoid(this) } - private fun assignVariable(variable: IrVariable, value: IrExpression) { + private fun assignValue(variable: IrValueDeclaration, value: IrExpression) { expressionValuesExtractor.forEachValue(value) { variableValues.add(variable, it) } @@ -410,15 +410,15 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag super.visitCatch(aCatch) } - override fun visitSetVariable(expression: IrSetVariable) { - super.visitSetVariable(expression) - assignVariable(expression.symbol.owner, expression.value) + override fun visitSetValue(expression: IrSetValue) { + super.visitSetValue(expression) + assignValue(expression.symbol.owner, expression.value) } override fun visitVariable(declaration: IrVariable) { variableValues.addEmpty(declaration, currentLoop) super.visitVariable(declaration) - declaration.initializer?.let { assignVariable(declaration, it) } + declaration.initializer?.let { assignValue(declaration, it) } } } @@ -473,7 +473,7 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag ?: error("Function ${declaration.descriptor} has no continuation parameter") private val nodes = mutableMapOf>() - private val variables = mutableMapOf>() + private val variables = mutableMapOf>() private val expressionsScopes = mutableMapOf() fun build(): DataFlowIR.Function { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt index 0772f394652..8ccb34d91b7 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt @@ -259,7 +259,7 @@ fun IrBuilderWithScope.irCallOp( } fun IrBuilderWithScope.irSetVar(variable: IrVariable, value: IrExpression) = - irSetVar(variable.symbol, value) + irSet(variable.symbol, value) fun IrBuilderWithScope.irCatch(type: IrType) = IrCatchImpl(