From 94a24790151b1e8c348f7348e717ed2fed14cd12 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Thu, 13 Oct 2022 22:23:38 +0300 Subject: [PATCH] [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. --- .../kotlin/backend/konan/CStubsManager.kt | 5 ++-- .../jetbrains/kotlin/backend/konan/Context.kt | 3 --- .../backend/konan/KonanLoweringPhases.kt | 6 +++++ .../backend/konan/NativeGenerationState.kt | 23 +++++++++++++++++-- .../kotlin/backend/konan/ToplevelPhases.kt | 1 + .../konan/lower/FunctionReferenceLowering.kt | 3 ++- .../backend/konan/lower/InteropLowering.kt | 8 ++++--- .../lower/NativeSuspendFunctionLowering.kt | 6 ++--- 8 files changed, 40 insertions(+), 15 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CStubsManager.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CStubsManager.kt index d1e868a2d6d..55d6c330b41 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CStubsManager.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CStubsManager.kt @@ -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, language: String) { val stubs = languageToStubs.getOrPut(language) { mutableListOf() } @@ -109,5 +109,4 @@ class CStubsManager(private val target: KonanTarget) { private val languageToStubs = mutableMapOf>() private class Stub(val kotlinLocation: CompilerMessageLocation?, val lines: List) - private var counter = 0 } \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt index ea8e969800b..3d9c71b3e51 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt @@ -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 diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt index 5acf23fdec8..c5372f9ab22 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt @@ -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", diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeGenerationState.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeGenerationState.kt index 283e3ccd771..fb1c0983e3d 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeGenerationState.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeGenerationState.kt @@ -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() diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt index 54f8555f420..79f27786778 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt @@ -247,6 +247,7 @@ internal val allLoweringsPhase = SameTypeNamedCompilerPhase( name = "IrLowerByFile", description = "IR Lowering by file", lower = listOf( + createFileLowerStatePhase, removeExpectDeclarationsPhase, stripTypeAliasDeclarationsPhase, lowerBeforeInlinePhase, diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt index a32a864bbdb..976fa132694 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionReferenceLowering.kt @@ -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 diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt index 9135f6a1460..9e4c3a11053 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt @@ -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) { - 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 diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt index 4c9b354ac3d..3d9581a793d 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt @@ -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(ctx) { +internal class NativeSuspendFunctionsLowering(ctx: Context) : AbstractSuspendFunctionsLowering(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, coroutineClassThis: IrValueDeclaration) { // Nothing to do: it's redundant to initialize the "label" field with null