Skip to content

[lldb] Introduce Language::AreEquivalentFunctions #112720

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

Conversation

felipepiovezan
Copy link
Contributor

This allows languages to provide an opinion on whether two symbol contexts are equivalent (i.e. belong to the same function).

It is useful to drive the comparisons done by stepping plans that need to ensure symbol contexts obtained from different points in time are actually the same.

@felipepiovezan
Copy link
Contributor Author

An example usage can be found here: swiftlang#9441

@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-lldb

Author: Felipe de Azevedo Piovezan (felipepiovezan)

Changes

This allows languages to provide an opinion on whether two symbol contexts are equivalent (i.e. belong to the same function).

It is useful to drive the comparisons done by stepping plans that need to ensure symbol contexts obtained from different points in time are actually the same.


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

2 Files Affected:

  • (modified) lldb/include/lldb/Target/Language.h (+8)
  • (modified) lldb/source/Target/ThreadPlanStepOverRange.cpp (+5)
diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index 41d8eeef469eab..fb1828a4dfd8a9 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -363,6 +363,14 @@ class Language : public PluginInterface {
     return false;
   }
 
+  /// Returns a boolean indicating whether two symbol contexts correspond to
+  /// the same function. If the plugin has no opinion, it should return nullopt.
+  virtual std::optional<bool>
+  AreEquivalentFunctions(const SymbolContext &sc1,
+                         const SymbolContext &sc2) const {
+    return {};
+  }
+
   /// Returns true if this Language supports exception breakpoints on throw via
   /// a corresponding LanguageRuntime plugin.
   virtual bool SupportsExceptionBreakpointsOnThrow() const { return false; }
diff --git a/lldb/source/Target/ThreadPlanStepOverRange.cpp b/lldb/source/Target/ThreadPlanStepOverRange.cpp
index 934f23b3b21a28..3f2826cb886f2d 100644
--- a/lldb/source/Target/ThreadPlanStepOverRange.cpp
+++ b/lldb/source/Target/ThreadPlanStepOverRange.cpp
@@ -11,6 +11,7 @@
 #include "lldb/Symbol/CompileUnit.h"
 #include "lldb/Symbol/Function.h"
 #include "lldb/Symbol/LineTable.h"
+#include "lldb/Target/Language.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/RegisterContext.h"
 #include "lldb/Target/Target.h"
@@ -103,6 +104,10 @@ void ThreadPlanStepOverRange::SetupAvoidNoDebug(
 
 bool ThreadPlanStepOverRange::IsEquivalentContext(
     const SymbolContext &context) {
+  if (Language *language = Language::FindPlugin(context.GetLanguage()))
+    if (std::optional<bool> maybe_equivalent =
+            language->AreEquivalentFunctions(context, m_addr_context))
+      return *maybe_equivalent;
   // Match as much as is specified in the m_addr_context: This is a fairly
   // loose sanity check.  Note, sometimes the target doesn't get filled in so I
   // left out the target check.  And sometimes the module comes in as the .o

@kastiglione
Copy link
Contributor

do we have in mind any other languages that will implement this?

/// Returns a boolean indicating whether two symbol contexts correspond to
/// the same function. If the plugin has no opinion, it should return nullopt.
virtual std::optional<bool>
AreEquivalentFunctions(const SymbolContext &sc1,
Copy link
Contributor

Choose a reason for hiding this comment

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

equivalent functions sounds quite vague to my mind. Given that this function operates on symbol contexts, and is called from IsEquivalentContext, I think this would be better off named AreEquivalentContexts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I agree. I'll just wait to see if others chime in and then update the patch

Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to somehow indicate the comparison w.r.t. which these functions/contexts are equivalent. "Equivalent" makes it sound like maybe I could use one in place of the other generally (like maybe I can call either one of them???) AreEqualForFrameComparison is a little verbose, but tells you what statement we are actually making.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, that name looks a lot more expressive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed it

@felipepiovezan
Copy link
Contributor Author

do we have in mind any other languages that will implement this?

I suspect any language wanting to implement this notion of function splitting (e.g. all the users of corosplitter) and virtual frames could have a use for this

@jimingham
Copy link
Collaborator

do we have in mind any other languages that will implement this?

I suspect any language wanting to implement this notion of function splitting (e.g. all the users of corosplitter) and virtual frames could have a use for this

We already have .cold functions that do this in clang. Might be nice to support that on the llvm side so this could be testable here.

This allows languages to provide an opinion on whether two symbol contexts are
equivalent (i.e. belong to the same function).

It is useful to drive the comparisons done by stepping plans that need to ensure
symbol contexts obtained from different points in time are actually the same.
@felipepiovezan felipepiovezan force-pushed the felipe/language_plugin_compare_funcs branch from 1f9bcfc to 231afe2 Compare October 17, 2024 18:34
@felipepiovezan
Copy link
Contributor Author

felipepiovezan commented Oct 17, 2024

do we have in mind any other languages that will implement this?

I suspect any language wanting to implement this notion of function splitting (e.g. all the users of corosplitter) and virtual frames could have a use for this

We already have .cold functions that do this in clang. Might be nice to support that on the llvm side so this could be testable here.

There are two things that confuse me about this idea:

  • I thought .cold functions came from the hot-cold-split pass, not from Clang?
  • Don't those create a new frame on the stack, i.e. a new CFA?

@jimingham
Copy link
Collaborator

do we have in mind any other languages that will implement this?

I suspect any language wanting to implement this notion of function splitting (e.g. all the users of corosplitter) and virtual frames could have a use for this

We already have .cold functions that do this in clang. Might be nice to support that on the llvm side so this could be testable here.

There are two things that confuse me about this idea:

  • I thought .cold functions came from the hot-cold-split pass, not from Clang?

I was speaking loosely, meaning "in the llvm.org not the swift fork of llvm-project (and thus testable there). We'd have to decide which runtime this would go in, maybe we need an "assembly language" or a general fallback runtime for this?

  • Don't those create a new frame on the stack, i.e. a new CFA?

I don't remember whether they push a frame or tail call.

@felipepiovezan
Copy link
Contributor Author

do we have in mind any other languages that will implement this?

I suspect any language wanting to implement this notion of function splitting (e.g. all the users of corosplitter) and virtual frames could have a use for this

We already have .cold functions that do this in clang. Might be nice to support that on the llvm side so this could be testable here.

There are two things that confuse me about this idea:

  • I thought .cold functions came from the hot-cold-split pass, not from Clang?

I was speaking loosely, meaning "in the llvm.org not the swift fork of llvm-project (and thus testable there). We'd have to decide which runtime this would go in, maybe we need an "assembly language" or a general fallback runtime for this?

  • Don't those create a new frame on the stack, i.e. a new CFA?

I don't remember whether they push a frame or tail call.

I looked at the code that creates .cold functions, and it doesn't seem to tail call, creating a new frame instead (at least the .ll tests have no musttail attribute in those functions).

Maybe some C++-specific construct that could go through the C++ language plugin? I tried their coroutines, but the IR generated wasn't very promising either and TTTT I'm not familiar with how they are compiled.

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

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

It's a bit sad we can't come up with a scenario that uses this for the languages in the llvm.org repo. But the idea is clear the code really simple, and it obviously belongs in the Language, so LGTM.

@felipepiovezan felipepiovezan merged commit cd938bf into llvm:main Oct 19, 2024
7 checks passed
@felipepiovezan felipepiovezan deleted the felipe/language_plugin_compare_funcs branch October 19, 2024 23:53
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Oct 19, 2024
This allows languages to provide an opinion on whether two symbol
contexts are equivalent (i.e. belong to the same function).

It is useful to drive the comparisons done by stepping plans that need
to ensure symbol contexts obtained from different points in time are
actually the same.

(cherry picked from commit cd938bf)
felipepiovezan added a commit to swiftlang/llvm-project that referenced this pull request Oct 22, 2024
…age_api

[cherry-pick][lldb] Introduce Language::AreEquivalentFunctions (llvm#112720)
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.

4 participants