Skip to content

Commit

Permalink
[mono] Separate LLVM optimization and compilation into two separate f…
Browse files Browse the repository at this point in the history
…unctions. (#53536)

Dump optimized IR before attempting compilation to machine code. Makes
more information available when debugging.

Split from #53132.
  • Loading branch information
imhameed authored Jun 2, 2021
1 parent e3069cf commit c103e2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/mono/mono/mini/llvm-jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,21 @@ struct MonoLLVMJIT {
return ret;
}

void
optimize (Function *func)
{
auto module = func->getParent ();
module->setDataLayout (data_layout);
fpm.run (*func);
}

gpointer
compile (
Function *func, int nvars, LLVMValueRef *callee_vars,
gpointer *callee_addrs, gpointer *eh_frame)
{
auto module = func->getParent ();
module->setDataLayout (data_layout);
fpm.run (*func);
// The lifetime of this module is managed by Mono, not LLVM, so
// the `unique_ptr` created here will be released in the
// NotifyCompiled callback.
Expand Down Expand Up @@ -416,9 +423,15 @@ class MonoLLVMJIT {
return MangledName;
}

gpointer compile (Function *F, int nvars, LLVMValueRef *callee_vars, gpointer *callee_addrs, gpointer *eh_frame) {
void
optimize (Function *func)
{
F->getParent ()->setDataLayout (TM->createDataLayout ());
fpm.run(*F);
}

gpointer compile (Function *F, int nvars, LLVMValueRef *callee_vars, gpointer *callee_addrs, gpointer *eh_frame) {
F->getParent ()->setDataLayout (TM->createDataLayout ());
// TODO: run module wide optimizations, e.g. remove dead globals/functions
// Orc uses a shared_ptr to refer to modules so we have to save them ourselves to keep a ref
std::shared_ptr<Module> m (F->getParent ());
Expand Down Expand Up @@ -565,6 +578,12 @@ mono_llvm_create_ee (LLVMExecutionEngineRef *ee)
return NULL;
}

void
mono_llvm_optimize_method (LLVMValueRef method)
{
jit->optimize (unwrap<Function> (method));
}

/*
* mono_llvm_compile_method:
*
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono/mini/llvm-jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ mono_llvm_create_ee (LLVMExecutionEngineRef *ee);
void
mono_llvm_dispose_ee (MonoEERef *mono_ee);

void
mono_llvm_optimize_method (LLVMValueRef method);

gpointer
mono_llvm_compile_method (MonoEERef mono_ee, MonoCompile *cfg, LLVMValueRef method, int nvars, LLVMValueRef *callee_vars, gpointer *callee_addrs, gpointer *eh_frame);

Expand Down
10 changes: 6 additions & 4 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13597,10 +13597,7 @@ llvm_jit_finalize_method (EmitContext *ctx)
while (g_hash_table_iter_next (&iter, NULL, (void**)&var))
callee_vars [i ++] = var;

mono_codeman_enable_write ();
cfg->native_code = (guint8*)mono_llvm_compile_method (ctx->module->mono_ee, cfg, ctx->lmethod, nvars, callee_vars, callee_addrs, &eh_frame);
mono_llvm_remove_gc_safepoint_poll (ctx->lmodule);
mono_codeman_disable_write ();
mono_llvm_optimize_method (ctx->lmethod);
if (cfg->verbose_level > 1) {
g_print ("\n*** Optimized LLVM IR for %s ***\n", mono_method_full_name (cfg->method, TRUE));
if (cfg->compile_aot) {
Expand All @@ -13611,6 +13608,11 @@ llvm_jit_finalize_method (EmitContext *ctx)
g_print ("***\n\n");
}

mono_codeman_enable_write ();
cfg->native_code = (guint8*)mono_llvm_compile_method (ctx->module->mono_ee, cfg, ctx->lmethod, nvars, callee_vars, callee_addrs, &eh_frame);
mono_llvm_remove_gc_safepoint_poll (ctx->lmodule);
mono_codeman_disable_write ();

decode_llvm_eh_info (ctx, eh_frame);

// FIXME:
Expand Down

0 comments on commit c103e2a

Please sign in to comment.