[FS-AFDO] Clean up non-zero discriminator for pseudo probes at the first FS discrimin...
authorHongtao Yu <hoy@fb.com>
Wed, 17 May 2023 00:26:43 +0000 (17:26 -0700)
committerHongtao Yu <hoy@fb.com>
Wed, 17 May 2023 04:35:17 +0000 (21:35 -0700)
The dwarf discriminator field for pseudo probes is not supposed to be used until the first FS discriminator pass. Unfortunately there are always corner cases that accidientally set this field. For example, the inliner could set this field for an inlined instruction if the instruction does not come with any debug information. While fixing all such spots is possible, but for future-proff I'd like to enforce a general cleanup before assigning probes any FS discriminator.

Reviewed By: wenlei

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

llvm/include/llvm/CodeGen/MIRFSDiscriminator.h
llvm/lib/CodeGen/MIRFSDiscriminator.cpp
llvm/test/CodeGen/X86/fsafdo_probe.ll

index 1ab55a7..da94326 100644 (file)
@@ -31,6 +31,7 @@ class MachineFunction;
 using namespace sampleprof;
 class MIRAddFSDiscriminators : public MachineFunctionPass {
   MachineFunction *MF = nullptr;
+  FSDiscriminatorPass Pass;
   unsigned LowBit;
   unsigned HighBit;
 
@@ -38,7 +39,7 @@ public:
   static char ID;
   /// PassNum is the sequence number this pass is called, start from 1.
   MIRAddFSDiscriminators(FSDiscriminatorPass P = FSDiscriminatorPass::Pass1)
-      : MachineFunctionPass(ID) {
+      : MachineFunctionPass(ID), Pass(P) {
     LowBit = getFSPassBitBegin(P);
     HighBit = getFSPassBitEnd(P);
     assert(LowBit < HighBit && "HighBit needs to be greater than Lowbit");
index 6f985df..bdafe21 100644 (file)
@@ -150,6 +150,12 @@ bool MIRAddFSDiscriminators::runOnMachineFunction(MachineFunction &MF) {
       if (LineNo == 0)
         continue;
       unsigned Discriminator = DIL->getDiscriminator();
+      // Clean up discriminators for pseudo probes at the first FS discriminator
+      // pass as their discriminators should not ever be used.
+      if ((Pass == FSDiscriminatorPass::Pass1) && I.isPseudoProbe()) {
+        Discriminator = 0;
+        I.setDebugLoc(DIL->cloneWithDiscriminator(0));
+      }
       uint64_t CallStackHashVal = 0;
       if (ImprovedFSDiscriminator)
         CallStackHashVal = getCallStackHash(DIL);
index bc19a29..fa91670 100644 (file)
@@ -65,7 +65,8 @@ attributes #0 = { inaccessiblememonly nocallback nofree nosync nounwind willretu
 !6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 5, type: !7, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2)
 !7 = !DISubroutineType(types: !2)
 !8 = !DILocation(line: 7, column: 15, scope: !9)
-!9 = !DILexicalBlockFile(scope: !6, file: !1, discriminator: 0)
+;; The discriminator with value 2 is to test that it can be cleaned up by the first FS discriminator pass.
+!9 = !DILexicalBlockFile(scope: !6, file: !1, discriminator: 2)
 !10 = !DILocation(line: 7, column: 3, scope: !9)
 !11 = !DILocation(line: 9, column: 5, scope: !9)
 !12 = !DILocation(line: 14, column: 3, scope: !6)