[IR] Escape removing extra boxes if no box was are in the function
Signed-off-by: Evgeniy.Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com> #KT-1179
This commit is contained in:
committed by
Space Team
parent
962265531e
commit
8c3fa6f09f
+22
-3
@@ -60,6 +60,8 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val boxUsageGenerated = mutableSetOf<IrDeclaration>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class is used to get replacing expression and MFVC instance if present for the given old value declaration.
|
* The class is used to get replacing expression and MFVC instance if present for the given old value declaration.
|
||||||
*/
|
*/
|
||||||
@@ -87,10 +89,14 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
oldValueSymbol2NewValueSymbol[expression.symbol]?.let { return irGet(it.owner) }
|
oldValueSymbol2NewValueSymbol[expression.symbol]?.let { return irGet(it.owner) }
|
||||||
val instance = oldSymbol2MfvcNodeInstance[expression.symbol] ?: return null
|
val instance = oldSymbol2MfvcNodeInstance[expression.symbol] ?: return null
|
||||||
val res = instance.makeGetterExpression(this)
|
val res = instance.makeGetterExpression(this)
|
||||||
|
boxUsageGenerated.add(irCurrentScope)
|
||||||
expression2MfvcNodeInstanceAccessor[res] = MfvcNodeInstanceAccessor.Getter(instance)
|
expression2MfvcNodeInstanceAccessor[res] = MfvcNodeInstanceAccessor.Getter(instance)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val irCurrentScope
|
||||||
|
get() = currentScope!!.irElement as IrDeclaration
|
||||||
|
|
||||||
private fun splitExpressions(expressions: List<IrExpression>): Pair<List<IrExpression>, List<IrExpression>> {
|
private fun splitExpressions(expressions: List<IrExpression>): Pair<List<IrExpression>, List<IrExpression>> {
|
||||||
val repeatable = expressions.takeLastWhile { it.isRepeatableGetter() }
|
val repeatable = expressions.takeLastWhile { it.isRepeatableGetter() }
|
||||||
return expressions.subList(0, expressions.size - repeatable.size) to repeatable
|
return expressions.subList(0, expressions.size - repeatable.size) to repeatable
|
||||||
@@ -135,6 +141,7 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
val instance: ReceiverBasedMfvcNodeInstance =
|
val instance: ReceiverBasedMfvcNodeInstance =
|
||||||
node.createInstanceFromBox(this, typeArguments, expression.receiver, AccessType.AlwaysPrivate, ::variablesSaver)
|
node.createInstanceFromBox(this, typeArguments, expression.receiver, AccessType.AlwaysPrivate, ::variablesSaver)
|
||||||
val getterExpression = instance.makeGetterExpression(this)
|
val getterExpression = instance.makeGetterExpression(this)
|
||||||
|
boxUsageGenerated.add(irCurrentScope)
|
||||||
expression2MfvcNodeInstanceAccessor[getterExpression] = MfvcNodeInstanceAccessor.Getter(instance)
|
expression2MfvcNodeInstanceAccessor[getterExpression] = MfvcNodeInstanceAccessor.Getter(instance)
|
||||||
+getterExpression
|
+getterExpression
|
||||||
return getterExpression
|
return getterExpression
|
||||||
@@ -170,6 +177,7 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
val instance: ReceiverBasedMfvcNodeInstance =
|
val instance: ReceiverBasedMfvcNodeInstance =
|
||||||
node.createInstanceFromBox(this, typeArguments, dispatchReceiver, accessType, ::variablesSaver)
|
node.createInstanceFromBox(this, typeArguments, dispatchReceiver, accessType, ::variablesSaver)
|
||||||
val getterExpression = instance.makeGetterExpression(this)
|
val getterExpression = instance.makeGetterExpression(this)
|
||||||
|
boxUsageGenerated.add(irCurrentScope)
|
||||||
expression2MfvcNodeInstanceAccessor[getterExpression] = MfvcNodeInstanceAccessor.Getter(instance)
|
expression2MfvcNodeInstanceAccessor[getterExpression] = MfvcNodeInstanceAccessor.Getter(instance)
|
||||||
+getterExpression
|
+getterExpression
|
||||||
return getterExpression
|
return getterExpression
|
||||||
@@ -217,8 +225,10 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
fun IrExpression.get(scope: IrBuilderWithScope, name: Name): IrExpression? = scope.handleSavedExpression(this) { accessor ->
|
fun IrExpression.get(scope: IrBuilderWithScope, name: Name): IrExpression? = scope.handleSavedExpression(this) { accessor ->
|
||||||
val newAccessor = accessor[name] ?: return@handleSavedExpression null
|
val newAccessor = accessor[name] ?: return@handleSavedExpression null
|
||||||
val expression = when (newAccessor) {
|
val expression = when (newAccessor) {
|
||||||
is MfvcNodeInstanceAccessor.Getter -> newAccessor.instance.makeGetterExpression(scope)
|
|
||||||
is MfvcNodeInstanceAccessor.Setter -> newAccessor.instance.makeSetterExpressions(scope, newAccessor.values)
|
is MfvcNodeInstanceAccessor.Setter -> newAccessor.instance.makeSetterExpressions(scope, newAccessor.values)
|
||||||
|
is MfvcNodeInstanceAccessor.Getter -> newAccessor.instance.makeGetterExpression(scope).also {
|
||||||
|
boxUsageGenerated.add(irCurrentScope)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
expression2MfvcNodeInstanceAccessor[expression] = newAccessor
|
expression2MfvcNodeInstanceAccessor[expression] = newAccessor
|
||||||
expression
|
expression
|
||||||
@@ -279,11 +289,19 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
when (replacingDeclaration) {
|
when (replacingDeclaration) {
|
||||||
is IrFunction -> replacingDeclaration.body = replacingDeclaration.body?.makeBodyWithAddedVariables(
|
is IrFunction -> replacingDeclaration.body = replacingDeclaration.body?.makeBodyWithAddedVariables(
|
||||||
context, variablesToAdd[replacingDeclaration] ?: emptySet(), replacingDeclaration.symbol
|
context, variablesToAdd[replacingDeclaration] ?: emptySet(), replacingDeclaration.symbol
|
||||||
)?.apply { removeAllExtraBoxes() }
|
)?.apply {
|
||||||
|
if (replacingDeclaration in boxUsageGenerated) {
|
||||||
|
removeAllExtraBoxes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
is IrAnonymousInitializer -> replacingDeclaration.body = replacingDeclaration.body.makeBodyWithAddedVariables(
|
is IrAnonymousInitializer -> replacingDeclaration.body = replacingDeclaration.body.makeBodyWithAddedVariables(
|
||||||
context, variablesToAdd[replacingDeclaration.parent] ?: emptySet(), replacingDeclaration.symbol
|
context, variablesToAdd[replacingDeclaration.parent] ?: emptySet(), replacingDeclaration.symbol
|
||||||
).apply { removeAllExtraBoxes() } as IrBlockBody
|
).apply {
|
||||||
|
if (replacingDeclaration in boxUsageGenerated) {
|
||||||
|
removeAllExtraBoxes()
|
||||||
|
}
|
||||||
|
} as IrBlockBody
|
||||||
|
|
||||||
else -> Unit
|
else -> Unit
|
||||||
}
|
}
|
||||||
@@ -726,6 +744,7 @@ private class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : JvmV
|
|||||||
)
|
)
|
||||||
flattenExpressionTo(expression, instance)
|
flattenExpressionTo(expression, instance)
|
||||||
val getterExpression = instance.makeGetterExpression(this)
|
val getterExpression = instance.makeGetterExpression(this)
|
||||||
|
boxUsageGenerated.add(currentScope)
|
||||||
valueDeclarationsRemapper.registerReplacement(getterExpression, instance)
|
valueDeclarationsRemapper.registerReplacement(getterExpression, instance)
|
||||||
+getterExpression
|
+getterExpression
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,13 @@ fun testIgnoredBoxed() {
|
|||||||
returnBoxed()
|
returnBoxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object Init {
|
||||||
|
init {
|
||||||
|
DPoint(1.0, 2.0)
|
||||||
|
DPoint(1.0, 2.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1 testFlattened2Boxed\(\)V(\n {3}.*)*((\n {3}.*box-impl .*)(\n {3}.*)*){1}
|
// 1 testFlattened2Boxed\(\)V(\n {3}.*)*((\n {3}.*box-impl .*)(\n {3}.*)*){1}
|
||||||
// 0 testFlattened2Boxed\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){2}
|
// 0 testFlattened2Boxed\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){2}
|
||||||
// 0 testBoxed2Boxed\(\)V(\n {3}.*)*((\n {3}.*(box-impl|DSTORE|DLOAD).*)(\n {3}.*)*){1}
|
// 0 testBoxed2Boxed\(\)V(\n {3}.*)*((\n {3}.*(box-impl|DSTORE|DLOAD).*)(\n {3}.*)*){1}
|
||||||
@@ -48,3 +55,4 @@ fun testIgnoredBoxed() {
|
|||||||
// 0 testBoxed2Flattened\(\)V(\n {3}.*)*((\n {3}.*unbox-impl.*)(\n {3}.*)*){3}
|
// 0 testBoxed2Flattened\(\)V(\n {3}.*)*((\n {3}.*unbox-impl.*)(\n {3}.*)*){3}
|
||||||
// 0 testIgnoredFlattened\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){1}
|
// 0 testIgnoredFlattened\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){1}
|
||||||
// 0 testIgnoredBoxed\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){1}
|
// 0 testIgnoredBoxed\(\)V(\n {3}.*)*((\n {3}.*box-impl.*)(\n {3}.*)*){1}
|
||||||
|
// 0 Init.*((\n {1}.*)*(\n {1}.*box-impl.*)){1}
|
||||||
|
|||||||
Reference in New Issue
Block a user