Skip to content

Commit

Permalink
[Coverage] Suppress covmap and profdata for system headers. (#97952)
Browse files Browse the repository at this point in the history
With `system-headers-coverage=false`, functions defined in system
headers were not instrumented but corresponding covmaps were emitted. It
caused wasting covmap and profraw.

This change improves:

- Reduce object size (due to reduced covmap)
- Reduce size of profraw (uninstrumented system headers occupied
counters)
- Smarter view of coverage report. Stubs of uninstrumented system
headers will be no longer seen.
  • Loading branch information
chapuni committed Jul 10, 2024
1 parent c188cb7 commit da31b68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7165,6 +7165,9 @@ void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
SourceManager &SM = getContext().getSourceManager();
if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
break;
if (!llvm::coverage::SystemHeadersCoverage &&
SM.isInSystemHeader(D->getBeginLoc()))
break;
DeferredEmptyCoverageMappingDecls.try_emplace(D, true);
break;
}
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/CodeGen/CodeGenPGO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,13 +1044,17 @@ void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) {
if (Fn->hasFnAttribute(llvm::Attribute::SkipProfile))
return;

SourceManager &SM = CGM.getContext().getSourceManager();
if (!llvm::coverage::SystemHeadersCoverage &&
SM.isInSystemHeader(D->getLocation()))
return;

setFuncName(Fn);

mapRegionCounters(D);
if (CGM.getCodeGenOpts().CoverageMapping)
emitCounterRegionMapping(D);
if (PGOReader) {
SourceManager &SM = CGM.getContext().getSourceManager();
loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
computeRegionCounts(D);
applyFunctionAttributes(PGOReader, Fn);
Expand Down
5 changes: 2 additions & 3 deletions clang/test/CoverageMapping/system_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

// LL_CHECK: @__covrec_
// LL_W_SYS: [[PROFC:@.*__profc_.*SysTmpl.*]] =
// LL_WOSYS: [[PROFC:@.*__profc_.*SysTmpl.*]] =
// LL_W_SYS: @{{.*}}__profd_{{.*}}SysTmpl{{.*}} =
// LL_WOSYS: @{{.*}}__profd_{{.*}}SysTmpl{{.*}} =
// LL_WOSYS-NOT: SysTmpl

// LL_CHECK: @llvm.used =

Expand All @@ -21,7 +20,7 @@
template <bool f> bool SysTmpl() { return f; }
// Check SysTmpl() is instrumented or not.
// LL_W_SYS: load i64, ptr [[PROFC]],
// LL_WOSYS: load i64, ptr [[PROFC]],
// LL_WOSYS-NOT: load i64, ptr @__profc_

#else

Expand Down

0 comments on commit da31b68

Please sign in to comment.