Skip to content

[Coverage] Mark coverage sections as metadata sections on COFF. #76834

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

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

ZequanWu
Copy link
Contributor

@ZequanWu ZequanWu commented Jan 3, 2024

Mark .lcovmap$M, .lcovfun$M, .lcovd and .lcovn as metadata sections on COFF so they are not loaded into memory.

@ZequanWu ZequanWu requested a review from rnk January 3, 2024 17:05
@llvmbot
Copy link
Member

llvmbot commented Jan 3, 2024

@llvm/pr-subscribers-backend-x86

Author: Zequan Wu (ZequanWu)

Changes

Mark .lcovmap$M, .lcovfun$M, .lcovd and .lcovn as metadata sections on COFF so they are not loaded into memory.


Full diff: https://github.com/llvm/llvm-project/pull/76834.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+10-1)
  • (added) llvm/test/CodeGen/X86/cov-sections.ll (+15)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 6e69dc66429d31..a69b71451736fa 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1669,9 +1669,18 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
 
 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
+  StringRef Name = GO->getSection();
+  if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
+                                      /*AddSegmentInfo=*/false) ||
+      Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
+                                      /*AddSegmentInfo=*/false) ||
+      Name == getInstrProfSectionName(IPSK_covdata, Triple::COFF,
+                                      /*AddSegmentInfo=*/false) ||
+      Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
+                                      /*AddSegmentInfo=*/false))
+    Kind = SectionKind::getMetadata();
   int Selection = 0;
   unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
-  StringRef Name = GO->getSection();
   StringRef COMDATSymName = "";
   if (GO->hasComdat()) {
     Selection = getSelectionForCOFF(GO);
diff --git a/llvm/test/CodeGen/X86/cov-sections.ll b/llvm/test/CodeGen/X86/cov-sections.ll
new file mode 100644
index 00000000000000..6c4f3f079f38e3
--- /dev/null
+++ b/llvm/test/CodeGen/X86/cov-sections.ll
@@ -0,0 +1,15 @@
+; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc -filetype=obj -o - | llvm-readobj -S - | FileCheck %s
+
+@covmap = private global i32 0, section ".lcovmap$M"
+@covfun = private global i32 0, section ".lcovfun$M"
+@covname = private global i32 0, section ".lcovd"
+@covdata= private global i32 0, section ".lcovn"
+
+; CHECK: Name: .lcovmap$M
+; CHECK: IMAGE_SCN_MEM_DISCARDABLE (0x2000000)
+; CHECK: Name: .lcovfun$M
+; CHECK: IMAGE_SCN_MEM_DISCARDABLE (0x2000000)
+; CHECK: Name: .lcovd
+; CHECK: IMAGE_SCN_MEM_DISCARDABLE (0x2000000)
+; CHECK: Name: .lcovn
+; CHECK: IMAGE_SCN_MEM_DISCARDABLE (0x2000000)

@rnk rnk requested a review from aeubanks January 8, 2024 21:51
Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

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

Arthur, is there a better way to set section flags in LLVM IR, or at least a better place to store special case known section names to assign their characteristics?

cc @MaskRay , perhaps you have ideas

@aeubanks
Copy link
Contributor

aeubanks commented Jan 8, 2024

I don't think it makes sense to put this in the IR, choosing when emitting an object file seems like the right place to make this decision

this looks in line with existing SectionKind overrides per object file format (e.g. getELFKindForNamedSection, TargetLoweringObjectFileWasm::getExplicitSectionGlobal). TargetLoweringObjectFile::getKindForGlobal() is where the common logic is, and there's an argument to be made to consolidate all of the per object file format overrides into that method, but I also think this is fine

@ZequanWu ZequanWu merged commit 4e8986f into llvm:main Jan 9, 2024
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…#76834)

Mark `.lcovmap$M`, `.lcovfun$M`, `.lcovd` and `.lcovn` as metadata
sections on COFF so they are not loaded into memory.
@ZequanWu ZequanWu deleted the win-cov-sections branch February 15, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants