From e4274cfe06fe48ed8f0c9f965c8b519e30433bf8 Mon Sep 17 00:00:00 2001 From: Pirama Arumuga Nainar Date: Mon, 3 May 2021 11:57:14 -0700 Subject: [CoverageMapping] Handle gaps in counter IDs for source-based coverage For source-based coverage, the frontend sets the counter IDs and the constraints of counter IDs is not defined. For e.g., the Rust frontend until recently had a reserved counter #0 (https://github.com/rust-lang/rust/pull/83774). Rust coverage instrumentation also creates counters on edges in addition to basic blocks. Some functions may have more counters than regions. This breaks an assumption in CoverageMapping.cpp where the number of counters in a function is assumed to be bounded by the number of regions: Counts.assign(Record.MappingRegions.size(), 0); This assumption causes CounterMappingContext::evaluate() to fail since there are not enough counter values created in the above call to `Counts.assign`. Consequently, some uncovered functions are not reported in coverage reports. This change walks a Function's CoverageMappingRecord to find the maximum counter ID, and uses it to initialize the counter array when instrprof records are missing for a function in sparse profiles. Differential Revision: https://reviews.llvm.org/D101780 --- llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'llvm/lib/ProfileData/Coverage/CoverageMapping.cpp') diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index 2fd70a0..94c2bee 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -186,6 +186,22 @@ Expected CounterMappingContext::evaluate(const Counter &C) const { llvm_unreachable("Unhandled CounterKind"); } +unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const { + switch (C.getKind()) { + case Counter::Zero: + return 0; + case Counter::CounterValueReference: + return C.getCounterID(); + case Counter::Expression: { + if (C.getExpressionID() >= Expressions.size()) + return 0; + const auto &E = Expressions[C.getExpressionID()]; + return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS)); + } + } + llvm_unreachable("Unhandled CounterKind"); +} + void FunctionRecordIterator::skipOtherFiles() { while (Current != Records.end() && !Filename.empty() && Filename != Current->Filenames[0]) @@ -203,6 +219,15 @@ ArrayRef CoverageMapping::getImpreciseRecordIndicesForFilename( return RecordIt->second; } +static unsigned getMaxCounterID(const CounterMappingContext &Ctx, + const CoverageMappingRecord &Record) { + unsigned MaxCounterID = 0; + for (const auto &Region : Record.MappingRegions) { + MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count)); + } + return MaxCounterID; +} + Error CoverageMapping::loadFunctionRecord( const CoverageMappingRecord &Record, IndexedInstrProfReader &ProfileReader) { @@ -227,7 +252,7 @@ Error CoverageMapping::loadFunctionRecord( return Error::success(); } else if (IPE != instrprof_error::unknown_function) return make_error(IPE); - Counts.assign(Record.MappingRegions.size(), 0); + Counts.assign(getMaxCounterID(Ctx, Record) + 1, 0); } Ctx.setCounts(Counts); -- cgit v1.1