[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.
This commit is contained in:
Sergey Bogolepov
2023-01-25 12:46:25 +02:00
committed by Space Team
parent a11be433e2
commit 2f1e9844df
3 changed files with 24 additions and 27 deletions
@@ -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<ObjectFile> =
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}!")
})
}
}
@@ -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<PhaseContext, ObjectFilesPhaseInput, List<ObjectFile>>(
internal val ObjectFilesPhase = createSimpleNamedCompilerPhase<PhaseContext, ObjectFilesPhaseInput>(
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)
}
@@ -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<PhaseContext>.runFrontend(config: KonanConfig, environment: KotlinCoreEnvironment): FrontendPhaseOutput.Full? {
val frontendOutput = useContext(FrontendContextImpl(config)) { it.runPhase(FrontendPhase, environment) }
@@ -111,7 +112,7 @@ private fun PhaseEngine<out Context>.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<NativeGenerationState>.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 <C : PhaseContext> PhaseEngine<C>.compileAndLink(
@@ -156,8 +157,9 @@ internal fun <C : PhaseContext> PhaseEngine<C>.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) {