Skip to content

Commit c8272c5

Browse files
tcgu-amdsebastiankreutzerlamb-j
authored
[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`. --------- Signed-off-by: Tim <[email protected]> Co-authored-by: Sebastian Kreutzer <[email protected]> Co-authored-by: Lambert, Jacob <[email protected]>
1 parent d6a61ad commit c8272c5

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
@@ -180,7 +180,6 @@ class ToolChain {
180180
Tool *getLinkerWrapper() const;
181181

182182
mutable bool SanitizerArgsChecked = false;
183-
mutable std::unique_ptr<XRayArgs> XRayArguments;
184183

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

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

325-
const XRayArgs& getXRayArgs() const;
324+
const XRayArgs getXRayArgs(const llvm::opt::ArgList &) const;
326325

327326
// Returns the Arg * that explicitly turned on/off rtti, or nullptr.
328327
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
@@ -393,10 +393,9 @@ ToolChain::getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const {
393393
return SanArgs;
394394
}
395395

396-
const XRayArgs& ToolChain::getXRayArgs() const {
397-
if (!XRayArguments)
398-
XRayArguments.reset(new XRayArgs(*this, Args));
399-
return *XRayArguments;
396+
const XRayArgs ToolChain::getXRayArgs(const llvm::opt::ArgList &JobArgs) const {
397+
XRayArgs XRayArguments(*this, JobArgs);
398+
return XRayArguments;
400399
}
401400

402401
namespace {

clang/lib/Driver/ToolChains/Clang.cpp

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

7086-
const XRayArgs &XRay = TC.getXRayArgs();
7086+
const XRayArgs &XRay = TC.getXRayArgs(Args);
70877087
XRay.addArgs(TC, Args, CmdArgs, InputType);
70887088

70897089
for (const auto &Filename :

clang/lib/Driver/ToolChains/CommonArgs.cpp

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

18321832
bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
1833+
const XRayArgs &XRay = TC.getXRayArgs(Args);
18331834
if (Args.hasArg(options::OPT_shared)) {
1834-
if (TC.getXRayArgs().needsXRayDSORt()) {
1835+
if (XRay.needsXRayDSORt()) {
18351836
CmdArgs.push_back("--whole-archive");
18361837
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray-dso"));
18371838
CmdArgs.push_back("--no-whole-archive");
18381839
return true;
18391840
}
1840-
} else if (TC.getXRayArgs().needsXRayRt()) {
1841+
} else if (XRay.needsXRayRt()) {
18411842
CmdArgs.push_back("--whole-archive");
18421843
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray"));
1843-
for (const auto &Mode : TC.getXRayArgs().modeList())
1844+
for (const auto &Mode : XRay.modeList())
18441845
CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode));
18451846
CmdArgs.push_back("--no-whole-archive");
18461847
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
}
16281628
}
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)