[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.phaser.BeforeOrAfter
import org.jetbrains.kotlin.backend.common.serialization.KlibIrVersion import org.jetbrains.kotlin.backend.common.serialization.KlibIrVersion
import org.jetbrains.kotlin.backend.konan.cexport.produceCAdapterBitcode 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.*
import org.jetbrains.kotlin.backend.konan.llvm.objc.patchObjCRuntimeModule import org.jetbrains.kotlin.backend.konan.llvm.objc.patchObjCRuntimeModule
import org.jetbrains.kotlin.backend.konan.objcexport.createObjCFramework import org.jetbrains.kotlin.backend.konan.objcexport.createObjCFramework
@@ -172,12 +173,11 @@ private fun linkAllDependencies(generationState: NativeGenerationState, generate
} }
} }
internal fun insertAliasToEntryPoint(generationState: NativeGenerationState) { internal fun insertAliasToEntryPoint(context: PhaseContext, module: LLVMModuleRef) {
val config = generationState.config val config = context.config
val nomain = config.configuration.get(KonanConfigKeys.NOMAIN) ?: false val nomain = config.configuration.get(KonanConfigKeys.NOMAIN) ?: false
if (config.produce != CompilerOutputKind.PROGRAM || nomain) if (config.produce != CompilerOutputKind.PROGRAM || nomain)
return return
val module = generationState.llvm.module
val entryPointName = config.entryPointName val entryPointName = config.entryPointName
val entryPoint = LLVMGetNamedFunction(module, entryPointName) val entryPoint = LLVMGetNamedFunction(module, entryPointName)
?: error("Module doesn't contain `$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.llvm.verifyModule
import org.jetbrains.kotlin.backend.konan.optimizations.RemoveRedundantSafepointsPass import org.jetbrains.kotlin.backend.konan.optimizations.RemoveRedundantSafepointsPass
import org.jetbrains.kotlin.backend.konan.optimizations.removeMultipleThreadDataLoads import org.jetbrains.kotlin.backend.konan.optimizations.removeMultipleThreadDataLoads
import java.io.File
private val nativeLLVMDumper = private val nativeLLVMDumper =
fun(actionState: ActionState, _: Unit, context: NativeGenerationState) { fun(actionState: ActionState, _: Unit, context: NativeGenerationState) {
@@ -29,23 +30,21 @@ private val nativeLLVMDumper =
private val llvmPhaseActions: Set<Action<Unit, NativeGenerationState>> = setOf(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. * 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<PhaseContext, WriteBitcodeFileInput>(
internal val WriteBitcodeFilePhase = createSimpleNamedCompilerPhase<NativeGenerationState, LLVMModuleRef, String>(
"WriteBitcodeFile", "WriteBitcodeFile",
"Write bitcode file", "Write bitcode file",
outputIfNotEnabled = { _, _, _, _ -> error("WriteBitcodeFile be disabled") } ) { context, (llvmModule, outputFile) ->
) { context, llvmModule ->
val output = context.tempFiles.nativeBinaryFileName
// Insert `_main` after pipeline, so we won't worry about optimizations corrupting entry point. // Insert `_main` after pipeline, so we won't worry about optimizations corrupting entry point.
insertAliasToEntryPoint(context) insertAliasToEntryPoint(context, llvmModule)
LLVMWriteBitcodeToFile(llvmModule, output) LLVMWriteBitcodeToFile(llvmModule, outputFile.canonicalPath)
output
} }
internal val CheckExternalCallsPhase = createSimpleNamedCompilerPhase( 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.ir.util.hasAnnotation
import org.jetbrains.kotlin.konan.TempFiles import org.jetbrains.kotlin.konan.TempFiles
import org.jetbrains.kotlin.konan.target.CompilerOutputKind 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? { internal fun PhaseEngine<PhaseContext>.runFrontend(config: KonanConfig, environment: KotlinCoreEnvironment): FrontendPhaseOutput.Full? {
val frontendOutput = useContext(FrontendContextImpl(config)) { it.runPhase(FrontendPhase, environment) } 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) { if (context.config.produce.isCache) {
runPhase(SaveAdditionalCacheInfoPhase) 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( val dependenciesTrackingResult = DependenciesTrackingResult(
context.dependenciesTracker.bitcodeToLink, context.dependenciesTracker.bitcodeToLink,
context.dependenciesTracker.allNativeDependencies, context.dependenciesTracker.allNativeDependencies,
context.dependenciesTracker.allCachedBitcodeDependencies, 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( internal fun <C : PhaseContext> PhaseEngine<C>.compileAndLink(