[K/N][IR][codegen] Made lazy init logic JVM-like

Now a file will be initialized only if a top level function has been called
rather than any function like was before the commit
This commit is contained in:
Igor Chevdar
2021-08-26 20:13:18 +05:00
parent e6be5e4266
commit ce44403d1b
6 changed files with 50 additions and 14 deletions
@@ -404,7 +404,7 @@ fun IrFunction.isMethodOfAny(): Boolean =
else -> false
}
fun IrClass.simpleFunctions() = declarations.flatMap {
fun IrDeclarationContainer.simpleFunctions() = declarations.flatMap {
when (it) {
is IrSimpleFunction -> listOf(it)
is IrProperty -> listOfNotNull(it.getter, it.setter)
@@ -487,6 +487,10 @@ internal class KonanSymbols(
context.builtIns.builtInsModule.findClassAcrossModuleDependencies(
ClassId.topLevel(KonanFqNames.sharedImmutable))!!)
val eagerInitialization = symbolTable.referenceClass(
context.builtIns.builtInsModule.findClassAcrossModuleDependencies(
ClassId.topLevel(KonanFqNames.eagerInitialization))!!)
private fun topLevelClass(fqName: FqName): IrClassSymbol = irBuiltIns.findClass(fqName.shortName(), fqName.parent())!!
private fun internalFunction(name: String): IrSimpleFunctionSymbol =
@@ -97,6 +97,7 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa
).apply {
parent = irFile
annotations += buildSimpleAnnotation(context.irBuiltIns, startOffset, endOffset, context.ir.symbols.sharedImmutable.owner)
annotations += buildSimpleAnnotation(context.irBuiltIns, startOffset, endOffset, context.ir.symbols.eagerInitialization.owner)
}
irFile.transformChildrenVoid(object : IrElementTransformerVoidWithContext() {
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.backend.konan.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.ir.simpleFunctions
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.KonanFqNames
@@ -81,20 +82,16 @@ internal class FileInitializersLowering(val context: Context) : FileLoweringPass
)
else null
irFile.transformChildrenVoid(object: IrElementTransformerVoid() {
override fun visitFunction(declaration: IrFunction): IrStatement {
declaration.transformChildrenVoid(this)
val body = declaration.body ?: return declaration
val statements = (body as IrBlockBody).statements
context.createIrBuilder(declaration.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).run {
// The order of calling initializers: first global, then thread-local.
// It is ok for a thread local top level property to reference a global, but not vice versa.
threadLocalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
globalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
}
return declaration
irFile.simpleFunctions().forEach {
val body = it.body ?: return@forEach
val statements = (body as IrBlockBody).statements
context.createIrBuilder(it.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).run {
// The order of calling initializers: first global, then thread-local.
// It is ok for a thread local top level property to reference a global, but not vice versa.
threadLocalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
globalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
}
})
}
}
private fun buildInitFileFunction(irFile: IrFile, name: String, origin: IrDeclarationOrigin) = context.irFactory.buildFun {
@@ -1481,6 +1481,11 @@ standaloneTest("initializers_globalInitedAfterAccessingFile") {
source = "codegen/initializers/globalInitedAfterAccessingFile.kt"
}
standaloneTest("initializers_globalNotInitedAfterAccessingClassInternals") {
enabled = project.globalTestArgs.contains('-Xir-property-lazy-initialization=enable') || isExperimentalMM
source = "codegen/initializers/globalNotInitedAfterAccessingClassInternals.kt"
}
standaloneTest("initializers_globalInitedBeforeThreadLocal") {
source = "codegen/initializers/globalInitedBeforeThreadLocal.kt"
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2021 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.
*/
// FILE: lib.kt
var z = false
// FILE: lib2.kt
import kotlin.test.*
val x = foo()
private fun foo(): Int {
z = true
return 42
}
class C {
fun bar() = 117
}
// FILE: main.kt
import kotlin.test.*
fun main() {
C().bar()
assertFalse(z)
}