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

Matt Arsenault arsenm2 at gmail.com
Wed Oct 8 09:15:39 PDT 2014


On Oct 6, 2014, at 12:44 PM, Tom Stellard <thomas.stellard at amd.com> wrote:

> ---
> .../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);

It would be much better to emit each of these to a separate file with a consistent naming scheme than to just dump everything to stderr. It’s easier to just have a separate file for the source, IR and ISA to look at and compare rather than having to split it out of the rest of the debug output, which also gets harder as programs get bigger and add multiple kernels. There’s also the problem with stderr not being flushed if the machine hangs, so you only get partial output. 

What we have is an environment variable that specifies the prefix to use for the file name, (defaulting to _temp_), so the output is like
_temp_0_Tahiti_foo.isa, _temp_0_Tahiti_foo.bc, _temp_1_Tahiti_bar.cl. The number is the compile index for the program, which is important for programs with multiple compiles, especially for ones which compile kernels with the same name multiple times (which seems to be strangely not uncommon). The specified prefix is useful for saving sets of slightly different output with a change or different compile options or something like that



> 
> -      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
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/mesa-dev



More information about the mesa-dev mailing list