From e37d669dc37ed369b39e2fdfb584f15f522b0c28 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Wed, 6 Oct 2021 17:47:14 +0700 Subject: [PATCH] [K/N] Opt-in for meaningful names for in "objc2kotlin" bridges It is hard to run FileCheck tests over generated ObjC bridges because they share the same name ("objc2kotlin"). To overcome this hurdle, this commit introduces a compiler flag that appends function name to "objc2_kotlin". This change might be useful for end-users, for example, it makes stacktraces more readable. But it will require additional testing and polishing which is out of the scope of this change. --- .../src/org/jetbrains/kotlin/cli/bc/K2Native.kt | 1 + .../kotlin/cli/bc/K2NativeCompilerArguments.kt | 3 +++ .../backend/konan/KonanConfigurationKeys.kt | 1 + .../llvm/objcexport/ObjCExportCodeGenerator.kt | 17 +++++++++++++++-- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index 71f734d4f07..1702a5c5355 100644 --- a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -391,6 +391,7 @@ class K2Native : CLICompiler() { }) putIfNotNull(RUNTIME_LOGS, arguments.runtimeLogs) putIfNotNull(BUNDLE_ID, parseBundleId(arguments, outputKind, configuration)) + put(MEANINGFUL_BRIDGE_NAMES, arguments.meaningfulBridgeNames) } } } diff --git a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt index bd563f79040..d5e865de967 100644 --- a/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt +++ b/kotlin-native/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt @@ -361,6 +361,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() { @Argument(value = "-Xruntime-logs", valueDescription = "", description = "Enable logging for runtime with tags.") var runtimeLogs: String? = null + @Argument(value = "-Xmeaningful-bridge-names", description = "(Unstable) Produce meaningful names for reverse bridges. Useful only for compiler tests.") + var meaningfulBridgeNames: Boolean = false + @Argument(value = "-Xlazy-ir-for-caches", valueDescription = "{disable|enable}", description = "Use lazy IR for cached libraries") var lazyIrForCaches: String? = null diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt index b3647358c38..b08310da73d 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt @@ -171,6 +171,7 @@ class KonanConfigKeys { val LLVM_VARIANT: CompilerConfigurationKey = CompilerConfigurationKey.create("llvm variant") val RUNTIME_LOGS: CompilerConfigurationKey = CompilerConfigurationKey.create("enable runtime logging") val LAZY_IR_FOR_CACHES: CompilerConfigurationKey = CompilerConfigurationKey.create("use lazy IR for cached libraries") + val MEANINGFUL_BRIDGE_NAMES: CompilerConfigurationKey = CompilerConfigurationKey.create("enable meaningful bridge names") } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt index bf058a6504e..0ca7ea8f793 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/objcexport/ObjCExportCodeGenerator.kt @@ -238,6 +238,13 @@ internal class ObjCExportCodeGenerator( generateContinuationToRetainedCompletionConverter(blockGenerator) } + fun meaningfulBridgeNameOrNull(irFunction: IrFunction?): String? { + if (!context.config.configuration.getBoolean(KonanConfigKeys.MEANINGFUL_BRIDGE_NAMES)) { + return null + } + return irFunction?.name?.asString() + } + fun FunctionGenerationContext.genSendMessage( returnType: LlvmParamType, parameterTypes: List, @@ -863,10 +870,12 @@ private fun ObjCExportFunctionGenerationContextBuilder.setupBridgeDebugInfo() { private inline fun ObjCExportCodeGenerator.generateObjCImpBy( methodBridge: MethodBridge, debugInfo: Boolean = false, + suffix: String? = null, genBody: ObjCExportFunctionGenerationContext.() -> Unit ): LLVMValueRef { val functionType = objCFunctionType(context, methodBridge) - val result = functionGenerator(functionType, "objc2kotlin") { + val functionName = "objc2kotlin" + (suffix?.let { "_$it" } ?: "") + val result = functionGenerator(functionType, functionName) { if (debugInfo) { this.setupBridgeDebugInfo() } @@ -926,7 +935,11 @@ private fun ObjCExportCodeGenerator.generateObjCImp( resultLifetime: Lifetime, exceptionHandler: ExceptionHandler ) -> LLVMValueRef? -): LLVMValueRef = generateObjCImpBy(methodBridge, debugInfo = isDirect /* see below */) { +): LLVMValueRef = generateObjCImpBy( + methodBridge, + debugInfo = isDirect /* see below */, + suffix = meaningfulBridgeNameOrNull(baseMethod) +) { // Considering direct calls inlinable above. If such a call is inlined into a bridge with no debug information, // lldb will not decode the inlined frame even if the callee has debug information. // So generate dummy debug information for bridge in this case.