From c0f6c9b8c16d90ca6886daa8679685d485acc8a6 Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Tue, 23 Aug 2016 18:12:58 +0000 Subject: [PATCH] ARM-Darwin: ignore and diagnose attempts to omit frame pointer. iOS (and other 32-bit ARM variants) always require a valid frame pointer to improve backtraces. Previously the -fomit-frame-pointer and -momit-leaf-frame-pointer options were being silently discarded via hacks in the backend. It's better if Clang configures itself to emit the correct IR and warns about (ignored) attempts to override this. llvm-svn: 279546 --- clang/include/clang/Basic/DiagnosticDriverKinds.td | 3 +++ clang/lib/Driver/ToolChains.cpp | 12 ++++++++++ clang/lib/Driver/Tools.cpp | 28 +++++++++++++++++----- clang/test/Driver/frame-pointer-elim.c | 18 ++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 27bcd77..33d2af0 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -169,6 +169,9 @@ def warn_drv_optimization_value : Warning<"optimization level '%0' is not suppor InGroup; def warn_ignored_gcc_optimization : Warning<"optimization flag '%0' is not supported">, InGroup; +def warn_drv_unsupported_opt_for_target : Warning< + "optimization flag '%0' is not supported for target '%1'">, + InGroup; def warn_c_kext : Warning< "ignoring -fapple-kext which is valid for C++ and Objective-C++ only">; def warn_drv_input_file_unused : Warning< diff --git a/clang/lib/Driver/ToolChains.cpp b/clang/lib/Driver/ToolChains.cpp index 68d5214..4b8182e 100644 --- a/clang/lib/Driver/ToolChains.cpp +++ b/clang/lib/Driver/ToolChains.cpp @@ -1085,6 +1085,18 @@ DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args, } } + auto Arch = tools::darwin::getArchTypeForMachOArchName(BoundArch); + if ((Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)) { + if (Args.hasFlag(options::OPT_fomit_frame_pointer, + options::OPT_fno_omit_frame_pointer, false)) + getDriver().Diag(clang::diag::warn_drv_unsupported_opt_for_target) + << "-fomit-frame-pointer" << BoundArch; + if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer, + options::OPT_mno_omit_leaf_frame_pointer, false)) + getDriver().Diag(clang::diag::warn_drv_unsupported_opt_for_target) + << "-momit-leaf-frame-pointer" << BoundArch; + } + return DAL; } diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 747f3ea..895104e 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -3232,8 +3232,20 @@ static bool areOptimizationsEnabled(const ArgList &Args) { return false; } -static bool shouldUseFramePointerForTarget(const ArgList &Args, - const llvm::Triple &Triple) { +static bool mustUseFramePointerForTarget(const llvm::Triple &Triple) { + switch (Triple.getArch()){ + default: + return false; + case llvm::Triple::arm: + case llvm::Triple::thumb: + // ARM Darwin targets require a frame pointer to be always present to aid + // offline debugging via backtraces. + return Triple.isOSDarwin(); + } +} + +static bool useFramePointerForTargetByDefault(const ArgList &Args, + const llvm::Triple &Triple) { switch (Triple.getArch()) { case llvm::Triple::xcore: case llvm::Triple::wasm32: @@ -3285,25 +3297,29 @@ static bool shouldUseFramePointer(const ArgList &Args, const llvm::Triple &Triple) { if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer, options::OPT_fomit_frame_pointer)) - return A->getOption().matches(options::OPT_fno_omit_frame_pointer); + return A->getOption().matches(options::OPT_fno_omit_frame_pointer) || + mustUseFramePointerForTarget(Triple); + if (Args.hasArg(options::OPT_pg)) return true; - return shouldUseFramePointerForTarget(Args, Triple); + return useFramePointerForTargetByDefault(Args, Triple); } static bool shouldUseLeafFramePointer(const ArgList &Args, const llvm::Triple &Triple) { if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer, options::OPT_momit_leaf_frame_pointer)) - return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer); + return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer) || + mustUseFramePointerForTarget(Triple); + if (Args.hasArg(options::OPT_pg)) return true; if (Triple.isPS4CPU()) return false; - return shouldUseFramePointerForTarget(Args, Triple); + return useFramePointerForTargetByDefault(Args, Triple); } /// Add a CC1 option to specify the debug compilation directory. diff --git a/clang/test/Driver/frame-pointer-elim.c b/clang/test/Driver/frame-pointer-elim.c index 139c0cc..d2a2715 100644 --- a/clang/test/Driver/frame-pointer-elim.c +++ b/clang/test/Driver/frame-pointer-elim.c @@ -35,6 +35,24 @@ // RUN: FileCheck --check-prefix=OMIT_LEAF %s // OMIT_LEAF: "-momit-leaf-frame-pointer" +// RUN: %clang -### -target armv7s-apple-ios -fomit-frame-pointer %s 2>&1 | \ +// RUN: FileCheck --check-prefix=WARN-OMIT-7S %s +// WARN-OMIT-7S: warning: optimization flag '-fomit-frame-pointer' is not supported for target 'armv7s' +// WARN-OMIT-7S: "-mdisable-fp-elim" +// WARN-OMIT-7S-NOT: "-momit-leaf-frame-pointer" + +// RUN: %clang -### -target armv7k-apple-watchos -fomit-frame-pointer %s 2>&1 | \ +// RUN: FileCheck --check-prefix=WARN-OMIT-7K %s +// WARN-OMIT-7K: warning: optimization flag '-fomit-frame-pointer' is not supported for target 'armv7k' +// WARN-OMIT-7K: "-mdisable-fp-elim" +// WARN-OMIT-7K-NOT: "-momit-leaf-frame-pointer" + +// RUN: %clang -### -target armv7s-apple-ios8.0 -momit-leaf-frame-pointer %s 2>&1 | \ +// RUN: FileCheck --check-prefix=WARN-OMIT-LEAF-7S %s +// WARN-OMIT-LEAF-7S: warning: optimization flag '-momit-leaf-frame-pointer' is not supported for target 'armv7s' +// WARN-OMIT-LEAF-7S: "-mdisable-fp-elim" +// WARN-OMIT-LEAF-7S-NOT: "-momit-leaf-frame-pointer" + // On the PS4, we default to omitting the frame pointer on leaf functions // (OMIT_LEAF check line is above) // RUN: %clang -### -target x86_64-scei-ps4 -S %s 2>&1 | \ -- 2.7.4