1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#ifndef ACO_INSTRUCTION_SELECTION_H
26bf215546Sopenharmony_ci#define ACO_INSTRUCTION_SELECTION_H
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "aco_ir.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "vulkan/radv_shader_args.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include <array>
33bf215546Sopenharmony_ci#include <unordered_map>
34bf215546Sopenharmony_ci#include <vector>
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cinamespace aco {
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cienum aco_color_output_type {
39bf215546Sopenharmony_ci   ACO_TYPE_ANY32,
40bf215546Sopenharmony_ci   ACO_TYPE_FLOAT16,
41bf215546Sopenharmony_ci   ACO_TYPE_INT16,
42bf215546Sopenharmony_ci   ACO_TYPE_UINT16,
43bf215546Sopenharmony_ci};
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistruct shader_io_state {
46bf215546Sopenharmony_ci   uint8_t mask[VARYING_SLOT_MAX];
47bf215546Sopenharmony_ci   Temp temps[VARYING_SLOT_MAX * 4u];
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   shader_io_state()
50bf215546Sopenharmony_ci   {
51bf215546Sopenharmony_ci      memset(mask, 0, sizeof(mask));
52bf215546Sopenharmony_ci      std::fill_n(temps, VARYING_SLOT_MAX * 4u, Temp(0, RegClass::v1));
53bf215546Sopenharmony_ci   }
54bf215546Sopenharmony_ci};
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_cistruct isel_context {
57bf215546Sopenharmony_ci   const struct aco_compiler_options* options;
58bf215546Sopenharmony_ci   const struct radv_shader_args* args;
59bf215546Sopenharmony_ci   Program* program;
60bf215546Sopenharmony_ci   nir_shader* shader;
61bf215546Sopenharmony_ci   uint32_t constant_data_offset;
62bf215546Sopenharmony_ci   Block* block;
63bf215546Sopenharmony_ci   uint32_t first_temp_id;
64bf215546Sopenharmony_ci   std::unordered_map<unsigned, std::array<Temp, NIR_MAX_VEC_COMPONENTS>> allocated_vec;
65bf215546Sopenharmony_ci   Stage stage;
66bf215546Sopenharmony_ci   struct {
67bf215546Sopenharmony_ci      bool has_branch;
68bf215546Sopenharmony_ci      struct {
69bf215546Sopenharmony_ci         unsigned header_idx;
70bf215546Sopenharmony_ci         Block* exit;
71bf215546Sopenharmony_ci         bool has_divergent_continue = false;
72bf215546Sopenharmony_ci         bool has_divergent_branch = false;
73bf215546Sopenharmony_ci      } parent_loop;
74bf215546Sopenharmony_ci      struct {
75bf215546Sopenharmony_ci         bool is_divergent = false;
76bf215546Sopenharmony_ci      } parent_if;
77bf215546Sopenharmony_ci      bool exec_potentially_empty_discard =
78bf215546Sopenharmony_ci         false; /* set to false when loop_nest_depth==0 && parent_if.is_divergent==false */
79bf215546Sopenharmony_ci      uint16_t exec_potentially_empty_break_depth = UINT16_MAX;
80bf215546Sopenharmony_ci      /* Set to false when loop_nest_depth==exec_potentially_empty_break_depth
81bf215546Sopenharmony_ci       * and parent_if.is_divergent==false. Called _break but it's also used for
82bf215546Sopenharmony_ci       * loop continues. */
83bf215546Sopenharmony_ci      bool exec_potentially_empty_break = false;
84bf215546Sopenharmony_ci      std::unique_ptr<unsigned[]> nir_to_aco; /* NIR block index to ACO block index */
85bf215546Sopenharmony_ci   } cf_info;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   /* NIR range analysis. */
88bf215546Sopenharmony_ci   struct hash_table* range_ht;
89bf215546Sopenharmony_ci   nir_unsigned_upper_bound_config ub_config;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   Temp arg_temps[AC_MAX_ARGS];
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   /* FS inputs */
94bf215546Sopenharmony_ci   Temp persp_centroid, linear_centroid;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   /* GS inputs */
97bf215546Sopenharmony_ci   Temp gs_wave_id;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   /* VS output information */
100bf215546Sopenharmony_ci   bool export_clip_dists;
101bf215546Sopenharmony_ci   unsigned num_clip_distances;
102bf215546Sopenharmony_ci   unsigned num_cull_distances;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   /* tessellation information */
105bf215546Sopenharmony_ci   uint64_t tcs_temp_only_inputs;
106bf215546Sopenharmony_ci   uint32_t tcs_num_patches;
107bf215546Sopenharmony_ci   bool tcs_in_out_eq = false;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   /* Fragment color output information */
110bf215546Sopenharmony_ci   uint16_t output_color_types;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   /* I/O information */
113bf215546Sopenharmony_ci   shader_io_state inputs;
114bf215546Sopenharmony_ci   shader_io_state outputs;
115bf215546Sopenharmony_ci};
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ciinline Temp
118bf215546Sopenharmony_ciget_arg(isel_context* ctx, struct ac_arg arg)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   assert(arg.used);
121bf215546Sopenharmony_ci   return ctx->arg_temps[arg.arg_index];
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_civoid init_context(isel_context* ctx, nir_shader* shader);
125bf215546Sopenharmony_civoid cleanup_context(isel_context* ctx);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ciisel_context setup_isel_context(Program* program, unsigned shader_count,
128bf215546Sopenharmony_ci                                struct nir_shader* const* shaders, ac_shader_config* config,
129bf215546Sopenharmony_ci                                const struct aco_compiler_options* options,
130bf215546Sopenharmony_ci                                const struct aco_shader_info* info,
131bf215546Sopenharmony_ci                                const struct radv_shader_args* args,
132bf215546Sopenharmony_ci                                bool is_gs_copy_shader, bool is_ps_epilog);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci} // namespace aco
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci#endif /* ACO_INSTRUCTION_SELECTION_H */
137