From c103e2a445a1e2a523f14936ecadf521f56f182f Mon Sep 17 00:00:00 2001 From: imhameed Date: Wed, 2 Jun 2021 07:21:33 -0700 Subject: [PATCH] [mono] Separate LLVM optimization and compilation into two separate functions. (#53536) Dump optimized IR before attempting compilation to machine code. Makes more information available when debugging. Split from https://github.com/dotnet/runtime/pull/53132. --- src/mono/mono/mini/llvm-jit.cpp | 23 +++++++++++++++++++++-- src/mono/mono/mini/llvm-jit.h | 3 +++ src/mono/mono/mini/mini-llvm.c | 10 ++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/mono/mono/mini/llvm-jit.cpp b/src/mono/mono/mini/llvm-jit.cpp index e536896..ed69471 100644 --- a/src/mono/mono/mini/llvm-jit.cpp +++ b/src/mono/mono/mini/llvm-jit.cpp @@ -306,6 +306,14 @@ 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, @@ -313,7 +321,6 @@ struct MonoLLVMJIT { { 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. @@ -416,9 +423,15 @@ public: 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 m (F->getParent ()); @@ -565,6 +578,12 @@ mono_llvm_create_ee (LLVMExecutionEngineRef *ee) return NULL; } +void +mono_llvm_optimize_method (LLVMValueRef method) +{ + jit->optimize (unwrap (method)); +} + /* * mono_llvm_compile_method: * diff --git a/src/mono/mono/mini/llvm-jit.h b/src/mono/mono/mini/llvm-jit.h index 0b9a943..2845a65 100644 --- a/src/mono/mono/mini/llvm-jit.h +++ b/src/mono/mono/mini/llvm-jit.h @@ -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); diff --git a/src/mono/mono/mini/mini-llvm.c b/src/mono/mono/mini/mini-llvm.c index 65b74f9..ffe0a83 100644 --- a/src/mono/mono/mini/mini-llvm.c +++ b/src/mono/mono/mini/mini-llvm.c @@ -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) { @@ -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: -- 2.7.4