[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:
@@ -404,7 +404,7 @@ fun IrFunction.isMethodOfAny(): Boolean =
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun IrClass.simpleFunctions() = declarations.flatMap {
|
fun IrDeclarationContainer.simpleFunctions() = declarations.flatMap {
|
||||||
when (it) {
|
when (it) {
|
||||||
is IrSimpleFunction -> listOf(it)
|
is IrSimpleFunction -> listOf(it)
|
||||||
is IrProperty -> listOfNotNull(it.getter, it.setter)
|
is IrProperty -> listOfNotNull(it.getter, it.setter)
|
||||||
|
|||||||
+4
@@ -487,6 +487,10 @@ internal class KonanSymbols(
|
|||||||
context.builtIns.builtInsModule.findClassAcrossModuleDependencies(
|
context.builtIns.builtInsModule.findClassAcrossModuleDependencies(
|
||||||
ClassId.topLevel(KonanFqNames.sharedImmutable))!!)
|
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 topLevelClass(fqName: FqName): IrClassSymbol = irBuiltIns.findClass(fqName.shortName(), fqName.parent())!!
|
||||||
|
|
||||||
private fun internalFunction(name: String): IrSimpleFunctionSymbol =
|
private fun internalFunction(name: String): IrSimpleFunctionSymbol =
|
||||||
|
|||||||
+1
@@ -97,6 +97,7 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa
|
|||||||
).apply {
|
).apply {
|
||||||
parent = irFile
|
parent = irFile
|
||||||
annotations += buildSimpleAnnotation(context.irBuiltIns, startOffset, endOffset, context.ir.symbols.sharedImmutable.owner)
|
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() {
|
irFile.transformChildrenVoid(object : IrElementTransformerVoidWithContext() {
|
||||||
|
|||||||
+10
-13
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.backend.konan.lower
|
package org.jetbrains.kotlin.backend.konan.lower
|
||||||
|
|
||||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
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.common.lower.createIrBuilder
|
||||||
import org.jetbrains.kotlin.backend.konan.Context
|
import org.jetbrains.kotlin.backend.konan.Context
|
||||||
import org.jetbrains.kotlin.backend.konan.KonanFqNames
|
import org.jetbrains.kotlin.backend.konan.KonanFqNames
|
||||||
@@ -81,20 +82,16 @@ internal class FileInitializersLowering(val context: Context) : FileLoweringPass
|
|||||||
)
|
)
|
||||||
else null
|
else null
|
||||||
|
|
||||||
irFile.transformChildrenVoid(object: IrElementTransformerVoid() {
|
irFile.simpleFunctions().forEach {
|
||||||
override fun visitFunction(declaration: IrFunction): IrStatement {
|
val body = it.body ?: return@forEach
|
||||||
declaration.transformChildrenVoid(this)
|
val statements = (body as IrBlockBody).statements
|
||||||
val body = declaration.body ?: return declaration
|
context.createIrBuilder(it.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).run {
|
||||||
val statements = (body as IrBlockBody).statements
|
// The order of calling initializers: first global, then thread-local.
|
||||||
context.createIrBuilder(declaration.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).run {
|
// It is ok for a thread local top level property to reference a global, but not vice versa.
|
||||||
// The order of calling initializers: first global, then thread-local.
|
threadLocalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
|
||||||
// It is ok for a thread local top level property to reference a global, but not vice versa.
|
globalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
|
||||||
threadLocalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
|
|
||||||
globalInitFunction?.let { statements.add(0, irCallFileInitializer(it.symbol)) }
|
|
||||||
}
|
|
||||||
return declaration
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildInitFileFunction(irFile: IrFile, name: String, origin: IrDeclarationOrigin) = context.irFactory.buildFun {
|
private fun buildInitFileFunction(irFile: IrFile, name: String, origin: IrDeclarationOrigin) = context.irFactory.buildFun {
|
||||||
|
|||||||
@@ -1481,6 +1481,11 @@ standaloneTest("initializers_globalInitedAfterAccessingFile") {
|
|||||||
source = "codegen/initializers/globalInitedAfterAccessingFile.kt"
|
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") {
|
standaloneTest("initializers_globalInitedBeforeThreadLocal") {
|
||||||
source = "codegen/initializers/globalInitedBeforeThreadLocal.kt"
|
source = "codegen/initializers/globalInitedBeforeThreadLocal.kt"
|
||||||
}
|
}
|
||||||
|
|||||||
+29
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user