Skip to content

Frontend: support dumping the JIT state #33301

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 1 commit into from
Aug 5, 2020
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
8 changes: 8 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ struct PointerAuthOptions : clang::PointerAuthOptions {
PointerAuthSchema ResilientClassStubInitCallbacks;
};

enum class JITDebugArtifact : unsigned {
None, ///< None
LLVMIR, ///< LLVM IR
Object, ///< Object File
};

/// The set of options supported by IR generation.
class IRGenOptions {
public:
Expand Down Expand Up @@ -326,6 +332,8 @@ class IRGenOptions {
Optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityLibraryVersion;
Optional<llvm::VersionTuple> AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion;

JITDebugArtifact DumpJIT = JITDebugArtifact::None;

IRGenOptions()
: DWARFVersion(2), OutputKind(IRGenOutputKind::LLVMAssembly),
Verify(true), OptMode(OptimizationMode::NotSet),
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ def disable_llvm_verify : Flag<["-"], "disable-llvm-verify">,
def disable_llvm_value_names : Flag<["-"], "disable-llvm-value-names">,
HelpText<"Don't add names to local values in LLVM IR">;

def dump_jit : JoinedOrSeparate<["-"], "dump-jit">,
HelpText<"Dump JIT contents">;

def enable_llvm_value_names : Flag<["-"], "enable-llvm-value-names">,
HelpText<"Add names to local values in LLVM IR">;

Expand Down
18 changes: 16 additions & 2 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,9 +1448,23 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
if (Args.hasArg(OPT_disable_concrete_type_metadata_mangled_name_accessors))
Opts.DisableConcreteTypeMetadataMangledNameAccessors = true;

if (Args.hasArg(OPT_use_jit))
if (Args.hasArg(OPT_use_jit)) {
Opts.UseJIT = true;

if (const Arg *A = Args.getLastArg(OPT_dump_jit)) {
llvm::Optional<swift::JITDebugArtifact> artifact =
llvm::StringSwitch<llvm::Optional<swift::JITDebugArtifact>>(A->getValue())
.Case("llvm-ir", JITDebugArtifact::LLVMIR)
.Case("object", JITDebugArtifact::Object)
.Default(None);
if (!artifact) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getOption().getName(), A->getValue());
return true;
}
Opts.DumpJIT = *artifact;
}
}

for (const Arg *A : Args.filtered(OPT_verify_type_layout)) {
Opts.VerifyTypeLayoutNames.push_back(A->getValue());
}
Expand Down
25 changes: 25 additions & 0 deletions lib/Immediate/Immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/config.h"
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/IR/LLVMContext.h"
Expand Down Expand Up @@ -75,6 +76,18 @@ static void *loadRuntimeLib(StringRef sharedLibName,
return nullptr;
}

static void DumpLLVMIR(const llvm::Module &M) {
std::string path = (M.getName() + ".ll").str();
for (size_t count = 0; llvm::sys::fs::exists(path); )
path = (M.getName() + llvm::utostr(count++) + ".ll").str();

std::error_code error;
llvm::raw_fd_ostream stream(path, error);
if (error)
return;
M.print(stream, /*AssemblyAnnotationWriter=*/nullptr);
}

void *swift::immediate::loadSwiftRuntime(ArrayRef<std::string>
runtimeLibPaths) {
#if defined(_WIN32)
Expand Down Expand Up @@ -290,6 +303,18 @@ int swift::RunImmediately(CompilerInstance &CI,
}

auto Module = GenModule.getModule();

switch (IRGenOpts.DumpJIT) {
case JITDebugArtifact::None:
break;
case JITDebugArtifact::LLVMIR:
DumpLLVMIR(*Module);
break;
case JITDebugArtifact::Object:
JIT->getObjTransformLayer().setTransform(llvm::orc::DumpObjects());
break;
}

{
// Get a generator for the process symbols and attach it to the main
// JITDylib.
Expand Down
15 changes: 15 additions & 0 deletions test/IRGen/jit-debugging.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// %empty-directory(%t)
// RUN: not %target-swift-frontend -use-jit -dump-jit invalid -interpret %s 2>&1 | %FileCheck -check-prefix CHECK-INVALID %s
// CHECK-INVALID: error: invalid value 'invalid' in 'dump-jit'

// RUN: %empty-directory(%t)
// RUN: cd %t && %target-swift-frontend -use-jit -dump-jit llvm-ir -interpret %s
// RUN: %FileCheck -check-prefix CHECK-LLIR %s < %t/main.ll
// CHECK-LLIR: ; ModuleID = 'main'

// RUN: %empty-directory(%t)
// RUN: cd %t && %target-swift-frontend -use-jit -dump-jit object -interpret %s
// RUN: %llvm-nm --defined-only --extern-only %t/main-jitted-objectbuffer.o | %FileCheck -check-prefix CHECK-OBJ %s
// CHECK-OBJ: T {{_?}}main

let zero = 0