From: Dean Michael Berris Date: Wed, 11 Apr 2018 01:28:25 +0000 (+0000) Subject: [XRay][clang+compiler-rt] Support build-time mode selection X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9fe543498f5a2d2b058fee55c99a9ca52cc5353c;p=clang [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 --- diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index a08003b75a..e2c503b0c5 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -1108,6 +1108,10 @@ def fxray_attr_list : JoinedOrSeparate<["-"], "fxray-attr-list=">, Group, Flags<[CC1Option]>, HelpText<"Filename defining the list of functions/types for imbuing XRay attributes.">; +def fxray_modes : + JoinedOrSeparate<["-"], "fxray-modes=">, + 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, Flags<[CC1Option]>, diff --git a/include/clang/Driver/XRayArgs.h b/include/clang/Driver/XRayArgs.h index 535c030e8c..e4a573ee85 100644 --- a/include/clang/Driver/XRayArgs.h +++ b/include/clang/Driver/XRayArgs.h @@ -23,6 +23,7 @@ class XRayArgs { std::vector NeverInstrumentFiles; std::vector AttrListFiles; std::vector ExtraDeps; + std::vector 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 modeList() const { return Modes; } + }; } // namespace driver diff --git a/lib/Driver/ToolChains/CommonArgs.cpp b/lib/Driver/ToolChains/CommonArgs.cpp index 6a7f8179b6..dac81ee902 100644 --- a/lib/Driver/ToolChains/CommonArgs.cpp +++ b/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; } diff --git a/lib/Driver/XRayArgs.cpp b/lib/Driver/XRayArgs.cpp index cc109d10cf..bfc5053dcd 100644 --- a/lib/Driver/XRayArgs.cpp +++ b/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 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)); diff --git a/test/Driver/XRay/xray-mode-flags.cpp b/test/Driver/XRay/xray-mode-flags.cpp new file mode 100644 index 0000000000..b592e157af --- /dev/null +++ b/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