From 746dcfdb7a4b983641ff52b58187c33f77e172fe Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Wed, 31 Jul 2019 11:38:50 +0300 Subject: [PATCH] Enforce frame pointer for code with debug info on iOS and macOS To improve call stack scanning in optimized -Xg0 binaries. --- .../backend/konan/llvm/LlvmAttributes.kt | 24 +++++++++++++++++++ .../backend/konan/llvm/LlvmDeclarations.kt | 5 ---- 2 files changed, 24 insertions(+), 5 deletions(-) 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) } }