[PATCH] D53593: [GCOV] Flush counters before forking to avoid counting the execution before fork twice

calixte via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 23 11:20:24 PDT 2018


calixte created this revision.
calixte added reviewers: vsk, davidxl.
Herald added a subscriber: llvm-commits.

This is replacement for patch in https://reviews.llvm.org/D49460.
When we fork, the counters are duplicate as they're and so the values are finally wrong when writing gcda for parent and child.
So just before to fork, we flush the counters and so the parent and the child have new counters set to zero.


Repository:
  rL LLVM

https://reviews.llvm.org/D53593

Files:
  lib/Transforms/Instrumentation/GCOVProfiling.cpp


Index: lib/Transforms/Instrumentation/GCOVProfiling.cpp
===================================================================
--- lib/Transforms/Instrumentation/GCOVProfiling.cpp
+++ lib/Transforms/Instrumentation/GCOVProfiling.cpp
@@ -109,6 +109,8 @@
   insertCounterWriteout(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
   Function *insertFlush(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
 
+  void AddFlushBeforeFork();
+
   enum class GCovFileType { GCNO, GCDA };
   std::string mangleName(const DICompileUnit *CU, GCovFileType FileType);
 
@@ -468,6 +470,8 @@
   this->TLI = &TLI;
   Ctx = &M.getContext();
 
+  AddFlushBeforeFork();
+
   if (Options.EmitNotes) emitProfileNotes();
   if (Options.EmitData) return emitProfileArcs();
   return false;
@@ -524,6 +528,35 @@
 	return false;
 }
 
+void GCOVProfiler::AddFlushBeforeFork() {
+  SmallVector<Instruction *, 2> Forks;
+  for (auto &F : M->functions()) {
+    for (auto &BB : F) {
+      for (auto &I : BB) {
+        if (CallInst *CI = dyn_cast<CallInst>(&I)) {
+          if (Function *Callee = CI->getCalledFunction()) {
+            if (Callee->getName() == "fork") {
+              FunctionType *FT = Callee->getFunctionType();
+              if (FT->getReturnType()->isIntegerTy(32) &&
+                  FT->getNumParams() == 0) {
+                Forks.push_back(&I);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  for (auto I : Forks) {
+    IRBuilder<> Builder(I);
+    FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
+    Constant *GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy);
+    Builder.CreateCall(GCOVFlush);
+    I->getParent()->splitBasicBlock(I);
+  }
+}
+
 void GCOVProfiler::emitProfileNotes() {
   NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
   if (!CU_Nodes) return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53593.170707.patch
Type: text/x-patch
Size: 1845 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181023/8c391ca0/attachment.bin>


More information about the llvm-commits mailing list