[K/N] Untie WriteBitcodeFilePhase from NativeGenerationState

This commit is contained in:
Sergey Bogolepov
2023-01-24 14:28:40 +02:00
committed by Space Team
parent a7518fed73
commit 1ffef21ce5
3 changed files with 17 additions and 16 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.backend.common.phaser.ActionState
import org.jetbrains.kotlin.backend.common.phaser.BeforeOrAfter
import org.jetbrains.kotlin.backend.common.serialization.KlibIrVersion
import org.jetbrains.kotlin.backend.konan.cexport.produceCAdapterBitcode
import org.jetbrains.kotlin.backend.konan.driver.PhaseContext
import org.jetbrains.kotlin.backend.konan.llvm.*
import org.jetbrains.kotlin.backend.konan.llvm.objc.patchObjCRuntimeModule
import org.jetbrains.kotlin.backend.konan.objcexport.createObjCFramework
@@ -172,12 +173,11 @@ private fun linkAllDependencies(generationState: NativeGenerationState, generate
}
}
internal fun insertAliasToEntryPoint(generationState: NativeGenerationState) {
val config = generationState.config
internal fun insertAliasToEntryPoint(context: PhaseContext, module: LLVMModuleRef) {
val config = context.config
val nomain = config.configuration.get(KonanConfigKeys.NOMAIN) ?: false
if (config.produce != CompilerOutputKind.PROGRAM || nomain)
return
val module = generationState.llvm.module
val entryPointName = config.entryPointName
val entryPoint = LLVMGetNamedFunction(module, entryPointName)
?: error("Module doesn't contain `$entryPointName`")
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.backend.konan.llvm.coverage.runCoveragePass
import org.jetbrains.kotlin.backend.konan.llvm.verifyModule
import org.jetbrains.kotlin.backend.konan.optimizations.RemoveRedundantSafepointsPass
import org.jetbrains.kotlin.backend.konan.optimizations.removeMultipleThreadDataLoads
import java.io.File
private val nativeLLVMDumper =
fun(actionState: ActionState, _: Unit, context: NativeGenerationState) {
@@ -29,23 +30,21 @@ private val nativeLLVMDumper =
private val llvmPhaseActions: Set<Action<Unit, NativeGenerationState>> = setOf(nativeLLVMDumper)
internal data class WriteBitcodeFileInput(
val llvmModule: LLVMModuleRef,
val outputFile: File,
)
/**
* Write in-memory LLVM module to filesystem as a bitcode.
*
* TODO: Use explicit input (LLVMModule) and output (File)
* after static driver removal.
*/
internal val WriteBitcodeFilePhase = createSimpleNamedCompilerPhase<NativeGenerationState, LLVMModuleRef, String>(
internal val WriteBitcodeFilePhase = createSimpleNamedCompilerPhase<PhaseContext, WriteBitcodeFileInput>(
"WriteBitcodeFile",
"Write bitcode file",
outputIfNotEnabled = { _, _, _, _ -> error("WriteBitcodeFile be disabled") }
) { context, llvmModule ->
val output = context.tempFiles.nativeBinaryFileName
) { context, (llvmModule, outputFile) ->
// Insert `_main` after pipeline, so we won't worry about optimizations corrupting entry point.
insertAliasToEntryPoint(context)
LLVMWriteBitcodeToFile(llvmModule, output)
output
insertAliasToEntryPoint(context, llvmModule)
LLVMWriteBitcodeToFile(llvmModule, outputFile.canonicalPath)
}
internal val CheckExternalCallsPhase = createSimpleNamedCompilerPhase(
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.ir.declarations.path
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.konan.TempFiles
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
import org.jetbrains.kotlin.library.impl.javaFile
internal fun PhaseEngine<PhaseContext>.runFrontend(config: KonanConfig, environment: KotlinCoreEnvironment): FrontendPhaseOutput.Full? {
val frontendOutput = useContext(FrontendContextImpl(config)) { it.runPhase(FrontendPhase, environment) }
@@ -136,13 +137,14 @@ internal fun PhaseEngine<NativeGenerationState>.compileModule(module: IrModuleFr
if (context.config.produce.isCache) {
runPhase(SaveAdditionalCacheInfoPhase)
}
val bitcodeFile = runPhase(WriteBitcodeFilePhase, context.llvm.module)
val bitcodeFile = context.tempFiles.nativeBinaryFile.javaFile()
runPhase(WriteBitcodeFilePhase, WriteBitcodeFileInput(context.llvm.module, bitcodeFile))
val dependenciesTrackingResult = DependenciesTrackingResult(
context.dependenciesTracker.bitcodeToLink,
context.dependenciesTracker.allNativeDependencies,
context.dependenciesTracker.allCachedBitcodeDependencies,
)
return ModuleCompilationOutput(bitcodeFile, dependenciesTrackingResult, context.tempFiles, context.outputFiles)
return ModuleCompilationOutput(bitcodeFile.canonicalPath, dependenciesTrackingResult, context.tempFiles, context.outputFiles)
}
internal fun <C : PhaseContext> PhaseEngine<C>.compileAndLink(