Skip to content

Commit

Permalink
[FS-AFDO] Clean up non-zero discriminator for pseudo probes at the fi…
Browse files Browse the repository at this point in the history
…rst FS discriminator pass.

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
  • Loading branch information
htyu committed May 17, 2023
1 parent 456eb4b commit d4d6b9a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/CodeGen/MIRFSDiscriminator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class MachineFunction;
using namespace sampleprof;
class MIRAddFSDiscriminators : public MachineFunctionPass {
MachineFunction *MF = nullptr;
FSDiscriminatorPass Pass;
unsigned LowBit;
unsigned HighBit;

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");
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/MIRFSDiscriminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/CodeGen/X86/fsafdo_probe.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d4d6b9a

Please sign in to comment.