[K/N] Refactor default argument lowering to avoid boxing

Current inline classes API is not compatible between different backends.
So implementing common function required for this optimization
was not possible in Native backend.

So, common default arguments lowering was refactored to make bigger
piece of code replaceable in backends.

^KT-57860
This commit is contained in:
Pavel Kunyavskiy
2023-04-24 12:52:17 +02:00
committed by Space Team
parent 2aea5822b2
commit f55fd481e9
18 changed files with 141 additions and 54 deletions
@@ -226,9 +226,9 @@ private val volatilePhase = createFileLoweringPhase(
private val defaultParameterExtentPhase = createFileLoweringPhase(
{ context, irFile ->
KonanDefaultArgumentStubGenerator(context).lower(irFile)
NativeDefaultArgumentStubGenerator(context).lower(irFile)
DefaultParameterCleaner(context, replaceDefaultValuesWithStubs = true).lower(irFile)
KonanDefaultParameterInjector(context).lower(irFile)
NativeDefaultParameterInjector(context).lower(irFile)
},
name = "DefaultParameterExtent",
description = "Default parameter extent lowering",
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.konan.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.konan.PrimitiveBinaryType
import org.jetbrains.kotlin.backend.konan.computePrimitiveBinaryTypeOrNull
import org.jetbrains.kotlin.ir.types.IrType
internal class NativeDefaultArgumentFunctionFactory(context: CommonBackendContext) : MaskedDefaultArgumentFunctionFactory(context) {
override fun IrType.hasNullAsUndefinedValue(): Boolean {
val binaryType = computePrimitiveBinaryTypeOrNull() ?: return true
return binaryType == PrimitiveBinaryType.POINTER || binaryType == PrimitiveBinaryType.VECTOR128
}
}
@@ -12,9 +12,11 @@ import org.jetbrains.kotlin.ir.declarations.IrValueDeclaration
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrExpression
internal class KonanDefaultArgumentStubGenerator(override val context: Context)
: DefaultArgumentStubGenerator(context, skipInlineMethods = false)
{
internal class NativeDefaultArgumentStubGenerator(context: Context) : DefaultArgumentStubGenerator<Context>(
context = context,
factory = NativeDefaultArgumentFunctionFactory(context),
skipInlineMethods = false
) {
override fun IrBlockBodyBuilder.selectArgumentOrDefault(
defaultFlag: IrExpression,
parameter: IrValueParameter,
@@ -15,11 +15,14 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.irCall
internal class KonanDefaultParameterInjector(private val konanContext: KonanBackendContext)
: DefaultParameterInjector(konanContext, skipInline = false) {
internal class NativeDefaultParameterInjector(context: KonanBackendContext) : DefaultParameterInjector<KonanBackendContext>(
context = context,
factory = NativeDefaultArgumentFunctionFactory(context),
skipInline = false
) {
override fun nullConst(startOffset: Int, endOffset: Int, type: IrType): IrExpression {
val symbols = konanContext.ir.symbols
val symbols = context.ir.symbols
val nullConstOfEquivalentType = when (type.computePrimitiveBinaryTypeOrNull()) {
null -> IrConstImpl.constNull(startOffset, endOffset, context.irBuiltIns.nothingNType)
@@ -6487,6 +6487,10 @@ fileCheckTest("filecheck_intrinsics") {
annotatedSource = project.file('filecheck/intrinsics.kt')
}
fileCheckTest("filecheck_default_parameters_dont_box") {
annotatedSource = project.file('filecheck/default_parameters_dont_box.kt')
}
task kt53119_side_effect(type: KonanLocalTest) {
useGoldenData = true
source = "codegen/stringConcatenationTypeNarrowing/kt53119_side_effect.kt"
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
value class A(val i: Int)
value class B(val a: A)
value class C(val s: String)
fun defaultInt(a: Int = 1, aa: Int = 1) = a
fun defaultA(a: A = A(1), aa: A = A(1)) = a.i
fun defaultB(b: B = B(A(1)), bb: B = B(A(1))) = b.a.i
fun defaultC(c: C = C("1"), cc: C = C("1")) = c.s
// CHECK-LABEL: "kfun:#main(){}"
fun main(){
// CHECK-LABEL: entry
// CHECK-NOT: <Int-box>
// CHECK-NOT: <A-box>
// CHECK-NOT: <B-box>
// CHECK-NOT: <C-box>
defaultInt()
defaultA()
defaultB()
defaultC()
defaultInt(1)
defaultA(A(1))
defaultB(B(A(1)))
defaultC(C("1"))
defaultInt(1, 1)
defaultA(A(1), A(1))
defaultB(B(A(1)), B(A(1)))
defaultC(C("1"), C("1"))
// CHECK-LABEL: epilogue
}