Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp
35294 views
//===--- VTuneSupportPlugin.cpp -- Support for VTune profiler --*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Handles support for registering code with VIntel Tune's Amplfiier JIT API.9//10//===----------------------------------------------------------------------===//11#include "llvm/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.h"12#include "llvm/DebugInfo/DWARF/DWARFContext.h"13#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"1415using namespace llvm;16using namespace llvm::orc;17using namespace llvm::jitlink;1819static constexpr StringRef RegisterVTuneImplName = "llvm_orc_registerVTuneImpl";20static constexpr StringRef UnregisterVTuneImplName =21"llvm_orc_unregisterVTuneImpl";22static constexpr StringRef RegisterTestVTuneImplName =23"llvm_orc_test_registerVTuneImpl";2425static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) {26VTuneMethodBatch Batch;27std::unique_ptr<DWARFContext> DC;28StringMap<std::unique_ptr<MemoryBuffer>> DCBacking;29if (EmitDebugInfo) {30auto EDC = createDWARFContext(G);31if (!EDC) {32EmitDebugInfo = false;33} else {34DC = std::move(EDC->first);35DCBacking = std::move(EDC->second);36}37}3839auto GetStringIdx = [Deduplicator = StringMap<uint32_t>(),40&Batch](StringRef S) mutable {41auto I = Deduplicator.find(S);42if (I != Deduplicator.end())43return I->second;4445Batch.Strings.push_back(S.str());46return Deduplicator[S] = Batch.Strings.size();47};48for (auto Sym : G.defined_symbols()) {49if (!Sym->isCallable())50continue;5152Batch.Methods.push_back(VTuneMethodInfo());53auto &Method = Batch.Methods.back();54Method.MethodID = 0;55Method.ParentMI = 0;56Method.LoadAddr = Sym->getAddress();57Method.LoadSize = Sym->getSize();58Method.NameSI = GetStringIdx(Sym->getName());59Method.ClassFileSI = 0;60Method.SourceFileSI = 0;6162if (!EmitDebugInfo)63continue;6465auto &Section = Sym->getBlock().getSection();66auto Addr = Sym->getAddress();67auto SAddr =68object::SectionedAddress{Addr.getValue(), Section.getOrdinal()};69DILineInfoTable LinesInfo = DC->getLineInfoForAddressRange(70SAddr, Sym->getSize(),71DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath);72Method.SourceFileSI = Batch.Strings.size();73Batch.Strings.push_back(DC->getLineInfoForAddress(SAddr).FileName);74for (auto &LInfo : LinesInfo) {75Method.LineTable.push_back(76std::pair<unsigned, unsigned>{/*unsigned*/ Sym->getOffset(),77/*DILineInfo*/ LInfo.second.Line});78}79}80return Batch;81}8283void VTuneSupportPlugin::modifyPassConfig(MaterializationResponsibility &MR,84LinkGraph &G,85PassConfiguration &Config) {86Config.PostFixupPasses.push_back([this, MR = &MR](LinkGraph &G) {87// the object file is generated but not linked yet88auto Batch = getMethodBatch(G, EmitDebugInfo);89if (Batch.Methods.empty()) {90return Error::success();91}92{93std::lock_guard<std::mutex> Lock(PluginMutex);94uint64_t Allocated = Batch.Methods.size();95uint64_t Start = NextMethodID;96NextMethodID += Allocated;97for (size_t i = Start; i < NextMethodID; ++i) {98Batch.Methods[i - Start].MethodID = i;99}100this->PendingMethodIDs[MR] = {Start, Allocated};101}102G.allocActions().push_back(103{cantFail(shared::WrapperFunctionCall::Create<104shared::SPSArgList<shared::SPSVTuneMethodBatch>>(105RegisterVTuneImplAddr, Batch)),106{}});107return Error::success();108});109}110111Error VTuneSupportPlugin::notifyEmitted(MaterializationResponsibility &MR) {112if (auto Err = MR.withResourceKeyDo([this, MR = &MR](ResourceKey K) {113std::lock_guard<std::mutex> Lock(PluginMutex);114auto I = PendingMethodIDs.find(MR);115if (I == PendingMethodIDs.end())116return;117118LoadedMethodIDs[K].push_back(I->second);119PendingMethodIDs.erase(I);120})) {121return Err;122}123return Error::success();124}125126Error VTuneSupportPlugin::notifyFailed(MaterializationResponsibility &MR) {127std::lock_guard<std::mutex> Lock(PluginMutex);128PendingMethodIDs.erase(&MR);129return Error::success();130}131132Error VTuneSupportPlugin::notifyRemovingResources(JITDylib &JD, ResourceKey K) {133// Unregistration not required if not provided134if (!UnregisterVTuneImplAddr) {135return Error::success();136}137VTuneUnloadedMethodIDs UnloadedIDs;138{139std::lock_guard<std::mutex> Lock(PluginMutex);140auto I = LoadedMethodIDs.find(K);141if (I == LoadedMethodIDs.end())142return Error::success();143144UnloadedIDs = std::move(I->second);145LoadedMethodIDs.erase(I);146}147if (auto Err = EPC.callSPSWrapper<void(shared::SPSVTuneUnloadedMethodIDs)>(148UnregisterVTuneImplAddr, UnloadedIDs))149return Err;150151return Error::success();152}153154void VTuneSupportPlugin::notifyTransferringResources(JITDylib &JD,155ResourceKey DstKey,156ResourceKey SrcKey) {157std::lock_guard<std::mutex> Lock(PluginMutex);158auto I = LoadedMethodIDs.find(SrcKey);159if (I == LoadedMethodIDs.end())160return;161162auto &Dest = LoadedMethodIDs[DstKey];163Dest.insert(Dest.end(), I->second.begin(), I->second.end());164LoadedMethodIDs.erase(SrcKey);165}166167Expected<std::unique_ptr<VTuneSupportPlugin>>168VTuneSupportPlugin::Create(ExecutorProcessControl &EPC, JITDylib &JD,169bool EmitDebugInfo, bool TestMode) {170auto &ES = EPC.getExecutionSession();171auto RegisterImplName =172ES.intern(TestMode ? RegisterTestVTuneImplName : RegisterVTuneImplName);173auto UnregisterImplName = ES.intern(UnregisterVTuneImplName);174SymbolLookupSet SLS{RegisterImplName, UnregisterImplName};175auto Res = ES.lookup(makeJITDylibSearchOrder({&JD}), std::move(SLS));176if (!Res)177return Res.takeError();178ExecutorAddr RegisterImplAddr(179Res->find(RegisterImplName)->second.getAddress());180ExecutorAddr UnregisterImplAddr(181Res->find(UnregisterImplName)->second.getAddress());182return std::make_unique<VTuneSupportPlugin>(183EPC, RegisterImplAddr, UnregisterImplAddr, EmitDebugInfo);184}185186187