LLVM: Add comments (string metadata) for calls (mono/mono#16956)
authorEgor Bogatov <egorbo@gmail.com>
Thu, 26 Sep 2019 07:51:36 +0000 (10:51 +0300)
committerMarek Safar <marek.safar@gmail.com>
Thu, 26 Sep 2019 07:51:36 +0000 (09:51 +0200)
* Add string metadata to all calls (comments)

* Update mini-llvm.c

* rename tramp_%d to full name too

Commit migrated from https://github.com/mono/mono/commit/4a72dadb34f313d242908b086f204cd5346d0bbc

src/mono/mono/mini/mini-llvm-cpp.cpp
src/mono/mono/mini/mini-llvm-cpp.h
src/mono/mono/mini/mini-llvm.c

index 33c0ad5..8e9a0b5 100644 (file)
@@ -222,6 +222,14 @@ mono_llvm_build_weighted_branch (LLVMBuilderRef builder, LLVMValueRef cond, LLVM
 }
 
 void
+mono_llvm_add_string_metadata (LLVMValueRef insref, const char* label, const char* text)
+{
+       auto ins = unwrap<Instruction> (insref);
+       auto &ctx = ins->getContext ();
+       ins->setMetadata (label, MDNode::get (ctx, MDString::get (ctx, text)));
+}
+
+void
 mono_llvm_set_implicit_branch (LLVMBuilderRef builder, LLVMValueRef branch)
 {
        auto b = unwrap (builder);
index f8f807f..1ebeddc 100644 (file)
@@ -95,6 +95,9 @@ LLVMValueRef
 mono_llvm_build_weighted_branch (LLVMBuilderRef builder, LLVMValueRef cond, LLVMBasicBlockRef t, LLVMBasicBlockRef f, uint32_t t_weight, uint32_t f_weight);
 
 void
+mono_llvm_add_string_metadata (LLVMValueRef insref, const char* label, const char* text);
+
+void
 mono_llvm_set_implicit_branch (LLVMBuilderRef builder, LLVMValueRef branch);
 
 void
index 2661c85..bb9172e 100644 (file)
@@ -3837,7 +3837,7 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
                                static int tramp_index;
                                char *name;
 
-                               name = g_strdup_printf ("tramp_%d", tramp_index);
+                               name = g_strdup_printf ("[tramp_%d] %s", tramp_index, mono_method_full_name (call->method, TRUE));
                                tramp_index ++;
 
                                /*
@@ -4101,6 +4101,10 @@ process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref,
        if (ins->opcode != OP_TAILCALL && ins->opcode != OP_TAILCALL_MEMBASE && LLVMGetInstructionOpcode (lcall) == LLVMCall)
                mono_llvm_set_call_notailcall (lcall);
 
+       // Add original method name we are currently emitting as a custom string metadata (the only way to leave comments in LLVM IR)
+       if (mono_debug_enabled () && call && call->method)
+               mono_llvm_add_string_metadata (lcall, "managed_name", mono_method_full_name (call->method, TRUE));
+
        // As per the LLVM docs, a function has a noalias return value if and only if
        // it is an allocation function. This is an allocation function.
        if (call->method && call->method->wrapper_type == MONO_WRAPPER_ALLOC)