[llvm] 61b294a - Introduce CounterExpressionBuilder::subst(C, Map) (#112698)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 23:27:38 PST 2025
Author: NAKAMURA Takumi
Date: 2025-01-09T16:27:35+09:00
New Revision: 61b294aa15e9e2149398a641121fc3e977284a17
URL: https://github.com/llvm/llvm-project/commit/61b294aa15e9e2149398a641121fc3e977284a17
DIFF: https://github.com/llvm/llvm-project/commit/61b294aa15e9e2149398a641121fc3e977284a17.diff
LOG: Introduce CounterExpressionBuilder::subst(C, Map) (#112698)
This return a counter for each term in the expression replaced by
ReplaceMap.
At the moment, this doesn't update the Map, so Map is marked as `const`.
Added:
Modified:
llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
llvm/unittests/ProfileData/CoverageMappingTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index 504c24c27d84c4..81307d7b025d92 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -34,6 +34,7 @@
#include <cassert>
#include <cstdint>
#include <iterator>
+#include <map>
#include <memory>
#include <optional>
#include <sstream>
@@ -214,6 +215,14 @@ class CounterExpressionBuilder {
/// Return a counter that represents the expression that subtracts RHS from
/// LHS.
Counter subtract(Counter LHS, Counter RHS, bool Simplify = true);
+
+ /// K to V map. K will be Counter in most cases. V may be Counter or
+ /// Expression.
+ using SubstMap = std::map<Counter, Counter>;
+
+ /// \return A counter equivalent to \C, with each term in its
+ /// expression replaced with term from \p Map.
+ Counter subst(Counter C, const SubstMap &Map);
};
using LineColPair = std::pair<unsigned, unsigned>;
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index e8f60d2ea82f7e..6d6678e9e4afe2 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -135,6 +135,31 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
return Simplify ? simplify(Cnt) : Cnt;
}
+Counter CounterExpressionBuilder::subst(Counter C, const SubstMap &Map) {
+ // Replace C with the value found in Map even if C is Expression.
+ if (auto I = Map.find(C); I != Map.end())
+ return I->second;
+
+ if (!C.isExpression())
+ return C;
+
+ auto CE = Expressions[C.getExpressionID()];
+ auto NewLHS = subst(CE.LHS, Map);
+ auto NewRHS = subst(CE.RHS, Map);
+
+ // Reconstruct Expression with induced subexpressions.
+ switch (CE.Kind) {
+ case CounterExpression::Add:
+ C = add(NewLHS, NewRHS);
+ break;
+ case CounterExpression::Subtract:
+ C = subtract(NewLHS, NewRHS);
+ break;
+ }
+
+ return C;
+}
+
void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
switch (C.getKind()) {
case Counter::Zero:
diff --git a/llvm/unittests/ProfileData/CoverageMappingTest.cpp b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
index ef147674591c51..46f881ecddb5f0 100644
--- a/llvm/unittests/ProfileData/CoverageMappingTest.cpp
+++ b/llvm/unittests/ProfileData/CoverageMappingTest.cpp
@@ -291,6 +291,45 @@ struct CoverageMappingTest : ::testing::TestWithParam<std::tuple<bool, bool>> {
}
};
+TEST(CoverageMappingTest, expression_subst) {
+ CounterExpressionBuilder Builder;
+ CounterExpressionBuilder::SubstMap MapToExpand;
+
+ auto C = [](unsigned ID) { return Counter::getCounter(ID); };
+ auto A = [&](Counter LHS, Counter RHS) { return Builder.add(LHS, RHS); };
+ // returns {E, N} in clangCodeGen
+ auto getBranchCounterPair = [&](Counter E, Counter P, Counter N) {
+ auto Skipped = Builder.subtract(P, E);
+ MapToExpand[N] = Builder.subst(Skipped, MapToExpand);
+ };
+
+ auto E18 = C(5);
+ auto P18 = C(2);
+ auto S18 = C(18);
+ // #18 => (#2 - #5)
+ getBranchCounterPair(E18, P18, S18);
+
+ auto E22 = S18;
+ auto P22 = C(0);
+ auto S22 = C(22);
+ // #22 => #0 - (#2 - #5)
+ getBranchCounterPair(E22, P22, S22);
+
+ auto E28 = A(A(C(9), C(11)), C(14));
+ auto P28 = S22;
+ auto S28 = C(28);
+ // #28 => (((((#0 + #5) - #2) - #9) - #11) - #14)
+ getBranchCounterPair(E28, P28, S28);
+
+ auto LHS = A(E28, A(S28, S18));
+ auto RHS = C(0);
+
+ // W/o subst, LHS cannot be reduced.
+ ASSERT_FALSE(Builder.subtract(LHS, RHS).isZero());
+ // W/ subst, C(18) and C(28) in LHS will be reduced.
+ ASSERT_TRUE(Builder.subst(Builder.subtract(LHS, RHS), MapToExpand).isZero());
+}
+
TEST_P(CoverageMappingTest, basic_write_read) {
startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
More information about the llvm-commits
mailing list