From f313f7e6639224c469136e84d677699a660d19e7 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Mon, 16 Sep 2019 03:43:10 +0300 Subject: [PATCH] LLVM: Dump Module instead of Function in JIT mode (mono/mono#16837) Since each function has its own module (in JIT mode) it makes sense to dump the whole module for MONO_VERBOSE_METHOD instead of just LLVM::Function. It adds metadata, attributes and global variables (so it will be possible to copy-paste it to godbolt) Example static int Test(int x) { return x / 10; } Diff for Unoptimized IR: https://www.diffchecker.com/NrF413Ts Diff for Optimized IR: https://www.diffchecker.com/jbEkeFFS So now we can see global variables and metadata for each function (and can easily paste them to godbolt.org) NOTE: optimized IR for this case contains "dead" variables (dump_module allowed us to see such), we probably need some Module-wide optimizations to strip dead code such as -globaldce NOTE2: why do we emit exceptions in the first place for this case at all? Commit migrated from https://github.com/mono/mono/commit/0438397b8afc51aac45da55c155162e13e83cf19 --- src/mono/mono/mini/mini-llvm-cpp.cpp | 10 +++++++++- src/mono/mono/mini/mini-llvm-cpp.h | 3 +++ src/mono/mono/mini/mini-llvm.c | 12 ++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/mini/mini-llvm-cpp.cpp b/src/mono/mono/mini/mini-llvm-cpp.cpp index 99d4487..08f04bd 100644 --- a/src/mono/mono/mini/mini-llvm-cpp.cpp +++ b/src/mono/mono/mini/mini-llvm-cpp.cpp @@ -51,8 +51,16 @@ void mono_llvm_dump_value (LLVMValueRef value) { /* Same as LLVMDumpValue (), but print to stdout */ - fflush (stdout); outs () << (*unwrap (value)); + fflush (stdout); +} + +void +mono_llvm_dump_module (LLVMModuleRef module) +{ + /* Same as LLVMDumpModule (), but print to stdout */ + outs () << (*unwrap (module)); + fflush (stdout); } /* Missing overload for building an alloca with an alignment */ diff --git a/src/mono/mono/mini/mini-llvm-cpp.h b/src/mono/mono/mini/mini-llvm-cpp.h index bc5cbf3..f8f807f 100644 --- a/src/mono/mono/mini/mini-llvm-cpp.h +++ b/src/mono/mono/mini/mini-llvm-cpp.h @@ -51,6 +51,9 @@ typedef enum { void mono_llvm_dump_value (LLVMValueRef value); +void +mono_llvm_dump_module (LLVMModuleRef module); + LLVMValueRef mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty, LLVMValueRef ArraySize, diff --git a/src/mono/mono/mini/mini-llvm.c b/src/mono/mono/mini/mini-llvm.c index 089d7d6..16b65eb 100644 --- a/src/mono/mono/mini/mini-llvm.c +++ b/src/mono/mono/mini/mini-llvm.c @@ -8456,7 +8456,11 @@ after_codegen: if (cfg->verbose_level > 1) { g_print ("\n*** Unoptimized LLVM IR for %s ***\n", mono_method_full_name (cfg->method, TRUE)); - mono_llvm_dump_value (method); + if (cfg->compile_aot) { + mono_llvm_dump_value (method); + } else { + mono_llvm_dump_module (ctx->lmodule); + } g_print ("***\n\n"); } @@ -10572,7 +10576,11 @@ llvm_jit_finalize_method (EmitContext *ctx) cfg->native_code = (guint8*)mono_llvm_compile_method (ctx->module->mono_ee, ctx->lmethod, nvars, callee_vars, callee_addrs, &eh_frame); if (cfg->verbose_level > 1) { g_print ("\n*** Optimized LLVM IR for %s ***\n", mono_method_full_name (cfg->method, TRUE)); - mono_llvm_dump_value (ctx->lmethod); + if (cfg->compile_aot) { + mono_llvm_dump_value (ctx->lmethod); + } else { + mono_llvm_dump_module (ctx->lmodule); + } g_print ("***\n\n"); } -- 2.7.4