Skip to content

Commit 3fe3baf

Browse files
sebastiankreutzersivan-shani
authored andcommitted
[XRay] Fix argument parsing with offloading (llvm#140748) (llvm#141043)
This PR addressed issue llvm#140748 to support XRay instrumentation on the host side when using offloading. It makes the following changes: - Initializes `XRayArgs` using the processed toolchain arguments instead of the raw input. - Removes the current caching mechanism of `XRayArgs` in the `ToolChain` class, as this is error-prone and potential benefits are questionable. For reference, `SanitizierArgs`, which is constructed in a similar manner but is much more complex, does not use any caching. - Adds driver tests to verify that XRay flags are set correctly with offloading and `-Xarch_host`.
1 parent ff79d58 commit 3fe3baf

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ class ToolChain {
179179
Tool *getLinkerWrapper() const;
180180

181181
mutable bool SanitizerArgsChecked = false;
182-
mutable std::unique_ptr<XRayArgs> XRayArguments;
183182

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

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

326-
const XRayArgs& getXRayArgs() const;
325+
const XRayArgs getXRayArgs(const llvm::opt::ArgList &) const;
327326

328327
// Returns the Arg * that explicitly turned on/off rtti, or nullptr.
329328
const llvm::opt::Arg *getRTTIArg() const { return CachedRTTIArg; }

clang/lib/Driver/ToolChain.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,9 @@ ToolChain::getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const {
389389
return SanArgs;
390390
}
391391

392-
const XRayArgs& ToolChain::getXRayArgs() const {
393-
if (!XRayArguments)
394-
XRayArguments.reset(new XRayArgs(*this, Args));
395-
return *XRayArguments;
392+
const XRayArgs ToolChain::getXRayArgs(const llvm::opt::ArgList &JobArgs) const {
393+
XRayArgs XRayArguments(*this, JobArgs);
394+
return XRayArguments;
396395
}
397396

398397
namespace {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6913,7 +6913,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
69136913
CmdArgs.push_back("--offload-new-driver");
69146914
}
69156915

6916-
const XRayArgs &XRay = TC.getXRayArgs();
6916+
const XRayArgs &XRay = TC.getXRayArgs(Args);
69176917
XRay.addArgs(TC, Args, CmdArgs, InputType);
69186918

69196919
for (const auto &Filename :

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,17 +1647,18 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
16471647
}
16481648

16491649
bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
1650+
const XRayArgs &XRay = TC.getXRayArgs(Args);
16501651
if (Args.hasArg(options::OPT_shared)) {
1651-
if (TC.getXRayArgs().needsXRayDSORt()) {
1652+
if (XRay.needsXRayDSORt()) {
16521653
CmdArgs.push_back("--whole-archive");
16531654
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso"));
16541655
CmdArgs.push_back("--no-whole-archive");
16551656
return true;
16561657
}
1657-
} else if (TC.getXRayArgs().needsXRayRt()) {
1658+
} else if (XRay.needsXRayRt()) {
16581659
CmdArgs.push_back("--whole-archive");
16591660
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray"));
1660-
for (const auto &Mode : TC.getXRayArgs().modeList())
1661+
for (const auto &Mode : XRay.modeList())
16611662
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode));
16621663
CmdArgs.push_back("--no-whole-archive");
16631664
return true;

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
16271627
CmdArgs,
16281628
llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data());
16291629

1630-
const XRayArgs &XRay = getXRayArgs();
1630+
const XRayArgs &XRay = getXRayArgs(Args);
16311631
if (XRay.needsXRayRt()) {
16321632
AddLinkRuntimeLib(Args, CmdArgs, "xray");
16331633
AddLinkRuntimeLib(Args, CmdArgs, "xray-basic");

clang/test/Driver/XRay/xray-instrument.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// RUN: %clang -### --target=x86_64-apple-darwin -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s
44
// RUN: not %clang -### --target=x86_64-pc-windows -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
55

6+
/// Checking -fxray-instrument with offloading and -Xarch_host
7+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -Xarch_host -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s
8+
// 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
9+
// 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
10+
611
// CHECK: "-cc1" {{.*}}"-fxray-instrument"
712
// ERR: error: unsupported option '-fxray-instrument' for target
813

0 commit comments

Comments
 (0)