[llvm] [Coverage] Use uint64_t when calculate execution count. (PR #70131)

Zequan Wu via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 26 09:33:23 PDT 2023


https://github.com/ZequanWu updated https://github.com/llvm/llvm-project/pull/70131

>From e6b55dbab7eced97fa617f6034c540d26ca27bc5 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Tue, 24 Oct 2023 17:46:41 -0400
Subject: [PATCH 1/2] [Coverage] Use uint64_t when calculate execution count.

---
 .../ProfileData/Coverage/CoverageMapping.h    |  2 +-
 .../ProfileData/Coverage/CoverageMapping.cpp  | 24 +++++++++----------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index 948203eea86365f..a355c0c92f435f6 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -344,7 +344,7 @@ class CounterMappingContext {
 
   /// Return the number of times that a region of code associated with this
   /// counter was executed.
-  Expected<int64_t> evaluate(const Counter &C) const;
+  Expected<uint64_t> evaluate(const Counter &C) const;
 
   unsigned getMaxCounterID(const Counter &C) const;
 };
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index 380a3aef9b14621..3b4ec3a44a6eaa2 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -157,7 +157,7 @@ void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
   }
   if (CounterValues.empty())
     return;
-  Expected<int64_t> Value = evaluate(C);
+  Expected<uint64_t> Value = evaluate(C);
   if (auto E = Value.takeError()) {
     consumeError(std::move(E));
     return;
@@ -165,10 +165,10 @@ void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
   OS << '[' << *Value << ']';
 }
 
-Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
+Expected<uint64_t> CounterMappingContext::evaluate(const Counter &C) const {
   struct StackElem {
     Counter ICounter;
-    int64_t LHS = 0;
+    uint64_t LHS = 0;
     enum {
       KNeverVisited = 0,
       KVisitedOnce = 1,
@@ -179,7 +179,7 @@ Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
   std::stack<StackElem> CounterStack;
   CounterStack.push({C});
 
-  int64_t LastPoppedValue;
+  uint64_t LastPoppedValue;
 
   while (!CounterStack.empty()) {
     StackElem &Current = CounterStack.top();
@@ -207,8 +207,8 @@ Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
         CounterStack.push(StackElem{E.RHS});
         Current.VisitCount = StackElem::KVisitedTwice;
       } else {
-        int64_t LHS = Current.LHS;
-        int64_t RHS = LastPoppedValue;
+        uint64_t LHS = Current.LHS;
+        uint64_t RHS = LastPoppedValue;
         LastPoppedValue =
             E.Kind == CounterExpression::Subtract ? LHS - RHS : LHS + RHS;
         CounterStack.pop();
@@ -224,7 +224,7 @@ Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
 unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
   struct StackElem {
     Counter ICounter;
-    int64_t LHS = 0;
+    uint64_t LHS = 0;
     enum {
       KNeverVisited = 0,
       KVisitedOnce = 1,
@@ -235,7 +235,7 @@ unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
   std::stack<StackElem> CounterStack;
   CounterStack.push({C});
 
-  int64_t LastPoppedValue;
+  uint64_t LastPoppedValue;
 
   while (!CounterStack.empty()) {
     StackElem &Current = CounterStack.top();
@@ -263,8 +263,8 @@ unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
           CounterStack.push(StackElem{E.RHS});
           Current.VisitCount = StackElem::KVisitedTwice;
         } else {
-          int64_t LHS = Current.LHS;
-          int64_t RHS = LastPoppedValue;
+          uint64_t LHS = Current.LHS;
+          uint64_t RHS = LastPoppedValue;
           LastPoppedValue = std::max(LHS, RHS);
           CounterStack.pop();
         }
@@ -345,12 +345,12 @@ Error CoverageMapping::loadFunctionRecord(
 
   FunctionRecord Function(OrigFuncName, Record.Filenames);
   for (const auto &Region : Record.MappingRegions) {
-    Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
+    Expected<uint64_t> ExecutionCount = Ctx.evaluate(Region.Count);
     if (auto E = ExecutionCount.takeError()) {
       consumeError(std::move(E));
       return Error::success();
     }
-    Expected<int64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
+    Expected<uint64_t> AltExecutionCount = Ctx.evaluate(Region.FalseCount);
     if (auto E = AltExecutionCount.takeError()) {
       consumeError(std::move(E));
       return Error::success();

>From 93c704c53dcb3416b572062745bc64d1dd77adcb Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Thu, 26 Oct 2023 12:33:07 -0400
Subject: [PATCH 2/2] Fix more places

---
 compiler-rt/include/profile/InstrProfData.inc   | 2 +-
 llvm/include/llvm/ProfileData/InstrProfData.inc | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/include/profile/InstrProfData.inc b/compiler-rt/include/profile/InstrProfData.inc
index 8ba7e186d4fb1a6..40013c6da0d479f 100644
--- a/compiler-rt/include/profile/InstrProfData.inc
+++ b/compiler-rt/include/profile/InstrProfData.inc
@@ -214,7 +214,7 @@ COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
                    NameValue.size()))
 #endif
 #ifdef COVMAP_V2_OR_V3
-COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
+COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
                    llvm::ConstantInt::get( \
                      llvm::Type::getInt64Ty(Ctx), NameHash))
 #endif
diff --git a/llvm/include/llvm/ProfileData/InstrProfData.inc b/llvm/include/llvm/ProfileData/InstrProfData.inc
index 13be2753e514efe..206a140b36c2b9f 100644
--- a/llvm/include/llvm/ProfileData/InstrProfData.inc
+++ b/llvm/include/llvm/ProfileData/InstrProfData.inc
@@ -214,7 +214,7 @@ COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
                    NameValue.size()))
 #endif
 #ifdef COVMAP_V2_OR_V3
-COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
+COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
                    llvm::ConstantInt::get( \
                      llvm::Type::getInt64Ty(Ctx), NameHash))
 #endif



More information about the llvm-commits mailing list