Skip to content

Revisit advanced LLJIT examples and tests #76236

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
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
Expand Up @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
IRReader
JITLink
OrcDebugging
OrcJIT
OrcShared
OrcTargetProcess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
Expand Down Expand Up @@ -174,32 +175,13 @@ int main(int argc, char *argv[]) {
TSMs.push_back(ExitOnErr(parseExampleModuleFromFile(Path)));
}

std::string TT;
StringRef MainModuleName;
TSMs.front().withModuleDo([&MainModuleName, &TT](Module &M) {
MainModuleName = M.getName();
TT = M.getTargetTriple();
if (TT.empty())
TT = sys::getProcessTriple();
});

// Create a target machine that matches the input triple.
JITTargetMachineBuilder JTMB((Triple(TT)));
JTMB.setCodeModel(CodeModel::Small);
JTMB.setRelocationModel(Reloc::PIC_);

// Create LLJIT and destroy it before disconnecting the target process.
outs() << "Initializing LLJIT for remote executor\n";
auto J = ExitOnErr(LLJITBuilder()
.setExecutorProcessControl(std::move(EPC))
.setJITTargetMachineBuilder(std::move(JTMB))
.setObjectLinkingLayerCreator([&](auto &ES, const auto &TT) {
return std::make_unique<ObjectLinkingLayer>(ES);
})
.create());
auto J = ExitOnErr(
LLJITBuilder().setExecutorProcessControl(std::move(EPC)).create());

// Add plugin for debug support.
ExitOnErr(addDebugSupport(J->getObjLinkingLayer()));
ExitOnErr(enableDebuggerSupport(*J));

// Load required shared libraries on the remote target and add a generator
// for each of it, so the compiler can lookup their symbols.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@
using namespace llvm;
using namespace llvm::orc;

Error addDebugSupport(ObjectLayer &ObjLayer) {
ExecutionSession &ES = ObjLayer.getExecutionSession();
auto Registrar = createJITLoaderGDBRegistrar(ES);
if (!Registrar)
return Registrar.takeError();

auto *ObjLinkingLayer = cast<ObjectLinkingLayer>(&ObjLayer);
if (!ObjLinkingLayer)
return createStringError(inconvertibleErrorCode(),
"No debug support for given object layer type");

ObjLinkingLayer->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
ES, std::move(*Registrar), true, true));
return Error::success();
}

Expected<std::unique_ptr<DefinitionGenerator>>
loadDylib(ExecutionSession &ES, StringRef RemotePath) {
if (auto Handle = ES.getExecutorProcessControl().loadDylib(RemotePath.data()))
Expand Down Expand Up @@ -111,11 +95,15 @@ launchLocalExecutor(StringRef ExecutablePath) {
close(FromExecutor[ReadEnd]);

// Execute the child process.
std::unique_ptr<char[]> ExecPath, FDSpecifier;
std::unique_ptr<char[]> ExecPath, FDSpecifier, TestOutputFlag;
{
ExecPath = std::make_unique<char[]>(ExecutablePath.size() + 1);
strcpy(ExecPath.get(), ExecutablePath.data());

const char *TestOutputFlagStr = "test-jitloadergdb";
TestOutputFlag = std::make_unique<char[]>(strlen(TestOutputFlagStr) + 1);
strcpy(TestOutputFlag.get(), TestOutputFlagStr);

std::string FDSpecifierStr("filedescs=");
FDSpecifierStr += utostr(ToExecutor[ReadEnd]);
FDSpecifierStr += ',';
Expand All @@ -124,7 +112,8 @@ launchLocalExecutor(StringRef ExecutablePath) {
strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
}

char *const Args[] = {ExecPath.get(), FDSpecifier.get(), nullptr};
char *const Args[] = {ExecPath.get(), TestOutputFlag.get(),
FDSpecifier.get(), nullptr};
int RC = execvp(ExecPath.get(), Args);
if (RC != 0)
return make_error<StringError>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ launchLocalExecutor(llvm::StringRef ExecutablePath);
llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
connectTCPSocket(llvm::StringRef NetworkAddress);

llvm::Error addDebugSupport(llvm::orc::ObjectLayer &ObjLayer);

llvm::Expected<std::unique_ptr<llvm::orc::DefinitionGenerator>>
loadDylib(llvm::orc::ExecutionSession &ES, llvm::StringRef RemotePath);

Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Examples/OrcV2Examples/Inputs/argc_sub1.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
define i32 @sub1(i32 %0) {
%2 = add i32 %0, -1
ret i32 %2
}

define i32 @main(i32 %0) {
%2 = call i32 @sub1(i32 %0)
ret i32 %2
}

!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang 18.0.0git", emissionKind: FullDebug)
!2 = !DIFile(filename: "argc_sub1.c", directory: ".")
51 changes: 0 additions & 51 deletions llvm/test/Examples/OrcV2Examples/Inputs/argc_sub1_elf.ll

This file was deleted.

16 changes: 11 additions & 5 deletions llvm/test/Examples/OrcV2Examples/lljit-with-remote-debugging.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# This test makes sure that the example builds and executes as expected.
# Check that the debug support plugin appends a jit_code_entry to the
# jit_descriptor of the child process.

# Instructions for debugging can be found in LLJITWithRemoteDebugging.cpp

# REQUIRES: default_triple
# UNSUPPORTED: target=powerpc64{{.*}}

# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll | FileCheck --check-prefix=CHECK0 %s
# CHECK0: Parsing input IR code from: {{.*}}/Inputs/argc_sub1_elf.ll
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1.ll 2>&1 | FileCheck --check-prefix=CHECK0 %s
# CHECK0: __jit_debug_descriptor.last_entry = [[BEFORE0:0x[0-9a-f]+]]
# CHECK0-NOT: __jit_debug_descriptor.last_entry = [[BEFORE0]]
# CHECK0: Parsing input IR code from: {{.*}}/Inputs/argc_sub1.ll
# CHECK0: Running: main()
# CHECK0: Exit code: 0

# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll --args 2nd 3rd 4th | FileCheck --check-prefix=CHECK3 %s
# CHECK3: Parsing input IR code from: {{.*}}/Inputs/argc_sub1_elf.ll
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1.ll --args 2nd 3rd 4th 2>&1 | FileCheck --check-prefix=CHECK3 %s
# CHECK3: __jit_debug_descriptor.last_entry = [[BEFORE3:0x[0-9a-f]+]]
# CHECK3-NOT: __jit_debug_descriptor.last_entry = [[BEFORE3]]
# CHECK3: Parsing input IR code from: {{.*}}/Inputs/argc_sub1.ll
# CHECK3: Running: main("2nd", "3rd", "4th")
# CHECK3: Exit code: 3
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ int openListener(std::string Host, std::string PortStr) {
#endif // LLVM_ON_UNIX
}

// This must be kept in sync with gdb/gdb/jit.h .
extern "C" {

typedef enum {
JIT_NOACTION = 0,
JIT_REGISTER_FN,
JIT_UNREGISTER_FN
} jit_actions_t;

struct jit_code_entry {
struct jit_code_entry *next_entry;
struct jit_code_entry *prev_entry;
const char *symfile_addr;
uint64_t symfile_size;
};

struct jit_descriptor {
uint32_t version;
// This should be jit_actions_t, but we want to be specific about the
// bit-width.
uint32_t action_flag;
struct jit_code_entry *relevant_entry;
struct jit_code_entry *first_entry;
};

// We put information about the JITed function in this global, which the
// debugger reads. Make sure to specify the version statically, because the
// debugger checks the version before we can set it during runtime.
extern struct jit_descriptor __jit_debug_descriptor;

static void *findLastDebugDescriptorEntryPtr() {
struct jit_code_entry *Last = __jit_debug_descriptor.first_entry;
while (Last && Last->next_entry)
Last = Last->next_entry;
return Last;
}
}

int main(int argc, char *argv[]) {
#if LLVM_ENABLE_THREADS

Expand All @@ -121,10 +159,11 @@ int main(int argc, char *argv[]) {
int InFD = 0;
int OutFD = 0;

std::vector<StringRef> TestOutputFlags;

if (argc < 2)
printErrorAndExit("insufficient arguments");
else {

StringRef ConnectArg = argv[FirstProgramArg++];
#ifndef NDEBUG
if (ConnectArg == "debug") {
Expand All @@ -133,6 +172,11 @@ int main(int argc, char *argv[]) {
}
#endif

while (ConnectArg.starts_with("test-")) {
TestOutputFlags.push_back(ConnectArg);
ConnectArg = argv[FirstProgramArg++];
}

StringRef SpecifierType, Specifier;
std::tie(SpecifierType, Specifier) = ConnectArg.split('=');
if (SpecifierType == "filedescs") {
Expand All @@ -156,6 +200,10 @@ int main(int argc, char *argv[]) {
printErrorAndExit("invalid specifier type \"" + SpecifierType + "\"");
}

if (llvm::is_contained(TestOutputFlags, "test-jitloadergdb"))
fprintf(stderr, "__jit_debug_descriptor.last_entry = 0x%016" PRIx64 "\n",
pointerToJITTargetAddress(findLastDebugDescriptorEntryPtr()));

auto Server =
ExitOnErr(SimpleRemoteEPCServer::Create<FDSimpleRemoteEPCTransport>(
[](SimpleRemoteEPCServer::Setup &S) -> Error {
Expand All @@ -173,6 +221,11 @@ int main(int argc, char *argv[]) {
InFD, OutFD));

ExitOnErr(Server->waitForDisconnect());

if (llvm::is_contained(TestOutputFlags, "test-jitloadergdb"))
fprintf(stderr, "__jit_debug_descriptor.last_entry = 0x%016" PRIx64 "\n",
pointerToJITTargetAddress(findLastDebugDescriptorEntryPtr()));

return 0;

#else
Expand Down