Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm-cov] Simplify and optimize MC/DC computation #79727

Merged

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Jan 28, 2024

Update code from https://reviews.llvm.org/D138847

buildTestVector is a standard DFS (walking a reduced ordered binary
decision diagram). Avoid shouldCopyOffTestVectorFor{True,False}Path
complexity and redundant Map[ID] lookups.

findIndependencePairs unnecessarily uses four nested loops (n<=6) to
find independence pairs. Instead, enumerate the two execution vectors
and find the number of mismatches. This algorithm can be optimized using
the marking function technique described in Efficient Test Coverage
Measurement for MC/DC, 2013
, but this may be overkill.

Created using spr 1.3.4
@chapuni
Copy link
Contributor

chapuni commented Jan 28, 2024

@MaskRay I'm trying to replace this for another purpose, to handle TestVectors also to clangCodeGen.

@chapuni
Copy link
Contributor

chapuni commented Jan 28, 2024

It is buildTestVector stuff for me to replace. findIndependencePairs is good.
Let me check.
Thanks!

Comment on lines +339 to 353
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;
unsigned Flip = NumConditions, Idx;
for (Idx = 0; Idx < NumConditions; ++Idx) {
MCDCRecord::CondState ACond = A[Idx], BCond = B[Idx];
if (ACond == BCond || ACond == MCDCRecord::MCDC_DontCare ||
BCond == MCDCRecord::MCDC_DontCare)
continue;

// If a matching pair of vectors is found, record them.
if ((PairFound = matchTestVectors(I, J, C)))
IndependencePairs[C] = std::make_pair(I + 1, J + 1);
if (Flip != NumConditions)
break;
Flip = Idx;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify more this if we could introduce TestVector stuff as the set of bitvectors?

  • Pair of bitvector
    • "Visited" (Do care) and "True/False map"
    • "True" map and "False" map (D/C should be zero both)
  • Array of two bits to represent three states.

Copy link
Member Author

@MaskRay MaskRay Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the presence of MCDC_DontCare, optimizing this further requires some dirty bit twiddling, which may make the code less readable.

Given that NumConditions is capped by 6, and |TestVectors| is capped by 6+1, the current time complexity should be acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I am planning to relax 6 to thousands.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help. I could understand perhaps extending beyond six, but why thousands? Achieving full MC/DC is not easy even with six, and most embedded cases would have no more than maybe 3 or 4 conditions. I'm concerned about overengineering.

Copy link
Member Author

@MaskRay MaskRay Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have studied MC/DC more and made some notes on https://maskray.me/blog/2024-01-28-mc-dc-and-compiler-implementations

GCC has a pending patch implementing MC/DC as well and they apply an algorithm described by Efficient Test Coverage Measurement for MC/DC, which is linear in terms of the number of conditions.

@evodius96
Copy link
Contributor

@MaskRay I'm trying to replace this for another purpose, to handle TestVectors also to clangCodeGen.

You mean construct the list of possible test vectors in clang? That would be useful.

@chapuni
Copy link
Contributor

chapuni commented Jan 28, 2024

@evodius96

You mean construct the list of possible test vectors in clang?

Planning let clangCodeGen emit TestVectorIndex w/o constructing square vectors.

I could understand perhaps extending beyond six, but why thousands? Achieving full MC/DC is not easy even with six, and most embedded cases would have no more than maybe 3 or 4 conditions. I'm concerned about overengineering.

"Thousands" is not my goal but my expected capacity. Not measured yet.
I think satisfying MC/DC Coverage would be the pain but not punishment, though. :) I also think MC/DC Coverage is one of useful method to discover missing cases.

I am doing it since it is very effective to apply it to the large codebase, LLVM itself, since we could keep it stable and preventing bitrot. I like such a dogfooding. :)

Thank you.

Copy link
Contributor

@chapuni chapuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great from my side.
@evodius96 Could you take a look, too?

/// Find all possible Independence Pairs for a boolean expression given its
/// executed Test Vectors. This process involves looking at each condition
/// and attempting to find two Test Vectors that "match", giving us a pair.
// Find an independence pair for each condition.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a little more commentary on what constitutes an independence pair, i.e. what the algorithm is looking for -- I had a comment around matchTestVectors() about this. I think it improves readability a bit more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two comments in the function body complement this one, but I can place the three conditions together...

@@ -458,7 +395,7 @@ class MCDCRecordProcessor {
MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare);

// Use the base test vector to build the list of all possible test vectors.
buildTestVector(TV);
buildTestVector(TV, 1, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: I wonder if it would be of some value to include a very brief comment that explains what the initial values mean? Up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Added

… Measurement for MC/DC)

Created using spr 1.3.4
@evodius96
Copy link
Contributor

Added a couple of comments, but otherwise LGTM. Thank you for optimizing this.

@MaskRay MaskRay merged commit 3d0a689 into main Jan 29, 2024
3 of 4 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/llvm-cov-simplify-and-optimize-mcdc-computation branch January 29, 2024 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants