diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt index cc3338f0a70..34a7d9986f8 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt @@ -5,18 +5,25 @@ package org.jetbrains.kotlin.backend.konan.llvm +import llvm.LLVMAddTargetDependentFunctionAttr import llvm.LLVMValueRef import org.jetbrains.kotlin.backend.konan.Context import org.jetbrains.kotlin.ir.declarations.IrConstructor import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.types.isNothing import org.jetbrains.kotlin.ir.util.isThrowable +import org.jetbrains.kotlin.konan.target.Family internal fun addLlvmAttributes(context: Context, irFunction: IrFunction, llvmFunction: LLVMValueRef) { if (irFunction.returnType.isNothing()) { setFunctionNoReturn(llvmFunction) } + if (shouldEnforceFramePointer(context)) { + // Note: this is default for clang on at least on iOS and macOS. + enforceFramePointer(llvmFunction) + } + if (mustNotInline(context, irFunction)) { setFunctionNoInline(llvmFunction) } @@ -32,3 +39,20 @@ private fun mustNotInline(context: Context, irFunction: IrFunction): Boolean { return false } + +private fun shouldEnforceFramePointer(context: Context): Boolean { + // TODO: do we still need it? + if (!context.shouldOptimize()) { + return true + } + + return when (context.config.target.family) { + Family.OSX, Family.IOS -> context.shouldContainLocationDebugInfo() + Family.LINUX, Family.MINGW, Family.ANDROID, Family.WASM, Family.ZEPHYR -> false + } +} + +private fun enforceFramePointer(llvmFunction: LLVMValueRef) { + LLVMAddTargetDependentFunctionAttr(llvmFunction, "no-frame-pointer-elim", "true") + LLVMAddTargetDependentFunctionAttr(llvmFunction, "no-frame-pointer-elim-non-leaf", "") +} diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt index bf313381b21..2434c0b01aa 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmDeclarations.kt @@ -371,11 +371,6 @@ private class DeclarationsGeneratorVisitor(override val context: Context) : function } - // TODO: do we still need it? - if (!context.shouldOptimize()) { - LLVMAddTargetDependentFunctionAttr(llvmFunction, "no-frame-pointer-elim", "true") - } - this.functions[declaration] = FunctionLlvmDeclarations(llvmFunction) } }