|
| 1 | +//===--- VTuneSupportPlugin.cpp -- Support for VTune profiler --*- C++ -*--===// |
| 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 | +// Handles support for registering code with VIntel Tune's Amplfiier JIT API. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#include "llvm/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.h" |
| 13 | +#include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 14 | +#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h" |
| 15 | + |
| 16 | +using namespace llvm; |
| 17 | +using namespace llvm::orc; |
| 18 | +using namespace llvm::jitlink; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +constexpr StringRef RegisterVTuneImplName = "llvm_orc_registerVTuneImpl"; |
| 23 | +constexpr StringRef UnregisterVTuneImplName = "llvm_orc_unregisterVTuneImpl"; |
| 24 | +constexpr StringRef RegisterTestVTuneImplName = |
| 25 | + "llvm_orc_test_registerVTuneImpl"; |
| 26 | + |
| 27 | +static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) { |
| 28 | + VTuneMethodBatch Batch; |
| 29 | + std::unique_ptr<DWARFContext> DC; |
| 30 | + StringMap<std::unique_ptr<MemoryBuffer>> DCBacking; |
| 31 | + if (EmitDebugInfo) { |
| 32 | + auto EDC = createDWARFContext(G); |
| 33 | + if (!EDC) { |
| 34 | + EmitDebugInfo = false; |
| 35 | + } else { |
| 36 | + DC = std::move(EDC->first); |
| 37 | + DCBacking = std::move(EDC->second); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + auto GetStringIdx = [Deduplicator = StringMap<uint32_t>(), |
| 42 | + &Batch](StringRef S) mutable { |
| 43 | + auto I = Deduplicator.find(S); |
| 44 | + if (I != Deduplicator.end()) |
| 45 | + return I->second; |
| 46 | + |
| 47 | + Batch.Strings.push_back(S.str()); |
| 48 | + return Deduplicator[S] = Batch.Strings.size(); |
| 49 | + }; |
| 50 | + for (auto Sym : G.defined_symbols()) { |
| 51 | + if (!Sym->isCallable()) |
| 52 | + continue; |
| 53 | + |
| 54 | + Batch.Methods.push_back(VTuneMethodInfo()); |
| 55 | + auto &Method = Batch.Methods.back(); |
| 56 | + Method.MethodID = 0; |
| 57 | + Method.ParentMI = 0; |
| 58 | + Method.LoadAddr = Sym->getAddress(); |
| 59 | + Method.LoadSize = Sym->getSize(); |
| 60 | + Method.NameSI = GetStringIdx(Sym->getName()); |
| 61 | + Method.ClassFileSI = 0; |
| 62 | + Method.SourceFileSI = 0; |
| 63 | + |
| 64 | + if (!EmitDebugInfo) |
| 65 | + continue; |
| 66 | + |
| 67 | + auto &Section = Sym->getBlock().getSection(); |
| 68 | + auto Addr = Sym->getAddress(); |
| 69 | + auto SAddr = |
| 70 | + object::SectionedAddress{Addr.getValue(), Section.getOrdinal()}; |
| 71 | + DILineInfoTable LinesInfo = DC->getLineInfoForAddressRange( |
| 72 | + SAddr, Sym->getSize(), |
| 73 | + DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath); |
| 74 | + Method.SourceFileSI = Batch.Strings.size(); |
| 75 | + Batch.Strings.push_back(DC->getLineInfoForAddress(SAddr).FileName); |
| 76 | + for (auto &LInfo : LinesInfo) { |
| 77 | + Method.LineTable.push_back( |
| 78 | + std::pair<unsigned, unsigned>{/*unsigned*/ Sym->getOffset(), |
| 79 | + /*DILineInfo*/ LInfo.second.Line}); |
| 80 | + } |
| 81 | + } |
| 82 | + return Batch; |
| 83 | +} |
| 84 | + |
| 85 | +} // namespace |
| 86 | + |
| 87 | +void VTuneSupportPlugin::modifyPassConfig(MaterializationResponsibility &MR, |
| 88 | + LinkGraph &G, |
| 89 | + PassConfiguration &Config) { |
| 90 | + Config.PostFixupPasses.push_back([this, MR = &MR](LinkGraph &G) { |
| 91 | + // the object file is generated but not linked yet |
| 92 | + auto Batch = getMethodBatch(G, EmitDebugInfo); |
| 93 | + if (Batch.Methods.empty()) { |
| 94 | + return Error::success(); |
| 95 | + } |
| 96 | + { |
| 97 | + std::lock_guard<std::mutex> Lock(PluginMutex); |
| 98 | + uint64_t Allocated = Batch.Methods.size(); |
| 99 | + uint64_t Start = NextMethodID; |
| 100 | + NextMethodID += Allocated; |
| 101 | + for (size_t i = Start; i < NextMethodID; ++i) { |
| 102 | + Batch.Methods[i - Start].MethodID = i; |
| 103 | + } |
| 104 | + this->PendingMethodIDs[MR] = {Start, Allocated}; |
| 105 | + } |
| 106 | + G.allocActions().push_back( |
| 107 | + {cantFail(shared::WrapperFunctionCall::Create< |
| 108 | + shared::SPSArgList<shared::SPSVTuneMethodBatch>>( |
| 109 | + RegisterVTuneImplAddr, Batch)), |
| 110 | + {}}); |
| 111 | + return Error::success(); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +Error VTuneSupportPlugin::notifyEmitted(MaterializationResponsibility &MR) { |
| 116 | + if (auto Err = MR.withResourceKeyDo([this, MR = &MR](ResourceKey K) { |
| 117 | + std::lock_guard<std::mutex> Lock(PluginMutex); |
| 118 | + auto I = PendingMethodIDs.find(MR); |
| 119 | + if (I == PendingMethodIDs.end()) |
| 120 | + return; |
| 121 | + |
| 122 | + LoadedMethodIDs[K].push_back(I->second); |
| 123 | + PendingMethodIDs.erase(I); |
| 124 | + })) { |
| 125 | + return Err; |
| 126 | + } |
| 127 | + return Error::success(); |
| 128 | +} |
| 129 | + |
| 130 | +Error VTuneSupportPlugin::notifyFailed(MaterializationResponsibility &MR) { |
| 131 | + std::lock_guard<std::mutex> Lock(PluginMutex); |
| 132 | + PendingMethodIDs.erase(&MR); |
| 133 | + return Error::success(); |
| 134 | +} |
| 135 | + |
| 136 | +Error VTuneSupportPlugin::notifyRemovingResources(JITDylib &JD, ResourceKey K) { |
| 137 | + // Unregistration not required if not provided |
| 138 | + if (!UnregisterVTuneImplAddr) { |
| 139 | + return Error::success(); |
| 140 | + } |
| 141 | + VTuneUnloadedMethodIDs UnloadedIDs; |
| 142 | + { |
| 143 | + std::lock_guard<std::mutex> Lock(PluginMutex); |
| 144 | + auto I = LoadedMethodIDs.find(K); |
| 145 | + if (I == LoadedMethodIDs.end()) |
| 146 | + return Error::success(); |
| 147 | + |
| 148 | + UnloadedIDs = std::move(I->second); |
| 149 | + LoadedMethodIDs.erase(I); |
| 150 | + } |
| 151 | + if (auto Err = EPC.callSPSWrapper<void(shared::SPSVTuneUnloadedMethodIDs)>( |
| 152 | + UnregisterVTuneImplAddr, UnloadedIDs)) |
| 153 | + return Err; |
| 154 | + |
| 155 | + return Error::success(); |
| 156 | +} |
| 157 | + |
| 158 | +void VTuneSupportPlugin::notifyTransferringResources(JITDylib &JD, |
| 159 | + ResourceKey DstKey, |
| 160 | + ResourceKey SrcKey) { |
| 161 | + std::lock_guard<std::mutex> Lock(PluginMutex); |
| 162 | + auto I = LoadedMethodIDs.find(SrcKey); |
| 163 | + if (I == LoadedMethodIDs.end()) |
| 164 | + return; |
| 165 | + |
| 166 | + auto &Dest = LoadedMethodIDs[DstKey]; |
| 167 | + Dest.insert(Dest.end(), I->second.begin(), I->second.end()); |
| 168 | + LoadedMethodIDs.erase(SrcKey); |
| 169 | +} |
| 170 | + |
| 171 | +Expected<std::unique_ptr<VTuneSupportPlugin>> |
| 172 | +VTuneSupportPlugin::Create(ExecutorProcessControl &EPC, JITDylib &JD, |
| 173 | + bool EmitDebugInfo, bool TestMode) { |
| 174 | + auto &ES = EPC.getExecutionSession(); |
| 175 | + auto RegisterImplName = |
| 176 | + ES.intern(TestMode ? RegisterTestVTuneImplName : RegisterVTuneImplName); |
| 177 | + auto UnregisterImplName = ES.intern(UnregisterVTuneImplName); |
| 178 | + SymbolLookupSet SLS{RegisterImplName, UnregisterImplName}; |
| 179 | + auto Res = ES.lookup(makeJITDylibSearchOrder({&JD}), std::move(SLS)); |
| 180 | + if (!Res) |
| 181 | + return Res.takeError(); |
| 182 | + ExecutorAddr RegisterImplAddr( |
| 183 | + Res->find(RegisterImplName)->second.getAddress()); |
| 184 | + ExecutorAddr UnregisterImplAddr( |
| 185 | + Res->find(UnregisterImplName)->second.getAddress()); |
| 186 | + return std::make_unique<VTuneSupportPlugin>( |
| 187 | + EPC, RegisterImplAddr, UnregisterImplAddr, EmitDebugInfo); |
| 188 | +} |
0 commit comments