PseudoProbePrinter.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===- llvm/CodeGen/PseudoProbePrinter.cpp - Pseudo Probe Emission -------===//
  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 contains support for writing pseudo probe info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "PseudoProbePrinter.h"
  13. #include "llvm/CodeGen/AsmPrinter.h"
  14. #include "llvm/IR/DebugInfoMetadata.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/IR/PseudoProbe.h"
  17. #include "llvm/MC/MCPseudoProbe.h"
  18. #include "llvm/MC/MCStreamer.h"
  19. using namespace llvm;
  20. PseudoProbeHandler::~PseudoProbeHandler() = default;
  21. void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
  22. uint64_t Type, uint64_t Attr,
  23. const DILocation *DebugLoc) {
  24. // Gather all the inlined-at nodes.
  25. // When it's done ReversedInlineStack looks like ([66, B], [88, A])
  26. // which means, Function A inlines function B at calliste with a probe id 88,
  27. // and B inlines C at probe 66 where C is represented by Guid.
  28. SmallVector<InlineSite, 8> ReversedInlineStack;
  29. auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;
  30. while (InlinedAt) {
  31. const DISubprogram *SP = InlinedAt->getScope()->getSubprogram();
  32. // Use linkage name for C++ if possible.
  33. auto Name = SP->getLinkageName();
  34. if (Name.empty())
  35. Name = SP->getName();
  36. // Use caching to avoid redundant md5 computation for build speed.
  37. uint64_t &CallerGuid = NameGuidMap[Name];
  38. if (!CallerGuid)
  39. CallerGuid = Function::getGUID(Name);
  40. uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
  41. InlinedAt->getDiscriminator());
  42. ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
  43. InlinedAt = InlinedAt->getInlinedAt();
  44. }
  45. SmallVector<InlineSite, 8> InlineStack(llvm::reverse(ReversedInlineStack));
  46. Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, InlineStack);
  47. }