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

Conversation

sebastiankreutzer
Copy link
Contributor

This PR addressed issue #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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels May 22, 2025
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Sebastian Kreutzer (sebastiankreutzer)

Changes

This PR addressed issue #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.

Full diff: https://github.com/llvm/llvm-project/pull/141043.diff

6 Files Affected:

  • (modified) clang/include/clang/Driver/ToolChain.h (+1-2)
  • (modified) clang/lib/Driver/ToolChain.cpp (+3-4)
  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+1-1)
  • (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+4-3)
  • (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+1-1)
  • (modified) clang/test/Driver/XRay/xray-instrument.c (+5)
diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h
index 58edf2b3887b0..b8899e78176b4 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -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;
@@ -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; }
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 4adea6a8e75ab..4dab08cb6fd88 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -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 {
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 355a48be9f493..2fb6cf8ea2bdc 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -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 :
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index d2535d1a2624a..fa641b5d8196d 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -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;
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 26e24ad0ab17c..452820159435f 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -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");
diff --git a/clang/test/Driver/XRay/xray-instrument.c b/clang/test/Driver/XRay/xray-instrument.c
index 48e20c45be4ac..d00d8972eb1a1 100644
--- a/clang/test/Driver/XRay/xray-instrument.c
+++ b/clang/test/Driver/XRay/xray-instrument.c
@@ -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 -### -Xarch_host -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s
+// RUN: not %clang -### -x hip --offload-arch=gfx906 -nogpulib -nogpuinc -fxray-instrument -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
+// RUN: %clang -### -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
 

@sebastiankreutzer
Copy link
Contributor Author

Pinging potential reviewers: @jhuber6 @MaskRay

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll now be creating the XRayArgs class when we do this every time, but I don't think it's expensive enough or done enough times to be an issue. Thanks.

@jhuber6 jhuber6 merged commit 8d0a484 into llvm:main May 22, 2025
11 checks passed
@sebastiankreutzer
Copy link
Contributor Author

@jhuber6 Thanks for the fast review and merge, I appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants