[K/N] Extract const val initializers to place of usage

This would make behaviour more consistent with jvm. There is still
a difference in behaviour about point where side effects happen.

^KT-52970
This commit is contained in:
Pavel Kunyavskiy
2022-07-11 16:39:13 +02:00
committed by Space
parent 6a8da8f9eb
commit 5cdda48487
15 changed files with 212 additions and 43 deletions
@@ -34964,6 +34964,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
@Test
@TestMetadata("nonConstValsAreProperlyInitialized.kt")
public void testNonConstValsAreProperlyInitialized() throws Exception {
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2022 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.common.lower
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.expressions.impl.copyWithOffsets
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
abstract class InlineConstTransformer : IrElementTransformerVoid() {
abstract val IrField.constantInitializer: IrConst<*>?
private fun IrExpression.lowerConstRead(receiver: IrExpression?, field: IrField?): IrExpression? {
val value = field?.constantInitializer ?: return null
transformChildrenVoid()
reportInlineConst(field, value)
val resultExpression = value.copyWithOffsets(startOffset, endOffset)
return if (receiver == null || receiver.shouldDropConstReceiver())
resultExpression
else
IrCompositeImpl(
startOffset, endOffset, resultExpression.type, null,
listOf(receiver, resultExpression)
)
}
abstract fun reportInlineConst(field: IrField, value: IrConst<*>)
abstract fun IrExpression.shouldDropConstReceiver(): Boolean
override fun visitCall(expression: IrCall): IrExpression {
val function = (expression.symbol.owner as? IrSimpleFunction) ?: return super.visitCall(expression)
val property = function.correspondingPropertySymbol?.owner ?: return super.visitCall(expression)
// If `constantValue` is not null, `function` can only be the getter because the property is immutable.
return expression.lowerConstRead(expression.dispatchReceiver, property.backingField) ?: super.visitCall(expression)
}
override fun visitGetField(expression: IrGetField): IrExpression =
expression.lowerConstRead(expression.receiver, expression.symbol.owner) ?: super.visitGetField(expression)
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower.InlineConstTransformer
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.ir.constantValue
@@ -13,12 +14,8 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.expressions.impl.copyWithOffsets
import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
internal val constPhase1 = makeIrFilePhase(
@@ -37,34 +34,12 @@ class ConstLowering(val context: JvmBackendContext) : FileLoweringPass {
val inlineConstTracker =
context.state.configuration[CommonConfigurationKeys.INLINE_CONST_TRACKER]
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid(ConstTransformer(irFile, context, inlineConstTracker))
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid(JvmInlineConstTransformer(irFile, inlineConstTracker))
}
private class ConstTransformer(
val irFile: IrFile,
val context: JvmBackendContext,
val inlineConstTracker: InlineConstTracker?
) : IrElementTransformerVoid() {
private fun IrExpression.lowerConstRead(receiver: IrExpression?, field: IrField?): IrExpression? {
val value = field?.constantValue() ?: return null
transformChildrenVoid()
reportInlineConst(field, value)
val resultExpression = if (context.state.shouldInlineConstVals)
value.copyWithOffsets(startOffset, endOffset)
else
IrGetFieldImpl(startOffset, endOffset, field.symbol, field.type)
return if (receiver == null || receiver.shouldDropConstReceiver())
resultExpression
else
IrCompositeImpl(
startOffset, endOffset, resultExpression.type, null,
listOf(receiver, resultExpression)
)
}
private fun reportInlineConst(field: IrField, value: IrConst<*>) {
private class JvmInlineConstTransformer(val irFile: IrFile, val inlineConstTracker: InlineConstTracker?) : InlineConstTransformer() {
override val IrField.constantInitializer get() = constantValue()
override fun reportInlineConst(field: IrField, value: IrConst<*>) {
if (inlineConstTracker == null) return
if (field.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB) return
@@ -76,17 +51,5 @@ private class ConstTransformer(
inlineConstTracker.report(path, owner, name, constType)
}
private fun IrExpression.shouldDropConstReceiver() =
this is IrConst<*> || this is IrGetValue ||
this is IrGetObjectValue
override fun visitCall(expression: IrCall): IrExpression {
val function = (expression.symbol.owner as? IrSimpleFunction) ?: return super.visitCall(expression)
val property = function.correspondingPropertySymbol?.owner ?: return super.visitCall(expression)
// If `constantValue` is not null, `function` can only be the getter because the property is immutable.
return expression.lowerConstRead(expression.dispatchReceiver, property.backingField) ?: super.visitCall(expression)
}
override fun visitGetField(expression: IrGetField): IrExpression =
expression.lowerConstRead(expression.receiver, expression.symbol.owner) ?: super.visitGetField(expression)
override fun IrExpression.shouldDropConstReceiver() = this is IrConst<*> || this is IrGetValue || this is IrGetObjectValue
}
@@ -0,0 +1,22 @@
// MODULE: lib1
// FILE: lib1.kt
object L1 {
lateinit var block: () -> Unit
}
// MODULE: lib2(lib1)
// FILE: lib2.kt
object L2 {
val sideEffect = L1.block()
const val x = 42
fun triggerInitialization() {}
}
// MODULE: main(lib1, lib2)
// FILE: main.kt
fun box(): String {
var result = 0
L1.block = { result = L2.x }
L2.triggerInitialization()
if (result != 42) return "FAIL: $result"
return "OK"
}
@@ -0,0 +1,7 @@
open class A(val a: String = DEFAULT_A){
companion object: A(){
const val DEFAULT_A = "OK"
}
}
fun box() = A().a
@@ -34388,6 +34388,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
@Test
@TestMetadata("nonConstValsAreProperlyInitialized.kt")
public void testNonConstValsAreProperlyInitialized() throws Exception {
@@ -34964,6 +34964,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
@Test
@TestMetadata("nonConstValsAreProperlyInitialized.kt")
public void testNonConstValsAreProperlyInitialized() throws Exception {
@@ -29340,6 +29340,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/properties/const/interfaceCompanion.kt");
}
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
@TestMetadata("nonConstValsAreProperlyInitialized.kt")
public void testNonConstValsAreProperlyInitialized() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/nonConstValsAreProperlyInitialized.kt");
@@ -25251,6 +25251,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public void testConstPropertyAccessor() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/constPropertyAccessor.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
}
@Nested
@@ -25245,6 +25245,18 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testConstPropertyAccessor() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/constPropertyAccessor.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
}
@Nested
@@ -22532,6 +22532,16 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
public void testConstPropertyAccessor() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/constPropertyAccessor.kt");
}
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/properties/lateinit")
@@ -407,6 +407,12 @@ internal val expressionBodyTransformPhase = makeKonanFileLoweringPhase(
description = "Replace IrExpressionBody with IrBlockBody"
)
internal val constantInliningPhase = makeKonanFileLoweringPhase(
::ConstLowering,
name = "ConstantInlining",
description = "Inline const fields reads",
)
internal val fileInitializersPhase = makeKonanFileLoweringPhase(
::FileInitializersLowering,
name = "FileInitializers",
@@ -302,6 +302,7 @@ internal val allLoweringsPhase = NamedCompilerPhase(
coroutinesPhase,
typeOperatorPhase,
expressionBodyTransformPhase,
constantInliningPhase,
fileInitializersPhase,
bridgesPhase,
autoboxPhase,
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 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.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower.InlineConstTransformer
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
internal class ConstLowering(val context: Context) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(NativeInlineConstTransformer())
}
}
private class NativeInlineConstTransformer : InlineConstTransformer() {
override val IrField.constantInitializer get() =
(initializer?.expression as? IrConst<*>)?.takeIf { correspondingPropertySymbol?.owner?.isConst == true }
override fun reportInlineConst(field: IrField, value: IrConst<*>) {}
// on jvm IrGetObjectValue is also dropped. This would be breaking change for native.
// Some design work is required to decide what is correct, let just keep current behaviour for now
override fun IrExpression.shouldDropConstReceiver() = this is IrConst<*> || this is IrGetValue
}
@@ -28056,6 +28056,18 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
public void testConstPropertyAccessor() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/constPropertyAccessor.kt");
}
@Test
@TestMetadata("intermoduleInlineConst.kt")
public void testIntermoduleInlineConst() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/intermoduleInlineConst.kt");
}
@Test
@TestMetadata("kt52970.kt")
public void testKt52970() throws Exception {
runTest("compiler/testData/codegen/box/properties/const/kt52970.kt");
}
}
@Nested