[llvm] e25f4e4 - [PseudoProbe] Do not emit pseudo probes when module is not probed.

Hongtao Yu via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 25 13:00:11 PDT 2022


Author: Hongtao Yu
Date: 2022-03-25T12:59:53-07:00
New Revision: e25f4e4c4a6df1d9f84e9506d2391fb0019bc941

URL: https://github.com/llvm/llvm-project/commit/e25f4e4c4a6df1d9f84e9506d2391fb0019bc941
DIFF: https://github.com/llvm/llvm-project/commit/e25f4e4c4a6df1d9f84e9506d2391fb0019bc941.diff

LOG: [PseudoProbe] Do not emit pseudo probes when module is not probed.

There is a case when a function has pseudo probe intrinsics but the module it resides does not have the probe desc. This could happen when the current module is not built with `-fpseudo-probe-for-profiling` while a function in it calls some other function from a probed module. In thinLTO mode, the callee function could be imported and inlined into the current function.
While this is undefined behavior, I'm fixing the asm printer to not ICE and warn user about this.

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D121737

Added: 
    

Modified: 
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/lib/Linker/IRMover.cpp
    llvm/test/ThinLTO/X86/pseudo-probe-desc-import.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 548a0c697bf23..51741e33ca4f8 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1158,12 +1158,14 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
 }
 
 void AsmPrinter::emitPseudoProbe(const MachineInstr &MI) {
-  auto GUID = MI.getOperand(0).getImm();
-  auto Index = MI.getOperand(1).getImm();
-  auto Type = MI.getOperand(2).getImm();
-  auto Attr = MI.getOperand(3).getImm();
-  DILocation *DebugLoc = MI.getDebugLoc();
-  PP->emitPseudoProbe(GUID, Index, Type, Attr, DebugLoc);
+  if (PP) {
+    auto GUID = MI.getOperand(0).getImm();
+    auto Index = MI.getOperand(1).getImm();
+    auto Type = MI.getOperand(2).getImm();
+    auto Attr = MI.getOperand(3).getImm();
+    DILocation *DebugLoc = MI.getDebugLoc();
+    PP->emitPseudoProbe(GUID, Index, Type, Attr, DebugLoc);
+  }
 }
 
 void AsmPrinter::emitStackSizeSection(const MachineFunction &MF) {

diff  --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index d48ad9a2f6e0b..1a5363d44af86 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -1233,8 +1233,15 @@ void IRLinker::linkNamedMDNodes() {
       continue;
     // Don't import pseudo probe descriptors here for thinLTO. They will be
     // emitted by the originating module.
-    if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName)
+    if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) {
+      if (!DstM.getNamedMetadata(NMD.getName()))
+        emitWarning("Pseudo-probe ignored: source module '" +
+                    SrcM->getModuleIdentifier() +
+                    "' is compiled with -fpseudo-probe-for-profiling while "
+                    "destination module '" +
+                    DstM.getModuleIdentifier() + "' is not\n");
       continue;
+    }
     NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
     // Add Src elements into Dest node.
     for (const MDNode *Op : NMD.operands())

diff  --git a/llvm/test/ThinLTO/X86/pseudo-probe-desc-import.ll b/llvm/test/ThinLTO/X86/pseudo-probe-desc-import.ll
index b385c78fffa09..21dd8c0fe9241 100644
--- a/llvm/test/ThinLTO/X86/pseudo-probe-desc-import.ll
+++ b/llvm/test/ThinLTO/X86/pseudo-probe-desc-import.ll
@@ -1,28 +1,31 @@
-; RUN: opt -module-summary %s -o %t1.bc
+; RUN: opt -module-summary -passes=pseudo-probe %s -o %t1.bc
 ; RUN: opt -module-summary %p/Inputs/pseudo-probe-desc-import.ll -o %t2.bc
 ; RUN: llvm-lto -thinlto-action=thinlink -o %t.index.bc %t1.bc %t2.bc
 
 ; Don't import pseudo probe desc.
 ; RUN: llvm-lto -thinlto-action=import %t1.bc -thinlto-index=%t.index.bc -o - | llvm-dis -o - | FileCheck %s
+
+
+; Warn that current module is not pseudo-probe instrumented.
+; RUN: opt -module-summary %s -o %t3.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t3.index.bc %t3.bc %t2.bc
+; RUN: llvm-lto -thinlto-action=import %t3.bc -thinlto-index=%t3.index.bc -o /dev/null 2>&1 | FileCheck %s  --check-prefix=WARN
+
+
 ; CHECK-NOT: {i64 6699318081062747564, i64 4294967295, !"foo"
 ; CHECK: !{i64 -2624081020897602054, i64 281479271677951, !"main"
 
+; WARN: warning: Pseudo-probe ignored: source module '{{.*}}' is compiled with -fpseudo-probe-for-profiling while destination module '{{.*}}' is not
+
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
 
 define i32 @main() {
 entry:
   call void (...) @foo()
-  call void @llvm.pseudoprobe(i64 -2624081020897602054, i64 1, i32 0, i64 -1)
   ret i32 0
 }
 
 declare void @foo(...)
 
-declare void @llvm.pseudoprobe(i64, i64, i32, i64) #0
-
 attributes #0 = { inaccessiblememonly nounwind willreturn }
-
-!llvm.pseudo_probe_desc = !{!0}
-
-!0 = !{i64 -2624081020897602054, i64 281479271677951, !"main", null}


        


More information about the llvm-commits mailing list