[llvm] [MC/DC] Refactor: Isolate the final result out of TestVectors (PR #82282)
NAKAMURA Takumi via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 26 04:35:21 PST 2024
https://github.com/chapuni updated https://github.com/llvm/llvm-project/pull/82282
>From f96be84c25e0c47a23231fe9dcf81ab9216dada2 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Mon, 19 Feb 2024 21:14:30 +0900
Subject: [PATCH 1/3] using TestVectors =
llvm::SmallVector<std::pair<TestVector, CondState>>;
---
.../ProfileData/Coverage/CoverageMapping.h | 6 ++---
.../ProfileData/Coverage/CoverageMapping.cpp | 25 +++++++++++--------
2 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index c5c9740f25c2ce..44d58aba7754ad 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -381,7 +381,7 @@ struct MCDCRecord {
enum CondState { MCDC_DontCare = -1, MCDC_False = 0, MCDC_True = 1 };
using TestVector = llvm::SmallVector<CondState>;
- using TestVectors = llvm::SmallVector<TestVector>;
+ using TestVectors = llvm::SmallVector<std::pair<TestVector, CondState>>;
using BoolVector = llvm::SmallVector<bool>;
using TVRowPair = std::pair<unsigned, unsigned>;
using TVPairMap = llvm::DenseMap<unsigned, TVRowPair>;
@@ -422,13 +422,13 @@ struct MCDCRecord {
/// accessing conditions in the TestVectors requires a translation from a
/// ordinal position to actual condition ID. This is done via PosToID[].
CondState getTVCondition(unsigned TestVectorIndex, unsigned Condition) {
- return TV[TestVectorIndex][PosToID[Condition]];
+ return TV[TestVectorIndex].first[PosToID[Condition]];
}
/// Return the Result evaluation for an executed test vector.
/// See MCDCRecordProcessor::RecordTestVector().
CondState getTVResult(unsigned TestVectorIndex) {
- return TV[TestVectorIndex][getNumConditions()];
+ return TV[TestVectorIndex].second;
}
/// Determine whether a given condition (indicated by Condition) is covered
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index ddce7580729170..e8440fa9fb09f7 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -257,6 +257,8 @@ class MCDCRecordProcessor {
/// Actual executed Test Vectors for the boolean expression, based on
/// ExecutedTestVectorBitmap.
MCDCRecord::TestVectors ExecVectors;
+ std::array<MCDCRecord::TestVectors, 2> ExecVectorsByCond;
+ unsigned NumExecVectorsF;
public:
MCDCRecordProcessor(const BitVector &Bitmap,
@@ -291,11 +293,9 @@ class MCDCRecordProcessor {
continue;
// Copy the completed test vector to the vector of testvectors.
- ExecVectors.push_back(TV);
-
// The final value (T,F) is equal to the last non-dontcare state on the
// path (in a short-circuiting system).
- ExecVectors.back().push_back(MCDCCond);
+ ExecVectorsByCond[MCDCCond].push_back({TV, MCDCCond});
}
// Reset back to DontCare.
@@ -310,6 +310,12 @@ class MCDCRecordProcessor {
// `Index` encodes the bitmask of true values and is initially 0.
MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare);
buildTestVector(TV, 0, 0);
+
+ auto &[ExecVectorsF, ExecVectorsT] = ExecVectorsByCond;
+ NumExecVectorsF = ExecVectorsF.size();
+ ExecVectors = std::move(ExecVectorsF);
+ ExecVectors.append(std::make_move_iterator(ExecVectorsT.begin()),
+ std::make_move_iterator(ExecVectorsT.end()));
}
// Find an independence pair for each condition:
@@ -318,13 +324,12 @@ class MCDCRecordProcessor {
// - All other conditions' values must be equal or marked as "don't care".
void findIndependencePairs() {
unsigned NumTVs = ExecVectors.size();
- for (unsigned I = 1; I < NumTVs; ++I) {
- const MCDCRecord::TestVector &A = ExecVectors[I];
- for (unsigned J = 0; J < I; ++J) {
- const MCDCRecord::TestVector &B = ExecVectors[J];
- // Enumerate two execution vectors whose outcomes are different.
- if (A[NumConditions] == B[NumConditions])
- continue;
+ for (unsigned I = NumExecVectorsF; I < NumTVs; ++I) {
+ const auto &[A, ACond] = ExecVectors[I];
+ assert(ACond == MCDCRecord::MCDC_True);
+ for (unsigned J = 0; J < NumExecVectorsF; ++J) {
+ const auto &[B, BCond] = ExecVectors[J];
+ assert(BCond == MCDCRecord::MCDC_False);
unsigned Flip = NumConditions, Idx;
for (Idx = 0; Idx < NumConditions; ++Idx) {
MCDCRecord::CondState ACond = A[Idx], BCond = B[Idx];
>From 7142905c77f2732c1cfe7aa1038d1c8b1ce8d379 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Thu, 22 Feb 2024 21:44:46 +0900
Subject: [PATCH 2/3] Add comments
---
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index e8440fa9fb09f7..53e7524fd6d6fa 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -257,7 +257,11 @@ class MCDCRecordProcessor {
/// Actual executed Test Vectors for the boolean expression, based on
/// ExecutedTestVectorBitmap.
MCDCRecord::TestVectors ExecVectors;
+
+ /// Temporary use
std::array<MCDCRecord::TestVectors, 2> ExecVectorsByCond;
+
+ /// Number of False items in ExecVectors
unsigned NumExecVectorsF;
public:
@@ -311,6 +315,7 @@ class MCDCRecordProcessor {
MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare);
buildTestVector(TV, 0, 0);
+ // Fill ExecVectors order by False items and True items.
auto &[ExecVectorsF, ExecVectorsT] = ExecVectorsByCond;
NumExecVectorsF = ExecVectorsF.size();
ExecVectors = std::move(ExecVectorsF);
>From 8aa476d92189b48795b7429ed01f77572b4b9ccd Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Mon, 26 Feb 2024 20:39:24 +0900
Subject: [PATCH 3/3] Let `ExecVectors` alias of `ExecVectorsByCond[false]`
---
.../ProfileData/Coverage/CoverageMapping.cpp | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index be8ea79af3fe00..94768fd40964b8 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -370,12 +370,13 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
/// Mapping of calculated MC/DC Independence Pairs for each condition.
MCDCRecord::TVPairMap IndependencePairs;
+ /// Storage for ExecVectors
+ /// ExecVectors is the alias of its 0th element.
+ std::array<MCDCRecord::TestVectors, 2> ExecVectorsByCond;
+
/// Actual executed Test Vectors for the boolean expression, based on
/// ExecutedTestVectorBitmap.
- MCDCRecord::TestVectors ExecVectors;
-
- /// Temporary use
- std::array<MCDCRecord::TestVectors, 2> ExecVectorsByCond;
+ MCDCRecord::TestVectors &ExecVectors;
/// Number of False items in ExecVectors
unsigned NumExecVectorsF;
@@ -391,7 +392,8 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
: NextIDsBuilder(Branches), TVIdxBuilder(this->NextIDs), Bitmap(Bitmap),
Region(Region), DecisionParams(Region.getDecisionParams()),
Branches(Branches), NumConditions(DecisionParams.NumConditions),
- Folded(NumConditions, false), IndependencePairs(NumConditions) {}
+ Folded(NumConditions, false), IndependencePairs(NumConditions),
+ ExecVectors(ExecVectorsByCond[false]) {}
private:
// Walk the binary decision diagram and try assigning both false and true to
@@ -443,9 +445,10 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
"TVIdxs wasn't fulfilled");
// Fill ExecVectors order by False items and True items.
- auto &[ExecVectorsF, ExecVectorsT] = ExecVectorsByCond;
- NumExecVectorsF = ExecVectorsF.size();
- ExecVectors = std::move(ExecVectorsF);
+ // ExecVectors is the alias of ExecVectorsByCond[false], so
+ // Append ExecVectorsByCond[true] on it.
+ NumExecVectorsF = ExecVectors.size();
+ auto &ExecVectorsT = ExecVectorsByCond[true];
ExecVectors.append(std::make_move_iterator(ExecVectorsT.begin()),
std::make_move_iterator(ExecVectorsT.end()));
}
More information about the llvm-commits
mailing list