[Wasm] remove redundant inline classes boxing/unboxing in State Machine function transformation code ^KT-87687
diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt index 806e819..e17f69a 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt
@@ -56,9 +56,6 @@ protected abstract fun IrBlockBodyBuilder.generateCoroutineStart(invokeSuspendFunction: IrFunction, receiver: IrExpression) - protected open fun IrBuilderWithScope.generateDelegatedCall(expectedType: IrType, delegatingCall: IrExpression): IrExpression = - delegatingCall - private val symbols = context.symbols private val getContinuationSymbol = symbols.getContinuation private val continuationClassSymbol = getContinuationSymbol.owner.returnType.classifierOrFail as IrClassSymbol
diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt index 264cf1e..ba4fa1d 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.backend.common.lower.FinallyBlocksLowering import org.jetbrains.kotlin.ir.backend.js.JsStatementOrigins import org.jetbrains.kotlin.backend.common.lower.ReturnableBlockTransformer -import org.jetbrains.kotlin.backend.common.lower.coroutines.defaultLoweredSuspendFunctionReturnType import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.optimizations.LivenessAnalysis import org.jetbrains.kotlin.ir.IrElement @@ -145,7 +144,7 @@ // This is done solely to improve the debugging experience. Otherwise, a breakpoint set to the closing brace of the function // cannot be hit. val tempVar = scope.createTemporaryVariable( - generateDelegatedCall(irFunction.returnType, returnValue), + irExpression = returnValue, irType = context.irBuiltIns.anyType, ) statements[statements.lastIndex] = tempVar @@ -333,36 +332,8 @@ rootLoop.transformChildrenVoid(dispatchPointTransformer) } - private fun needUnboxingOrUnit(fromType: IrType, toType: IrType): Boolean { - val icUtils = context.inlineClassesUtils - - return (icUtils.getInlinedClass(fromType) == null && icUtils.getInlinedClass(toType) != null) || - (fromType.isUnit() && !toType.isUnit()) - } - - override fun IrBuilderWithScope.generateDelegatedCall(expectedType: IrType, delegatingCall: IrExpression): IrExpression { - val functionReturnType = (delegatingCall as? IrCall)?.symbol?.owner?.let { function -> - defaultLoweredSuspendFunctionReturnType(function.returnType, context.irBuiltIns) - } ?: delegatingCall.type - - if (!needUnboxingOrUnit(functionReturnType, expectedType)) return delegatingCall - - return irComposite(resultType = expectedType) { - val tmp = createTmpVariable(delegatingCall, irType = functionReturnType) - val coroutineSuspended = irCall(this@JsSuspendFunctionsLowering.context.symbols.coroutineSuspendedGetter) - val condition = irEqeqeq(irGet(tmp), coroutineSuspended) - +irIfThen(context.irBuiltIns.unitType, condition, irReturn(irGet(tmp))) - +irImplicitCast(irGet(tmp), expectedType) - } - } - - override fun IrBlockBodyBuilder.generateCoroutineStart(invokeSuspendFunction: IrFunction, receiver: IrExpression) { - val call = irCall(invokeSuspendFunction.symbol).apply { - arguments[0] = receiver - } - val functionReturnType = scope.scopeOwnerSymbol.assertedCast<IrSimpleFunctionSymbol> { "Expected function symbol" }.owner.returnType - +irReturn(generateDelegatedCall(functionReturnType, call)) - } + override fun IrBlockBodyBuilder.generateCoroutineStart(invokeSuspendFunction: IrFunction, receiver: IrExpression) = + +irReturn(irCall(invokeSuspendFunction.symbol).apply { arguments[0] = receiver }) } internal sealed class SuspendFunctionKind {
diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt index 6bc2350..02f99d4 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/StateMachineBuilder.kt
@@ -284,14 +284,8 @@ if (expression.isSuspend) { val result = lastExpression() - val expectedType = expression.symbol.owner.returnType - val isInlineClassExpected = context.inlineClassesUtils.getInlinedClass(expectedType) != null val continueState = SuspendState(unit) - val unboxState = if (isInlineClassExpected) SuspendState(unit) else null - - val dispatch = createDispatchPoint(unboxState ?: continueState) - - if (unboxState != null) currentState.successors += unboxState + val dispatch = createDispatchPoint(continueState) currentState.successors += continueState @@ -313,33 +307,13 @@ val suspensionBlock = JsIrBuilder.buildBlock(unit, listOf(irReturn)) addStatement(JsIrBuilder.buildIfElse(unit, check, suspensionBlock)) - if (isInlineClassExpected) { - addStatement(JsIrBuilder.buildCall(stateSymbolSetter.symbol, unit).apply { - arguments[0] = thisReceiver - arguments[1] = createDispatchPoint(continueState) - }) - } - doContinue() - unboxState?.let { buildUnboxingState(it, continueState, expectedType) } - updateState(continueState) addStatement(getSuspendResultAsType(expression.type)) } } - private fun buildUnboxingState(unboxState: SuspendState, continueState: SuspendState, expectedType: IrType) { - unboxState.successors += continueState - updateState(unboxState) - val result = getSuspendResultAsType(anyN) - val tmp = JsIrBuilder.buildVar(expectedType, function.owner, name = "unboxed", initializer = result) - addStatement(tmp) - addStatement(setSuspendResultValue(JsIrBuilder.buildGetValue(tmp.symbol))) - - doDispatch(continueState) - } - override fun visitBreak(jump: IrBreak) { val exitState = loopMap[jump.loop]!!.exitState resetExceptionStateIfNeeded(jump.loop)
diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt index 0a69b7a..6c2762a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt
@@ -85,9 +85,7 @@ return if (!expression.isSuspend || expression !in tailSuspendCalls) shortCut - else irBuilder.at(expression).irReturn( - irBuilder.generateDelegatedCall(irFunction.returnType, shortCut) - ) + else irBuilder.at(expression).irReturn(shortCut) } }) }