aco: skip unnecessary compiler pass for the trap handler program
[mesa.git] / src / amd / compiler / aco_interface.cpp
index 3031bb43f7a31a354dd17d06cb19c379c067ddec..5d402495ae545fa9ecbf79a5887c5eaad29f14d6 100644 (file)
 #include "aco_ir.h"
 #include "vulkan/radv_shader.h"
 #include "vulkan/radv_shader_args.h"
-#include "c11/threads.h"
-#include "util/debug.h"
 
 #include <iostream>
 #include <sstream>
 
-namespace aco {
-uint64_t debug_flags = 0;
-
-static const struct debug_control aco_debug_options[] = {
-   {"validateir", DEBUG_VALIDATE},
-   {"validatera", DEBUG_VALIDATE_RA},
-   {"perfwarn", DEBUG_PERFWARN},
-   {NULL, 0}
-};
-
-static once_flag init_once_flag = ONCE_FLAG_INIT;
-
-static void init()
-{
-   debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);
-
-   #ifndef NDEBUG
-   /* enable some flags by default on debug builds */
-   debug_flags |= aco::DEBUG_VALIDATE;
-   #endif
-}
-}
-
 static radv_compiler_statistic_info statistic_infos[] = {
    [aco::statistic_hash] = {"Hash", "CRC32 hash of code and constant data"},
    [aco::statistic_instructions] = {"Instructions", "Instruction count"},
@@ -68,12 +43,21 @@ static radv_compiler_statistic_info statistic_infos[] = {
    [aco::statistic_vgpr_presched] = {"Pre-Sched VGPRs", "VGPR usage before scheduling"},
 };
 
+static void validate(aco::Program *program)
+{
+   if (!(aco::debug_flags & aco::DEBUG_VALIDATE_IR))
+      return;
+
+   bool is_valid = aco::validate_ir(program);
+   assert(is_valid);
+}
+
 void aco_compile_shader(unsigned shader_count,
                         struct nir_shader *const *shaders,
                         struct radv_shader_binary **binary,
                         struct radv_shader_args *args)
 {
-   call_once(&aco::init_once_flag, aco::init);
+   aco::init();
 
    ac_shader_config config = {0};
    std::unique_ptr<aco::Program> program{new aco::Program};
@@ -82,6 +66,9 @@ void aco_compile_shader(unsigned shader_count,
    if (program->collect_statistics)
       memset(program->statistics, 0, sizeof(program->statistics));
 
+   program->debug.func = args->options->debug.func;
+   program->debug.private_data = args->options->debug.private_data;
+
    /* Instruction Selection */
    if (args->is_gs_copy_shader)
       aco::select_gs_copy_shader(program.get(), shaders[0], &config, args);
@@ -91,33 +78,27 @@ void aco_compile_shader(unsigned shader_count,
       std::cerr << "After Instruction Selection:\n";
       aco_print_program(program.get(), stderr);
    }
-   aco::validate(program.get(), stderr);
-
-   /* Phi lowering */
-   aco::lower_phis(program.get());
-   //std::cerr << "After Phi Lowering:\n";
-   //aco_print_program(program.get(), stderr);
-
-   aco::dominator_tree(program.get());
-
-   /* Optimization */
-   aco::value_numbering(program.get());
-   aco::optimize(program.get());
-   aco::validate(program.get(), stderr);
-
-   aco::setup_reduce_temp(program.get());
-   aco::insert_exec_mask(program.get());
-   aco::validate(program.get(), stderr);
 
-   aco::live live_vars = aco::live_var_analysis(program.get(), args->options);
-   aco::spill(program.get(), live_vars, args->options);
-
-   if (program->collect_statistics)
-      aco::collect_presched_stats(program.get());
-
-   //std::cerr << "Before Schedule:\n";
-   //aco_print_program(program.get(), stderr);
-   aco::schedule_program(program.get(), live_vars);
+   aco::live live_vars;
+   if (!args->is_trap_handler_shader) {
+      /* Phi lowering */
+      aco::lower_phis(program.get());
+      aco::dominator_tree(program.get());
+      validate(program.get());
+
+      /* Optimization */
+      aco::value_numbering(program.get());
+      aco::optimize(program.get());
+
+      /* cleanup and exec mask handling */
+      aco::setup_reduce_temp(program.get());
+      aco::insert_exec_mask(program.get());
+      validate(program.get());
+
+      /* spilling and scheduling */
+      live_vars = aco::live_var_analysis(program.get(), args->options);
+      aco::spill(program.get(), live_vars, args->options);
+   }
 
    std::string llvm_ir;
    if (args->options->record_ir) {
@@ -134,32 +115,38 @@ void aco_compile_shader(unsigned shader_count,
       free(data);
    }
 
-   /* Register Allocation */
-   aco::register_allocation(program.get(), live_vars.live_out);
-   if (args->options->dump_shader) {
-      std::cerr << "After RA:\n";
-      aco_print_program(program.get(), stderr);
-   }
+   if (program->collect_statistics)
+      aco::collect_presched_stats(program.get());
 
-   if (aco::validate_ra(program.get(), args->options, stderr)) {
-      std::cerr << "Program after RA validation failure:\n";
-      aco_print_program(program.get(), stderr);
-      abort();
+   if (!args->is_trap_handler_shader) {
+      aco::schedule_program(program.get(), live_vars);
+      validate(program.get());
+
+      /* Register Allocation */
+      aco::register_allocation(program.get(), live_vars.live_out);
+      if (args->options->dump_shader) {
+         std::cerr << "After RA:\n";
+         aco_print_program(program.get(), stderr);
+      }
+
+      if (aco::validate_ra(program.get(), args->options)) {
+         std::cerr << "Program after RA validation failure:\n";
+         aco_print_program(program.get(), stderr);
+         abort();
+      }
+
+      validate(program.get());
+
+      aco::ssa_elimination(program.get());
    }
 
-   aco::ssa_elimination(program.get());
    /* Lower to HW Instructions */
    aco::lower_to_hw_instr(program.get());
-   //std::cerr << "After Eliminate Pseudo Instr:\n";
-   //aco_print_program(program.get(), stderr);
 
    /* Insert Waitcnt */
    aco::insert_wait_states(program.get());
    aco::insert_NOPs(program.get());
 
-   //std::cerr << "After Insert-Waitcnt:\n";
-   //aco_print_program(program.get(), stderr);
-
    if (program->collect_statistics)
       aco::collect_preasm_stats(program.get());