Skip to content

Commit

Permalink
[ORC] Add some debugging output for initializers.
Browse files Browse the repository at this point in the history
This output can be useful in tracking down initialization failures in the JIT.
  • Loading branch information
lhames committed Mar 4, 2020
1 parent 9e1319d commit 8363ff0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 8 deletions.
9 changes: 9 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ThreadPool.h"

namespace llvm {
Expand Down Expand Up @@ -137,12 +138,20 @@ class LLJIT {

/// Run the initializers for the given JITDylib.
Error initialize(JITDylib &JD) {
DEBUG_WITH_TYPE("orc", {
dbgs() << "LLJIT running initializers for JITDylib \"" << JD.getName()
<< "\"\n";
});
assert(PS && "PlatformSupport must be set to run initializers.");
return PS->initialize(JD);
}

/// Run the deinitializers for the given JITDylib.
Error deinitialize(JITDylib &JD) {
DEBUG_WITH_TYPE("orc", {
dbgs() << "LLJIT running deinitializers for JITDylib \"" << JD.getName()
<< "\"\n";
});
assert(PS && "PlatformSupport must be set to run initializers.");
return PS->deinitialize(JD);
}
Expand Down
2 changes: 0 additions & 2 deletions llvm/include/llvm/ExecutionEngine/Orc/MachOPlatform.h
Expand Up @@ -59,8 +59,6 @@ class MachOJITDylibInitializers {
void registerObjCSelectors() const;
Error registerObjCClasses() const;

void dump() const;

private:
using RawPointerSectionList = std::vector<SectionExtent>;

Expand Down
39 changes: 39 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
Expand Up @@ -21,6 +21,8 @@

#include <map>

#define DEBUG_TYPE "orc"

using namespace llvm;
using namespace llvm::orc;

Expand Down Expand Up @@ -191,8 +193,17 @@ class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
}

Error initialize(JITDylib &JD) override {
LLVM_DEBUG({
dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
});
if (auto Initializers = getInitializers(JD)) {
LLVM_DEBUG(
{ dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
for (auto InitFnAddr : *Initializers) {
LLVM_DEBUG({
dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr)
<< "...\n";
});
auto *InitFn = jitTargetAddressToFunction<void (*)()>(InitFnAddr);
InitFn();
}
Expand All @@ -202,8 +213,18 @@ class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
}

Error deinitialize(JITDylib &JD) override {
LLVM_DEBUG({
dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
});
if (auto Deinitializers = getDeinitializers(JD)) {
LLVM_DEBUG({
dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
});
for (auto DeinitFnAddr : *Deinitializers) {
LLVM_DEBUG({
dbgs() << " Running init " << formatv("{0:x16}", DeinitFnAddr)
<< "...\n";
});
auto *DeinitFn = jitTargetAddressToFunction<void (*)()>(DeinitFnAddr);
DeinitFn();
}
Expand Down Expand Up @@ -239,6 +260,16 @@ class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
}
}

LLVM_DEBUG({
dbgs() << "JITDylib init order is [ ";
for (auto *JD : llvm::reverse(DFSLinkOrder))
dbgs() << "\"" << JD->getName() << "\" ";
dbgs() << "]\n";
dbgs() << "Looking up init functions:\n";
for (auto &KV : LookupSymbols)
dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
});

auto &ES = getExecutionSession();
auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);

Expand Down Expand Up @@ -519,6 +550,11 @@ class MachOPlatformSupport : public LLJIT::PlatformSupport {
}

Error initialize(JITDylib &JD) override {
LLVM_DEBUG({
dbgs() << "MachOPlatformSupport initializing \"" << JD.getName()
<< "\"\n";
});

if (auto InitSeq = MP.getInitializerSequence(JD)) {
for (auto &KV : *InitSeq) {
KV.second.registerObjCSelectors();
Expand Down Expand Up @@ -1039,10 +1075,13 @@ Error LLJIT::applyDataLayout(Module &M) {
}

void setUpGenericLLVMIRPlatform(LLJIT &J) {
LLVM_DEBUG(
{ dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
J.setPlatformSupport(std::make_unique<GenericLLVMIRPlatformSupport>(J));
}

Error setUpMachOPlatform(LLJIT &J) {
LLVM_DEBUG({ dbgs() << "Setting up MachOPlatform support for LLJIT\n"; });
auto MP = MachOPlatformSupport::Create(J, J.getMainJITDylib());
if (!MP)
return MP.takeError();
Expand Down
59 changes: 53 additions & 6 deletions llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp
Expand Up @@ -10,6 +10,9 @@

#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "orc"

namespace {

Expand Down Expand Up @@ -141,12 +144,6 @@ Error MachOJITDylibInitializers::registerObjCClasses() const {
return Error::success();
}

void MachOJITDylibInitializers::dump() const {
for (auto &Extent : ModInitSections)
dbgs() << formatv("{0:x16}", Extent.Address) << " -- "
<< formatv("{0:x16}", Extent.Address + 8 * Extent.NumPtrs) << "\n";
}

MachOPlatform::MachOPlatform(
ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
std::unique_ptr<MemoryBuffer> StandardSymbolsObject)
Expand All @@ -168,6 +165,10 @@ Error MachOPlatform::notifyAdding(JITDylib &JD, const MaterializationUnit &MU) {

std::lock_guard<std::mutex> Lock(PlatformMutex);
RegisteredInitSymbols[&JD].add(InitSym);
LLVM_DEBUG({
dbgs() << "MachOPlatform: Registered init symbol " << *InitSym << " for MU "
<< MU.getName() << "\n";
});
return Error::success();
}

Expand All @@ -178,6 +179,11 @@ Error MachOPlatform::notifyRemoving(JITDylib &JD, VModuleKey K) {
Expected<MachOPlatform::InitializerSequence>
MachOPlatform::getInitializerSequence(JITDylib &JD) {

LLVM_DEBUG({
dbgs() << "MachOPlatform: Building initializer sequence for "
<< JD.getName() << "\n";
});

std::vector<JITDylib *> DFSLinkOrder;

while (true) {
Expand All @@ -200,18 +206,34 @@ MachOPlatform::getInitializerSequence(JITDylib &JD) {
if (NewInitSymbols.empty())
break;

LLVM_DEBUG({
dbgs() << "MachOPlatform: Issuing lookups for new init symbols: "
"(lookup may require multiple rounds)\n";
for (auto &KV : NewInitSymbols)
dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
});

// Outside the lock, issue the lookup.
if (auto R = lookupInitSymbols(JD.getExecutionSession(), NewInitSymbols))
; // Nothing to do in the success case.
else
return R.takeError();
}

LLVM_DEBUG({
dbgs() << "MachOPlatform: Init symbol lookup complete, building init "
"sequence\n";
});

// Lock again to collect the initializers.
InitializerSequence FullInitSeq;
{
std::lock_guard<std::mutex> Lock(PlatformMutex);
for (auto *InitJD : reverse(DFSLinkOrder)) {
LLVM_DEBUG({
dbgs() << "MachOPlatform: Appending inits for \"" << InitJD->getName()
<< "\" to sequence\n";
});
auto ISItr = InitSeqs.find(InitJD);
if (ISItr != InitSeqs.end()) {
FullInitSeq.emplace_back(InitJD, std::move(ISItr->second));
Expand Down Expand Up @@ -345,6 +367,31 @@ void MachOPlatform::InitScraperPlugin::modifyPassConfig(
else
return ObjCClassListOrErr.takeError();

// Dump the scraped inits.
LLVM_DEBUG({
dbgs() << "MachOPlatform: Scraped " << G.getName() << " init sections:\n";
dbgs() << " __objc_selrefs: ";
if (ObjCSelRefs.NumPtrs)
dbgs() << ObjCSelRefs.NumPtrs << " pointer(s) at "
<< formatv("{0:x16}", ObjCSelRefs.Address) << "\n";
else
dbgs() << "none\n";

dbgs() << " __objc_classlist: ";
if (ObjCClassList.NumPtrs)
dbgs() << ObjCClassList.NumPtrs << " pointer(s) at "
<< formatv("{0:x16}", ObjCClassList.Address) << "\n";
else
dbgs() << "none\n";

dbgs() << "__mod_init_func: ";
if (ModInits.NumPtrs)
dbgs() << ModInits.NumPtrs << " pointer(s) at "
<< formatv("{0:x16}", ModInits.Address) << "\n";
else
dbgs() << "none\n";
});

MP.registerInitInfo(JD, ObjCImageInfoAddr, std::move(ModInits),
std::move(ObjCSelRefs), std::move(ObjCClassList));

Expand Down

0 comments on commit 8363ff0

Please sign in to comment.