aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-06-13Reland "[llvm-cov] Add support for baseline coverage" (#144130)Fabian Meumertzheim1-48/+75
When no profile is provided, but the new --empty-profile option is specified, the export/report/show commands now emit coverage data equivalent to that obtained from a profile with all zero counters ("baseline coverage"). This is useful for build systems (e.g. Bazel) that can track coverage information for each build target, even those that are never linked into tests and thus don't have runtime coverage data recorded. By merging in baseline coverage, lines in files that aren't linked into tests are correctly reported as uncovered. Reland with fixes to `CoverageMappingTest.cpp`. Reverts llvm/llvm-project#144121
2025-06-13Revert "[llvm-cov] Add support for baseline coverage" (#144121)Keith Smiley1-75/+48
Reverts llvm/llvm-project#117910 ``` /home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/llvm/unittests/ProfileData/CoverageMappingTest.cpp /home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/llvm/unittests/ProfileData/CoverageMappingTest.cpp:281:28: error: 'std::reference_wrapper' may not intend to support class template argument deduction [-Werror,-Wctad-maybe-unsupported] 281 | std::make_optional(std::reference_wrapper(*ProfileReader)); | ^ /usr/lib/gcc/ppc64le-redhat-linux/8/../../../../include/c++/8/bits/refwrap.h:289:11: note: add a deduction guide to suppress this warning 289 | class reference_wrapper | ^ ```
2025-06-13[llvm-cov] Add support for baseline coverage (#117910)Fabian Meumertzheim1-48/+75
When no profile is provided, but the new --empty-profile option is specifed, the export/report/show commands now emit coverage data equivalent to that obtained from a profile with all zero counters ("baseline coverage"). This is useful for build systems (e.g. Bazel) that can track coverage information for each build target, even those that are never linked into tests and thus don't have runtime coverage data recorded. By merging in baseline coverage, lines in files that aren't linked into tests are correctly reported as uncovered.
2025-05-12[llvm] Use llvm::find_if (NFC) (#139654)Kazu Hirata1-2/+2
2025-04-27[llvm] Use hash_combine_range with ranges (NFC) (#137530)Kazu Hirata1-2/+1
2025-01-17[Coverage] Speed up function record iteration (#122050)Mike Hommey1-1/+1
When iterating over function records, filtered by file name, currently, the iteration goes over all the function records, repeatedly for each source file, essentially giving quadratic behavior. 413647d730972eac9675f695c2ea63fb393a5531 sped up some cases by keeping track of the indices of the function records corresponding to each file name. This change expands the use of that map to FunctionRecordIterator. On a test case with Firefox's libxul.so and a 2.5MB profile, this brings down the runtime of `llvm-cov export $lib --instr-profile $prof -t lcov` from 12 minutes with 90% spent in skipOtherFiles to 19 seconds with no samples in skipOtherFiles at all under a sampling profiler (with a sampling interval of 1ms). Fixes #62079
2025-01-09Introduce CounterExpressionBuilder::subst(C, Map) (#112698)NAKAMURA Takumi1-0/+25
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`.
2025-01-07[Coverage] MCDC: Move findIndependencePairs deferred into MCDCRecord (#121188)NAKAMURA Takumi1-32/+35
The result of "Independence pairs" is not mergeable. This change makes defers re-calculation of "Independence pairs" after merging test vectors. No apparent behavior changes.
2024-12-28[Coverage] Make `MCDCRecord::Folded` as `[false/true]` with BitVector. NFC. ↵NAKAMURA Takumi1-5/+6
(#121190) For merging `MCDCRecord`s, `Folded` is expected to be promoted as "Non-folded".
2024-12-27[Coverage] Move SingleByteCoverage out of CountedRegion (#110966)NAKAMURA Takumi1-16/+13
`SingleByteCoverage` is not per-region attribute at least. Move it into `CoverageData` since it comes from `profdata`. Depends on: #120841
2024-12-24[Coverage][Single] Round Counters to boolean after evaluation (#110972)NAKAMURA Takumi1-2/+5
Rounding in merging segments has been done after #75425. Depends on: #113114
2024-12-19Allow `CoverageMapping::getCoverageForFile()` to show Branches also outside ↵NAKAMURA Takumi1-1/+1
functions (#120416) Fixes #119952
2024-10-20[Coverage] Introduce "partial fold" on BranchRegion (#112694)NAKAMURA Takumi1-1/+1
Currently both True/False counts were folded. It lost the information, "It is True or False before folding." It prevented recalling branch counts in merging template instantiations. In `llvm-cov`, a folded branch is shown as: - `[True: n, Folded]` - `[Folded, False n]` In the case If `n` is zero, a branch is reported as "uncovered". This is distinguished from "folded" branch. When folded branches are merged, `Folded` may be dissolved. In the coverage map, either `Counter` is `Zero`. Currently both were `Zero`. Since "partial fold" has been introduced, either case in `switch` is omitted as `Folded`. Each `case:` in `switch` is reported as `[True: n, Folded]`, since `False` count doesn't show meaningful value. When `switch` doesn't have `default:`, `switch (Cond)` is reported as `[Folded, False: n]`, since `True` count was just the sum of `case`(s). `switch` with `default` can be considered as "the statement that doesn't have any `False`(s)".
2024-10-01[ProfileData] Avoid repeated hash lookups (NFC) (#110619)Kazu Hirata1-7/+4
2024-09-09Revert "[Coverage] Ignore unused functions if the count is 0." (#107901)Zequan Wu1-1/+1
Reverts llvm/llvm-project#107661 Breaks llvm-project/llvm/unittests/ProfileData/CoverageMappingTest.cpp
2024-09-09[Coverage] Ignore unused functions if the count is 0. (#107661)Zequan Wu1-1/+1
Relax the condition to ignore the case when count is 0. This fixes a bug on https://github.com/llvm/llvm-project/commit/381e9d2386facea7f2acc0f8c16a6d0731267f80. This was reported at https://discourse.llvm.org/t/coverage-from-multiple-test-executables/81024/.
2024-08-15[llvm] Use llvm::any_of (NFC) (#104443)Kazu Hirata1-3/+3
2024-06-21[VFS] Avoid <stack> include (NFC)Nikita Popov1-0/+1
Directly use a vector instead of wrapping it in a stack, like we do in most places.
2024-06-14[llvm] Use llvm::unique (NFC) (#95628)Kazu Hirata1-1/+1
2024-06-14Reapply: [MC/DC][Coverage] Loosen the limit of NumConds from 6 (#82448)NAKAMURA Takumi1-11/+23
By storing possible test vectors instead of combinations of conditions, the restriction is dramatically relaxed. This introduces two options to `cc1`: * `-fmcdc-max-conditions=32767` * `-fmcdc-max-test-vectors=2147483646` This change makes coverage mapping, profraw, and profdata incompatible with Clang-18. - Bitmap semantics changed. It is incompatible with previous format. - `BitmapIdx` in `Decision` points to the end of the bitmap. - Bitmap is packed per function. - `llvm-cov` can understand `profdata` generated by `llvm-profdata-18`. RFC: https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798 -- Change(s) since llvmorg-19-init-14288-g7ead2d8c7e91 - Update compiler-rt/test/profile/ContinuousSyncMode/image-with-mcdc.c
2024-06-14Revert "[MC/DC][Coverage] Loosen the limit of NumConds from 6 (#82448)"Hans Wennborg1-23/+11
This broke the lit tests on Mac: https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-RA/1096/ > By storing possible test vectors instead of combinations of conditions, > the restriction is dramatically relaxed. > > This introduces two options to `cc1`: > > * `-fmcdc-max-conditions=32767` > * `-fmcdc-max-test-vectors=2147483646` > > This change makes coverage mapping, profraw, and profdata incompatible > with Clang-18. > > - Bitmap semantics changed. It is incompatible with previous format. > - `BitmapIdx` in `Decision` points to the end of the bitmap. > - Bitmap is packed per function. > - `llvm-cov` can understand `profdata` generated by `llvm-profdata-18`. > > RFC: > https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798 This reverts commit 7ead2d8c7e9114b3f23666209a1654939987cb30.
2024-06-13[MC/DC][Coverage] Loosen the limit of NumConds from 6 (#82448)NAKAMURA Takumi1-11/+23
By storing possible test vectors instead of combinations of conditions, the restriction is dramatically relaxed. This introduces two options to `cc1`: * `-fmcdc-max-conditions=32767` * `-fmcdc-max-test-vectors=2147483646` This change makes coverage mapping, profraw, and profdata incompatible with Clang-18. - Bitmap semantics changed. It is incompatible with previous format. - `BitmapIdx` in `Decision` points to the end of the bitmap. - Bitmap is packed per function. - `llvm-cov` can understand `profdata` generated by `llvm-profdata-18`. RFC: https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798
2024-05-10Cleanup asserts in BranchParameters and DecisionParametersNAKAMURA Takumi1-2/+0
2024-02-27[MC/DC] Introduce `class TestVector` with a pair of `BitVector` (#82174)NAKAMURA Takumi1-22/+11
This replaces `SmallVector<CondState>` and emulates it. - -------- True False DontCare - Values: True False False - Visited: True True False `findIndependencePairs()` can be optimized with logical ops. FIXME: Specialize `findIndependencePairs()` for the single word.
2024-02-27[MC/DC] Refactor: Isolate the final result out of TestVector (#82282)NAKAMURA Takumi1-12/+25
To reduce conditional judges in the loop in `findIndependencePairs()`, I have tried a couple of tweaks. * Isolate the final result in `TestVectors` `using TestVectors = llvm::SmallVector<std::pair<TestVector, CondState>>;` The final result was just piggybacked on `TestVector`, so it has been isolated. * Filter out and sort `ExecVectors` by the final result It will cost more in constructing `ExecVectors`, but it can reduce at least one conditional judgement in the loop.
2024-02-26[InstrProf] Single byte counters in coverage (#75425)gulfemsavrun1-3/+10
This patch inserts 1-byte counters instead of an 8-byte counters into llvm profiles for source-based code coverage. The origial idea was proposed as block-cov for PGO, and this patch repurposes that idea for coverage: https://groups.google.com/g/llvm-dev/c/r03Z6JoN7d4 The current 8-byte counters mechanism add counters to minimal regions, and infer the counters in the remaining regions via adding or subtracting counters. For example, it infers the counter in the if.else region by subtracting the counters between if.entry and if.then regions in an if statement. Whenever there is a control-flow merge, it adds the counters from all the incoming regions. However, we are not going to be able to infer counters by subtracting two execution counts when using single-byte counters. Therefore, this patch conservatively inserts additional counters for the cases where we need to add or subtract counters. RFC: https://discourse.llvm.org/t/rfc-single-byte-counters-for-source-based-code-coverage/75685
2024-02-26Introduce mcdc::TVIdxBuilder (LLVM side, NFC) (#80676)NAKAMURA Takumi1-12/+141
This is a preparation of incoming Clang changes (#82448) and just checks `TVIdx` is calculated correctly. NFC. `TVIdxBuilder` calculates deterministic Indices for each Condition Node. It is used for `clang` to emit `TestVector` indices (aka ID) and for `llvm-cov` to reconstruct `TestVectors`. This includes the unittest `CoverageMappingTest.TVIdxBuilder`. See also https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798
2024-02-25Refactor: Let MCDC::State have DecisionByStmt and BranchByStmtNAKAMURA Takumi1-4/+1
- Prune `RegionMCDCBitmapMap` and `RegionCondIDMap`. They are handled by `MCDCState`. - Rename `s/BitmapMap/DecisionByStmt/`. It can handle Decision stuff. - Rename `s/CondIDMap/BranchByStmt/`. It can be handle Branch stuff. - `MCDCRecordProcessor`: Use `DecisionParams.BitmapIdx` directly.
2024-02-15[MC/DC] Refactor: Let MCDCConditionID int16_t with zero-origin (#81257)NAKAMURA Takumi1-13/+13
Also, Let `NumConditions` `uint16_t`. It is smarter to handle the ID as signed. Narrowing to `int16_t` will reduce costs of handling byvalue. (See also #81221 and #81227) External behavior doesn't change. They below handle values as internal values plus 1. * `-dump-coverage-mapping` * `CoverageMappingReader.cpp` * `CoverageMappingWriter.cpp`
2024-02-14[MC/DC] Refactor: Introduce `ConditionIDs` as `std::array<2>` (#81221)NAKAMURA Takumi1-27/+23
Its 0th element corresponds to `FalseID` and 1st to `TrueID`. CoverageMappingGen.cpp: `DecisionIDPair` is replaced with `ConditionIDs`
2024-02-13[MC/DC] Refactor: Make `MCDCParams` as `std::variant` (#81227)NAKAMURA Takumi1-23/+30
Introduce `mcdc::DecisionParameters` and `mcdc::BranchParameters` and make sure them not initialized as zero. FIXME: Could we make `CoverageMappingRegion` as a smart tagged union?
2024-02-13CoverageMapping.cpp: Apply std::move to MCDCRecord (#81220)NAKAMURA Takumi1-4/+4
2024-02-13[MC/DC] Refactor: Introduce `MCDCTypes.h` for `coverage::mcdc` (#81459)NAKAMURA Takumi1-1/+1
They can be also used in `clang`. Introduce the lightweight header instead of `CoverageMapping.h`. This includes for now: * `mcdc::ConditionID` * `mcdc::Parameters`
2024-02-09[Coverage] MCDCRecordProcessor: Find `ExecVectors` directly (#80816)NAKAMURA Takumi1-20/+11
Deprecate `TestVectors`, since no one uses it. This affects the output order of ExecVectors. The current impl emits sorted by binary value of ExecVector. This impl emits along the traversal of `buildTestVector()`.
2024-02-06Anonymize `MCDCRecordProcessor`NAKAMURA Takumi1-0/+4
2024-02-06CoverageMapping.cpp: s/MaxBitmapID/MaxBitmapIdx/ in getMaxBitmapSize()NAKAMURA Takumi1-5/+5
2024-02-05[Coverage] ProfileData: Handle MC/DC Bitmap as BitVector. NFC. (#80608)NAKAMURA Takumi1-49/+20
* `getFunctionBitmap()` stores not `std::vector<uint8_t>` but `BitVector`. * `CounterMappingContext` holds `Bitmap` (instead of the ref of bytes) * `Bitmap` and `BitmapIdx` are used instead of `evaluateBitmap()`. FIXME: `InstrProfRecord` itself should handle `Bitmap` as `BitVector`.
2024-02-02[Coverage] Let `Decision` take account of expansions (#78969)NAKAMURA Takumi1-43/+197
The current implementation (D138849) assumes `Branch`(es) would follow after the corresponding `Decision`. It is not true if `Branch`(es) are forwarded to expanded file ID. As a result, consecutive `Decision`(s) would be confused with insufficient number of `Branch`(es). `Expansion` will point `Branch`(es) in other file IDs if `Expansion` is included in the range of `Decision`. Fixes #77871 --------- Co-authored-by: Alan Phipps <a-phipps@ti.com>
2024-01-29[llvm-cov] Simplify and optimize MC/DC computation (#79727)Fangrui Song1-101/+41
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.
2024-01-23[Coverage] getMaxBitmapSize: Scan `max(BitmapIdx)` instead of the last ↵NAKAMURA Takumi1-5/+6
`Decision` (#78963) In `CoverageMapping.cpp:getMaxBitmapSize()`, this assumed that the last `Decision` has the maxmum `BitmapIdx`. Let it scan `max(BitmapIdx)`. Note that `<=` is used insted of `<`, because `BitmapIdx == 0` is valid and `MaxBitmapID` is `unsigned`. `BitmapIdx` is unique in the record. Fixes #78922
2024-01-23[Coverage] Const-ize `MCDCRecordProcessor` stuff (#78918)NAKAMURA Takumi1-15/+17
The life of `MCDCRecordProcessor`'s instance is short. It may accept `const` objects to process. On the other hand, the life of `MCDCBranches` is shorter than `Record`. It may be rewritten with reference, rather than copying.
2024-01-22[coverage] skipping code coverage for 'if constexpr' and 'if consteval' (#78033)Hana Dusíková1-1/+8
`if constexpr` and `if consteval` conditional statements code coverage should behave more like a preprocesor `#if`-s than normal ConditionalStmt. This PR should fix that. --------- Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2023-12-14[CoverageMapping] Avoid use of pow() resulting in solaris build fail (#75559)Alan Phipps1-1/+1
Fixes a build failure introduced by commit 8ecbb0404d74 ("Reland [Coverage][llvm-cov] Enable MC/DC Support in LLVM Source-based Code Coverage (2/3)") Use of pow() is not necessary.
2023-12-13Reland "[Coverage][llvm-cov] Enable MC/DC Support in LLVM Source-based Code ↵Alan Phipps1-1/+353
Coverage (2/3)" Part 2 of 3. This includes the Visualization and Evaluation components. Differential Revision: https://reviews.llvm.org/D138847
2023-10-22[llvm] Stop including llvm/ADT/SmallString.h (NFC)Kazu Hirata1-1/+0
Identified with misc-include-cleaner.
2023-10-02[NFC] [Coverage] Do not use recursion for getMaxCounterID (#67870)shen3qing11-12/+52
This causes stack overflows for real-world coverage reports. Tested with build/bin/llvm-lit -a llvm/test/tools/llvm-cov Co-authored-by: Qing Shen <qingshen@google.com>
2023-09-26Revert "[Coverage] Allow Clang coverage to be used with debug info correlation."Zequan Wu1-3/+2
This reverts commit 32db121b29f78e4c41116b2a8f1c730f9522b202 and subsequent commits. This causes time regression on llvm-cov even with debug info correlation off.
2023-09-24[llvm-cov] Properly fix -Wcovered-switch-default in CoverageMapping.cppFangrui Song1-11/+9
And revert commit 1f3fa96e516fda26244afb8877e6cee619c21205.
2023-09-22[Coverage] Fix a warningKazu Hirata1-2/+0
This patch fixes: llvm/lib/ProfileData/Coverage/CoverageMapping.cpp:219:5: error: default label in switch which covers all enumeration values [-Werror,-Wcovered-switch-default]
2023-09-22[NFC]Do not use recursion for CounterMappingContext::evaluate (#66961)shen3qing11-20/+54
This causes stack overflows for real-world coverage reports. Ran $ build/bin/llvm-lit -a llvm/test/tools/llvm-cov locally and passed. Co-authored-by: Qing Shen <qingshen@google.com>