[clang] Thread Safety Analysis: Support reentrant capabilities (PR #137133)
Marco Elver via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 29 07:34:54 PDT 2025
================
@@ -235,6 +266,20 @@ class FactSet {
return false;
}
+ std::optional<FactID> replaceLock(FactManager &FM, iterator It,
+ std::unique_ptr<FactEntry> Entry) {
+ if (It == end())
+ return std::nullopt;
+ FactID F = FM.newFact(std::move(Entry));
+ *It = F;
+ return F;
+ }
+
+ std::optional<FactID> replaceLock(FactManager &FM, const CapabilityExpr &CapE,
+ std::unique_ptr<FactEntry> Entry) {
+ return replaceLock(FM, findLockIter(FM, CapE), std::move(Entry));
+ }
+
----------------
melver wrote:
We could, but it's less efficient, esp. if we used findLockIter():
1. without findLockIter: we save a pop_back() and push_back().
2. with preceding findLockIter: like (1), but also saves a search.
Sure, maybe we're just shaving off some constant overheads, but I also find it makes it less verbose and more readable.
I have this later patch I would send out as a separate PR later:
```
commit 9a41f2b68a28e6a2239b45a11a0cd360dce320af
Author: Marco Elver <elver at google.com>
Date: Fri Apr 25 18:51:16 2025 +0200
Thread Safety Analysis: Use replaceLock instead of removeLock+addLock
In ScopedLockableFactEntry::unlock(), we can avoid a second search,
pop_back(), and push_back() if we use the already obtained iterator into
the FactSet to replace the old FactEntry and take its position in the
vector.
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index cb62f11d6f15..429f22715886 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1090,9 +1090,9 @@ private:
return;
}
- FSet.removeLock(FactMan, Cp);
- FSet.addLock(FactMan,
- std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
+ FSet.replaceLock(
+ FactMan, It,
+ std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
} else if (Handler) {
SourceLocation PrevLoc;
if (const FactEntry *Neg = FSet.findLock(FactMan, !Cp))
```
https://github.com/llvm/llvm-project/pull/137133
More information about the cfe-commits
mailing list