MCPseudoProbe.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //===- lib/MC/MCPseudoProbe.cpp - Pseudo probe encoding support ----------===//
  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. #include "llvm/MC/MCPseudoProbe.h"
  9. #include "llvm/MC/MCAsmInfo.h"
  10. #include "llvm/MC/MCContext.h"
  11. #include "llvm/MC/MCObjectFileInfo.h"
  12. #include "llvm/MC/MCObjectStreamer.h"
  13. #include "llvm/MC/MCStreamer.h"
  14. #define DEBUG_TYPE "mcpseudoprobe"
  15. using namespace llvm;
  16. #ifndef NDEBUG
  17. int MCPseudoProbeTable::DdgPrintIndent = 0;
  18. #endif
  19. static const MCExpr *buildSymbolDiff(MCObjectStreamer *MCOS, const MCSymbol *A,
  20. const MCSymbol *B) {
  21. MCContext &Context = MCOS->getContext();
  22. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  23. const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
  24. const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
  25. const MCExpr *AddrDelta =
  26. MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context);
  27. return AddrDelta;
  28. }
  29. void MCPseudoProbe::emit(MCObjectStreamer *MCOS,
  30. const MCPseudoProbe *LastProbe) const {
  31. // Emit Index
  32. MCOS->emitULEB128IntValue(Index);
  33. // Emit Type and the flag:
  34. // Type (bit 0 to 3), with bit 4 to 6 for attributes.
  35. // Flag (bit 7, 0 - code address, 1 - address delta). This indicates whether
  36. // the following field is a symbolic code address or an address delta.
  37. assert(Type <= 0xF && "Probe type too big to encode, exceeding 15");
  38. assert(Attributes <= 0x7 &&
  39. "Probe attributes too big to encode, exceeding 7");
  40. uint8_t PackedType = Type | (Attributes << 4);
  41. uint8_t Flag = LastProbe ? ((int8_t)MCPseudoProbeFlag::AddressDelta << 7) : 0;
  42. MCOS->emitInt8(Flag | PackedType);
  43. if (LastProbe) {
  44. // Emit the delta between the address label and LastProbe.
  45. const MCExpr *AddrDelta =
  46. buildSymbolDiff(MCOS, Label, LastProbe->getLabel());
  47. int64_t Delta;
  48. if (AddrDelta->evaluateAsAbsolute(Delta, MCOS->getAssemblerPtr())) {
  49. MCOS->emitSLEB128IntValue(Delta);
  50. } else {
  51. MCOS->insert(new MCPseudoProbeAddrFragment(AddrDelta));
  52. }
  53. } else {
  54. // Emit label as a symbolic code address.
  55. MCOS->emitSymbolValue(
  56. Label, MCOS->getContext().getAsmInfo()->getCodePointerSize());
  57. }
  58. LLVM_DEBUG({
  59. dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
  60. dbgs() << "Probe: " << Index << "\n";
  61. });
  62. }
  63. MCPseudoProbeInlineTree::~MCPseudoProbeInlineTree() {
  64. for (auto &Inlinee : Inlinees)
  65. delete Inlinee.second;
  66. }
  67. MCPseudoProbeInlineTree *
  68. MCPseudoProbeInlineTree::getOrAddNode(InlineSite Site) {
  69. auto Iter = Inlinees.find(Site);
  70. if (Iter == Inlinees.end()) {
  71. auto *Node = new MCPseudoProbeInlineTree(std::get<0>(Site));
  72. Inlinees[Site] = Node;
  73. return Node;
  74. } else {
  75. return Iter->second;
  76. }
  77. }
  78. void MCPseudoProbeInlineTree::addPseudoProbe(
  79. const MCPseudoProbe &Probe, const MCPseudoProbeInlineStack &InlineStack) {
  80. // The function should not be called on the root.
  81. assert(isRoot() && "Should not be called on root");
  82. // When it comes here, the input look like:
  83. // Probe: GUID of C, ...
  84. // InlineStack: [88, A], [66, B]
  85. // which means, Function A inlines function B at call site with a probe id of
  86. // 88, and B inlines C at probe 66. The tri-tree expects a tree path like {[0,
  87. // A], [88, B], [66, C]} to locate the tree node where the probe should be
  88. // added. Note that the edge [0, A] means A is the top-level function we are
  89. // emitting probes for.
  90. // Make a [0, A] edge.
  91. // An empty inline stack means the function that the probe originates from
  92. // is a top-level function.
  93. InlineSite Top;
  94. if (InlineStack.empty()) {
  95. Top = InlineSite(Probe.getGuid(), 0);
  96. } else {
  97. Top = InlineSite(std::get<0>(InlineStack.front()), 0);
  98. }
  99. auto *Cur = getOrAddNode(Top);
  100. // Make interior edges by walking the inline stack. Once it's done, Cur should
  101. // point to the node that the probe originates from.
  102. if (!InlineStack.empty()) {
  103. auto Iter = InlineStack.begin();
  104. auto Index = std::get<1>(*Iter);
  105. Iter++;
  106. for (; Iter != InlineStack.end(); Iter++) {
  107. // Make an edge by using the previous probe id and current GUID.
  108. Cur = Cur->getOrAddNode(InlineSite(std::get<0>(*Iter), Index));
  109. Index = std::get<1>(*Iter);
  110. }
  111. Cur = Cur->getOrAddNode(InlineSite(Probe.getGuid(), Index));
  112. }
  113. Cur->Probes.push_back(Probe);
  114. }
  115. void MCPseudoProbeInlineTree::emit(MCObjectStreamer *MCOS,
  116. const MCPseudoProbe *&LastProbe) {
  117. LLVM_DEBUG({
  118. dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
  119. dbgs() << "Group [\n";
  120. MCPseudoProbeTable::DdgPrintIndent += 2;
  121. });
  122. // Emit probes grouped by GUID.
  123. if (Guid != 0) {
  124. LLVM_DEBUG({
  125. dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
  126. dbgs() << "GUID: " << Guid << "\n";
  127. });
  128. // Emit Guid
  129. MCOS->emitInt64(Guid);
  130. // Emit number of probes in this node
  131. MCOS->emitULEB128IntValue(Probes.size());
  132. // Emit number of direct inlinees
  133. MCOS->emitULEB128IntValue(Inlinees.size());
  134. // Emit probes in this group
  135. for (const auto &Probe : Probes) {
  136. Probe.emit(MCOS, LastProbe);
  137. LastProbe = &Probe;
  138. }
  139. } else {
  140. assert(Probes.empty() && "Root should not have probes");
  141. }
  142. // Emit descendent
  143. for (const auto &Inlinee : Inlinees) {
  144. if (Guid) {
  145. // Emit probe index
  146. MCOS->emitULEB128IntValue(std::get<1>(Inlinee.first));
  147. LLVM_DEBUG({
  148. dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
  149. dbgs() << "InlineSite: " << std::get<1>(Inlinee.first) << "\n";
  150. });
  151. }
  152. // Emit the group
  153. Inlinee.second->emit(MCOS, LastProbe);
  154. }
  155. LLVM_DEBUG({
  156. MCPseudoProbeTable::DdgPrintIndent -= 2;
  157. dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
  158. dbgs() << "]\n";
  159. });
  160. }
  161. void MCPseudoProbeSection::emit(MCObjectStreamer *MCOS) {
  162. MCContext &Ctx = MCOS->getContext();
  163. for (auto &ProbeSec : MCProbeDivisions) {
  164. const MCPseudoProbe *LastProbe = nullptr;
  165. if (auto *S =
  166. Ctx.getObjectFileInfo()->getPseudoProbeSection(ProbeSec.first)) {
  167. // Switch to the .pseudoprobe section or a comdat group.
  168. MCOS->SwitchSection(S);
  169. // Emit probes grouped by GUID.
  170. ProbeSec.second.emit(MCOS, LastProbe);
  171. }
  172. }
  173. }
  174. //
  175. // This emits the pseudo probe tables.
  176. //
  177. void MCPseudoProbeTable::emit(MCObjectStreamer *MCOS) {
  178. MCContext &Ctx = MCOS->getContext();
  179. auto &ProbeTable = Ctx.getMCPseudoProbeTable();
  180. // Bail out early so we don't switch to the pseudo_probe section needlessly
  181. // and in doing so create an unnecessary (if empty) section.
  182. auto &ProbeSections = ProbeTable.getProbeSections();
  183. if (ProbeSections.empty())
  184. return;
  185. LLVM_DEBUG(MCPseudoProbeTable::DdgPrintIndent = 0);
  186. // Put out the probe.
  187. ProbeSections.emit(MCOS);
  188. }