[2/3] Support changing fentry name per function

Message ID 20181105000221.32180-2-andi@firstfloor.org
State New
Headers show
Series
  • [1/3] Support instrumenting returns of instrumented functions
Related show

Commit Message

Andi Kleen Nov. 5, 2018, 12:02 a.m.
From: Andi Kleen <ak@linux.intel.com>


It can be useful to have some classes of functions that use a different
__fentry__ instrumentation than others.  Currently it is only
possible to disable instrumentation on the command line or with
no_instrument_function, but not to change the instrumentation function
on a case by case base.

Add some flexibility to allow to change the instrumentation function
name per file with an option or per function with a new attribute.
This also allows switching to nops for individual functions.

gcc/:

2018-11-04  Andi Kleen  <ak@linux.intel.com>

	* config/i386/i386.c (x86_print_call_or_nop): Handle nop name.
	(current_fentry_name): Add.
	(x86_function_profiler): Handle fentry_name attribute.
	(ix86_handle_fentry_name): Add.
	(ix86_attribute_table): Add fentry_name.
	* config/i386/i386.opt: Add -mfentry-name
	* doc/extend.texi: Document fentry_name.
	* doc/invoke.texi: Document minstrument-return.

gcc/testsuite/:

2018-11-04  Andi Kleen  <ak@linux.intel.com>

	* gcc.target/i386/fentryname1.c: New test.
---
 gcc/config/i386/i386.c                      | 48 ++++++++++++++++++---
 gcc/config/i386/i386.opt                    |  4 ++
 gcc/doc/extend.texi                         |  7 +++
 gcc/doc/invoke.texi                         |  6 ++-
 gcc/testsuite/gcc.target/i386/fentryname1.c | 15 +++++++
 5 files changed, 74 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/fentryname1.c

-- 
2.19.1

Patch

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index f7cd94a8139..8af8a523cb3 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -41206,24 +41206,41 @@  x86_field_alignment (tree type, int computed)
 static void
 x86_print_call_or_nop (FILE *file, const char *target)
 {
-  if (flag_nop_mcount)
+  if (flag_nop_mcount || !strcmp (target, "nop"))
     /* 5 byte nop: nopl 0(%[re]ax,%[re]ax,1) */
     fprintf (file, "1:" ASM_BYTE "0x0f, 0x1f, 0x44, 0x00, 0x00\n");
   else
     fprintf (file, "1:\tcall\t%s\n", target);
 }
 
+static bool
+current_fentry_name (const char **name)
+{
+  tree attr = lookup_attribute ("fentry_name",
+				DECL_ATTRIBUTES (current_function_decl));
+  if (!attr)
+    return false;
+  *name = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
+  return true;
+}
+
 /* Output assembler code to FILE to increment profiler label # LABELNO
    for profiling a function entry.  */
 void
 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
 {
-  const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
-					 : MCOUNT_NAME);
-
   if (cfun->machine->endbr_queued_at_entrance)
     fprintf (file, "\t%s\n", TARGET_64BIT ? "endbr64" : "endbr32");
 
+  const char *mcount_name = MCOUNT_NAME;
+
+  if (current_fentry_name (&mcount_name))
+    ;
+  else if (fentry_name)
+    mcount_name = fentry_name;
+  else if (flag_fentry)
+    mcount_name = MCOUNT_NAME_BEFORE_PROLOGUE;
+
   if (TARGET_64BIT)
     {
 #ifndef NO_PROFILE_COUNTERS
@@ -45044,6 +45061,26 @@  ix86_expand_round_sse4 (rtx op0, rtx op1)
 
   emit_move_insn (op0, res);
 }
+
+/* Handle fentry_name attribute.  */
+
+static tree
+ix86_handle_fentry_name (tree *node, tree name, tree args,
+			 int, bool *no_add_attrs)
+{
+  if (TREE_CODE (*node) == FUNCTION_DECL
+      && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
+    /* Do nothing else, just set the attribute.  We'll get at
+       it later with lookup_attribute.  */
+    ;
+  else
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored", name);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
 
 
 /* Table of valid machine attributes.  */
@@ -45120,7 +45157,8 @@  static const struct attribute_spec ix86_attribute_table[] =
     ix86_handle_fndecl_attribute, NULL },
   { "indirect_return", 0, 0, false, true, true, false,
     NULL, NULL },
-
+  { "fentry_name", 1, 1, true, false, false, false,
+    ix86_handle_fentry_name, NULL },
   /* End element.  */
   { NULL, 0, 0, false, false, false, false, NULL, NULL }
 };
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
index 5925b75244f..e56f9bce9b0 100644
--- a/gcc/config/i386/i386.opt
+++ b/gcc/config/i386/i386.opt
@@ -930,6 +930,10 @@  Target Report Var(flag_nop_mcount)
 Generate mcount/__fentry__ calls as nops. To activate they need to be
 patched in.
 
+mfentry-name=
+Target RejectNegative Joined Var(fentry_name)
+Set name of __fentry__ symbol called at function entry.
+
 mskip-rax-setup
 Target Report Var(flag_skip_rax_setup)
 Skip setting up RAX register when passing variable arguments.
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 924037ff586..c8761cda151 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5966,6 +5966,13 @@  The @code{indirect_return} attribute can be applied to a function,
 as well as variable or type of function pointer to inform the
 compiler that the function may return via indirect branch.
 
+@item fentry_name("@var{name}")
+@cindex @code{fentry_name} function attribute, x86
+On x86 targets, the @code{fentry_name} attribute sets the function to
+call on function entry when function instrumentation is enabled
+with @option{-pg -mfentry}. When @var{name} is nop then a 5 byte
+nop sequence is generated.
+
 @end table
 
 On the x86, the inliner does not inline a
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 939be3e251b..aaa4a596a31 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1301,7 +1301,7 @@  See RS/6000 and PowerPC Options.
 -mcmodel=@var{code-model}  -mabi=@var{name}  -maddress-mode=@var{mode} @gol
 -m32  -m64  -mx32  -m16  -miamcu  -mlarge-data-threshold=@var{num} @gol
 -msse2avx  -mfentry  -mrecord-mcount  -mnop-mcount  -m8bit-idiv @gol
--minstrument-return=@var{type} @gol
+-minstrument-return=@var{type} -mfentry-name=@var{name} @gol
 -mavx256-split-unaligned-load  -mavx256-split-unaligned-store @gol
 -malign-data=@var{type}  -mstack-protector-guard=@var{guard} @gol
 -mstack-protector-guard-reg=@var{reg} @gol
@@ -28456,6 +28456,10 @@  or @var{nop5} to generate a 5 byte nop.
 @opindex mrecord-return
 Generate a __return_loc section pointing to all return instrumentation code.
 
+@item -mfentry-name=@var{name}
+@opindex mfentry-name
+Set name of __fentry__ symbol called at function entry for -pg -mfentry functions.
+
 @item -mskip-rax-setup
 @itemx -mno-skip-rax-setup
 @opindex mskip-rax-setup
diff --git a/gcc/testsuite/gcc.target/i386/fentryname1.c b/gcc/testsuite/gcc.target/i386/fentryname1.c
new file mode 100644
index 00000000000..6d2e76d223a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/fentryname1.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-options "-pg -mfentry -mfentry-name=foo" } */
+/* { dg-final { scan-assembler "call.*foo" } } */
+/* { dg-final { scan-assembler "call.*bar" } } */
+
+int func(int a)
+{
+  return a+1;
+}
+
+__attribute__((fentry_name("bar")))
+int func2(int a)
+{
+  return a+1;
+}