87 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains support for writing pseudo probe info into asm files.10//11//===----------------------------------------------------------------------===//12 13#include "PseudoProbePrinter.h"14#include "llvm/CodeGen/AsmPrinter.h"15#include "llvm/IR/DebugInfoMetadata.h"16#include "llvm/IR/Function.h"17#include "llvm/IR/PseudoProbe.h"18#include "llvm/MC/MCPseudoProbe.h"19#include "llvm/MC/MCStreamer.h"20 21#ifndef NDEBUG22#include "llvm/IR/Module.h"23#include "llvm/Support/WithColor.h"24#endif25 26using namespace llvm;27 28void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,29 uint64_t Type, uint64_t Attr,30 const DILocation *DebugLoc) {31 // Gather all the inlined-at nodes.32 // When it's done ReversedInlineStack looks like ([66, B], [88, A])33 // which means, Function A inlines function B at calliste with a probe id 88,34 // and B inlines C at probe 66 where C is represented by Guid.35 SmallVector<InlineSite, 8> ReversedInlineStack;36 auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;37 while (InlinedAt) {38 auto Name = InlinedAt->getSubprogramLinkageName();39 // Use caching to avoid redundant md5 computation for build speed.40 uint64_t &CallerGuid = NameGuidMap[Name];41 if (!CallerGuid)42 CallerGuid = Function::getGUIDAssumingExternalLinkage(Name);43#ifndef NDEBUG44 verifyGuidExistenceInDesc(CallerGuid, Name);45#endif46 uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(47 InlinedAt->getDiscriminator());48 ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);49 InlinedAt = InlinedAt->getInlinedAt();50 }51 uint64_t Discriminator = 0;52 // For now only block probes have FS discriminators. See53 // MIRFSDiscriminator.cpp for more details.54 if (EnableFSDiscriminator && DebugLoc &&55 (Type == (uint64_t)PseudoProbeType::Block))56 Discriminator = DebugLoc->getDiscriminator();57 assert((EnableFSDiscriminator || Discriminator == 0) &&58 "Discriminator should not be set in non-FSAFDO mode");59 SmallVector<InlineSite, 8> InlineStack(llvm::reverse(ReversedInlineStack));60 Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,61 InlineStack, Asm->CurrentFnSym);62#ifndef NDEBUG63 verifyGuidExistenceInDesc(64 Guid, DebugLoc ? DebugLoc->getSubprogramLinkageName() : "");65#endif66}67 68#ifndef NDEBUG69void PseudoProbeHandler::verifyGuidExistenceInDesc(uint64_t Guid,70 StringRef FuncName) {71 NamedMDNode *Desc = Asm->MF->getFunction().getParent()->getNamedMetadata(72 PseudoProbeDescMetadataName);73 assert(Desc && "pseudo probe does not exist");74 75 // Keep DescGuidSet up to date.76 for (size_t I = DescGuidSet.size(), E = Desc->getNumOperands(); I != E; ++I) {77 const auto *MD = cast<MDNode>(Desc->getOperand(I));78 auto *ID = mdconst::extract<ConstantInt>(MD->getOperand(0));79 DescGuidSet.insert(ID->getZExtValue());80 }81 82 if (!DescGuidSet.contains(Guid))83 WithColor::warning() << "Guid:" << Guid << " Name:" << FuncName84 << " does not exist in pseudo probe desc\n";85}86#endif87