35 changes: 4 additions & 31 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/SanitizerArgs.h"
#include "clang/Driver/XRayArgs.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/CodeGen.h"
Expand Down Expand Up @@ -2818,37 +2819,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,

Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);

if (Args.hasFlag(options::OPT_fxray_instrument,
options::OPT_fnoxray_instrument, false)) {
const char *const XRayInstrumentOption = "-fxray-instrument";
if (Triple.getOS() == llvm::Triple::Linux)
switch (Triple.getArch()) {
case llvm::Triple::x86_64:
case llvm::Triple::arm:
case llvm::Triple::aarch64:
case llvm::Triple::ppc64le:
case llvm::Triple::mips:
case llvm::Triple::mipsel:
case llvm::Triple::mips64:
case llvm::Triple::mips64el:
// Supported.
break;
default:
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-Linux target OS");
CmdArgs.push_back(XRayInstrumentOption);
if (const Arg *A =
Args.getLastArg(options::OPT_fxray_instruction_threshold_,
options::OPT_fxray_instruction_threshold_EQ)) {
CmdArgs.push_back("-fxray-instruction-threshold");
CmdArgs.push_back(A->getValue());
}
}

addPGOAndCoverageFlags(C, D, Output, Args, CmdArgs);

// Add runtime flag for PS4 when PGO or Coverage are enabled.
Expand Down Expand Up @@ -3238,6 +3208,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
Sanitize.addArgs(getToolChain(), Args, CmdArgs, InputType);

const XRayArgs &XRay = getToolChain().getXRayArgs();
XRay.addArgs(getToolChain(), Args, CmdArgs, InputType);

if (getToolChain().SupportsProfiling())
Args.AddLastArg(CmdArgs, options::OPT_pg);

Expand Down
113 changes: 113 additions & 0 deletions clang/lib/Driver/XRayArgs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Driver/XRayArgs.h"
#include "ToolChains/CommonArgs.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/ToolChain.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SpecialCaseList.h"

using namespace clang;
using namespace clang::driver;
using namespace llvm::opt;

namespace {
constexpr char XRayInstrumentOption[] = "-fxray-instrument";
constexpr char XRayInstructionThresholdOption[] =
"-fxray-instruction-threshold=";
constexpr char XRayAlwaysInstrumentOption[] = "-fxray-always-instrument=";
constexpr char XRayNeverInstrumentOption[] = "-fxray-never-instrument=";
} // namespace

XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
const Driver &D = TC.getDriver();
const llvm::Triple &Triple = TC.getTriple();
if (Args.hasFlag(options::OPT_fxray_instrument,
options::OPT_fnoxray_instrument, false)) {
if (Triple.getOS() == llvm::Triple::Linux)
switch (Triple.getArch()) {
case llvm::Triple::x86_64:
case llvm::Triple::arm:
case llvm::Triple::aarch64:
case llvm::Triple::ppc64le:
case llvm::Triple::mips:
case llvm::Triple::mipsel:
case llvm::Triple::mips64:
case llvm::Triple::mips64el:
break;
default:
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-Linux target OS");
XRayInstrument = true;
if (const Arg *A =
Args.getLastArg(options::OPT_fxray_instruction_threshold_,
options::OPT_fxray_instruction_threshold_EQ)) {
StringRef S = A->getValue();
if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
}

// Validate the always/never attribute files. We also make sure that they
// are treated as actual dependencies.
for (const auto &Filename :
Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
if (llvm::sys::fs::exists(Filename)) {
AlwaysInstrumentFiles.push_back(Filename);
ExtraDeps.push_back(Filename);
} else
D.Diag(clang::diag::err_drv_no_such_file) << Filename;
}

for (const auto &Filename :
Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
if (llvm::sys::fs::exists(Filename)) {
NeverInstrumentFiles.push_back(Filename);
ExtraDeps.push_back(Filename);
} else
D.Diag(clang::diag::err_drv_no_such_file) << Filename;
}
}
}

void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
ArgStringList &CmdArgs, types::ID InputType) const {
if (!XRayInstrument)
return;

CmdArgs.push_back(XRayInstrumentOption);
CmdArgs.push_back(Args.MakeArgString(XRayInstructionThresholdOption +
std::to_string(InstructionThreshold)));

for (const auto &Always : AlwaysInstrumentFiles) {
SmallString<64> AlwaysInstrumentOpt(XRayAlwaysInstrumentOption);
AlwaysInstrumentOpt += Always;
CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
}

for (const auto &Never : NeverInstrumentFiles) {
SmallString<64> NeverInstrumentOpt(XRayNeverInstrumentOption);
NeverInstrumentOpt += Never;
CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
}

for (const auto &Dep : ExtraDeps) {
SmallString<64> ExtraDepOpt("-fdepfile-entry=");
ExtraDepOpt += Dep;
CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
}
}
8 changes: 8 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,14 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Opts.SanitizeAddressFieldPadding =
getLastArgIntValue(Args, OPT_fsanitize_address_field_padding, 0, Diags);
Opts.SanitizerBlacklistFiles = Args.getAllArgValues(OPT_fsanitize_blacklist);

// -fxray-{always,never}-instrument= filenames.
Opts.XRayInstrument =
Args.hasFlag(OPT_fxray_instrument, OPT_fnoxray_instrument, false);
Opts.XRayAlwaysInstrumentFiles =
Args.getAllArgValues(OPT_fxray_always_instrument);
Opts.XRayNeverInstrumentFiles =
Args.getAllArgValues(OPT_fxray_never_instrument);
}

static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CodeGen/xray-always-instrument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: echo "fun:*foo*" > %t.always-instrument
// RUN: echo "src:*xray-always-instrument.cpp" >> %t.always-instrument
// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 -fxray-always-instrument=%t.always-instrument -emit-llvm -o - %s -triple x86_64-unknown-linux-gnu | FileCheck %s

void foo() {}

[[clang::xray_never_instrument]] void bar() {}

void baz() {}

// CHECK: define void @_Z3foov() #[[ALWAYSATTR:[0-9]+]] {
// CHECK: define void @_Z3barv() #[[NEVERATTR:[0-9]+]] {
// CHECK: define void @_Z3bazv() #[[ALWAYSATTR:[0-9]+]] {
// CHECK: attributes #[[ALWAYSATTR]] = {{.*}} "function-instrument"="xray-always" {{.*}}
// CHECK: attributes #[[NEVERATTR]] = {{.*}} "function-instrument"="xray-never" {{.*}}