[K/N] Move c++ adapter compilation to a separate phase
Besides purely architectural reasons or, rather, as a direct effect of those, it allows moving tempFiles usages to the driver.
This commit is contained in:
committed by
Space Team
parent
bb1feabb79
commit
1182903b20
+4
-12
@@ -5,7 +5,6 @@
|
|||||||
package org.jetbrains.kotlin.backend.konan
|
package org.jetbrains.kotlin.backend.konan
|
||||||
|
|
||||||
import llvm.*
|
import llvm.*
|
||||||
import org.jetbrains.kotlin.backend.konan.cexport.produceCAdapterBitcode
|
|
||||||
import org.jetbrains.kotlin.backend.konan.driver.PhaseContext
|
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
|
||||||
@@ -15,6 +14,7 @@ import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
|||||||
import org.jetbrains.kotlin.library.BaseKotlinLibrary
|
import org.jetbrains.kotlin.library.BaseKotlinLibrary
|
||||||
import org.jetbrains.kotlin.library.uniqueName
|
import org.jetbrains.kotlin.library.uniqueName
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
|
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supposed to be true for a single LLVM module within final binary.
|
* Supposed to be true for a single LLVM module within final binary.
|
||||||
@@ -142,23 +142,15 @@ internal fun insertAliasToEntryPoint(context: PhaseContext, module: LLVMModuleRe
|
|||||||
LLVMAddAlias(module, LLVMTypeOf(entryPoint)!!, entryPoint, "main")
|
LLVMAddAlias(module, LLVMTypeOf(entryPoint)!!, entryPoint, "main")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun linkBitcodeDependencies(generationState: NativeGenerationState) {
|
internal fun linkBitcodeDependencies(generationState: NativeGenerationState,
|
||||||
|
generatedBitcodeFiles: List<File>) {
|
||||||
val config = generationState.config
|
val config = generationState.config
|
||||||
val tempFiles = generationState.tempFiles
|
|
||||||
val produce = config.produce
|
val produce = config.produce
|
||||||
|
|
||||||
val generatedBitcodeFiles =
|
|
||||||
if (produce == CompilerOutputKind.DYNAMIC || produce == CompilerOutputKind.STATIC) {
|
|
||||||
produceCAdapterBitcode(
|
|
||||||
config.clang,
|
|
||||||
tempFiles.cAdapterCppName,
|
|
||||||
tempFiles.cAdapterBitcodeName)
|
|
||||||
listOf(tempFiles.cAdapterBitcodeName)
|
|
||||||
} else emptyList()
|
|
||||||
if (produce == CompilerOutputKind.FRAMEWORK && config.produceStaticFramework) {
|
if (produce == CompilerOutputKind.FRAMEWORK && config.produceStaticFramework) {
|
||||||
embedAppleLinkerOptionsToBitcode(generationState.llvm, config)
|
embedAppleLinkerOptionsToBitcode(generationState.llvm, config)
|
||||||
}
|
}
|
||||||
linkAllDependencies(generationState, generatedBitcodeFiles)
|
linkAllDependencies(generationState, generatedBitcodeFiles.map { it.absoluteFile.normalize().path })
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-4
@@ -7,10 +7,17 @@ package org.jetbrains.kotlin.backend.konan.cexport
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.konan.exec.*
|
import org.jetbrains.kotlin.konan.exec.*
|
||||||
import org.jetbrains.kotlin.konan.target.*
|
import org.jetbrains.kotlin.konan.target.*
|
||||||
import org.jetbrains.kotlin.konan.file.*
|
import java.io.File
|
||||||
|
|
||||||
// Fourth phase of C export: compile runtime bindings to bitcode.
|
/**
|
||||||
fun produceCAdapterBitcode(clang: ClangArgs, cppFileName: String, bitcodeFileName: String) {
|
* Fourth phase of C export: compile runtime bindings to bitcode.
|
||||||
val clangCommand = clang.clangCXX("-std=c++17", cppFileName, "-emit-llvm", "-c", "-o", bitcodeFileName)
|
*/
|
||||||
|
fun produceCAdapterBitcode(clang: ClangArgs, cppFile: File, bitcodeFile: File) {
|
||||||
|
val clangCommand = clang.clangCXX(
|
||||||
|
"-std=c++17",
|
||||||
|
cppFile.absoluteFile.normalize().path,
|
||||||
|
"-emit-llvm", "-c",
|
||||||
|
"-o", bitcodeFile.absoluteFile.normalize().path
|
||||||
|
)
|
||||||
Command(clangCommand).execute()
|
Command(clangCommand).execute()
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -102,11 +102,11 @@ internal val CStubsPhase = createSimpleNamedCompilerPhase<NativeGenerationState,
|
|||||||
op = { context, _ -> produceCStubs(context) }
|
op = { context, _ -> produceCStubs(context) }
|
||||||
)
|
)
|
||||||
|
|
||||||
internal val LinkBitcodeDependenciesPhase = createSimpleNamedCompilerPhase<NativeGenerationState, Unit>(
|
internal val LinkBitcodeDependenciesPhase = createSimpleNamedCompilerPhase<NativeGenerationState, List<File>>(
|
||||||
name = "LinkBitcodeDependencies",
|
name = "LinkBitcodeDependencies",
|
||||||
description = "Link bitcode dependencies",
|
description = "Link bitcode dependencies",
|
||||||
postactions = getDefaultLlvmModuleActions(),
|
postactions = getDefaultLlvmModuleActions(),
|
||||||
op = { context, _ -> linkBitcodeDependencies(context) }
|
op = { context, input -> linkBitcodeDependencies(context, input) }
|
||||||
)
|
)
|
||||||
|
|
||||||
internal val VerifyBitcodePhase = createSimpleNamedCompilerPhase<PhaseContext, LLVMModuleRef>(
|
internal val VerifyBitcodePhase = createSimpleNamedCompilerPhase<PhaseContext, LLVMModuleRef>(
|
||||||
|
|||||||
+13
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.backend.konan.driver.phases
|
package org.jetbrains.kotlin.backend.konan.driver.phases
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.backend.konan.cexport.*
|
||||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterApiExporter
|
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterApiExporter
|
||||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterExportedElements
|
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterExportedElements
|
||||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterGenerator
|
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterGenerator
|
||||||
@@ -40,3 +41,15 @@ internal val CExportGenerateApiPhase = createSimpleNamedCompilerPhase<PhaseConte
|
|||||||
target = context.config.target,
|
target = context.config.target,
|
||||||
).makeGlobalStruct()
|
).makeGlobalStruct()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class CExportCompileAdapterInput(
|
||||||
|
val cppAdapterFile: File,
|
||||||
|
val bitcodeAdapterFile: File,
|
||||||
|
)
|
||||||
|
|
||||||
|
internal val CExportCompileAdapterPhase = createSimpleNamedCompilerPhase<PhaseContext, CExportCompileAdapterInput>(
|
||||||
|
name = "CExportCompileAdapter",
|
||||||
|
description = "Compile C++ adapter to bitcode"
|
||||||
|
) { context, input ->
|
||||||
|
produceCAdapterBitcode(context.config.clang, input.cppAdapterFile, input.bitcodeAdapterFile)
|
||||||
|
}
|
||||||
+9
-3
@@ -174,14 +174,20 @@ internal fun PhaseEngine<NativeGenerationState>.runBackendCodegen(module: IrModu
|
|||||||
}
|
}
|
||||||
mergeDependencies(module, dependenciesToCompile)
|
mergeDependencies(module, dependenciesToCompile)
|
||||||
runCodegen(module)
|
runCodegen(module)
|
||||||
if (context.config.produce.isNativeLibrary) {
|
val generatedBitcodeFiles = if (context.config.produce.isNativeLibrary) {
|
||||||
|
val cppAdapterFile = context.tempFiles.cAdapterCpp.javaFile()
|
||||||
|
val bitcodeAdapterFile = context.tempFiles.cAdapterBitcode.javaFile()
|
||||||
val input = CExportGenerateApiInput(
|
val input = CExportGenerateApiInput(
|
||||||
context.context.cAdapterExportedElements!!,
|
context.context.cAdapterExportedElements!!,
|
||||||
headerFile = context.outputFiles.cAdapterHeader.javaFile(),
|
headerFile = context.outputFiles.cAdapterHeader.javaFile(),
|
||||||
defFile = if (context.config.target.family == Family.MINGW) context.outputFiles.cAdapterDef.javaFile() else null,
|
defFile = if (context.config.target.family == Family.MINGW) context.outputFiles.cAdapterDef.javaFile() else null,
|
||||||
cppAdapterFile = context.tempFiles.cAdapterCpp.javaFile()
|
cppAdapterFile = cppAdapterFile
|
||||||
)
|
)
|
||||||
runPhase(CExportGenerateApiPhase, input)
|
runPhase(CExportGenerateApiPhase, input)
|
||||||
|
runPhase(CExportCompileAdapterPhase, CExportCompileAdapterInput(cppAdapterFile, bitcodeAdapterFile))
|
||||||
|
listOf(bitcodeAdapterFile)
|
||||||
|
} else {
|
||||||
|
emptyList()
|
||||||
}
|
}
|
||||||
runPhase(CStubsPhase)
|
runPhase(CStubsPhase)
|
||||||
// TODO: Consider extracting llvmModule and friends from nativeGenerationState and pass them explicitly.
|
// TODO: Consider extracting llvmModule and friends from nativeGenerationState and pass them explicitly.
|
||||||
@@ -194,7 +200,7 @@ internal fun PhaseEngine<NativeGenerationState>.runBackendCodegen(module: IrModu
|
|||||||
if (context.shouldPrintBitCode()) {
|
if (context.shouldPrintBitCode()) {
|
||||||
runPhase(PrintBitcodePhase, llvmModule)
|
runPhase(PrintBitcodePhase, llvmModule)
|
||||||
}
|
}
|
||||||
runPhase(LinkBitcodeDependenciesPhase)
|
runPhase(LinkBitcodeDependenciesPhase, generatedBitcodeFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user