[K/N] Untie DependenciesTracker from NativeGenerationState

This commit is contained in:
Sergey Bogolepov
2023-02-02 16:02:49 +02:00
committed by Space Team
parent 7402ef24bb
commit eba1b4c388
3 changed files with 17 additions and 12 deletions
@@ -52,6 +52,8 @@ interface DependenciesTracker {
val nativeDependenciesToLink: List<KonanLibrary> val nativeDependenciesToLink: List<KonanLibrary>
val allNativeDependencies: List<KonanLibrary> val allNativeDependencies: List<KonanLibrary>
val bitcodeToLink: List<KonanLibrary> val bitcodeToLink: List<KonanLibrary>
fun collectResult(): DependenciesTrackingResult
} }
private sealed class FileOrigin { private sealed class FileOrigin {
@@ -66,12 +68,13 @@ private sealed class FileOrigin {
class CertainFile(val library: KotlinLibrary, val fqName: String, val filePath: String) : FileOrigin() class CertainFile(val library: KotlinLibrary, val fqName: String, val filePath: String) : FileOrigin()
} }
internal class DependenciesTrackerImpl(private val generationState: NativeGenerationState) : DependenciesTracker { internal class DependenciesTrackerImpl(
private val llvmModuleSpecification: LlvmModuleSpecification,
private val config: KonanConfig,
private val context: Context,
) : DependenciesTracker {
private data class LibraryFile(val library: KotlinLibrary, val fqName: String, val filePath: String) private data class LibraryFile(val library: KotlinLibrary, val fqName: String, val filePath: String)
private val config = generationState.config
private val context = generationState.context
private val usedBitcode = mutableSetOf<KotlinLibrary>() private val usedBitcode = mutableSetOf<KotlinLibrary>()
private val usedNativeDependencies = mutableSetOf<KotlinLibrary>() private val usedNativeDependencies = mutableSetOf<KotlinLibrary>()
private val usedBitcodeOfFile = mutableSetOf<LibraryFile>() private val usedBitcodeOfFile = mutableSetOf<LibraryFile>()
@@ -297,11 +300,11 @@ internal class DependenciesTrackerImpl(private val generationState: NativeGenera
val bitcodeToLink = topSortedLibraries.filter { shouldContainBitcode(it) } val bitcodeToLink = topSortedLibraries.filter { shouldContainBitcode(it) }
private fun shouldContainBitcode(library: KonanLibrary): Boolean { private fun shouldContainBitcode(library: KonanLibrary): Boolean {
if (!generationState.llvmModuleSpecification.containsLibrary(library)) { if (!llvmModuleSpecification.containsLibrary(library)) {
return false return false
} }
if (!generationState.llvmModuleSpecification.isFinal) { if (!llvmModuleSpecification.isFinal) {
return true return true
} }
@@ -322,6 +325,12 @@ internal class DependenciesTrackerImpl(private val generationState: NativeGenera
override val nativeDependenciesToLink get() = dependencies.nativeDependenciesToLink override val nativeDependenciesToLink get() = dependencies.nativeDependenciesToLink
override val allNativeDependencies get() = dependencies.allNativeDependencies override val allNativeDependencies get() = dependencies.allNativeDependencies
override val bitcodeToLink get() = dependencies.bitcodeToLink override val bitcodeToLink get() = dependencies.bitcodeToLink
override fun collectResult(): DependenciesTrackingResult = DependenciesTrackingResult(
bitcodeToLink,
allNativeDependencies,
allCachedBitcodeDependencies,
)
} }
internal object DependenciesSerializer { internal object DependenciesSerializer {
@@ -86,7 +86,7 @@ internal class NativeGenerationState(
val producedLlvmModuleContainsStdlib get() = llvmModuleSpecification.containsModule(context.stdlibModule) val producedLlvmModuleContainsStdlib get() = llvmModuleSpecification.containsModule(context.stdlibModule)
val dependenciesTracker: DependenciesTracker = DependenciesTrackerImpl(this) val dependenciesTracker: DependenciesTracker = DependenciesTrackerImpl(llvmModuleSpecification, config, context)
private val runtimeDelegate = lazy { Runtime(llvmContext, config.distribution.compilerInterface(config.target)) } private val runtimeDelegate = lazy { Runtime(llvmContext, config.distribution.compilerInterface(config.target)) }
private val llvmDelegate = lazy { Llvm(this, LLVMModuleCreateWithNameInContext("out", llvmContext)!!) } private val llvmDelegate = lazy { Llvm(this, LLVMModuleCreateWithNameInContext("out", llvmContext)!!) }
@@ -142,11 +142,7 @@ internal fun PhaseEngine<NativeGenerationState>.compileModule(module: IrModuleFr
// TODO: Currently, all llvm modules are named as "out" which might lead to collisions. // TODO: Currently, all llvm modules are named as "out" which might lead to collisions.
val bitcodeFile = context.tempFiles.create(context.llvm.module.getName(), ".bc").javaFile() val bitcodeFile = context.tempFiles.create(context.llvm.module.getName(), ".bc").javaFile()
runPhase(WriteBitcodeFilePhase, WriteBitcodeFileInput(context.llvm.module, bitcodeFile)) runPhase(WriteBitcodeFilePhase, WriteBitcodeFileInput(context.llvm.module, bitcodeFile))
val dependenciesTrackingResult = DependenciesTrackingResult( val dependenciesTrackingResult = context.dependenciesTracker.collectResult()
context.dependenciesTracker.bitcodeToLink,
context.dependenciesTracker.allNativeDependencies,
context.dependenciesTracker.allCachedBitcodeDependencies,
)
return ModuleCompilationOutput(bitcodeFile, dependenciesTrackingResult, context.tempFiles, context.outputFiles) return ModuleCompilationOutput(bitcodeFile, dependenciesTrackingResult, context.tempFiles, context.outputFiles)
} }