[K/N][IR] Replaced end-to-end indexing with in-file one

We don't want the names of classes/functions to be dependant on the files order,
this could be bad for per-file caches and/or incremental compilation.
This commit is contained in:
Igor Chevdar
2022-10-13 22:23:38 +03:00
committed by Space Team
parent 7a7f1d559d
commit 94a2479015
8 changed files with 40 additions and 15 deletions
@@ -10,9 +10,9 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
private const val dumpBridges = false
class CStubsManager(private val target: KonanTarget) {
internal class CStubsManager(private val target: KonanTarget, private val generationState: NativeGenerationState) {
fun getUniqueName(prefix: String) = "$prefix${counter++}"
fun getUniqueName(prefix: String) = generationState.fileLowerState.getCStubUniqueName(prefix)
fun addStub(kotlinLocation: CompilerMessageLocation?, lines: List<String>, language: String) {
val stubs = languageToStubs.getOrPut(language) { mutableListOf() }
@@ -109,5 +109,4 @@ class CStubsManager(private val target: KonanTarget) {
private val languageToStubs = mutableMapOf<String, MutableList<Stub>>()
private class Stub(val kotlinLocation: CompilerMessageLocation?, val lines: List<String>)
private var counter = 0
}
@@ -162,9 +162,6 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config), Confi
config.librariesWithDependencies(moduleDescriptor)
}
var functionReferenceCount = 0
var coroutineCount = 0
fun needGlobalInit(field: IrField): Boolean {
if (field.descriptor.containingDeclaration !is PackageFragmentDescriptor) return false
// TODO: add some smartness here. Maybe if package of the field is in never accessed
@@ -96,6 +96,12 @@ internal val propertyAccessorInlinePhase = makeKonanModuleLoweringPhase(
/* IrFile phases */
internal val createFileLowerStatePhase = makeKonanFileOpPhase(
{ context, _ -> context.generationState.fileLowerState = FileLowerState() },
name = "CreateFileLowerState",
description = "Create FileLowerState"
)
internal val removeExpectDeclarationsPhase = makeKonanFileLoweringPhase(
::ExpectDeclarationsRemoving,
name = "RemoveExpectDeclarations",
@@ -19,10 +19,27 @@ import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.konan.TempFiles
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.name.ClassId
internal class InlineFunctionOriginInfo(val irFunction: IrFunction, val irFile: IrFile, val startOffset: Int, val endOffset: Int)
internal class FileLowerState {
private var functionReferenceCount = 0
private var coroutineCount = 0
private var cStubCount = 0
fun getFunctionReferenceImplUniqueName(targetFunction: IrFunction): String =
getFunctionReferenceImplUniqueName("${targetFunction.name}\$FUNCTION_REFERENCE\$")
fun getCoroutineImplUniqueName(function: IrFunction): String =
"${function.name}COROUTINE\$${coroutineCount++}"
fun getFunctionReferenceImplUniqueName(prefix: String) =
"$prefix${functionReferenceCount++}"
fun getCStubUniqueName(prefix: String) =
"$prefix${cStubCount++}"
}
internal class NativeGenerationState(private val context: Context) {
private val config = context.config
@@ -54,6 +71,8 @@ internal class NativeGenerationState(private val context: Context) {
getLocalClassName(source)?.let { name -> putLocalClassName(destination, name) }
}
lateinit var fileLowerState: FileLowerState
private val runtimeDelegate = lazy { Runtime(llvmContext, config.distribution.compilerInterface(config.target)) }
private val llvmDelegate = lazy { Llvm(context, LLVMModuleCreateWithNameInContext("out", llvmContext)!!) }
private val debugInfoDelegate = lazy { DebugInfo(context) }
@@ -63,7 +82,7 @@ internal class NativeGenerationState(private val context: Context) {
val runtime by runtimeDelegate
val llvm by llvmDelegate
val debugInfo by debugInfoDelegate
val cStubsManager = CStubsManager(config.target)
val cStubsManager = CStubsManager(config.target, this)
lateinit var llvmDeclarations: LlvmDeclarations
fun hasDebugInfo() = debugInfoDelegate.isInitialized()
@@ -247,6 +247,7 @@ internal val allLoweringsPhase = SameTypeNamedCompilerPhase(
name = "IrLowerByFile",
description = "IR Lowering by file",
lower = listOf(
createFileLowerStatePhase,
removeExpectDeclarationsPhase,
stripTypeAliasDeclarationsPhase,
lowerBeforeInlinePhase,
@@ -162,6 +162,7 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
private val irBuiltIns = context.irBuiltIns
private val symbols = context.ir.symbols
private val irFactory = context.irFactory
private val fileLowerState = context.generationState.fileLowerState
private val startOffset = functionReference.startOffset
private val endOffset = functionReference.endOffset
@@ -194,7 +195,7 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
startOffset = this@FunctionReferenceBuilder.startOffset
endOffset = this@FunctionReferenceBuilder.endOffset
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL
name = "${functionReferenceTarget.name}\$FUNCTION_REFERENCE\$${context.functionReferenceCount++}".synthesizedName
name = fileLowerState.getFunctionReferenceImplUniqueName(functionReferenceTarget).synthesizedName
visibility = DescriptorVisibilities.PRIVATE
}.apply {
parent = this@FunctionReferenceBuilder.parent
@@ -81,6 +81,8 @@ private abstract class BaseInteropIrTransformer(private val context: Context) :
}
return object : KotlinStubs {
private val cStubsManager = context.generationState.cStubsManager
override val irBuiltIns get() = context.irBuiltIns
override val symbols get() = context.ir.symbols
override val typeSystem: IrTypeSystemContext get() = context.typeSystem
@@ -97,14 +99,14 @@ private abstract class BaseInteropIrTransformer(private val context: Context) :
}
override fun addC(lines: List<String>) {
context.generationState.cStubsManager.addStub(location, lines, language)
cStubsManager.addStub(location, lines, language)
}
override fun getUniqueCName(prefix: String) =
"$uniquePrefix${context.generationState.cStubsManager.getUniqueName(prefix)}"
"$uniquePrefix${cStubsManager.getUniqueName(prefix)}"
override fun getUniqueKotlinFunctionReferenceClassName(prefix: String) =
"$prefix${context.functionReferenceCount++}"
context.generationState.fileLowerState.getFunctionReferenceImplUniqueName(prefix)
override val target get() = context.config.target
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrSetValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSuspendableExpressionImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSuspensionPointImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
@@ -29,8 +28,9 @@ import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.*
import org.jetbrains.kotlin.name.Name
internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunctionsLowering<Context>(ctx) {
internal class NativeSuspendFunctionsLowering(ctx: Context) : AbstractSuspendFunctionsLowering<Context>(ctx) {
private val symbols = context.ir.symbols
private val fileLowerState = context.generationState.fileLowerState
override val stateMachineMethodName = Name.identifier("invokeSuspend")
@@ -42,7 +42,7 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc
})
override fun nameForCoroutineClass(function: IrFunction) =
"${function.name}COROUTINE\$${context.coroutineCount++}".synthesizedName
fileLowerState.getCoroutineImplUniqueName(function).synthesizedName
override fun initializeStateMachine(coroutineConstructors: List<IrConstructor>, coroutineClassThis: IrValueDeclaration) {
// Nothing to do: it's redundant to initialize the "label" field with null