[clang] [llvm] [CaptureTracking][FunctionAttrs] Add support for CaptureInfo (PR #125880)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 11:18:18 PST 2025


================
@@ -94,31 +96,53 @@ namespace llvm {
     /// U->getUser() is always an Instruction.
     virtual bool shouldExplore(const Use *U);
 
-    /// captured - Information about the pointer was captured by the user of
-    /// use U. Return true to stop the traversal or false to continue looking
-    /// for more capturing instructions.
-    virtual bool captured(const Use *U) = 0;
+    /// When returned from captures(), stop the traversal.
+    static std::optional<CaptureComponents> stop() { return std::nullopt; }
+
+    /// When returned from captures(), continue traversal, but do not follow
+    /// the return value of this user, even if it has additional capture
+    /// components. Should only be used if captures() has already taken the
+    /// potential return caputres into account.
+    static std::optional<CaptureComponents> continueIgnoringReturn() {
+      return CaptureComponents::None;
+    }
+
+    /// When returned from captures(), continue traversal, and also follow
+    /// the return value of this user if it has additional capture components
+    /// (that is, capture components in Ret that are not part of Other).
+    static std::optional<CaptureComponents> continueDefault(CaptureInfo CI) {
+      CaptureComponents RetCC = CI.getRetComponents();
+      if (!capturesNothing(RetCC & ~CI.getOtherComponents()))
+        return RetCC;
+      return CaptureComponents::None;
+    }
+
+    /// Use U directly captures CI.getOtherComponents() and additionally
+    /// CI.getRetComponents() through the return value of the user of U.
+    ///
+    /// Return std::nullopt to stop the traversal, or the CaptureComponents to
+    /// follow via the return value, which must be a subset of
+    /// CI.getRetComponents().
+    ///
+    /// For convenience, prefer returning one of stop(), continueDefault(CI) or
+    /// continueIgnoringReturn().
+    virtual std::optional<CaptureComponents> captured(const Use *U,
+                                                      CaptureInfo CI) = 0;
----------------
nikic wrote:

Looking at this again, I think I made this API more complicated than it needs to be. I think the return value can be replaced with an enum { Stop, Continue, ContinueIgnoringReturn }. It's not really necessary to return the CaptureComponents, the calling code knows the right ones to use.

https://github.com/llvm/llvm-project/pull/125880


More information about the llvm-commits mailing list