Refine LLVM bitcode imports detection
Don't link native libraries if external function requires only library-stored bitcode
This commit is contained in:
committed by
SvyatoslavScherbina
parent
f8393819fc
commit
06f8864b69
+6
-3
@@ -27,7 +27,8 @@ class GlobalVariableStub(global: GlobalDecl, stubGenerator: StubGenerator) : Kot
|
||||
stubGenerator.simpleBridgeGenerator.kotlinToNative(
|
||||
nativeBacked = this,
|
||||
returnType = BridgedType.NATIVE_PTR,
|
||||
kotlinValues = emptyList()
|
||||
kotlinValues = emptyList(),
|
||||
independent = false
|
||||
) {
|
||||
"&${global.name}"
|
||||
}
|
||||
@@ -58,7 +59,8 @@ class GlobalVariableStub(global: GlobalDecl, stubGenerator: StubGenerator) : Kot
|
||||
getter = mirror.info.argFromBridged(stubGenerator.simpleBridgeGenerator.kotlinToNative(
|
||||
nativeBacked = this,
|
||||
returnType = mirror.info.bridgedType,
|
||||
kotlinValues = emptyList()
|
||||
kotlinValues = emptyList(),
|
||||
independent = false
|
||||
) {
|
||||
mirror.info.cToBridged(expr = global.name)
|
||||
}, kotlinScope, nativeBacked = this)
|
||||
@@ -71,7 +73,8 @@ class GlobalVariableStub(global: GlobalDecl, stubGenerator: StubGenerator) : Kot
|
||||
stubGenerator.simpleBridgeGenerator.kotlinToNative(
|
||||
nativeBacked = setterStub,
|
||||
returnType = BridgedType.VOID,
|
||||
kotlinValues = listOf(bridgedValue)
|
||||
kotlinValues = listOf(bridgedValue),
|
||||
independent = false
|
||||
) { nativeValues ->
|
||||
out("${global.name} = ${mirror.info.cFromBridged(
|
||||
nativeValues.single(),
|
||||
|
||||
+2
@@ -157,6 +157,8 @@ data class KotlinFunctionType(
|
||||
internal val cnamesStructsPackageName = "cnames.structs"
|
||||
|
||||
object KotlinTypes {
|
||||
val independent = Classifier.topLevel("kotlin.native.internal", "Independent")
|
||||
|
||||
val boolean by BuiltInType
|
||||
val byte by BuiltInType
|
||||
val short by BuiltInType
|
||||
|
||||
+1
@@ -32,6 +32,7 @@ interface MappingBridgeGenerator {
|
||||
nativeBacked: NativeBacked,
|
||||
returnType: Type,
|
||||
kotlinValues: List<TypedKotlinValue>,
|
||||
independent: Boolean,
|
||||
block: NativeCodeBuilder.(nativeValues: List<NativeExpression>) -> NativeExpression
|
||||
): KotlinExpression
|
||||
|
||||
|
||||
+2
-1
@@ -35,6 +35,7 @@ class MappingBridgeGeneratorImpl(
|
||||
nativeBacked: NativeBacked,
|
||||
returnType: Type,
|
||||
kotlinValues: List<TypedKotlinValue>,
|
||||
independent: Boolean,
|
||||
block: NativeCodeBuilder.(nativeValues: List<NativeExpression>) -> NativeExpression
|
||||
): KotlinExpression {
|
||||
val bridgeArguments = mutableListOf<BridgeTypedKotlinValue>()
|
||||
@@ -70,7 +71,7 @@ class MappingBridgeGeneratorImpl(
|
||||
}
|
||||
|
||||
val callExpr = simpleBridgeGenerator.kotlinToNative(
|
||||
nativeBacked, bridgeReturnType, bridgeArguments
|
||||
nativeBacked, bridgeReturnType, bridgeArguments, independent
|
||||
) { bridgeNativeValues ->
|
||||
|
||||
val nativeValues = mutableListOf<String>()
|
||||
|
||||
+2
-1
@@ -307,7 +307,8 @@ sealed class TypeInfo {
|
||||
type.returnType,
|
||||
type.parameterTypes.mapIndexed { index, it ->
|
||||
TypedKotlinValue(it, "p$index")
|
||||
} + TypedKotlinValue(PointerType(VoidType), "interpretCPointer<COpaque>($kniBlockPtr)")
|
||||
} + TypedKotlinValue(PointerType(VoidType), "interpretCPointer<COpaque>($kniBlockPtr)"),
|
||||
independent = true
|
||||
|
||||
) { nativeValues ->
|
||||
val type = type
|
||||
|
||||
+5
-1
@@ -226,7 +226,11 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator,
|
||||
bodyGenerator,
|
||||
this@ObjCMethodStub,
|
||||
returnType,
|
||||
nativeBridgeArguments
|
||||
nativeBridgeArguments,
|
||||
independent = when (container) {
|
||||
is ObjCClassOrProtocol -> true // Every proper instance has this method in its method table.
|
||||
is ObjCCategory -> false // Method is contributed by native dependency.
|
||||
}
|
||||
) { nativeValues ->
|
||||
val messengerParameterTypes = mutableListOf<String>()
|
||||
messengerParameterTypes.add("void*")
|
||||
|
||||
+1
@@ -60,6 +60,7 @@ interface SimpleBridgeGenerator {
|
||||
nativeBacked: NativeBacked,
|
||||
returnType: BridgedType,
|
||||
kotlinValues: List<BridgeTypedKotlinValue>,
|
||||
independent: Boolean,
|
||||
block: NativeCodeBuilder.(nativeValues: List<NativeExpression>) -> NativeExpression
|
||||
): KotlinExpression
|
||||
|
||||
|
||||
+3
-1
@@ -71,7 +71,8 @@ class SimpleBridgeGeneratorImpl(
|
||||
nativeBacked: NativeBacked,
|
||||
returnType: BridgedType,
|
||||
kotlinValues: List<BridgeTypedKotlinValue>,
|
||||
block: NativeCodeBuilder.(arguments: List<NativeExpression>) -> NativeExpression
|
||||
independent: Boolean,
|
||||
block: NativeCodeBuilder.(nativeValues: List<NativeExpression>) -> NativeExpression
|
||||
): KotlinExpression {
|
||||
|
||||
val kotlinLines = mutableListOf<String>()
|
||||
@@ -116,6 +117,7 @@ class SimpleBridgeGeneratorImpl(
|
||||
}
|
||||
KotlinPlatform.NATIVE -> {
|
||||
val functionName = pkgName.replace(INVALID_CLANG_IDENTIFIER_REGEX, "_") + "_$kotlinFunctionName"
|
||||
if (independent) kotlinLines.add("@" + topLevelKotlinScope.reference(KotlinTypes.independent))
|
||||
kotlinLines.add("@SymbolName(${functionName.quoteAsKotlinLiteral()})")
|
||||
"$cReturnType $functionName ($joinedCParameters)"
|
||||
}
|
||||
|
||||
+2
-1
@@ -660,7 +660,8 @@ class StubGenerator(
|
||||
bodyGenerator,
|
||||
this,
|
||||
func.returnType,
|
||||
bridgeArguments
|
||||
bridgeArguments,
|
||||
independent = false
|
||||
) { nativeValues ->
|
||||
"${func.name}(${nativeValues.joinToString()})"
|
||||
}
|
||||
|
||||
+7
-4
@@ -40,7 +40,10 @@ internal class LinkStage(val context: Context) {
|
||||
|
||||
private val nomain = config.get(KonanConfigKeys.NOMAIN) ?: false
|
||||
private val emitted = context.bitcodeFileName
|
||||
private val libraries = context.llvm.librariesToLink
|
||||
|
||||
private val bitcodeLibraries = context.llvm.bitcodeToLink
|
||||
private val nativeDependencies = context.llvm.nativeDependenciesToLink
|
||||
|
||||
private fun MutableList<String>.addNonEmpty(elements: List<String>) {
|
||||
addAll(elements.filter { !it.isEmpty() })
|
||||
}
|
||||
@@ -221,7 +224,7 @@ internal class LinkStage(val context: Context) {
|
||||
fun makeObjectFiles() {
|
||||
|
||||
val bitcodeFiles = listOf(emitted) +
|
||||
libraries.map { it.bitcodePaths }.flatten().filter { it.isBitcode }
|
||||
bitcodeLibraries.map { it.bitcodePaths }.flatten().filter { it.isBitcode }
|
||||
|
||||
objectFiles.add(when (platform.configurables) {
|
||||
is WasmConfigurables
|
||||
@@ -235,10 +238,10 @@ internal class LinkStage(val context: Context) {
|
||||
|
||||
fun linkStage() {
|
||||
val includedBinaries =
|
||||
libraries.map { it.includedPaths }.flatten()
|
||||
nativeDependencies.map { it.includedPaths }.flatten()
|
||||
|
||||
val libraryProvidedLinkerFlags =
|
||||
libraries.map { it.linkerOpts }.flatten()
|
||||
nativeDependencies.map { it.linkerOpts }.flatten()
|
||||
|
||||
link(objectFiles, includedBinaries, libraryProvidedLinkerFlags)
|
||||
}
|
||||
|
||||
+1
@@ -8,4 +8,5 @@ object RuntimeNames {
|
||||
val exportForCompilerAnnotation = FqName("kotlin.native.internal.ExportForCompiler")
|
||||
val exportTypeInfoAnnotation = FqName("kotlin.native.internal.ExportTypeInfo")
|
||||
val cCall = FqName("kotlinx.cinterop.internal.CCall")
|
||||
val independent = FqName("kotlin.native.internal.Independent")
|
||||
}
|
||||
|
||||
+26
-8
@@ -296,8 +296,13 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
||||
return LLVMAddFunction(llvmModule, "llvm.memcpy.p0i8.p0i8.i32", functionType)!!
|
||||
}
|
||||
|
||||
internal fun externalFunction(name: String, type: LLVMTypeRef, origin: CompiledKonanModuleOrigin): LLVMValueRef {
|
||||
this.imports.add(origin)
|
||||
internal fun externalFunction(
|
||||
name: String,
|
||||
type: LLVMTypeRef,
|
||||
origin: CompiledKonanModuleOrigin,
|
||||
independent: Boolean = false
|
||||
): LLVMValueRef {
|
||||
this.imports.add(origin, onlyBitcode = independent)
|
||||
|
||||
val found = LLVMGetNamedFunction(llvmModule, name)
|
||||
if (found != null) {
|
||||
@@ -336,11 +341,12 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
||||
|
||||
class ImportsImpl(private val context: Context) : LlvmImports {
|
||||
|
||||
private val usedLibraries = mutableSetOf<KonanLibrary>()
|
||||
private val usedBitcode = mutableSetOf<KonanLibrary>()
|
||||
private val usedNativeDependencies = mutableSetOf<KonanLibrary>()
|
||||
|
||||
private val allLibraries by lazy { context.librariesWithDependencies.toSet() }
|
||||
|
||||
override fun add(origin: CompiledKonanModuleOrigin) {
|
||||
override fun add(origin: CompiledKonanModuleOrigin, onlyBitcode: Boolean) {
|
||||
val library = when (origin) {
|
||||
CurrentKonanModuleOrigin -> return
|
||||
is DeserializedKonanModuleOrigin -> origin.library
|
||||
@@ -350,16 +356,28 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
||||
error("Library (${library.libraryName}) is used but not requested.\nRequested libraries: ${allLibraries.joinToString { it.libraryName }}")
|
||||
}
|
||||
|
||||
usedLibraries.add(library)
|
||||
usedBitcode.add(library)
|
||||
if (!onlyBitcode) {
|
||||
usedNativeDependencies.add(library)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isImported(library: KonanLibrary): Boolean = library in usedLibraries
|
||||
override fun bitcodeIsUsed(library: KonanLibrary) = library in usedBitcode
|
||||
|
||||
override fun nativeDependenciesAreUsed(library: KonanLibrary) = library in usedNativeDependencies
|
||||
}
|
||||
|
||||
val librariesToLink: List<KonanLibrary> by lazy {
|
||||
val nativeDependenciesToLink: List<KonanLibrary> by lazy {
|
||||
context.config.resolvedLibraries
|
||||
.filterRoots { (!it.isDefault && !context.config.purgeUserLibs) || imports.isImported(it.library) }
|
||||
.getFullList(TopologicalLibraryOrder)
|
||||
.filter { (!it.isDefault && !context.config.purgeUserLibs) || imports.nativeDependenciesAreUsed(it) }
|
||||
|
||||
}
|
||||
|
||||
val bitcodeToLink: List<KonanLibrary> by lazy {
|
||||
context.config.resolvedLibraries
|
||||
.getFullList(TopologicalLibraryOrder)
|
||||
.filter { (!it.isDefault && !context.config.purgeUserLibs) || imports.bitcodeIsUsed(it) }
|
||||
}
|
||||
|
||||
val staticData = StaticData(context)
|
||||
|
||||
+3
-2
@@ -15,8 +15,9 @@ import org.jetbrains.kotlin.konan.library.KonanLibrary
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
|
||||
internal interface LlvmImports {
|
||||
fun add(origin: CompiledKonanModuleOrigin)
|
||||
fun isImported(library: KonanLibrary): Boolean
|
||||
fun add(origin: CompiledKonanModuleOrigin, onlyBitcode: Boolean = false)
|
||||
fun bitcodeIsUsed(library: KonanLibrary): Boolean
|
||||
fun nativeDependenciesAreUsed(library: KonanLibrary): Boolean
|
||||
}
|
||||
|
||||
internal val DeclarationDescriptor.llvmSymbolOrigin: CompiledKonanModuleOrigin
|
||||
|
||||
+2
-1
@@ -2123,7 +2123,8 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
||||
val protocolGetter = context.llvm.externalFunction(
|
||||
protocolGetterName,
|
||||
functionType(int8TypePtr, false),
|
||||
irClass.llvmSymbolOrigin
|
||||
irClass.llvmSymbolOrigin,
|
||||
independent = true // Protocol is header-only declaration.
|
||||
)
|
||||
|
||||
return call(protocolGetter, emptyList())
|
||||
|
||||
+2
-1
@@ -120,7 +120,8 @@ internal class KotlinObjCClassInfoGenerator(override val context: Context) : Con
|
||||
context.llvm.externalFunction(
|
||||
imp,
|
||||
functionType(voidType),
|
||||
origin = info.bridge.llvmSymbolOrigin
|
||||
origin = info.bridge.llvmSymbolOrigin,
|
||||
independent = true
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
+2
-1
@@ -397,7 +397,8 @@ private class DeclarationsGeneratorVisitor(override val context: Context) :
|
||||
|
||||
context.llvm.externalFunction(declaration.symbolName, llvmFunctionType,
|
||||
// Assume that `external fun` is defined in native libs attached to this module:
|
||||
origin = declaration.llvmSymbolOrigin
|
||||
origin = declaration.llvmSymbolOrigin,
|
||||
independent = declaration.hasAnnotation(RuntimeNames.independent)
|
||||
)
|
||||
} else {
|
||||
val symbolName = if (declaration.isExported()) {
|
||||
|
||||
@@ -104,4 +104,12 @@ internal annotation class PointsTo(vararg val onWhom: Int)
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
internal annotation class TypedIntrinsic(val kind: String)
|
||||
internal annotation class TypedIntrinsic(val kind: String)
|
||||
|
||||
/**
|
||||
* Indicates that `@SymbolName external` function is implemented in library-stored bitcode
|
||||
* and doesn't have native dependencies.
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class Independent
|
||||
Reference in New Issue
Block a user