[PATCH] D121910: [Instruction] Introduce getAtomicSyncScopeID()
Marco Elver via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 17 06:41:51 PDT 2022
melver created this revision.
melver added a reviewer: dvyukov.
Herald added subscribers: dexonsmith, hiraditya.
Herald added a project: All.
melver requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
An analysis may just be interested in checking if an instruction is
atomic but system scoped or single-thread scoped, like ThreadSanitizer's
isAtomic(). Unfortunately Instruction::isAtomic() can only answer the
"atomic" part of the question, but to also check scope becomes rather
verbose.
To simplify and reduce redundancy, introduce a common helper
getAtomicSyncScopeID() which returns the scope of an atomic operation.
Start using it in ThreadSanitizer.
NFCI.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D121910
Files:
llvm/include/llvm/IR/Instructions.h
llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
Index: llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
@@ -526,19 +526,14 @@
Local.clear();
}
-static bool isAtomic(Instruction *I) {
+static bool isTsanAtomic(const Instruction *I) {
// TODO: Ask TTI whether synchronization scope is between threads.
- if (LoadInst *LI = dyn_cast<LoadInst>(I))
- return LI->isAtomic() && LI->getSyncScopeID() != SyncScope::SingleThread;
- if (StoreInst *SI = dyn_cast<StoreInst>(I))
- return SI->isAtomic() && SI->getSyncScopeID() != SyncScope::SingleThread;
- if (isa<AtomicRMWInst>(I))
- return true;
- if (isa<AtomicCmpXchgInst>(I))
- return true;
- if (isa<FenceInst>(I))
- return true;
- return false;
+ auto SSID = getAtomicSyncScopeID(I);
+ if (!SSID.hasValue())
+ return false;
+ if (isa<LoadInst>(I) || isa<StoreInst>(I))
+ return SSID.getValue() != SyncScope::SingleThread;
+ return true;
}
void ThreadSanitizer::InsertRuntimeIgnores(Function &F) {
@@ -580,7 +575,7 @@
// Traverse all instructions, collect loads/stores/returns, check for calls.
for (auto &BB : F) {
for (auto &Inst : BB) {
- if (isAtomic(&Inst))
+ if (isTsanAtomic(&Inst))
AtomicAccesses.push_back(&Inst);
else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
LocalLoadsAndStores.push_back(&Inst);
Index: llvm/include/llvm/IR/Instructions.h
===================================================================
--- llvm/include/llvm/IR/Instructions.h
+++ llvm/include/llvm/IR/Instructions.h
@@ -5310,6 +5310,10 @@
}
};
+//===----------------------------------------------------------------------===//
+// Helper functions
+//===----------------------------------------------------------------------===//
+
/// A helper function that returns the pointer operand of a load or store
/// instruction. Returns nullptr if not load or store.
inline const Value *getLoadStorePointerOperand(const Value *V) {
@@ -5365,6 +5369,24 @@
return cast<StoreInst>(I)->getValueOperand()->getType();
}
+/// A helper function that returns an atomic operation's sync scope; returns
+/// None if it is not an atomic operation.
+inline Optional<SyncScope::ID> getAtomicSyncScopeID(const Instruction *I) {
+ if (!I->isAtomic())
+ return None;
+ if (auto *AI = dyn_cast<LoadInst>(I))
+ return AI->getSyncScopeID();
+ if (auto *AI = dyn_cast<StoreInst>(I))
+ return AI->getSyncScopeID();
+ if (auto *AI = dyn_cast<FenceInst>(I))
+ return AI->getSyncScopeID();
+ if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I))
+ return AI->getSyncScopeID();
+ if (auto *AI = dyn_cast<AtomicRMWInst>(I))
+ return AI->getSyncScopeID();
+ llvm_unreachable("unhandled atomic operation");
+}
+
//===----------------------------------------------------------------------===//
// FreezeInst Class
//===----------------------------------------------------------------------===//
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121910.416165.patch
Type: text/x-patch
Size: 3119 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220317/cd2474bb/attachment.bin>
More information about the llvm-commits
mailing list