================
@@ -266,41 +479,156 @@ class FactSet {
     return F;
   }
 
-  std::optional<FactID> replaceLock(FactManager &FM, const CapabilityExpr 
&CapE,
+  std::optional<FactID> replaceFact(FactManager &FM, const FactEntry &Old,
                                     const FactEntry *Entry) {
-    return replaceLock(FM, findLockIter(FM, CapE), Entry);
-  }
-
-  iterator findLockIter(FactManager &FM, const CapabilityExpr &CapE) {
-    return llvm::find_if(*this,
-                         [&](FactID ID) { return FM[ID].matches(CapE); });
+    return replaceFact(FM, findFactIter(FM, Old), Entry);
   }
+  /// \}
 
-  const FactEntry *findLock(FactManager &FM, const CapabilityExpr &CapE) const 
{
-    auto I =
-        llvm::find_if(*this, [&](FactID ID) { return FM[ID].matches(CapE); });
-    return I != end() ? &FM[*I] : nullptr;
+  /// \name Definite facts
+  /// The one fact stating that \p CapE is held (or, negated, not held).
+  /// \{
+  iterator findDefiniteIter(FactManager &FM, const CapabilityExpr &CapE) {
+    return findIf(FM, [&](const FactEntry &FE) {
+      return isDefinite(FE) && FE.matches(CapE);
+    });
   }
 
-  const FactEntry *findLockUniv(FactManager &FM,
+  const FactEntry *findDefinite(FactManager &FM,
                                 const CapabilityExpr &CapE) const {
-    auto I = llvm::find_if(
-        *this, [&](FactID ID) -> bool { return FM[ID].matchesUniv(CapE); });
-    return I != end() ? &FM[*I] : nullptr;
+    return findEntry(FM, [&](const FactEntry &FE) {
+      return isDefinite(FE) && FE.matches(CapE);
+    });
   }
 
-  const FactEntry *findPartialMatch(FactManager &FM,
+  const FactEntry *findDefiniteUniv(FactManager &FM,
                                     const CapabilityExpr &CapE) const {
-    auto I = llvm::find_if(*this, [&](FactID ID) -> bool {
-      return FM[ID].partiallyMatches(CapE);
+    return findEntry(FM, [&](const FactEntry &FE) {
+      return isDefinite(FE) && FE.matchesUniv(CapE);
     });
-    return I != end() ? &FM[*I] : nullptr;
   }
 
-  bool containsMutexDecl(FactManager &FM, const ValueDecl* Vd) const {
-    auto I = llvm::find_if(
-        *this, [&](FactID ID) -> bool { return FM[ID].valueDecl() == Vd; });
-    return I != end();
+  const FactEntry *findDefinitePartialMatch(FactManager &FM,
+                                            const CapabilityExpr &CapE) const {
+    return findEntry(FM, [&](const FactEntry &FE) {
+      return isDefinite(FE) && FE.partiallyMatches(CapE);
+    });
+  }
+
+  bool removeDefinite(FactManager &FM, const CapabilityExpr &CapE) {
+    return removeAt(findDefiniteIter(FM, CapE));
+  }
+  /// \}
+
+  /// \name Try facts
+  /// The try facts of \p CapE.
+  /// \{
+  iterator findTryFactIter(FactManager &FM, const CapabilityExpr &CapE,
+                           const Expr *Origin, LockKind Kind) {
+    return findIf(FM, [&](const FactEntry &FE) {
+      return isTryFactOf(FE, CapE, Origin, Kind);
+    });
+  }
+
+  const TryFactEntry *findTryFact(FactManager &FM, const CapabilityExpr &CapE,
+                                  const Expr *Origin, LockKind Kind) const {
+    return cast_or_null<TryFactEntry>(findEntry(FM, [&](const FactEntry &FE) {
+      return isTryFactOf(FE, CapE, Origin, Kind);
+    }));
+  }
+
+  /// The first unresolved try fact of \p CapE, whichever its origin: the
+  /// capability may be held.
+  const TryFactEntry *firstConditional(FactManager &FM,
+                                       const CapabilityExpr &CapE) const {
+    return cast_or_null<TryFactEntry>(findEntry(
+        FM, [&](const FactEntry &FE) { return isConditionalOf(FE, CapE); }));
+  }
+
+  bool anyConditional(FactManager &FM, const CapabilityExpr &CapE) const {
+    return firstConditional(FM, CapE) != nullptr;
+  }
+
+  /// Whether every unresolved try fact of \p CapE has the lock kind \p Kind.
+  bool conditionalsAllOfKind(FactManager &FM, const CapabilityExpr &CapE,
+                             LockKind Kind) const {
+    return llvm::all_of(*this, [&](FactID ID) {
+      return !isConditionalOf(FM[ID], CapE) || FM[ID].kind() == Kind;
+    });
+  }
+
+  /// Collect every try fact of \p CapE, resolved or not, into \p Out, so
+  /// that a caller can mutate the set while visiting them.
+  void collectTryFacts(FactManager &FM, const CapabilityExpr &CapE,
+                       SmallVectorImpl<const TryFactEntry *> &Out) const {
+    for (FactID ID : *this)
+      if (const auto *W = dyn_cast<TryFactEntry>(&FM[ID]);
+          W && W->matches(CapE))
+        Out.push_back(W);
+  }
+
+  /// Remove every unresolved try fact of \p CapE originating from
+  /// \p Origin (one per lock kind); returns whether there was any.
+  bool removeConditionalsOf(FactManager &FM, const CapabilityExpr &CapE,
+                            const Expr *Origin) {
+    const size_t Before = FactIDs.size();
+    llvm::erase_if(FactIDs, [&](FactID ID) {
+      const auto *W = dyn_cast<TryFactEntry>(&FM[ID]);
+      return W && W->conditional() && W->origin() == Origin && 
W->matches(CapE);
+    });
+    return FactIDs.size() != Before;
+  }
+
+  /// Remove every unresolved try fact of \p CapE: the capability is no
+  /// longer possibly held through any of them.
+  void removeAllConditional(FactManager &FM, const CapabilityExpr &CapE) {
+    llvm::erase_if(FactIDs,
+                   [&](FactID ID) { return isConditionalOf(FM[ID], CapE); });
+  }
+
+  /// The hold of \p CapE is gone: every ProvedHeld try fact of it no longer
+  /// proves a live level, and is dropped.
+  void releaseProved(FactManager &FM, const CapabilityExpr &CapE) {
+    llvm::erase_if(FactIDs, [&](FactID ID) {
+      const auto *W = dyn_cast<TryFactEntry>(&FM[ID]);
+      return W && W->provedHeld() && W->matches(CapE);
+    });
+  }
+  /// \}
+
+  /// The definite fact of \p CapE, or else its first unresolved try fact:
+  /// whether the capability is held or may be held. A resolved try fact is
+  /// neither.
+  const FactEntry *findDefiniteOrConditional(FactManager &FM,
+                                             const CapabilityExpr &CapE) const 
{
+    return findEntry(FM, [&](const FactEntry &FE) {
+      return isa<TryFactEntry>(FE) ? isConditionalOf(FE, CapE)
+                                   : FE.matches(CapE);
+    });
+  }
+
+  /// \name Counterparts
+  /// The fact of the same form as \p F -- definite, or the try fact of the
+  /// same call in the same kind -- for \p F's capability: what a join
+  /// pairs \p F with.
+  /// \{
+  iterator findCounterpartIter(FactManager &FM, const FactEntry &F) {
+    if (const auto *W = dyn_cast<TryFactEntry>(&F))
+      return findTryFactIter(FM, F, W->origin(), W->kind());
+    return findDefiniteIter(FM, F);
+  }
+
+  const FactEntry *findCounterpart(FactManager &FM, const FactEntry &F) const {
+    if (const auto *W = dyn_cast<TryFactEntry>(&F))
+      return findTryFact(FM, F, W->origin(), W->kind());
+    return findDefinite(FM, F);
+  }
+  /// \}
+  /// Whether a definite fact of the capability declared by \p Vd is present.
+  bool containsMutexDecl(FactManager &FM, const ValueDecl *Vd) const {
+    return llvm::any_of(*this, [&](FactID ID) {
+      return !isa<TryFactEntry>(FM[ID]) && FM[ID].valueDecl() == Vd;
+    });
----------------
aaronpuchert wrote:

It seems like the `FactManager` is massively complicated by this. I thinking 
replacing the "lock" terminology with "facts" is reasonable, but if we have 
multiple facts per lock, we now need multiple lockups and then combine the 
facts in upper layers. My understanding of the existing fact design that it 
intents to manage what is known about a particular lock in one single entry. 
See e.g. the reentrancy counter instead of having multiple facts for multiple 
locks.

I could imagine that we ask the FactManager to provide a set of facts for a 
single lock (perhaps as iterator range), but I keep coming back to the 
observation that we need to combine those facts eventually and make sense of 
them together.

https://github.com/llvm/llvm-project/pull/241550
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to