[PATCH] D11579: [InstrProfiling] Fix data race on profile counters by using AtomicRMW.

Alexey Samsonov vonosmas at gmail.com
Tue Jul 28 15:14:16 PDT 2015


samsonov created this revision.
samsonov added reviewers: dnovillo, bogner.
samsonov added a subscriber: llvm-commits.

Since we introduced counters for functions in COMDAT sections (e.g.
inline functions from STL headers), these headers can easily be
incremented concurrently by multiple threads. Replace load-add-store
with a single "atomicrmw add" with monotonic memory ordering.

http://reviews.llvm.org/D11579

Files:
  lib/Transforms/Instrumentation/InstrProfiling.cpp

Index: lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -147,9 +147,9 @@
   IRBuilder<> Builder(Inc->getParent(), *Inc);
   uint64_t Index = Inc->getIndex()->getZExtValue();
   Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
-  Value *Count = Builder.CreateLoad(Addr, "pgocount");
-  Count = Builder.CreateAdd(Count, Builder.getInt64(1));
-  Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr));
+  Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Builder.getInt64(1),
+                          llvm::Monotonic);
+  assert(Inc->use_empty() && "InstrProfIncrementInst has uses!");
   Inc->eraseFromParent();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11579.30864.patch
Type: text/x-patch
Size: 821 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150728/de5c35fe/attachment.bin>


More information about the llvm-commits mailing list