LLVM: Dump Module instead of Function in JIT mode (mono/mono#16837)
authorEgor Bogatov <egorbo@gmail.com>
Mon, 16 Sep 2019 00:43:10 +0000 (03:43 +0300)
committerSteve Pfister <steveisok@users.noreply.github.com>
Mon, 16 Sep 2019 00:43:10 +0000 (20:43 -0400)
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
src/mono/mono/mini/mini-llvm-cpp.h
src/mono/mono/mini/mini-llvm.c

index 99d4487..08f04bd 100644 (file)
@@ -51,8 +51,16 @@ void
 mono_llvm_dump_value (LLVMValueRef value)
 {
        /* Same as LLVMDumpValue (), but print to stdout */
-       fflush (stdout);
        outs () << (*unwrap<Value> (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 */
index bc5cbf3..f8f807f 100644 (file)
@@ -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,
index 089d7d6..16b65eb 100644 (file)
@@ -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");
        }