LLVM  14.0.0git
PseudoProbe.cpp
Go to the documentation of this file.
1 //===- PseudoProbe.cpp - Pseudo Probe Helpers -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the helpers to manipulate pseudo probe IR intrinsic
10 // calls.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/PseudoProbe.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/Instruction.h"
18 #include <unordered_set>
19 
20 using namespace llvm;
21 
22 namespace llvm {
23 
25  assert(isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst) &&
26  "Only call instructions should have pseudo probe encodes as their "
27  "Dwarf discriminators");
28  if (const DebugLoc &DLoc = Inst.getDebugLoc()) {
29  const DILocation *DIL = DLoc;
30  auto Discriminator = DIL->getDiscriminator();
31  if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
32  PseudoProbe Probe;
33  Probe.Id =
35  Probe.Type =
37  Probe.Attr =
39  Probe.Factor =
42  return Probe;
43  }
44  }
45  return None;
46 }
47 
49  if (const auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
50  PseudoProbe Probe;
51  Probe.Id = II->getIndex()->getZExtValue();
53  Probe.Attr = II->getAttributes()->getZExtValue();
54  Probe.Factor = II->getFactor()->getZExtValue() /
56  return Probe;
57  }
58 
59  if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst))
60  return extractProbeFromDiscriminator(Inst);
61 
62  return None;
63 }
64 
65 void setProbeDistributionFactor(Instruction &Inst, float Factor) {
66  assert(Factor >= 0 && Factor <= 1 &&
67  "Distribution factor must be in [0, 1.0]");
68  if (auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
69  IRBuilder<> Builder(&Inst);
71  if (Factor < 1)
72  IntFactor *= Factor;
73  auto OrigFactor = II->getFactor()->getZExtValue();
74  if (IntFactor != OrigFactor)
75  II->replaceUsesOfWith(II->getFactor(), Builder.getInt64(IntFactor));
76  } else if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst)) {
77  if (const DebugLoc &DLoc = Inst.getDebugLoc()) {
78  const DILocation *DIL = DLoc;
79  auto Discriminator = DIL->getDiscriminator();
80  if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
81  auto Index =
83  auto Type =
86  Discriminator);
87  // Round small factors to 0 to avoid over-counting.
88  uint32_t IntFactor =
90  if (Factor < 1)
91  IntFactor *= Factor;
93  Index, Type, Attr, IntFactor);
94  DIL = DIL->cloneWithDiscriminator(V);
95  Inst.setDebugLoc(DIL);
96  }
97  }
98  }
99 }
100 
101 } // namespace llvm
llvm::PseudoProbeFullDistributionFactor
constexpr static uint64_t PseudoProbeFullDistributionFactor
Definition: PseudoProbe.h:31
llvm::PseudoProbeDwarfDiscriminator::extractProbeAttributes
static uint32_t extractProbeAttributes(uint32_t Value)
Definition: PseudoProbe.h:62
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::PseudoProbeDwarfDiscriminator::FullDistributionFactor
constexpr static uint8_t FullDistributionFactor
Definition: PseudoProbe.h:71
DebugInfoMetadata.h
llvm::IRBuilder<>
llvm::DILocation::isPseudoProbeDiscriminator
static bool isPseudoProbeDiscriminator(unsigned Discriminator)
Definition: DebugInfoMetadata.h:1701
llvm::DILocation
Debug location.
Definition: DebugInfoMetadata.h:1580
llvm::PseudoProbeDwarfDiscriminator::extractProbeType
static uint32_t extractProbeType(uint32_t Value)
Definition: PseudoProbe.h:58
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::DILocation::cloneWithDiscriminator
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
Definition: DebugInfoMetadata.h:2213
llvm::Optional
Definition: APInt.h:33
llvm::PseudoProbe::Factor
float Factor
Definition: PseudoProbe.h:81
llvm::PseudoProbeDwarfDiscriminator::extractProbeFactor
static uint32_t extractProbeFactor(uint32_t Value)
Definition: PseudoProbe.h:66
llvm::PseudoProbeDwarfDiscriminator::packProbeData
static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags, uint32_t Factor)
Definition: PseudoProbe.h:44
llvm::PseudoProbe::Id
uint32_t Id
Definition: PseudoProbe.h:75
Instruction.h
llvm::Instruction
Definition: Instruction.h:45
llvm::setProbeDistributionFactor
void setProbeDistributionFactor(Instruction &Inst, float Factor)
Definition: PseudoProbe.cpp:65
llvm::None
const NoneType None
Definition: None.h:23
llvm::PseudoProbe::Type
uint32_t Type
Definition: PseudoProbe.h:76
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:367
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PseudoProbeDwarfDiscriminator::extractProbeIndex
static uint32_t extractProbeIndex(uint32_t Value)
Definition: PseudoProbe.h:54
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:650
uint32_t
llvm::extractProbeFromDiscriminator
Optional< PseudoProbe > extractProbeFromDiscriminator(const Instruction &Inst)
Definition: PseudoProbe.cpp:24
PseudoProbe.h
llvm::extractProbe
Optional< PseudoProbe > extractProbe(const Instruction &Inst)
Definition: PseudoProbe.cpp:48
llvm::PseudoProbeType::Block
@ Block
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:370
llvm::PseudoProbe
Definition: PseudoProbe.h:74
llvm::PseudoProbe::Attr
uint32_t Attr
Definition: PseudoProbe.h:77
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33