Skip to content

Commit 8c45ebc

Browse files
kunyavskiySpace Team
authored andcommitted
[Inliner] Don't create additional blocks around variable initializers
^KT-80953
1 parent 4a9e54b commit 8c45ebc

File tree

24 files changed

+144
-170
lines changed

24 files changed

+144
-170
lines changed

compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/FunctionInlining.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private class CallInlining(
346346
) {
347347
for ((parameter, argument) in callee.parameters.zip(callSite.arguments)) {
348348
val isDefaultArg = argument == null && parameter.defaultValue != null
349-
val variableInitializer = when {
349+
val argumentValue = when {
350350
argument != null -> argument
351351
parameter.defaultValue != null -> parameter.defaultValue!!.expression
352352
parameter.varargElementType != null -> callSiteBuilder.emptyVararg(parameter)
@@ -358,14 +358,14 @@ private class CallInlining(
358358
* For simplicity and to produce simpler IR we don't create temporaries for every immutable variable,
359359
* not only for those referring to inlinable lambdas.
360360
*/
361-
if (parameter.isInlineParameter() && variableInitializer is IrRichCallableReference<*>) {
361+
if (parameter.isInlineParameter() && argumentValue is IrRichCallableReference<*>) {
362362
val evaluationBuilder = if (isDefaultArg) inlinedBlockBuilder else callSiteBuilder
363363
// If function reference has bound values, they need to be computed in advance, not at call-site
364364
// So, we store them to local variables, if they are untrivial
365-
substituteMap[parameter] = variableInitializer
366-
for (index in variableInitializer.boundValues.indices) {
367-
val irExpression = variableInitializer.boundValues[index]
368-
val boundParameter = variableInitializer.invokeFunction.parameters[index]
365+
substituteMap[parameter] = argumentValue
366+
for (index in argumentValue.boundValues.indices) {
367+
val irExpression = argumentValue.boundValues[index]
368+
val boundParameter = argumentValue.invokeFunction.parameters[index]
369369
val variableSymbol =
370370
if (irExpression is IrGetValue && irExpression.symbol.owner.isImmutable && irExpression.type == boundParameter.type) {
371371
irExpression.symbol
@@ -376,29 +376,25 @@ private class CallInlining(
376376
isMutable = false,
377377
).symbol
378378
}
379-
variableInitializer.boundValues[index] = irGetValueWithoutLocation(variableSymbol)
379+
argumentValue.boundValues[index] = irGetValueWithoutLocation(variableSymbol)
380380
}
381381
continue
382382
}
383383
// inline parameters should never be stored to temporaries, as it would prevent their inlining
384-
variableInitializer.tryGetLoadedInlineParameter()?.let {
384+
argumentValue.tryGetLoadedInlineParameter()?.let {
385385
substituteMap[parameter] = irGetValueWithoutLocation(it)
386386
continue
387387
}
388388

389-
fun IrBuilderWithScope.computeInitializer() = irBlock(resultType = parameter.type) {
390-
+variableInitializer.doImplicitCastIfNeededTo(parameter.type)
391-
}
389+
val castedArgumentValue = argumentValue.doImplicitCastIfNeededTo(parameter.type)
392390

393391
val valueForTmpVar = if (isDefaultArg) {
394-
inlinedBlockBuilder
395-
.at(variableInitializer)
396-
.computeInitializer()
392+
castedArgumentValue
397393
} else {
398394
// This variable is required to trigger argument evaluation outside the scope of the inline function
399395
val tempVarOutsideInlineBlock = callSiteBuilder
400396
.at(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
401-
.irTemporary(callSiteBuilder.computeInitializer())
397+
.irTemporary(castedArgumentValue)
402398
inlinedBlockBuilder
403399
.at(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
404400
.irGet(tempVarOutsideInlineBlock)

compiler/testData/codegen/box/delegatedProperty/lazyDelegateInObject.fir.inlined.ir

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,22 @@ FILE fqName:<root> fileName:/lazyDelegateInObject.kt
325325
RETURN type=kotlin.Nothing from='public final fun <get-original> (): kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>'
326326
BLOCK type=kotlin.Any? origin=INLINE_ARGS_CONTAINER
327327
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.Lazy<kotlin.Any?> [val]
328-
BLOCK type=kotlin.Lazy<kotlin.Any?> origin=null
329-
TYPE_OP type=kotlin.Lazy<kotlin.Any?> origin=IMPLICIT_CAST typeOperand=kotlin.Lazy<kotlin.Any?>
330-
GET_FIELD 'FIELD PROPERTY_DELEGATE name:original$delegate type:kotlin.Lazy<kotlin.Any?> visibility:private [final] declared in <root>.lazyDelegateInObject.<no name provided>' type=kotlin.Lazy<kotlin.Any?> origin=null
331-
receiver: GET_VAR '<this>: <root>.lazyDelegateInObject.<no name provided> declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>' type=<root>.lazyDelegateInObject.<no name provided> origin=IMPLICIT_ARGUMENT
328+
TYPE_OP type=kotlin.Lazy<kotlin.Any?> origin=IMPLICIT_CAST typeOperand=kotlin.Lazy<kotlin.Any?>
329+
GET_FIELD 'FIELD PROPERTY_DELEGATE name:original$delegate type:kotlin.Lazy<kotlin.Any?> visibility:private [final] declared in <root>.lazyDelegateInObject.<no name provided>' type=kotlin.Lazy<kotlin.Any?> origin=null
330+
receiver: GET_VAR '<this>: <root>.lazyDelegateInObject.<no name provided> declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>' type=<root>.lazyDelegateInObject.<no name provided> origin=IMPLICIT_ARGUMENT
332331
VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Any? [val]
333-
BLOCK type=kotlin.Any? origin=null
334-
TYPE_OP type=kotlin.Any? origin=IMPLICIT_CAST typeOperand=kotlin.Any?
335-
GET_VAR '<this>: <root>.lazyDelegateInObject.<no name provided> declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>' type=<root>.lazyDelegateInObject.<no name provided> origin=null
332+
TYPE_OP type=kotlin.Any? origin=IMPLICIT_CAST typeOperand=kotlin.Any?
333+
GET_VAR '<this>: <root>.lazyDelegateInObject.<no name provided> declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>' type=<root>.lazyDelegateInObject.<no name provided> origin=null
336334
VAR IR_TEMPORARY_VARIABLE name:tmp_2 type:kotlin.reflect.KProperty<*> [val]
337-
BLOCK type=kotlin.reflect.KProperty<*> origin=null
338-
TYPE_OP type=kotlin.reflect.KProperty<*> origin=IMPLICIT_CAST typeOperand=kotlin.reflect.KProperty<*>
339-
RICH_PROPERTY_REFERENCE type=kotlin.reflect.KProperty1<*, kotlin.Any?> origin=PROPERTY_REFERENCE_FOR_DELEGATE reflectionTarget='public final original: kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>'
340-
getter: FUN LOCAL_FUNCTION_FOR_LAMBDA name:<get-original> visibility:local modality:FINAL returnType:kotlin.Any?
341-
VALUE_PARAMETER kind:Regular name:p0 index:0 type:kotlin.Any?
342-
BLOCK_BODY
343-
RETURN type=kotlin.Nothing from='local final fun <get-original> (p0: kotlin.Any?): kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>'
344-
CALL 'public final fun <get-original> (): kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>' type=kotlin.Any? origin=null
345-
ARG <this>: TYPE_OP type=<root>.lazyDelegateInObject.<no name provided> origin=IMPLICIT_CAST typeOperand=<root>.lazyDelegateInObject.<no name provided>
346-
GET_VAR 'p0: kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>.<get-original>' type=kotlin.Any? origin=null
335+
TYPE_OP type=kotlin.reflect.KProperty<*> origin=IMPLICIT_CAST typeOperand=kotlin.reflect.KProperty<*>
336+
RICH_PROPERTY_REFERENCE type=kotlin.reflect.KProperty1<*, kotlin.Any?> origin=PROPERTY_REFERENCE_FOR_DELEGATE reflectionTarget='public final original: kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>'
337+
getter: FUN LOCAL_FUNCTION_FOR_LAMBDA name:<get-original> visibility:local modality:FINAL returnType:kotlin.Any?
338+
VALUE_PARAMETER kind:Regular name:p0 index:0 type:kotlin.Any?
339+
BLOCK_BODY
340+
RETURN type=kotlin.Nothing from='local final fun <get-original> (p0: kotlin.Any?): kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>'
341+
CALL 'public final fun <get-original> (): kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>' type=kotlin.Any? origin=null
342+
ARG <this>: TYPE_OP type=<root>.lazyDelegateInObject.<no name provided> origin=IMPLICIT_CAST typeOperand=<root>.lazyDelegateInObject.<no name provided>
343+
GET_VAR 'p0: kotlin.Any? declared in <root>.lazyDelegateInObject.<no name provided>.<get-original>.<get-original>' type=kotlin.Any? origin=null
347344
RETURNABLE_BLOCK type=kotlin.Any? origin=null
348345
INLINED_BLOCK type=kotlin.Any? origin=null
349346
inlinedFunctionSymbol: public final fun getValue <T> (<this>: kotlin.Lazy<T of kotlin.getValue>, thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): T of kotlin.getValue declared in kotlin

compiler/testData/klib/syntheticAccessors/privateMember/crossFilePrivateLeak/leakingPrivateMethodFromDefaultArguments.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public class Foo
2121
/* FILE package=<root> fileName=main.kt */
2222

2323
public fun box(): String
24-
val oo: String =
24+
val oo: String = access$o($this = this)
2525
/* ACCESSOR use-site */ access$o($this = this)
26-
/* ACCESSOR use-site */ access$k($this = this)
26+
/* ACCESSOR use-site */ access$k($this = this)

compiler/testData/klib/syntheticAccessors/privateMember/crossFilePrivateLeak/leakingPrivateVarThroughInlineFunParameter.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class A
2222
/* FILE package=<root> fileName=main.kt */
2323

2424
public fun box(): String
25-
val param: Int =
25+
val param: Int = access$<get-privateVal>($this = this)
2626
/* ACCESSOR use-site */ access$<get-privateVal>($this = this)
27-
val block: Function0<Int> =
27+
val block: Function0<Int> = access$<get-privateValFunctional>($this = this)
2828
/* ACCESSOR use-site */ access$<get-privateValFunctional>($this = this)

compiler/testData/klib/syntheticAccessors/privateMember/crossModulePrivateLeak/leakingPrivateMethodFromDefaultArguments.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public class Foo
2323
/* FILE package=<root> fileName=main.kt */
2424

2525
public fun box(): String
26-
val oo: String =
26+
val oo: String = access$o($this = this)
2727
/* ACCESSOR use-site */ access$o($this = this)
28-
/* ACCESSOR use-site */ access$k($this = this)
28+
/* ACCESSOR use-site */ access$k($this = this)

compiler/testData/klib/syntheticAccessors/privateMember/crossModulePrivateLeak/leakingPrivateVarThroughInlineFunParameter.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class A
2424
/* FILE package=<root> fileName=main.kt */
2525

2626
public fun box(): String
27-
val param: Int =
27+
val param: Int = access$<get-privateVal>($this = this)
2828
/* ACCESSOR use-site */ access$<get-privateVal>($this = this)
29-
val block: Function0<Int> =
29+
val block: Function0<Int> = access$<get-privateValFunctional>($this = this)
3030
/* ACCESSOR use-site */ access$<get-privateValFunctional>($this = this)

compiler/testData/klib/syntheticAccessors/privateMember/singleFile/usePrivateMethodFromDefaultArguments.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public class Foo
1717
/* ACCESSOR declaration */ internal /* static */ fun access$k($this: Foo): String
1818
/* TARGET use-site */ $this.k()
1919
public fun box(): String
20-
val oo: String =
20+
val oo: String = access$o($this = this)
2121
/* ACCESSOR use-site */ access$o($this = this)
22-
/* ACCESSOR use-site */ access$k($this = this)
22+
/* ACCESSOR use-site */ access$k($this = this)

compiler/testData/klib/syntheticAccessors/topLevelPrivate/crossFilePrivateLeak/leakingPrivateTopLevelFunFromDefaultArguments.accessors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ internal inline fun internalInlineFun(oo: String, kk: Function0<String>): String
2020
/* FILE package=<root> fileName=main.kt */
2121

2222
public fun box(): String
23-
val oo: String =
23+
val oo: String = access$o$tAKt()
2424
/* ACCESSOR use-site */ access$o$tAKt()
25-
/* ACCESSOR use-site */ access$k$tAKt()
25+
/* ACCESSOR use-site */ access$k$tAKt()

compiler/testData/klib/syntheticAccessors/topLevelPrivate/crossFilePrivateLeak/leakingPrivateTopLevelFunThroughPublicInlineMethodInLocalObject.accessors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* FILE package=<root> fileName=A.kt */
44

55
internal inline fun internalInlineMethod(crossinline f: Function0<String>): String
6-
val tmp0: <no name provided> =
6+
val tmp0: <no name provided> = <anonymous object>
77
/* LOCAL declaration @0 */ local class <no name provided>
88
/* LOCAL declaration @1 */ public inline fun run(): String
99
/* ACCESSOR use-site */ access$privateMethod$tAKt()
@@ -17,9 +17,9 @@ internal inline fun internalInlineMethod(crossinline f: Function0<String>): Stri
1717
/* FILE package=<root> fileName=main.kt */
1818

1919
public fun box(): String
20-
val tmp0: <no name provided> =
20+
val tmp0: <no name provided> = <anonymous object>
2121
/* LOCAL declaration @0 */ local class <no name provided>
2222
/* LOCAL declaration @1 */ public inline fun run(): String
2323
/* ACCESSOR use-site */ access$privateMethod$tAKt()
2424
/* INLINED use-site @1 */ public inline fun run(): String
25-
/* ACCESSOR use-site */ access$privateMethod$tAKt()
25+
/* ACCESSOR use-site */ access$privateMethod$tAKt()

compiler/testData/klib/syntheticAccessors/topLevelPrivate/crossFilePrivateLeak/leakingPrivateTopLevelVarThroughInlineFunParameter.accessors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal inline fun executorFunctional(noinline block: Function0<Int>): Int
2121
/* FILE package=<root> fileName=main.kt */
2222

2323
public fun box(): String
24-
val param: Int =
24+
val param: Int = access$<get-privateVal>$tAKt()
2525
/* ACCESSOR use-site */ access$<get-privateVal>$tAKt()
26-
val block: Function0<Int> =
27-
/* ACCESSOR use-site */ access$<get-privateValFunctional>$tAKt()
26+
val block: Function0<Int> = access$<get-privateValFunctional>$tAKt()
27+
/* ACCESSOR use-site */ access$<get-privateValFunctional>$tAKt()

0 commit comments

Comments
 (0)