Skip to content

[BOLT] Introduce BinaryFunction::canClone() #138771

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

maksfb
Copy link
Contributor

@maksfb maksfb commented May 6, 2025

In some scenarios, we want to allow execution of the original function code together with its rewritten (optimized) copy. We used to limit such capability when the function uses C++ exceptions, since we cannot duplicate exception tables. There's more metadata that cannot be duplicated, and this PR adds more checks via the introduction of canClone() function that checks all such limitations.

In some scenarios, we want to allow execution of the original function
code together with its rewritten (optimized) copy. We used to limit such
capability when the function uses C++ exceptions, since we cannot
duplicate exception tables. There's more metadata that cannot be
duplicated, and this PR adds more checks via the introduction of
canClone() function that checks all such limitations.
@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-bolt

Author: Maksim Panchenko (maksfb)

Changes

In some scenarios, we want to allow execution of the original function code together with its rewritten (optimized) copy. We used to limit such capability when the function uses C++ exceptions, since we cannot duplicate exception tables. There's more metadata that cannot be duplicated, and this PR adds more checks via the introduction of canClone() function that checks all such limitations.


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

3 Files Affected:

  • (modified) bolt/include/bolt/Core/BinaryFunction.h (+5)
  • (modified) bolt/lib/Core/BinaryFunction.cpp (+10)
  • (modified) bolt/lib/Passes/PatchEntries.cpp (+1-1)
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index a52998564ee1b..edeaa51481e6f 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -1375,6 +1375,11 @@ class BinaryFunction {
   /// Return true if the function should not have associated symbol table entry.
   bool isAnonymous() const { return IsAnonymous; }
 
+  /// Return true if we can allow the execution of the original body of the
+  /// function together with its rewritten copy. This means, e.g., that metadata
+  /// associated with the function can be duplicated/cloned.
+  bool canClone() const;
+
   /// If this function was folded, return the function it was folded into.
   BinaryFunction *getFoldedIntoFunction() const { return FoldedIntoFunction; }
 
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 9773e21aa7522..24972bac58409 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -3744,6 +3744,16 @@ void BinaryFunction::postProcessBranches() {
   assert(validateCFG() && "invalid CFG");
 }
 
+bool BinaryFunction::canClone() const {
+  // For instrumentation, we need to restrict the execution to the rewritten
+  // version of the function.
+  if (opts::Instrument)
+    return false;
+
+  // Check for the presence of metadata that cannot be duplicated.
+  return !hasEHRanges() && !hasSDTMarker() && !hasPseudoProbe() && !hasORC();
+}
+
 MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
   assert(Offset && "cannot add primary entry point");
   assert(CurrentState == State::Empty || CurrentState == State::Disassembled);
diff --git a/bolt/lib/Passes/PatchEntries.cpp b/bolt/lib/Passes/PatchEntries.cpp
index 4877e7dd8fdf3..8d07d163ef412 100644
--- a/bolt/lib/Passes/PatchEntries.cpp
+++ b/bolt/lib/Passes/PatchEntries.cpp
@@ -65,7 +65,7 @@ Error PatchEntries::runOnFunctions(BinaryContext &BC) {
       continue;
 
     // Check if we can skip patching the function.
-    if (!opts::ForcePatch && !Function.hasEHRanges() &&
+    if (!opts::ForcePatch && Function.canClone() &&
         Function.getSize() < PatchThreshold)
       continue;
 

Copy link
Contributor

@aaupov aaupov left a comment

Choose a reason for hiding this comment

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

LGTM with one question inline

return false;

// Check for the presence of metadata that cannot be duplicated.
return !hasEHRanges() && !hasSDTMarker() && !hasPseudoProbe() && !hasORC();
Copy link
Contributor

Choose a reason for hiding this comment

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

These checks seem quite restrictive (especially pseudo probes that are supposed to be in every function), what are the functions that can be cloned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expect only tiny leaf function to be cloned, everything else should be patched.

Copy link
Member

Choose a reason for hiding this comment

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

Note that I'm missing the practical details here.

What happens if we clone the pseudo-probes as well? Since they are uuids for blocks, would the cloned blocks also be identified as the original ones?

And if we don't clone them, are users expected to rely on pseudo-probes after a binary has been bolted?

Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

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

LGTM.

return false;

// Check for the presence of metadata that cannot be duplicated.
return !hasEHRanges() && !hasSDTMarker() && !hasPseudoProbe() && !hasORC();
Copy link
Member

Choose a reason for hiding this comment

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

Note that I'm missing the practical details here.

What happens if we clone the pseudo-probes as well? Since they are uuids for blocks, would the cloned blocks also be identified as the original ones?

And if we don't clone them, are users expected to rely on pseudo-probes after a binary has been bolted?

@yota9
Copy link
Member

yota9 commented May 7, 2025

As in previous review LG, but I fill it might be a bit over optimization, since it is very rare case and we're rely on conditions that might not cover all the cases in theory, maybe we should not have such thing at all...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants