Skip to content
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

[mono] Separate LLVM optimization and compilation into two separate functions #53536

Merged
merged 1 commit into from
Jun 2, 2021
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
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 @@ -13581,10 +13581,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 @@ -13595,6 +13592,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