[PATCH] D67763: [Clang FE] Recognize -mnop-mcount CL option

Jonas Paulsson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 07:44:31 PDT 2019


jonpa created this revision.
jonpa added reviewers: uweigand, hfinkel, rengolin.

Recognize -mnop-mcount from the command line and add a function attribute "mnop-mcount"="true" when passed.

When this option is passed, a nop is added instead of a call to fentry. This is used when building the Linux Kernel.

I followed the steps of fentry and implemented this the same way. The (SystemZ) backend can recognize the function attribute and output the nop instead of the call (patch pending). Is this approach acceptable?


https://reviews.llvm.org/D67763

Files:
  include/clang/Basic/CodeGenOptions.def
  include/clang/Driver/Options.td
  lib/CodeGen/CodeGenFunction.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/mnop-mcount.c


Index: test/CodeGen/mnop-mcount.c
===================================================================
--- /dev/null
+++ test/CodeGen/mnop-mcount.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -pg -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s | FileCheck  %s
+// RUN: %clang_cc1 -pg -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s | FileCheck  %s
+// RUN: %clang_cc1 -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s | FileCheck -check-prefix=NOPG %s
+// RUN: %clang_cc1 -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s | FileCheck -check-prefix=NOPG %s
+
+int foo(void) {
+  return 0;
+}
+
+int __attribute__((no_instrument_function)) no_instrument(void) {
+  return foo();
+}
+
+//CHECK: attributes #0 = { {{.*}}"mnop-mcount"="true"{{.*}} }
+//CHECK: attributes #1 = { {{.*}} }
+//CHECK-NOT: attributes #1 = { {{.*}}"mnop-mcount"="true"{{.*}} }
+//NOPG-NOT: attributes #0 = { {{.*}}"mnop-mcount"{{.*}} }
+//NOPG-NOT: attributes #1 = { {{.*}}"mnop-mcount"{{.*}} }
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -1092,6 +1092,7 @@
 
   Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
   Opts.CallFEntry = Args.hasArg(OPT_mfentry);
+  Opts.MNopMCount = Args.hasArg(OPT_mnop_mcount);
   Opts.EmitOpenCLArgMetadata = Args.hasArg(OPT_cl_kernel_arg_info);
 
   if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) {
Index: lib/Driver/ToolChains/Clang.cpp
===================================================================
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -4688,6 +4688,9 @@
   if (TC.SupportsProfiling())
     Args.AddLastArg(CmdArgs, options::OPT_mfentry);
 
+  if (TC.SupportsProfiling())
+    Args.AddLastArg(CmdArgs, options::OPT_mnop_mcount);
+
   if (Args.getLastArg(options::OPT_fapple_kext) ||
       (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
     CmdArgs.push_back("-fapple-kext");
Index: lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- lib/CodeGen/CodeGenFunction.cpp
+++ lib/CodeGen/CodeGenFunction.cpp
@@ -889,6 +889,8 @@
         Fn->addFnAttr("instrument-function-entry-inlined",
                       getTarget().getMCountName());
       }
+      if (CGM.getCodeGenOpts().MNopMCount)
+        Fn->addFnAttr("mnop-mcount", "true");
     }
   }
 
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -2383,6 +2383,8 @@
 def mno_pie_copy_relocations : Flag<["-"], "mno-pie-copy-relocations">, Group<m_Group>;
 def mfentry : Flag<["-"], "mfentry">, HelpText<"Insert calls to fentry at function entry (x86 only)">,
   Flags<[CC1Option]>, Group<m_Group>;
+def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
+  Flags<[CC1Option]>, Group<m_Group>;
 def mips16 : Flag<["-"], "mips16">, Group<m_mips_Features_Group>;
 def mno_mips16 : Flag<["-"], "mno-mips16">, Group<m_mips_Features_Group>;
 def mmicromips : Flag<["-"], "mmicromips">, Group<m_mips_Features_Group>;
Index: include/clang/Basic/CodeGenOptions.def
===================================================================
--- include/clang/Basic/CodeGenOptions.def
+++ include/clang/Basic/CodeGenOptions.def
@@ -109,6 +109,7 @@
 
 CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled.
 CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled.
+CODEGENOPT(MNopMCount , 1, 0) ///< Set when -mnop-mcount is enabled.
 CODEGENOPT(LessPreciseFPMAD  , 1, 0) ///< Enable less precise MAD instructions to
                                      ///< be generated.
 CODEGENOPT(PrepareForLTO     , 1, 0) ///< Set when -flto is enabled on the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67763.220864.patch
Type: text/x-patch
Size: 3951 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190919/ced0b074/attachment.bin>


More information about the llvm-commits mailing list