[Mesa-dev] [PATCH 05/10] clover: Add environment variables for dumping kernel code

Tom Stellard thomas.stellard at amd.com
Mon Oct 6 12:44:27 PDT 2014


---
 .../state_trackers/clover/llvm/invocation.cpp      | 74 ++++++++++++++++++----
 1 file changed, 63 insertions(+), 11 deletions(-)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index a1a54e0..3e6a186 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -61,6 +61,8 @@
 #include <llvm/Support/TargetRegistry.h>
 #include <llvm/Transforms/IPO.h>
 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
+#include "llvm/Transforms/Utils/Cloning.h"
+
 
 #if HAVE_LLVM < 0x0302
 #include <llvm/Target/TargetData.h>
@@ -433,19 +435,39 @@ namespace {
       return m;
    }
 
+   static void emit_code(LLVMTargetMachineRef tm, LLVMModuleRef mod,
+                         LLVMCodeGenFileType file_type,
+                         LLVMMemoryBufferRef *out_buffer,
+                         compat::string &r_log) {
+      LLVMBool err;
+      char *err_message = NULL;
+
+      err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod, file_type,
+                                                &err_message, out_buffer);
+
+      if (err) {
+         r_log = std::string(err_message);
+      }
+
+      LLVMDisposeMessage(err_message);
+
+      if (err) {
+         throw build_error();
+      }
+   }
+
    module
    build_module_native(llvm::Module *mod,
                      const std::vector<llvm::Function *> &kernels,
                      clang::LangAS::Map& address_spaces,
                      std::string triple, std::string processor,
-                     compat::string &r_log) {
+                     bool dump_asm, compat::string &r_log) {
       std::string log;
       LLVMTargetRef target;
       char *error_message;
       LLVMMemoryBufferRef out_buffer;
       unsigned buffer_size;
       const char *buffer_data;
-      LLVMBool err;
       LLVMModuleRef mod_ref = wrap(mod);
 
       if (LLVMGetTargetFromTriple(triple.c_str(), &target, &error_message)) {
@@ -463,16 +485,27 @@ namespace {
          throw build_error();
       }
 
-      err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod_ref, LLVMObjectFile,
-                                                &error_message, &out_buffer);
+      if (dump_asm) {
+         LLVMSetTargetMachineAsmVerbosity(tm, true);
+#if HAVE_LLVM >= 0x0306
+         LLVMSetTargetMachineShowMCEncoding(tm, true);
+#endif
+         LLVMModuleRef debug_mod = wrap(llvm::CloneModule(mod));
+         emit_code(tm, debug_mod, LLVMAssemblyFile, &out_buffer, r_log);
+         buffer_size = LLVMGetBufferSize(out_buffer);
+         buffer_data = LLVMGetBufferStart(out_buffer);
+         fprintf(stderr, "%.*s\n", buffer_size, buffer_data);
 
-      if (err) {
-         LLVMDisposeTargetMachine(tm);
-         r_log = std::string(error_message);
-         LLVMDisposeMessage(error_message);
-         throw build_error();
+         LLVMSetTargetMachineAsmVerbosity(tm, false);
+#if HAVE_LLVM >= 0x0306
+         LLVMSetTargetMachineShowMCEncoding(tm, false);
+#endif
+         LLVMDisposeMemoryBuffer(out_buffer);
+         LLVMDisposeModule(debug_mod);
       }
 
+      emit_code(tm, mod_ref, LLVMObjectFile, &out_buffer, r_log);
+
       buffer_size = LLVMGetBufferSize(out_buffer);
       buffer_data = LLVMGetBufferStart(out_buffer);
 
@@ -569,6 +602,18 @@ static void diagnostic_handler(const llvm::DiagnosticInfo &di, void *err_string)
 
 #endif
 
+#define DBG_CLC  (1 << 0)
+#define DBG_LLVM (1 << 1)
+#define DBG_ASM  (1 << 2)
+
+static const struct debug_named_value debug_options[] = {
+   {"clc", DBG_CLC, "Dump the OpenCL C code for all kernels."},
+   {"llvm", DBG_LLVM, "Dump the generated LLVM IR for all kernels."},
+   {"asm", DBG_ASM, "Dump kernel assembly code for targets specifying "
+                    "PIPE_SHADER_IR_NATIVE"},
+	DEBUG_NAMED_VALUE_END // must be last
+};
+
 module
 clover::compile_program_llvm(const compat::string &source,
                              enum pipe_shader_ir ir,
@@ -576,8 +621,9 @@ clover::compile_program_llvm(const compat::string &source,
                              const compat::string &opts,
                              compat::string &r_log) {
 
+   static unsigned debug_flags = debug_get_flags_option("CLOVER_DEBUG",
+                                                         debug_options, 0);
    static bool target_init = false;
-
    if (!target_init) {
 
       LLVMInitializeAllTargets();
@@ -610,6 +656,12 @@ clover::compile_program_llvm(const compat::string &source,
 
    optimize(mod, optimization_level, kernels);
 
+   if (debug_flags & DBG_CLC)
+      std::cerr << std::string(source);
+
+   if (debug_flags & DBG_LLVM)
+      mod->dump();
+
    module m;
    // Build the clover::module
    switch (ir) {
@@ -623,7 +675,7 @@ clover::compile_program_llvm(const compat::string &source,
          break;
       case PIPE_SHADER_IR_NATIVE:
          m = build_module_native(mod, kernels, address_spaces, triple,
-                                 processor, r_log);
+                                 processor, debug_flags & DBG_ASM, r_log);
          break;
    }
 #if HAVE_LLVM >= 0x0306
-- 
1.8.5.5



More information about the mesa-dev mailing list