[Driver] Allow -mnop-mcount for SystemZ and -mfentry for X86 and SystemZ
authorFangrui Song <maskray@google.com>
Sun, 22 Dec 2019 07:18:24 +0000 (23:18 -0800)
committerFangrui Song <maskray@google.com>
Sun, 22 Dec 2019 08:01:42 +0000 (00:01 -0800)
gcc/config/{i386,s390} support -mnop-mcount. We currently only support
-mnop-mcount for SystemZ. The function attribute "mnop-mcount" is
ignored on other targets.

gcc/config/{i386,s390} support -mfentry. We currently only support
-mfentry for X86 and SystemZ. TargetOpcode::FENTRY_CALL is not handled
on other targets.

  % clang -target aarch64 -pg -mfentry a.c -c
  fatal error: error in backend: Not supported instr: <MCInst 21>

-mfentry, -mrecord-mcount, and -mnop-mcount were invented for Linux
ftrace. Linux uses $(call cc-option-yn,-mrecord-mcount) to detect if the
specific feature is available. Reject unsupported features so that Linux
build system will not wrongly consider them available and cause
build/runtime failures.

Note, GCC has stricter checks that we do not implement, e.g. -fpic/-fpie
-fnop-mcount is not allowed on x86, -fpic/-fpie -mfentry is not allowed
on x86-32.

clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/mcount.c
clang/test/Driver/mfentry.c [new file with mode: 0644]

index 0310094..1b8eca0 100644 (file)
@@ -4986,18 +4986,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   const XRayArgs &XRay = TC.getXRayArgs();
   XRay.addArgs(TC, Args, CmdArgs, InputType);
 
-  if (TC.SupportsProfiling())
+  if (TC.SupportsProfiling()) {
     Args.AddLastArg(CmdArgs, options::OPT_pg);
 
-  if (TC.SupportsProfiling())
-    Args.AddLastArg(CmdArgs, options::OPT_mfentry);
-
-  if (TC.SupportsProfiling())
-    Args.AddLastArg(CmdArgs, options::OPT_mnop_mcount);
-
-  if (TC.SupportsProfiling()) {
+    llvm::Triple::ArchType Arch = TC.getArch();
+    if (Arg *A = Args.getLastArg(options::OPT_mfentry)) {
+      if (Arch == llvm::Triple::systemz || Arch == llvm::Triple::x86 ||
+          Arch == llvm::Triple::x86_64)
+        A->render(Args, CmdArgs);
+      else
+        D.Diag(diag::err_drv_unsupported_opt_for_target)
+            << A->getAsString(Args) << TripleStr;
+    }
+    if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) {
+      if (Arch == llvm::Triple::systemz)
+        A->render(Args, CmdArgs);
+      else
+        D.Diag(diag::err_drv_unsupported_opt_for_target)
+            << A->getAsString(Args) << TripleStr;
+    }
     if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) {
-      if (TC.getArch() == llvm::Triple::systemz)
+      if (Arch == llvm::Triple::systemz)
         A->render(Args, CmdArgs);
       else
         D.Diag(diag::err_drv_unsupported_opt_for_target)
index 7086abf..a89150c 100644 (file)
@@ -1,8 +1,12 @@
-// RUN: %clang -target s390x -c -### %s -mrecord-mcount 2>&1 | FileCheck %s
+// RUN: %clang -target s390x -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck %s
+
+// CHECK: "-mnop-mcount"
 // CHECK: "-mrecord-mcount"
 
-// RUN: %clang -target x86_64 -c -### %s -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR1 %s
-// RUN: %clang -target aarch64 -c -### %s -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR2 %s
+// RUN: %clang -target x86_64 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR1 %s
+// RUN: %clang -target aarch64 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR2 %s
 
+// ERR1: error: unsupported option '-mnop-mcount' for target 'x86_64'
 // ERR1: error: unsupported option '-mrecord-mcount' for target 'x86_64'
+// ERR2: error: unsupported option '-mnop-mcount' for target 'aarch64'
 // ERR2: error: unsupported option '-mrecord-mcount' for target 'aarch64'
diff --git a/clang/test/Driver/mfentry.c b/clang/test/Driver/mfentry.c
new file mode 100644 (file)
index 0000000..ee402ea
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang -target s390x -c -### %s -mfentry 2>&1 | FileCheck %s
+// RUN: %clang -target i386 -c -### %s -mfentry 2>&1 | FileCheck %s
+// RUN: %clang -target x86_64 -c -### %s -mfentry 2>&1 | FileCheck %s
+
+// CHECK: "-mfentry"
+
+// RUN: %clang -target powerpc64le -c -### %s -mfentry 2>&1 | FileCheck --check-prefix=ERR %s
+
+// ERR: error: unsupported option '-mfentry' for target 'powerpc64le'