[PATCH] D14215: Disable frame pointer elimination when using -pg

Stefan Kempf via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 1 04:12:31 PST 2015

sisnkemp created this revision.
sisnkemp added a subscriber: cfe-commits.

When using -pg in combination with optimizations (e.g. -O3), compiled programs crash in the profiling routine mcount().
This is because mcount (on x86_64 at least) relies on the frame pointer to be valid. See test case to reproduce.

This diff makes sure that the driver does not pass -fomit-frame-pointer or -momit-leaf-frame-pointer to the frontend when -pg is used. Currently, clang gives an error if -fomit-frame-pointer is used in combination with -pg, but -momit-leaf-frame-pointer was forgotten.
Also, disable frame pointer elimination in the frontend when -pg is set.

The behavior is consistent with gcc now: clang produces an error if -fomit-frame-pointer is used with -pg, and silently disables -momit-leaf-frame-pointer when -pg is used.



Index: test/CodeGen/x86_64-profiling-keep-fp.c
--- /dev/null
+++ test/CodeGen/x86_64-profiling-keep-fp.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -O3 -pg -S -o - %s | \
+// RUN:   FileCheck %s
+// Test that the frame pointer is kept when compiling with
+// profiling.
+//CHECK: pushq %rbp
+int main(void)
+  return 0;
Index: lib/Frontend/CompilerInvocation.cpp
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -450,7 +450,8 @@
   Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);
   Opts.CodeModel = getCodeModel(Args, Diags);
   Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);
-  Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);
+  Opts.DisableFPElim =
+      (Args.hasArg(OPT_mdisable_fp_elim) || Args.hasArg(OPT_pg));
   Opts.DisableFree = Args.hasArg(OPT_disable_free);
   Opts.DisableTailCalls = Args.hasArg(OPT_mdisable_tail_calls);
   Opts.FloatABI = Args.getLastArgValue(OPT_mfloat_abi);
Index: lib/Driver/Tools.cpp
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -2716,6 +2716,8 @@
   if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
     return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
+  if (Args.hasArg(options::OPT_pg))
+    return true;
   return shouldUseFramePointerForTarget(Args, Triple);
@@ -2725,6 +2727,8 @@
   if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
     return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
+  if (Args.hasArg(options::OPT_pg))
+    return true;
   if (Triple.isPS4CPU())
     return false;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14215.38842.patch
Type: text/x-patch
Size: 1974 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151101/04019d11/attachment.bin>

More information about the cfe-commits mailing list