From 17c5647ea4122f25dc0bc2356f116296bdf10614 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 24 Dec 2019 15:58:22 +0300 Subject: [PATCH] DCE on linked binaries, to avoid huge size due to static cache. (#3715) (#3717) --- .../org/jetbrains/kotlin/backend/konan/Linker.kt | 15 ++++++++++----- .../llvm/objcexport/ObjCExportCodeGenerator.kt | 7 +++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt index c9724ddbeee..b6121d5fe3a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Linker.kt @@ -90,10 +90,15 @@ internal class Linker(val context: Context) { val executable: String if (context.config.produce != CompilerOutputKind.FRAMEWORK) { - if (context.config.produce == CompilerOutputKind.DYNAMIC_CACHE && target.family.isAppleFamily) - additionalLinkerArgs = listOf("-install_name", context.config.outputFiles.mainFile) - else - additionalLinkerArgs = emptyList() + additionalLinkerArgs = if (target.family.isAppleFamily) { + when (context.config.produce) { + CompilerOutputKind.DYNAMIC_CACHE -> + listOf("-install_name", context.config.outputFiles.mainFile) + else -> listOf("-dead_strip") + } + } else { + emptyList() + } executable = context.config.outputFiles.mainFileMangled } else { val framework = File(context.config.outputFile) @@ -105,7 +110,7 @@ internal class Linker(val context: Context) { Family.OSX -> "Versions/A/$dylibName" else -> error(target) } - additionalLinkerArgs = listOf("-install_name", "@rpath/${framework.name}/$dylibRelativePath") + additionalLinkerArgs = listOf("-dead_strip", "-install_name", "@rpath/${framework.name}/$dylibRelativePath") val dylibPath = framework.child(dylibRelativePath) dylibPath.parentFile.mkdirs() executable = dylibPath.absolutePath diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt index a66c1660d79..0a8d07d55b7 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt @@ -588,8 +588,11 @@ private fun ObjCExportBlockCodeGenerator.emitBlockToKotlinFunctionConverters() { val functionClassesByArity = context.ir.symbols.functionIrClassFactory.builtFunctionNClasses.associateBy { it.arity } - val count = (functionClassesByArity.keys.max() ?: -1) + 1 - + var count = ((functionClassesByArity.keys.max() ?: -1) + 1) + // TODO: ugly hack to avoid huge unneeded adaptors linked into every binary, needs rework. + if (context.config.produce.isCache) { + count = count.coerceAtMost(33) + } val converters = (0 until count).map { arity -> val functionClass = functionClassesByArity[arity] if (functionClass != null) {