17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2018 Valve Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#ifndef ACO_INSTRUCTION_SELECTION_H
267ec681f3Smrg#define ACO_INSTRUCTION_SELECTION_H
277ec681f3Smrg
287ec681f3Smrg#include "aco_ir.h"
297ec681f3Smrg
307ec681f3Smrg#include "vulkan/radv_shader_args.h"
317ec681f3Smrg
327ec681f3Smrg#include <array>
337ec681f3Smrg#include <unordered_map>
347ec681f3Smrg#include <vector>
357ec681f3Smrg
367ec681f3Smrgnamespace aco {
377ec681f3Smrg
387ec681f3Smrgstruct shader_io_state {
397ec681f3Smrg   uint8_t mask[VARYING_SLOT_MAX];
407ec681f3Smrg   Temp temps[VARYING_SLOT_MAX * 4u];
417ec681f3Smrg
427ec681f3Smrg   shader_io_state()
437ec681f3Smrg   {
447ec681f3Smrg      memset(mask, 0, sizeof(mask));
457ec681f3Smrg      std::fill_n(temps, VARYING_SLOT_MAX * 4u, Temp(0, RegClass::v1));
467ec681f3Smrg   }
477ec681f3Smrg};
487ec681f3Smrg
497ec681f3Smrgstruct isel_context {
507ec681f3Smrg   const struct radv_nir_compiler_options* options;
517ec681f3Smrg   const struct radv_shader_args* args;
527ec681f3Smrg   Program* program;
537ec681f3Smrg   nir_shader* shader;
547ec681f3Smrg   uint32_t constant_data_offset;
557ec681f3Smrg   Block* block;
567ec681f3Smrg   uint32_t first_temp_id;
577ec681f3Smrg   std::unordered_map<unsigned, std::array<Temp, NIR_MAX_VEC_COMPONENTS>> allocated_vec;
587ec681f3Smrg   Stage stage;
597ec681f3Smrg   struct {
607ec681f3Smrg      bool has_branch;
617ec681f3Smrg      struct {
627ec681f3Smrg         unsigned header_idx;
637ec681f3Smrg         Block* exit;
647ec681f3Smrg         bool has_divergent_continue = false;
657ec681f3Smrg         bool has_divergent_branch = false;
667ec681f3Smrg      } parent_loop;
677ec681f3Smrg      struct {
687ec681f3Smrg         bool is_divergent = false;
697ec681f3Smrg      } parent_if;
707ec681f3Smrg      bool exec_potentially_empty_discard =
717ec681f3Smrg         false; /* set to false when loop_nest_depth==0 && parent_if.is_divergent==false */
727ec681f3Smrg      uint16_t exec_potentially_empty_break_depth = UINT16_MAX;
737ec681f3Smrg      /* Set to false when loop_nest_depth==exec_potentially_empty_break_depth
747ec681f3Smrg       * and parent_if.is_divergent==false. Called _break but it's also used for
757ec681f3Smrg       * loop continues. */
767ec681f3Smrg      bool exec_potentially_empty_break = false;
777ec681f3Smrg      std::unique_ptr<unsigned[]> nir_to_aco; /* NIR block index to ACO block index */
787ec681f3Smrg   } cf_info;
797ec681f3Smrg
807ec681f3Smrg   /* NIR range analysis. */
817ec681f3Smrg   struct hash_table* range_ht;
827ec681f3Smrg   nir_unsigned_upper_bound_config ub_config;
837ec681f3Smrg
847ec681f3Smrg   Temp arg_temps[AC_MAX_ARGS];
857ec681f3Smrg
867ec681f3Smrg   /* FS inputs */
877ec681f3Smrg   Temp persp_centroid, linear_centroid;
887ec681f3Smrg
897ec681f3Smrg   /* GS inputs */
907ec681f3Smrg   Temp gs_wave_id;
917ec681f3Smrg
927ec681f3Smrg   /* VS output information */
937ec681f3Smrg   bool export_clip_dists;
947ec681f3Smrg   unsigned num_clip_distances;
957ec681f3Smrg   unsigned num_cull_distances;
967ec681f3Smrg
977ec681f3Smrg   /* tessellation information */
987ec681f3Smrg   uint64_t tcs_temp_only_inputs;
997ec681f3Smrg   uint32_t tcs_num_patches;
1007ec681f3Smrg   bool tcs_in_out_eq = false;
1017ec681f3Smrg
1027ec681f3Smrg   /* I/O information */
1037ec681f3Smrg   shader_io_state inputs;
1047ec681f3Smrg   shader_io_state outputs;
1057ec681f3Smrg};
1067ec681f3Smrg
1077ec681f3Smrginline Temp
1087ec681f3Smrgget_arg(isel_context* ctx, struct ac_arg arg)
1097ec681f3Smrg{
1107ec681f3Smrg   assert(arg.used);
1117ec681f3Smrg   return ctx->arg_temps[arg.arg_index];
1127ec681f3Smrg}
1137ec681f3Smrg
1147ec681f3Smrgvoid init_context(isel_context* ctx, nir_shader* shader);
1157ec681f3Smrgvoid cleanup_context(isel_context* ctx);
1167ec681f3Smrg
1177ec681f3Smrgisel_context setup_isel_context(Program* program, unsigned shader_count,
1187ec681f3Smrg                                struct nir_shader* const* shaders, ac_shader_config* config,
1197ec681f3Smrg                                const struct radv_shader_args* args, bool is_gs_copy_shader);
1207ec681f3Smrg
1217ec681f3Smrg} // namespace aco
1227ec681f3Smrg
1237ec681f3Smrg#endif /* ACO_INSTRUCTION_SELECTION_H */
124