From eaa6a75de4e60e3e8c42e2f76cef7c4536b5d95a Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 13 Jan 2022 12:37:38 +0300 Subject: [PATCH] ObjCExport: don't generate redundant exception handler for objc_release --- .../jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt | 6 ++++++ .../konan/llvm/objcexport/ObjCExportCodeGenerator.kt | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index f11737ca9f9..a3eb6454adf 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -661,6 +661,12 @@ internal abstract class FunctionGenerationContext( }.let {} // Force exhaustive. } + fun switchThreadStateIfExperimentalMM(state: ThreadState) { + if (context.memoryModel == MemoryModel.EXPERIMENTAL) { + switchThreadState(state) + } + } + fun memset(pointer: LLVMValueRef, value: Byte, size: Int, isVolatile: Boolean = false) = call(context.llvm.memsetFunction, listOf(pointer, 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 cc45628db12..4d27d778050 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 @@ -86,6 +86,13 @@ internal class ObjCExportFunctionGenerationContext( rawRet(result) } + fun objcReleaseFromRunnableThreadState(objCReference: LLVMValueRef) { + switchThreadStateIfExperimentalMM(ThreadState.Native) + // It is nounwind, so no exception handler is required. + call(objCExportCodegen.objcRelease, listOf(objCReference), exceptionHandler = ExceptionHandler.None) + switchThreadStateIfExperimentalMM(ThreadState.Runnable) + } + override fun processReturns() { // Do nothing. } @@ -1041,7 +1048,7 @@ private fun ObjCExportCodeGenerator.generateObjCImp( is MethodBridge.ReturnValue.WithError.ZeroForError -> { if (returnType.successBridge == MethodBridge.ReturnValue.Instance.InitResult) { // Release init receiver, as required by convention. - callFromBridge(objcRelease, listOf(param(0)), toNative = true) + objcReleaseFromRunnableThreadState(param(0)) } Zero(returnType.toLlvmRetType(context).llvmType).llvm }