Skip to content

[XRay] Fix argument parsing with offloading (#140748) #141043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class ToolChain {
Tool *getLinkerWrapper() const;

mutable bool SanitizerArgsChecked = false;
mutable std::unique_ptr<XRayArgs> XRayArguments;

/// The effective clang triple for the current Job.
mutable llvm::Triple EffectiveTriple;
Expand Down Expand Up @@ -323,7 +322,7 @@ class ToolChain {

SanitizerArgs getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const;

const XRayArgs& getXRayArgs() const;
const XRayArgs getXRayArgs(const llvm::opt::ArgList &) const;

// Returns the Arg * that explicitly turned on/off rtti, or nullptr.
const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,9 @@ ToolChain::getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const {
return SanArgs;
}

const XRayArgs& ToolChain::getXRayArgs() const {
if (!XRayArguments)
XRayArguments.reset(new XRayArgs(*this, Args));
return *XRayArguments;
const XRayArgs ToolChain::getXRayArgs(const llvm::opt::ArgList &JobArgs) const {
XRayArgs XRayArguments(*this, JobArgs);
return XRayArguments;
}

namespace {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6913,7 +6913,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("--offload-new-driver");
}

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

for (const auto &Filename :
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1647,17 +1647,18 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
}

bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
const XRayArgs &XRay = TC.getXRayArgs(Args);
if (Args.hasArg(options::OPT_shared)) {
if (TC.getXRayArgs().needsXRayDSORt()) {
if (XRay.needsXRayDSORt()) {
CmdArgs.push_back("--whole-archive");
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso"));
CmdArgs.push_back("--no-whole-archive");
return true;
}
} else if (TC.getXRayArgs().needsXRayRt()) {
} else if (XRay.needsXRayRt()) {
CmdArgs.push_back("--whole-archive");
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray"));
for (const auto &Mode : TC.getXRayArgs().modeList())
for (const auto &Mode : XRay.modeList())
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode));
CmdArgs.push_back("--no-whole-archive");
return true;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
CmdArgs,
llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data());

const XRayArgs &XRay = getXRayArgs();
const XRayArgs &XRay = getXRayArgs(Args);
if (XRay.needsXRayRt()) {
AddLinkRuntimeLib(Args, CmdArgs, "xray");
AddLinkRuntimeLib(Args, CmdArgs, "xray-basic");
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Driver/XRay/xray-instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s
// RUN: not %clang -### --target=x86_64-pc-windows -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR

/// Checking -fxray-instrument with offloading and -Xarch_host
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -Xarch_host -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -x hip --offload-arch=gfx906 -nogpulib -nogpuinc -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -x hip --offload-arch=gfx906 -nogpulib -nogpuinc -Xarch_host -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s

// CHECK: "-cc1" {{.*}}"-fxray-instrument"
// ERR: error: unsupported option '-fxray-instrument' for target

Expand Down