[PATCH] Make LLVM profiling thread-safe
Matthew Dempsky
matthew at dempsky.org
Mon Jun 24 15:25:14 PDT 2013
On Mon, Jun 24, 2013 at 12:17:17PM -0700, Matthew Dempsky wrote:
> Instead of emitting separate read-modify-write instructions to
> increment the profiling counters, an monotonically ordered atomic add
> instruction should be emitted.
Oops, need to fix the tests too!
Index: llvm/lib/Transforms/Instrumentation/ProfilingUtils.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/ProfilingUtils.cpp (revision 184494)
+++ llvm/lib/Transforms/Instrumentation/ProfilingUtils.cpp (working copy)
@@ -121,12 +121,10 @@
Constant *ElementPtr =
ConstantExpr::getGetElementPtr(CounterArray, Indices);
- // Load, increment and store the value back.
- Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
- Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
- ConstantInt::get(Type::getInt32Ty(Context), 1),
- "NewFuncCounter", InsertPos);
- new StoreInst(NewVal, ElementPtr, InsertPos);
+ // Atomically increment the counter by one.
+ Constant *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
+ new AtomicRMWInst(AtomicRMWInst::Add, ElementPtr, One, Monotonic,
+ CrossThread, InsertPos);
}
void llvm::InsertProfilingShutdownCall(Function *Callee, Module *Mod) {
Index: llvm/test/Analysis/Profiling/edge-profiling.ll
===================================================================
--- llvm/test/Analysis/Profiling/edge-profiling.ll (revision 184494)
+++ llvm/test/Analysis/Profiling/edge-profiling.ll (working copy)
@@ -16,19 +16,14 @@
define void @oneblock() nounwind {
entry:
; CHECK:entry:
-; CHECK:%OldFuncCounter
-; CHECK:load
+; CHECK:atomicrmw
+; CHECK:add
; CHECK:getelementptr
; CHECK:@EdgeProfCounters
; CHECK:i32 0
; CHECK:i32 0
-; CHECK:%NewFuncCounter
-; CHECK:add
-; CHECK:%OldFuncCounter
-; CHECK:store
-; CHECK:%NewFuncCounter
-; CHECK:getelementptr
-; CHECK:@EdgeProfCounters
+; CHECK:i32 1
+; CHECK:monotonic
%0 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
ret void
}
More information about the llvm-commits
mailing list