[llvm] [Instrumentation] Mark instrumented calls as implicit (PR #106447)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 13:20:49 PDT 2024


https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/106447

Make sure that instrumentation code doesn't interferes with with prologue endpoint computation when generating debug information.

Fix #54873

>From 0060c7d950cabd3638a15a58bc4b318bc08d1a46 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Wed, 28 Aug 2024 21:22:15 +0200
Subject: [PATCH] [Instrumentation] Mark instrumented calls as implicit

Make sure that instrumentation code doesn't interferes with with
prologue endpoint computation when generating debug information.

Fix #54873
---
 .../include/llvm/Transforms/Instrumentation.h | 20 ++++++++++++++++---
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp    |  6 ++++--
 .../Instrumentation/SanitizerCoverage.cpp     |  2 +-
 .../AddressSanitizer/missing_dbg.ll           |  2 +-
 .../SanitizerCoverage/crit-edge-sancov.ll     |  6 +++---
 .../SanitizerCoverage/missing_dbg.ll          |  4 ++--
 .../ThreadSanitizer/missing_dbg.ll            |  4 ++--
 7 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Instrumentation.h b/llvm/include/llvm/Transforms/Instrumentation.h
index 1a4824a806dc6e..7f7e107f41e776 100644
--- a/llvm/include/llvm/Transforms/Instrumentation.h
+++ b/llvm/include/llvm/Transforms/Instrumentation.h
@@ -194,15 +194,29 @@ static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
 // if the enclosing function does.
 struct InstrumentationIRBuilder : IRBuilder<> {
   static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
-    if (IRB.getCurrentDebugLocation())
+    if (DebugLoc L = IRB.getCurrentDebugLocation()) {
+      if (!L.isImplicitCode()) {
+        L.setImplicitCode(true);
+        IRB.SetCurrentDebugLocation(L);
+      }
       return;
-    if (DISubprogram *SP = F.getSubprogram())
-      IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
+    }
+    if (DISubprogram *SP = F.getSubprogram()) {
+      DebugLoc L = DILocation::get(SP->getContext(), 0, 0, SP, nullptr,
+                                   /*ImplicitCode=*/true);
+      IRB.SetCurrentDebugLocation(L);
+    }
   }
 
   explicit InstrumentationIRBuilder(Instruction *IP) : IRBuilder<>(IP) {
     ensureDebugInfo(*this, *IP->getFunction());
   }
+
+  void SetCurrentImplicitDebugLocation(DebugLoc L) {
+    if (!L.isImplicitCode())
+      L.setImplicitCode(true);
+    SetCurrentDebugLocation(L);
+  }
 };
 } // end namespace llvm
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index f111b4aea06f1b..457eab6ed2df33 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -2145,14 +2145,16 @@ static std::pair<DebugLoc, bool> findPrologueEndLoc(const MachineFunction *MF) {
   for (const auto &MBB : *MF) {
     for (const auto &MI : MBB) {
       if (!MI.isMetaInstruction()) {
-        if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
+        if (!MI.getFlag(MachineInstr::FrameSetup) &&
+            !MI.getDebugLoc().isImplicitCode()) {
           // Scan forward to try to find a non-zero line number. The
           // prologue_end marks the first breakpoint in the function after the
           // frame setup, and a compiler-generated line 0 location is not a
           // meaningful breakpoint. If none is found, return the first
           // location after the frame setup.
-          if (MI.getDebugLoc().getLine())
+          if (MI.getDebugLoc().getLine()) {
             return std::make_pair(MI.getDebugLoc(), IsEmptyPrologue);
+          }
 
           LineZeroLoc = MI.getDebugLoc();
         }
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 6a89cee9aaf6cc..cfbc21b69faebe 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -958,7 +958,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
 
   InstrumentationIRBuilder IRB(&*IP);
   if (EntryLoc)
-    IRB.SetCurrentDebugLocation(EntryLoc);
+    IRB.SetCurrentImplicitDebugLocation(EntryLoc);
   if (Options.TracePC) {
     IRB.CreateCall(SanCovTracePC)
         ->setCannotMerge(); // gets the PC using GET_CALLER_PC.
diff --git a/llvm/test/Instrumentation/AddressSanitizer/missing_dbg.ll b/llvm/test/Instrumentation/AddressSanitizer/missing_dbg.ll
index f6553322fd6787..5bb1c01e9d1231 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/missing_dbg.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/missing_dbg.ll
@@ -34,4 +34,4 @@ entry:
 !4 = !DISubroutineType(types: !5)
 !5 = !{}
 
-; CHECK:       [[DBG]] = !DILocation(line: 0, scope: !4)
+; CHECK:       [[DBG]] = !DILocation(line: 0, scope: !4, isImplicitCode: true)
diff --git a/llvm/test/Instrumentation/SanitizerCoverage/crit-edge-sancov.ll b/llvm/test/Instrumentation/SanitizerCoverage/crit-edge-sancov.ll
index f42fa7139fd585..cbab3ac9fdf302 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/crit-edge-sancov.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/crit-edge-sancov.ll
@@ -21,9 +21,9 @@ define void @update_shadow(i1 %c) !dbg !3 {
 ; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc() #[[ATTR0]], !dbg [[DBG7:![0-9]+]]
 ; CHECK:       if.end22.i:
 ; CHECK-NEXT:    call void @__sanitizer_cov_trace_pc() #[[ATTR0]], !dbg [[DBG8:![0-9]+]]
-; CHECK:       [[DBG6]] = !DILocation(line: 192, scope: !3)
-; CHECK:       [[DBG7]] = !DILocation(line: 0, scope: !3)
-; CHECK:       [[DBG8]] = !DILocation(line: 129, column: 2, scope: !3)
+; CHECK:       [[DBG6]] = !DILocation(line: 192, scope: !3, isImplicitCode: true)
+; CHECK:       [[DBG7]] = !DILocation(line: 0, scope: !3, isImplicitCode: true)
+; CHECK:       [[DBG8]] = !DILocation(line: 129, column: 2, scope: !3, isImplicitCode: true)
 entry:
   br i1 %c, label %for.inc.i, label %if.end22.i
 
diff --git a/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll b/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
index 21c6fcdb3a84b0..67af5663ceff64 100644
--- a/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
+++ b/llvm/test/Instrumentation/SanitizerCoverage/missing_dbg.ll
@@ -46,5 +46,5 @@ entry:
 !6 = !DILocation(line: 192, scope: !3)
 !7 = !DILocation(line: 0, scope: !3)
 
-; CHECK:       [[DBG1]] = !DILocation(line: 192, scope: !3)
-; CHECK:       [[DBG2]] = !DILocation(line: 0, scope: !3)
+; CHECK:       [[DBG1]] = !DILocation(line: 192, scope: !3, isImplicitCode: true)
+; CHECK:       [[DBG2]] = !DILocation(line: 0, scope: !3, isImplicitCode: true)
diff --git a/llvm/test/Instrumentation/ThreadSanitizer/missing_dbg.ll b/llvm/test/Instrumentation/ThreadSanitizer/missing_dbg.ll
index a9d6bcd5191487..a5432fa97e2b96 100644
--- a/llvm/test/Instrumentation/ThreadSanitizer/missing_dbg.ll
+++ b/llvm/test/Instrumentation/ThreadSanitizer/missing_dbg.ll
@@ -51,5 +51,5 @@ entry:
 !5 = !{}
 !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 195, type: !4, scopeLine: 199, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
 
-; CHECK:       [[DBG1]] = !DILocation(line: 0, scope: !3)
-; CHECK:       [[DBG2]] = !DILocation(line: 0, scope: !7)
+; CHECK:       [[DBG1]] = !DILocation(line: 0, scope: !3, isImplicitCode: true)
+; CHECK:       [[DBG2]] = !DILocation(line: 0, scope: !7, isImplicitCode: true)



More information about the llvm-commits mailing list