Skip to content

[ORC][JITLink] Add Intel VTune support to JITLink #83957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===--- VTuneSupportPlugin.h -- Support for VTune profiler ---*- C++ -*---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Handles support for registering code with VIntel Tune's Amplifier JIT API.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_DEBUGGING_VTUNESUPPORT_H
#define LLVM_EXECUTIONENGINE_ORC_DEBUGGING_VTUNESUPPORT_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
#include "llvm/ExecutionEngine/Orc/Shared/VTuneSharedStructs.h"

namespace llvm {

namespace orc {

class VTuneSupportPlugin : public ObjectLinkingLayer::Plugin {
public:
VTuneSupportPlugin(ExecutorProcessControl &EPC, ExecutorAddr RegisterImplAddr,
ExecutorAddr UnregisterImplAddr, bool EmitDebugInfo)
: EPC(EPC), RegisterVTuneImplAddr(RegisterImplAddr),
UnregisterVTuneImplAddr(UnregisterImplAddr),
EmitDebugInfo(EmitDebugInfo) {}

void modifyPassConfig(MaterializationResponsibility &MR,
jitlink::LinkGraph &G,
jitlink::PassConfiguration &Config) override;

Error notifyEmitted(MaterializationResponsibility &MR) override;
Error notifyFailed(MaterializationResponsibility &MR) override;
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override;
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
ResourceKey SrcKey) override;

static Expected<std::unique_ptr<VTuneSupportPlugin>>
Create(ExecutorProcessControl &EPC, JITDylib &JD, bool EmitDebugInfo,
bool TestMode = false);

private:
ExecutorProcessControl &EPC;
ExecutorAddr RegisterVTuneImplAddr;
ExecutorAddr UnregisterVTuneImplAddr;
std::mutex PluginMutex;
uint64_t NextMethodID = 0;
DenseMap<MaterializationResponsibility *, std::pair<uint64_t, uint64_t>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include DenseMap for IWYU

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

PendingMethodIDs;
DenseMap<ResourceKey, SmallVector<std::pair<uint64_t, uint64_t>>>
LoadedMethodIDs;
bool EmitDebugInfo;
};

} // end namespace orc

} // end namespace llvm

#endif
102 changes: 102 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/Shared/VTuneSharedStructs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//===-------------------- VTuneSharedStructs.h ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Structs and serialization to share VTune-related information
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_VTUNESHAREDSTRUCTS_H
#define LLVM_EXECUTIONENGINE_ORC_SHARED_VTUNESHAREDSTRUCTS_H

namespace llvm {
namespace orc {

using VTuneLineTable = std::vector<std::pair<unsigned, unsigned>>;

// SI = String Index, 1-indexed into the VTuneMethodBatch::Strings table.
// SI == 0 means replace with nullptr.

// MI = Method Index, 1-indexed into the VTuneMethodBatch::Methods table.
// MI == 0 means this is a parent method and was not inlined.

struct VTuneMethodInfo {
VTuneLineTable LineTable;
ExecutorAddr LoadAddr;
uint64_t LoadSize;
uint64_t MethodID;
uint32_t NameSI;
uint32_t ClassFileSI;
uint32_t SourceFileSI;
uint32_t ParentMI;
};

using VTuneMethodTable = std::vector<VTuneMethodInfo>;
using VTuneStringTable = std::vector<std::string>;

struct VTuneMethodBatch {
VTuneMethodTable Methods;
VTuneStringTable Strings;
};

using VTuneUnloadedMethodIDs = SmallVector<std::pair<uint64_t, uint64_t>>;

namespace shared {

using SPSVTuneLineTable = SPSSequence<SPSTuple<uint32_t, uint32_t>>;
using SPSVTuneMethodInfo =
SPSTuple<SPSVTuneLineTable, SPSExecutorAddr, uint64_t, uint64_t, uint32_t,
uint32_t, uint32_t, uint32_t>;
using SPSVTuneMethodTable = SPSSequence<SPSVTuneMethodInfo>;
using SPSVTuneStringTable = SPSSequence<SPSString>;
using SPSVTuneMethodBatch = SPSTuple<SPSVTuneMethodTable, SPSVTuneStringTable>;
using SPSVTuneUnloadedMethodIDs = SPSSequence<SPSTuple<uint64_t, uint64_t>>;

template <> class SPSSerializationTraits<SPSVTuneMethodInfo, VTuneMethodInfo> {
public:
static size_t size(const VTuneMethodInfo &MI) {
return SPSVTuneMethodInfo::AsArgList::size(
MI.LineTable, MI.LoadAddr, MI.LoadSize, MI.MethodID, MI.NameSI,
MI.ClassFileSI, MI.SourceFileSI, MI.ParentMI);
}

static bool deserialize(SPSInputBuffer &IB, VTuneMethodInfo &MI) {
return SPSVTuneMethodInfo::AsArgList::deserialize(
IB, MI.LineTable, MI.LoadAddr, MI.LoadSize, MI.MethodID, MI.NameSI,
MI.ClassFileSI, MI.SourceFileSI, MI.ParentMI);
}

static bool serialize(SPSOutputBuffer &OB, const VTuneMethodInfo &MI) {
return SPSVTuneMethodInfo::AsArgList::serialize(
OB, MI.LineTable, MI.LoadAddr, MI.LoadSize, MI.MethodID, MI.NameSI,
MI.ClassFileSI, MI.SourceFileSI, MI.ParentMI);
}
};

template <>
class SPSSerializationTraits<SPSVTuneMethodBatch, VTuneMethodBatch> {
public:
static size_t size(const VTuneMethodBatch &MB) {
return SPSVTuneMethodBatch::AsArgList::size(MB.Methods, MB.Strings);
}

static bool deserialize(SPSInputBuffer &IB, VTuneMethodBatch &MB) {
return SPSVTuneMethodBatch::AsArgList::deserialize(IB, MB.Methods,
MB.Strings);
}

static bool serialize(SPSOutputBuffer &OB, const VTuneMethodBatch &MB) {
return SPSVTuneMethodBatch::AsArgList::serialize(OB, MB.Methods,
MB.Strings);
}
};

} // end namespace shared
} // end namespace orc
} // end namespace llvm

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

//===------ JITLoaderVTune.h --- Register profiler objects ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Register objects for access by profilers via the perf JIT interface.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_JITLOADERVTUNE_H
#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_JITLOADERVTUNE_H

#include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
#include <cstdint>

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_registerVTuneImpl(const char *Data, uint64_t Size);

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_unregisterVTuneImpl(const char *Data, uint64_t Size);

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_test_registerVTuneImpl(const char *Data, uint64_t Size);

#endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_JITLOADERVTUNE_H


1 change: 1 addition & 0 deletions llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_llvm_component_library(LLVMOrcDebugging
DebuggerSupportPlugin.cpp
LLJITUtilsCBindings.cpp
PerfSupportPlugin.cpp
VTuneSupportPlugin.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/ExecutionEngine/Orc/Debugging/
Expand Down
185 changes: 185 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//===--- VTuneSupportPlugin.cpp -- Support for VTune profiler --*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Handles support for registering code with VIntel Tune's Amplfiier JIT API.
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"

using namespace llvm;
using namespace llvm::orc;
using namespace llvm::jitlink;

static constexpr StringRef RegisterVTuneImplName = "llvm_orc_registerVTuneImpl";
static constexpr StringRef UnregisterVTuneImplName =
"llvm_orc_unregisterVTuneImpl";
static constexpr StringRef RegisterTestVTuneImplName =
"llvm_orc_test_registerVTuneImpl";

static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) {
VTuneMethodBatch Batch;
std::unique_ptr<DWARFContext> DC;
StringMap<std::unique_ptr<MemoryBuffer>> DCBacking;
if (EmitDebugInfo) {
auto EDC = createDWARFContext(G);
if (!EDC) {
EmitDebugInfo = false;
} else {
DC = std::move(EDC->first);
DCBacking = std::move(EDC->second);
}
}

auto GetStringIdx = [Deduplicator = StringMap<uint32_t>(),
&Batch](StringRef S) mutable {
auto I = Deduplicator.find(S);
if (I != Deduplicator.end())
return I->second;

Batch.Strings.push_back(S.str());
return Deduplicator[S] = Batch.Strings.size();
};
for (auto Sym : G.defined_symbols()) {
if (!Sym->isCallable())
continue;

Batch.Methods.push_back(VTuneMethodInfo());
auto &Method = Batch.Methods.back();
Method.MethodID = 0;
Method.ParentMI = 0;
Method.LoadAddr = Sym->getAddress();
Method.LoadSize = Sym->getSize();
Method.NameSI = GetStringIdx(Sym->getName());
Method.ClassFileSI = 0;
Method.SourceFileSI = 0;

if (!EmitDebugInfo)
continue;

auto &Section = Sym->getBlock().getSection();
auto Addr = Sym->getAddress();
auto SAddr =
object::SectionedAddress{Addr.getValue(), Section.getOrdinal()};
DILineInfoTable LinesInfo = DC->getLineInfoForAddressRange(
SAddr, Sym->getSize(),
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath);
Method.SourceFileSI = Batch.Strings.size();
Batch.Strings.push_back(DC->getLineInfoForAddress(SAddr).FileName);
for (auto &LInfo : LinesInfo) {
Method.LineTable.push_back(
std::pair<unsigned, unsigned>{/*unsigned*/ Sym->getOffset(),
/*DILineInfo*/ LInfo.second.Line});
}
}
return Batch;
}

void VTuneSupportPlugin::modifyPassConfig(MaterializationResponsibility &MR,
LinkGraph &G,
PassConfiguration &Config) {
Config.PostFixupPasses.push_back([this, MR = &MR](LinkGraph &G) {
// the object file is generated but not linked yet
auto Batch = getMethodBatch(G, EmitDebugInfo);
if (Batch.Methods.empty()) {
return Error::success();
}
{
std::lock_guard<std::mutex> Lock(PluginMutex);
uint64_t Allocated = Batch.Methods.size();
uint64_t Start = NextMethodID;
NextMethodID += Allocated;
for (size_t i = Start; i < NextMethodID; ++i) {
Batch.Methods[i - Start].MethodID = i;
}
this->PendingMethodIDs[MR] = {Start, Allocated};
}
G.allocActions().push_back(
{cantFail(shared::WrapperFunctionCall::Create<
shared::SPSArgList<shared::SPSVTuneMethodBatch>>(
RegisterVTuneImplAddr, Batch)),
{}});
return Error::success();
});
}

Error VTuneSupportPlugin::notifyEmitted(MaterializationResponsibility &MR) {
if (auto Err = MR.withResourceKeyDo([this, MR = &MR](ResourceKey K) {
std::lock_guard<std::mutex> Lock(PluginMutex);
auto I = PendingMethodIDs.find(MR);
if (I == PendingMethodIDs.end())
return;

LoadedMethodIDs[K].push_back(I->second);
PendingMethodIDs.erase(I);
})) {
return Err;
}
return Error::success();
}

Error VTuneSupportPlugin::notifyFailed(MaterializationResponsibility &MR) {
std::lock_guard<std::mutex> Lock(PluginMutex);
PendingMethodIDs.erase(&MR);
return Error::success();
}

Error VTuneSupportPlugin::notifyRemovingResources(JITDylib &JD, ResourceKey K) {
// Unregistration not required if not provided
if (!UnregisterVTuneImplAddr) {
return Error::success();
}
VTuneUnloadedMethodIDs UnloadedIDs;
{
std::lock_guard<std::mutex> Lock(PluginMutex);
auto I = LoadedMethodIDs.find(K);
if (I == LoadedMethodIDs.end())
return Error::success();

UnloadedIDs = std::move(I->second);
LoadedMethodIDs.erase(I);
}
if (auto Err = EPC.callSPSWrapper<void(shared::SPSVTuneUnloadedMethodIDs)>(
UnregisterVTuneImplAddr, UnloadedIDs))
return Err;

return Error::success();
}

void VTuneSupportPlugin::notifyTransferringResources(JITDylib &JD,
ResourceKey DstKey,
ResourceKey SrcKey) {
std::lock_guard<std::mutex> Lock(PluginMutex);
auto I = LoadedMethodIDs.find(SrcKey);
if (I == LoadedMethodIDs.end())
return;

auto &Dest = LoadedMethodIDs[DstKey];
Dest.insert(Dest.end(), I->second.begin(), I->second.end());
LoadedMethodIDs.erase(SrcKey);
}

Expected<std::unique_ptr<VTuneSupportPlugin>>
VTuneSupportPlugin::Create(ExecutorProcessControl &EPC, JITDylib &JD,
bool EmitDebugInfo, bool TestMode) {
auto &ES = EPC.getExecutionSession();
auto RegisterImplName =
ES.intern(TestMode ? RegisterTestVTuneImplName : RegisterVTuneImplName);
auto UnregisterImplName = ES.intern(UnregisterVTuneImplName);
SymbolLookupSet SLS{RegisterImplName, UnregisterImplName};
auto Res = ES.lookup(makeJITDylibSearchOrder({&JD}), std::move(SLS));
if (!Res)
return Res.takeError();
ExecutorAddr RegisterImplAddr(
Res->find(RegisterImplName)->second.getAddress());
ExecutorAddr UnregisterImplAddr(
Res->find(UnregisterImplName)->second.getAddress());
return std::make_unique<VTuneSupportPlugin>(
EPC, RegisterImplAddr, UnregisterImplAddr, EmitDebugInfo);
}
Loading