Browse Source

[XRay][clang+compiler-rt] Support build-time mode selection

Summary:
This patch implements the `-fxray-modes=` flag which allows users
building with XRay instrumentation to decide which modes to pre-package
into the binary being linked. The default is the status quo, which will
link all the available modes.

For this to work we're also breaking apart the mode implementations
(xray-fdr and xray-basic) from the main xray runtime. This gives more
granular control of which modes are pre-packaged, and picked from
clang's invocation.

This fixes llvm.org/PR37066.

Note that in the future, we may change the default for clang to only
contain the profiling implementation under development in D44620, when
that implementation is ready.

Reviewers: echristo, eizan, chandlerc

Reviewed By: echristo

Subscribers: mgorny, mgrang, cfe-commits, llvm-commits

Differential Revision: https://reviews.llvm.org/D45474

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329772 91177308-0d34-0410-b5e6-96231b3b80d8
Dean Michael Berris 7 years ago
parent
commit
9fe543498f

+ 4 - 0
include/clang/Driver/Options.td

@@ -1108,6 +1108,10 @@ def fxray_attr_list :
   JoinedOrSeparate<["-"], "fxray-attr-list=">,
   Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Filename defining the list of functions/types for imbuing XRay attributes.">;
+def fxray_modes :
+  JoinedOrSeparate<["-"], "fxray-modes=">,
+  Group<f_Group>, Flags<[CC1Option]>,
+  HelpText<"List of modes to link in by default into XRay instrumented binaries.">;
 
 def fxray_always_emit_customevents : Flag<["-"], "fxray-always-emit-customevents">, Group<f_Group>,
   Flags<[CC1Option]>,

+ 3 - 0
include/clang/Driver/XRayArgs.h

@@ -23,6 +23,7 @@ class XRayArgs {
   std::vector<std::string> NeverInstrumentFiles;
   std::vector<std::string> AttrListFiles;
   std::vector<std::string> ExtraDeps;
+  std::vector<std::string> Modes;
   bool XRayInstrument = false;
   int InstructionThreshold = 200;
   bool XRayAlwaysEmitCustomEvents = false;
@@ -35,6 +36,8 @@ public:
                llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
 
   bool needsXRayRt() const { return XRayInstrument && XRayRT; }
+  llvm::ArrayRef<std::string> modeList() const { return Modes; }
+
 };
 
 } // namespace driver

+ 2 - 0
lib/Driver/ToolChains/CommonArgs.cpp

@@ -713,6 +713,8 @@ bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringLis
   if (TC.getXRayArgs().needsXRayRt()) {
     CmdArgs.push_back("-whole-archive");
     CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
+    for (const auto &Mode : TC.getXRayArgs().modeList())
+      CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode, false));
     CmdArgs.push_back("-no-whole-archive");
     return true;
   }

+ 35 - 6
lib/Driver/XRayArgs.cpp

@@ -27,6 +27,7 @@ namespace {
 constexpr char XRayInstrumentOption[] = "-fxray-instrument";
 constexpr char XRayInstructionThresholdOption[] =
     "-fxray-instruction-threshold=";
+constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
 } // namespace
 
 XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
@@ -51,13 +52,14 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
       }
     } else if (Triple.getOS() == llvm::Triple::FreeBSD ||
                Triple.getOS() == llvm::Triple::OpenBSD) {
-        if (Triple.getArch() != llvm::Triple::x86_64) {
-          D.Diag(diag::err_drv_clang_unsupported)
-              << (std::string(XRayInstrumentOption) + " on " + Triple.str());
-        }
+      if (Triple.getArch() != llvm::Triple::x86_64) {
+        D.Diag(diag::err_drv_clang_unsupported)
+            << (std::string(XRayInstrumentOption) + " on " + Triple.str());
+      }
     } else {
       D.Diag(diag::err_drv_clang_unsupported)
-          << (std::string(XRayInstrumentOption) + " on non-supported target OS");
+          << (std::string(XRayInstrumentOption) +
+              " on non-supported target OS");
     }
     XRayInstrument = true;
     if (const Arg *A =
@@ -108,6 +110,33 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
       } else
         D.Diag(clang::diag::err_drv_no_such_file) << Filename;
     }
+
+    // Get the list of modes we want to support.
+    auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
+    if (SpecifiedModes.empty())
+      llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
+    else
+      for (const auto &Arg : SpecifiedModes) {
+        if (Arg == "none") {
+          Modes.clear();
+          break;
+        }
+        if (Arg == "all") {
+          Modes.clear();
+          llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
+          break;
+        }
+
+        // Parse CSV values for -fxray-modes=...
+        llvm::SmallVector<StringRef, 2> ModeParts;
+        llvm::SplitString(Arg, ModeParts, ",");
+        for (const auto &M : ModeParts)
+          Modes.push_back(M);
+      }
+
+    // Then we want to sort and unique the modes we've collected.
+    std::sort(Modes.begin(), Modes.end());
+    Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
   }
 }
 
@@ -136,7 +165,7 @@ void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
     CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
   }
 
-  for (const auto&AttrFile : AttrListFiles) {
+  for (const auto& AttrFile : AttrListFiles) {
     SmallString<64> AttrListFileOpt("-fxray-attr-list=");
     AttrListFileOpt += AttrFile;
     CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));

+ 21 - 0
test/Driver/XRay/xray-mode-flags.cpp

@@ -0,0 +1,21 @@
+// RUN: %clang -v -o /dev/null -fxray-instrument -fxray-modes=xray-fdr %s -### \
+// RUN:     2>&1 | FileCheck --check-prefix FDR %s
+// RUN: %clang -v -o /dev/null -fxray-instrument -fxray-modes=xray-basic %s \
+// RUN:     -### 2>&1 | FileCheck --check-prefix BASIC %s
+// RUN: %clang -v -o /dev/null -fxray-instrument -fxray-modes=all -### %s \
+// RUN:     2>&1 | FileCheck --check-prefixes FDR,BASIC %s
+// RUN: %clang -v -o /dev/null -fxray-instrument \
+// RUN:     -fxray-modes=xray-fdr,xray-basic -### %s 2>&1 | \
+// RUN:     FileCheck --check-prefixes FDR,BASIC %s
+// RUN: %clang -v -o /dev/null -fxray-instrument \
+// RUN:     -fxray-modes=xray-fdr -fxray-modes=xray-basic -### %s 2>&1 | \
+// RUN:     FileCheck --check-prefixes FDR,BASIC %s
+// RUN: %clang -v -o /dev/null -fxray-instrument -### %s \
+// RUN:     2>&1 | FileCheck --check-prefixes FDR,BASIC %s
+// RUN: %clang -v -o /dev/null -fxray-instrument -fxray-modes=none -### %s \
+// RUN:     2>&1 | FileCheck --check-prefixes NONE %s
+
+// BASIC: libclang_rt.xray-basic
+// FDR: libclang_rt.xray-fdr
+// NONE-NOT: libclang_rt.xray-basic
+// NONE-NOT: libclang_rt.xray-fdr