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