From 2f1e9844df853e74d54167959f17f6fe3a1d120d Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Wed, 25 Jan 2023 12:46:25 +0200 Subject: [PATCH] [K/N] Move temp file from BitcodeCompiler to the driver Continue extracting all temporary files to the driver, so we have a better control over them. For example, this allows creating of a new driver where pipeline split between multiple compiler invocations. --- .../kotlin/backend/konan/BitcodeCompiler.kt | 29 +++++++++---------- .../konan/driver/phases/ObjectFiles.kt | 12 ++++---- .../konan/driver/phases/TopLevelPhases.kt | 10 ++++--- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/BitcodeCompiler.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/BitcodeCompiler.kt index 6e6a1f2a320..a8229b8f0d4 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/BitcodeCompiler.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/BitcodeCompiler.kt @@ -6,17 +6,15 @@ package org.jetbrains.kotlin.backend.konan import org.jetbrains.kotlin.backend.konan.driver.PhaseContext -import org.jetbrains.kotlin.konan.TempFiles import org.jetbrains.kotlin.konan.exec.Command import org.jetbrains.kotlin.konan.target.* +import java.io.File -typealias BitcodeFile = String typealias ObjectFile = String typealias ExecutableFile = String internal class BitcodeCompiler( private val context: PhaseContext, - private val temporaryFiles: TempFiles, ) { private val config = context.config @@ -36,9 +34,6 @@ internal class BitcodeCompiler( .logWith(context::log) .execute() - private fun temporary(name: String, suffix: String): String = - temporaryFiles.create(name, suffix).absolutePath - private fun targetTool(tool: String, vararg arg: String) { val absoluteToolName = if (platform.configurables is AppleConfigurables) { "${platform.absoluteTargetToolchain}/usr/bin/$tool" @@ -53,9 +48,7 @@ internal class BitcodeCompiler( runTool(absoluteToolName, *arg) } - private fun clang(configurables: ClangFlags, file: BitcodeFile): ObjectFile { - val objectFile = temporary("result", ".o") - + private fun clang(configurables: ClangFlags, bitcodeFile: File, objectFile: File) { val targetTriple = if (configurables is AppleConfigurables) { platform.targetTriple.withOSVersion(configurables.osVersionMin) } else { @@ -76,12 +69,13 @@ internal class BitcodeCompiler( addNonEmpty(BitcodeEmbedding.getClangOptions(config)) addNonEmpty(configurables.currentRelocationMode(context).translateToClangCc1Flag()) } + val bitcodePath = bitcodeFile.absoluteFile.normalize().path + val objectPath = objectFile.absoluteFile.normalize().path if (configurables is AppleConfigurables) { - targetTool("clang++", *flags.toTypedArray(), file, "-o", objectFile) + targetTool("clang++", *flags.toTypedArray(), bitcodePath, "-o", objectPath) } else { - hostLlvmTool("clang++", *flags.toTypedArray(), file, "-o", objectFile) + hostLlvmTool("clang++", *flags.toTypedArray(), bitcodePath, "-o", objectPath) } - return objectFile } private fun RelocationModeFlags.Mode.translateToClangCc1Flag() = when (this) { @@ -90,9 +84,12 @@ internal class BitcodeCompiler( RelocationModeFlags.Mode.DEFAULT -> emptyList() } - fun makeObjectFiles(bitcodeFile: BitcodeFile): List = - listOf(when (val configurables = platform.configurables) { - is ClangFlags -> clang(configurables, bitcodeFile) + /** + * Compile [bitcodeFile] to [objectFile]. + */ + fun makeObjectFile(bitcodeFile: File, objectFile: File) = + when (val configurables = platform.configurables) { + is ClangFlags -> clang(configurables, bitcodeFile, objectFile) else -> error("Unsupported configurables kind: ${configurables::class.simpleName}!") - }) + } } \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/ObjectFiles.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/ObjectFiles.kt index d919e118cc8..f608a9faefc 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/ObjectFiles.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/ObjectFiles.kt @@ -6,19 +6,17 @@ package org.jetbrains.kotlin.backend.konan.driver.phases import org.jetbrains.kotlin.backend.konan.BitcodeCompiler -import org.jetbrains.kotlin.backend.konan.ObjectFile import org.jetbrains.kotlin.backend.konan.driver.PhaseContext -import org.jetbrains.kotlin.konan.TempFiles +import java.io.File internal data class ObjectFilesPhaseInput( - val bitcodeFileName: String, - val temporaryFiles: TempFiles + val bitcodeFile: File, + val objectFile: File, ) -internal val ObjectFilesPhase = createSimpleNamedCompilerPhase>( +internal val ObjectFilesPhase = createSimpleNamedCompilerPhase( name = "ObjectFiles", description = "Bitcode to object file", - outputIfNotEnabled = { _, _, _, _ -> emptyList() } ) { context, input -> - BitcodeCompiler(context, input.temporaryFiles).makeObjectFiles(input.bitcodeFileName) + BitcodeCompiler(context).makeObjectFile(input.bitcodeFile, input.objectFile) } \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/TopLevelPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/TopLevelPhases.kt index e6e44b00c03..425057943b0 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/TopLevelPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/TopLevelPhases.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.konan.TempFiles import org.jetbrains.kotlin.konan.target.CompilerOutputKind import org.jetbrains.kotlin.konan.target.Family import org.jetbrains.kotlin.library.impl.javaFile +import java.io.File internal fun PhaseEngine.runFrontend(config: KonanConfig, environment: KotlinCoreEnvironment): FrontendPhaseOutput.Full? { val frontendOutput = useContext(FrontendContextImpl(config)) { it.runPhase(FrontendPhase, environment) } @@ -111,7 +112,7 @@ private fun PhaseEngine.splitIntoFragments( } internal data class ModuleCompilationOutput( - val bitcodeFile: String, + val bitcodeFile: File, val dependenciesTrackingResult: DependenciesTrackingResult, // Passing tempFiles and output files through this file looks silly and incorrect. // TODO: Refactor these classes and remove them from here. @@ -146,7 +147,7 @@ internal fun PhaseEngine.compileModule(module: IrModuleFr context.dependenciesTracker.allNativeDependencies, context.dependenciesTracker.allCachedBitcodeDependencies, ) - return ModuleCompilationOutput(bitcodeFile.canonicalPath, dependenciesTrackingResult, context.tempFiles, context.outputFiles) + return ModuleCompilationOutput(bitcodeFile, dependenciesTrackingResult, context.tempFiles, context.outputFiles) } internal fun PhaseEngine.compileAndLink( @@ -156,8 +157,9 @@ internal fun PhaseEngine.compileAndLink( temporaryFiles: TempFiles, isCoverageEnabled: Boolean, ) { - val objectFiles = runPhase(ObjectFilesPhase, ObjectFilesPhaseInput(moduleCompilationOutput.bitcodeFile, temporaryFiles)) - val linkerPhaseInput = LinkerPhaseInput(linkerOutputFile, objectFiles, moduleCompilationOutput.dependenciesTrackingResult, + val objectFile = temporaryFiles.create("result", ".o").javaFile() + runPhase(ObjectFilesPhase, ObjectFilesPhaseInput(moduleCompilationOutput.bitcodeFile, objectFile)) + val linkerPhaseInput = LinkerPhaseInput(linkerOutputFile, listOf(objectFile.canonicalPath), moduleCompilationOutput.dependenciesTrackingResult, outputFiles, temporaryFiles, isCoverageEnabled = isCoverageEnabled) runPhase(LinkerPhase, linkerPhaseInput) if (context.config.produce.isCache) {