[ORC] Add withResourceKeyDo method to ResourceTracker.
authorLang Hames <lhames@gmail.com>
Mon, 14 Nov 2022 00:41:04 +0000 (16:41 -0800)
committerLang Hames <lhames@gmail.com>
Mon, 19 Dec 2022 22:56:08 +0000 (14:56 -0800)
This method behaves the same as MaterializationResponsibility::withResourceKeyDo
(which now forwards to the new method): It locks the session while providing
access to the ResourceKey associated with the tracker.

Adding this method to ResourceTracker allows resources to be allocated and
tracked for a given MaterializationUnit prior to that MaterializationUnit being
materialized. E.g. Platforms can now track and remove initializers and other
symbols.

llvm/include/llvm/ExecutionEngine/Orc/Core.h

index cb3de04..b6d1bf0 100644 (file)
@@ -70,6 +70,10 @@ public:
                                          ~static_cast<uintptr_t>(1));
   }
 
+  /// Runs the given callback under the session lock, passing in the associated
+  /// ResourceKey. This is the safe way to associate resources with trackers.
+  template <typename Func> Error withResourceKeyDo(Func &&F);
+
   /// Remove all resources associated with this key.
   Error remove();
 
@@ -530,8 +534,11 @@ public:
   ///        emitted or notified of an error.
   ~MaterializationResponsibility();
 
-  /// Returns the ResourceTracker for this instance.
-  template <typename Func> Error withResourceKeyDo(Func &&F) const;
+  /// Runs the given callback under the session lock, passing in the associated
+  /// ResourceKey. This is the safe way to associate resources with trackers.
+  template <typename Func> Error withResourceKeyDo(Func &&F) const {
+    return RT->withResourceKeyDo(std::forward<Func>(F));
+  }
 
   /// Returns the target JITDylib that these symbols are being materialized
   ///        into.
@@ -1770,21 +1777,20 @@ private:
       JITDispatchHandlers;
 };
 
+template <typename Func> Error ResourceTracker::withResourceKeyDo(Func &&F) {
+  return getJITDylib().getExecutionSession().runSessionLocked([&]() -> Error {
+    if (isDefunct())
+      return make_error<ResourceTrackerDefunct>(this);
+    F(getKeyUnsafe());
+    return Error::success();
+  });
+}
+
 inline ExecutionSession &
 MaterializationResponsibility::getExecutionSession() const {
   return JD.getExecutionSession();
 }
 
-template <typename Func>
-Error MaterializationResponsibility::withResourceKeyDo(Func &&F) const {
-  return JD.getExecutionSession().runSessionLocked([&]() -> Error {
-    if (RT->isDefunct())
-      return make_error<ResourceTrackerDefunct>(RT);
-    F(RT->getKeyUnsafe());
-    return Error::success();
-  });
-}
-
 template <typename GeneratorT>
 GeneratorT &JITDylib::addGenerator(std::unique_ptr<GeneratorT> DefGenerator) {
   auto &G = *DefGenerator;