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

Zequan Wu via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 14:50:28 PDT 2023


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

To avoid casting from int64_t to uint64_t later.

>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] [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();



More information about the llvm-commits mailing list