Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 Collabora Ltd. |
| 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 FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | * |
| 23 | * Authors (Collabora): |
| 24 | * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> |
| 25 | */ |
| 26 | |
| 27 | #include "main/mtypes.h" |
| 28 | #include "compiler/glsl/glsl_to_nir.h" |
| 29 | #include "compiler/nir_types.h" |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 30 | #include "compiler/nir/nir_builder.h" |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 31 | #include "util/u_debug.h" |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 32 | |
| 33 | #include "disassemble.h" |
| 34 | #include "bifrost_compile.h" |
Alyssa Rosenzweig | 3a1baaf | 2020-03-10 08:20:59 -0400 | [diff] [blame] | 35 | #include "bifrost_nir.h" |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 36 | #include "compiler.h" |
Alyssa Rosenzweig | 0b26cb1 | 2020-03-03 14:27:05 -0500 | [diff] [blame] | 37 | #include "bi_quirks.h" |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 38 | #include "bi_print.h" |
| 39 | |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 40 | static const struct debug_named_value debug_options[] = { |
| 41 | {"msgs", BIFROST_DBG_MSGS, "Print debug messages"}, |
| 42 | {"shaders", BIFROST_DBG_SHADERS, "Dump shaders in NIR and MIR"}, |
| 43 | DEBUG_NAMED_VALUE_END |
| 44 | }; |
| 45 | |
| 46 | DEBUG_GET_ONCE_FLAGS_OPTION(bifrost_debug, "BIFROST_MESA_DEBUG", debug_options, 0) |
| 47 | |
| 48 | int bifrost_debug = 0; |
| 49 | |
| 50 | #define DBG(fmt, ...) \ |
| 51 | do { if (bifrost_debug & BIFROST_DBG_MSGS) \ |
| 52 | fprintf(stderr, "%s:%d: "fmt, \ |
| 53 | __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0) |
| 54 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 55 | static bi_block *emit_cf_list(bi_context *ctx, struct exec_list *list); |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 56 | static bi_instruction *bi_emit_branch(bi_context *ctx); |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 57 | |
| 58 | static void |
| 59 | emit_jump(bi_context *ctx, nir_jump_instr *instr) |
| 60 | { |
| 61 | bi_instruction *branch = bi_emit_branch(ctx); |
| 62 | |
| 63 | switch (instr->type) { |
| 64 | case nir_jump_break: |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 65 | branch->branch_target = ctx->break_block; |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 66 | break; |
| 67 | case nir_jump_continue: |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 68 | branch->branch_target = ctx->continue_block; |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 69 | break; |
| 70 | default: |
| 71 | unreachable("Unhandled jump type"); |
| 72 | } |
| 73 | |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 74 | pan_block_add_successor(&ctx->current_block->base, &branch->branch_target->base); |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 77 | static bi_instruction |
| 78 | bi_load(enum bi_class T, nir_intrinsic_instr *instr) |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 79 | { |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 80 | bi_instruction load = { |
| 81 | .type = T, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 82 | .vector_channels = instr->num_components, |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 83 | .src = { BIR_INDEX_CONSTANT }, |
Alyssa Rosenzweig | df69304 | 2020-04-14 20:09:00 -0400 | [diff] [blame] | 84 | .src_types = { nir_type_uint32 }, |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 85 | .constant = { .u64 = nir_intrinsic_base(instr) }, |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 88 | const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; |
| 89 | |
| 90 | if (info->has_dest) |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 91 | load.dest = pan_dest_index(&instr->dest); |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 92 | |
Jason Ekstrand | 0aa08ae | 2020-09-30 21:20:53 -0500 | [diff] [blame^] | 93 | if (info->has_dest && nir_intrinsic_has_dest_type(instr)) |
| 94 | load.dest_type = nir_intrinsic_dest_type(instr); |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 95 | |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 96 | nir_src *offset = nir_get_io_offset_src(instr); |
| 97 | |
| 98 | if (nir_src_is_const(*offset)) |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 99 | load.constant.u64 += nir_src_as_uint(*offset); |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 100 | else |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 101 | load.src[0] = pan_src_index(offset); |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 102 | |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 103 | return load; |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | bi_emit_ld_vary(bi_context *ctx, nir_intrinsic_instr *instr) |
| 108 | { |
| 109 | bi_instruction ins = bi_load(BI_LOAD_VAR, instr); |
| 110 | ins.load_vary.interp_mode = BIFROST_INTERP_DEFAULT; /* TODO */ |
| 111 | ins.load_vary.reuse = false; /* TODO */ |
| 112 | ins.load_vary.flat = instr->intrinsic != nir_intrinsic_load_interpolated_input; |
Alyssa Rosenzweig | 37f14c9 | 2020-03-18 11:55:10 -0400 | [diff] [blame] | 113 | ins.dest_type = nir_type_float | nir_dest_bit_size(instr->dest); |
Alyssa Rosenzweig | 8dd3a81 | 2020-07-31 18:48:27 -0400 | [diff] [blame] | 114 | ins.format = ins.dest_type; |
Alyssa Rosenzweig | 37f14c9 | 2020-03-18 11:55:10 -0400 | [diff] [blame] | 115 | |
| 116 | if (nir_src_is_const(*nir_get_io_offset_src(instr))) { |
| 117 | /* Zero it out for direct */ |
| 118 | ins.src[1] = BIR_INDEX_ZERO; |
| 119 | } else { |
| 120 | /* R61 contains sample mask stuff, TODO RA XXX */ |
| 121 | ins.src[1] = BIR_INDEX_REGISTER | 61; |
| 122 | } |
| 123 | |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 124 | bi_emit(ctx, ins); |
| 125 | } |
| 126 | |
| 127 | static void |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 128 | bi_emit_frag_out(bi_context *ctx, nir_intrinsic_instr *instr) |
| 129 | { |
| 130 | if (!ctx->emitted_atest) { |
| 131 | bi_instruction ins = { |
Alyssa Rosenzweig | b18d0ef | 2020-03-18 23:02:12 -0400 | [diff] [blame] | 132 | .type = BI_ATEST, |
| 133 | .src = { |
| 134 | BIR_INDEX_REGISTER | 60 /* TODO: RA */, |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 135 | pan_src_index(&instr->src[0]) |
Alyssa Rosenzweig | b18d0ef | 2020-03-18 23:02:12 -0400 | [diff] [blame] | 136 | }, |
| 137 | .src_types = { |
| 138 | nir_type_uint32, |
Jason Ekstrand | 0aa08ae | 2020-09-30 21:20:53 -0500 | [diff] [blame^] | 139 | nir_intrinsic_src_type(instr) |
Alyssa Rosenzweig | b18d0ef | 2020-03-18 23:02:12 -0400 | [diff] [blame] | 140 | }, |
| 141 | .swizzle = { |
| 142 | { 0 }, |
| 143 | { 3, 0 } /* swizzle out the alpha */ |
| 144 | }, |
| 145 | .dest = BIR_INDEX_REGISTER | 60 /* TODO: RA */, |
| 146 | .dest_type = nir_type_uint32, |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | bi_emit(ctx, ins); |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 150 | ctx->emitted_atest = true; |
| 151 | } |
| 152 | |
| 153 | bi_instruction blend = { |
| 154 | .type = BI_BLEND, |
| 155 | .blend_location = nir_intrinsic_base(instr), |
| 156 | .src = { |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 157 | pan_src_index(&instr->src[0]), |
Alyssa Rosenzweig | a4fb887 | 2020-03-18 23:12:23 -0400 | [diff] [blame] | 158 | BIR_INDEX_REGISTER | 60 /* Can this be arbitrary? */, |
Alyssa Rosenzweig | 3d63a47 | 2020-08-03 12:48:25 -0400 | [diff] [blame] | 159 | /* Blend descriptor */ |
| 160 | BIR_INDEX_PASS | BIFROST_SRC_CONST_LO, |
| 161 | BIR_INDEX_PASS | BIFROST_SRC_CONST_HI, |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 162 | }, |
Alyssa Rosenzweig | 116c541 | 2020-03-11 21:45:32 -0400 | [diff] [blame] | 163 | .src_types = { |
Jason Ekstrand | 0aa08ae | 2020-09-30 21:20:53 -0500 | [diff] [blame^] | 164 | nir_intrinsic_src_type(instr), |
Alyssa Rosenzweig | 3439c24 | 2020-04-09 23:04:41 -0400 | [diff] [blame] | 165 | nir_type_uint32 |
Alyssa Rosenzweig | 116c541 | 2020-03-11 21:45:32 -0400 | [diff] [blame] | 166 | }, |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 167 | .swizzle = { |
Alyssa Rosenzweig | 3439c24 | 2020-04-09 23:04:41 -0400 | [diff] [blame] | 168 | { 0, 1, 2, 3 }, |
| 169 | { 0 } |
Alyssa Rosenzweig | a4fb887 | 2020-03-18 23:12:23 -0400 | [diff] [blame] | 170 | }, |
| 171 | .dest = BIR_INDEX_REGISTER | 48 /* Looks like magic */, |
| 172 | .dest_type = nir_type_uint32, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 173 | .vector_channels = 4 |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 174 | }; |
| 175 | |
Alyssa Rosenzweig | 5a3088e | 2020-08-05 18:10:41 -0400 | [diff] [blame] | 176 | assert(blend.blend_location < 8); |
Alyssa Rosenzweig | 1a8f1a3 | 2020-04-23 19:26:01 -0400 | [diff] [blame] | 177 | assert(ctx->blend_types); |
Tomeu Vizoso | 3c98c45 | 2020-04-24 08:40:51 +0200 | [diff] [blame] | 178 | assert(blend.src_types[0]); |
Alyssa Rosenzweig | 1a8f1a3 | 2020-04-23 19:26:01 -0400 | [diff] [blame] | 179 | ctx->blend_types[blend.blend_location] = blend.src_types[0]; |
| 180 | |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 181 | bi_emit(ctx, blend); |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Alyssa Rosenzweig | 1097c69 | 2020-03-21 15:25:54 -0400 | [diff] [blame] | 184 | static bi_instruction |
| 185 | bi_load_with_r61(enum bi_class T, nir_intrinsic_instr *instr) |
| 186 | { |
| 187 | bi_instruction ld = bi_load(T, instr); |
| 188 | ld.src[1] = BIR_INDEX_REGISTER | 61; /* TODO: RA */ |
| 189 | ld.src[2] = BIR_INDEX_REGISTER | 62; |
Alyssa Rosenzweig | 1097c69 | 2020-03-21 15:25:54 -0400 | [diff] [blame] | 190 | ld.src_types[1] = nir_type_uint32; |
| 191 | ld.src_types[2] = nir_type_uint32; |
Jason Ekstrand | 0aa08ae | 2020-09-30 21:20:53 -0500 | [diff] [blame^] | 192 | ld.format = nir_intrinsic_dest_type(instr); |
Alyssa Rosenzweig | 1097c69 | 2020-03-21 15:25:54 -0400 | [diff] [blame] | 193 | return ld; |
| 194 | } |
| 195 | |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 196 | static void |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 197 | bi_emit_st_vary(bi_context *ctx, nir_intrinsic_instr *instr) |
| 198 | { |
Alyssa Rosenzweig | 1097c69 | 2020-03-21 15:25:54 -0400 | [diff] [blame] | 199 | bi_instruction address = bi_load_with_r61(BI_LOAD_VAR_ADDRESS, instr); |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 200 | address.dest = bi_make_temp(ctx); |
Alyssa Rosenzweig | 9458b01 | 2020-03-20 12:25:08 -0400 | [diff] [blame] | 201 | address.dest_type = nir_type_uint32; |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 202 | address.vector_channels = 3; |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 203 | |
Alyssa Rosenzweig | aba7f09 | 2020-04-14 20:20:37 -0400 | [diff] [blame] | 204 | unsigned nr = nir_intrinsic_src_components(instr, 0); |
| 205 | assert(nir_intrinsic_write_mask(instr) == ((1 << nr) - 1)); |
| 206 | |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 207 | bi_instruction st = { |
| 208 | .type = BI_STORE_VAR, |
| 209 | .src = { |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 210 | pan_src_index(&instr->src[0]), |
Alyssa Rosenzweig | 9458b01 | 2020-03-20 12:25:08 -0400 | [diff] [blame] | 211 | address.dest, address.dest, address.dest, |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 212 | }, |
Alyssa Rosenzweig | 116c541 | 2020-03-11 21:45:32 -0400 | [diff] [blame] | 213 | .src_types = { |
Alyssa Rosenzweig | 9458b01 | 2020-03-20 12:25:08 -0400 | [diff] [blame] | 214 | nir_type_uint32, |
| 215 | nir_type_uint32, nir_type_uint32, nir_type_uint32, |
Alyssa Rosenzweig | 116c541 | 2020-03-11 21:45:32 -0400 | [diff] [blame] | 216 | }, |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 217 | .swizzle = { |
Alyssa Rosenzweig | aba7f09 | 2020-04-14 20:20:37 -0400 | [diff] [blame] | 218 | { 0 }, |
Alyssa Rosenzweig | 9458b01 | 2020-03-20 12:25:08 -0400 | [diff] [blame] | 219 | { 0 }, { 1 }, { 2} |
Alyssa Rosenzweig | 9213b25 | 2020-03-20 12:38:53 -0400 | [diff] [blame] | 220 | }, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 221 | .vector_channels = nr, |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 222 | }; |
| 223 | |
Alyssa Rosenzweig | aba7f09 | 2020-04-14 20:20:37 -0400 | [diff] [blame] | 224 | for (unsigned i = 0; i < nr; ++i) |
| 225 | st.swizzle[0][i] = i; |
| 226 | |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 227 | bi_emit(ctx, address); |
| 228 | bi_emit(ctx, st); |
| 229 | } |
| 230 | |
| 231 | static void |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 232 | bi_emit_ld_uniform(bi_context *ctx, nir_intrinsic_instr *instr) |
| 233 | { |
Alyssa Rosenzweig | 69c66ff | 2020-03-09 19:52:56 -0400 | [diff] [blame] | 234 | bi_instruction ld = bi_load(BI_LOAD_UNIFORM, instr); |
| 235 | ld.src[1] = BIR_INDEX_ZERO; /* TODO: UBO index */ |
Alyssa Rosenzweig | 6f5b788 | 2020-07-31 17:29:50 -0400 | [diff] [blame] | 236 | ld.segment = BI_SEGMENT_UBO; |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 237 | |
| 238 | /* TODO: Indirect access, since we need to multiply by the element |
| 239 | * size. I believe we can get this lowering automatically via |
| 240 | * nir_lower_io (as mul instructions) with the proper options, but this |
| 241 | * is TODO */ |
| 242 | assert(ld.src[0] & BIR_INDEX_CONSTANT); |
| 243 | ld.constant.u64 += ctx->sysvals.sysval_count; |
| 244 | ld.constant.u64 *= 16; |
| 245 | |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 246 | bi_emit(ctx, ld); |
| 247 | } |
| 248 | |
| 249 | static void |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 250 | bi_emit_sysval(bi_context *ctx, nir_instr *instr, |
| 251 | unsigned nr_components, unsigned offset) |
| 252 | { |
| 253 | nir_dest nir_dest; |
| 254 | |
| 255 | /* Figure out which uniform this is */ |
| 256 | int sysval = panfrost_sysval_for_instr(instr, &nir_dest); |
| 257 | void *val = _mesa_hash_table_u64_search(ctx->sysvals.sysval_to_id, sysval); |
| 258 | |
| 259 | /* Sysvals are prefix uniforms */ |
| 260 | unsigned uniform = ((uintptr_t) val) - 1; |
| 261 | |
| 262 | /* Emit the read itself -- this is never indirect */ |
| 263 | |
| 264 | bi_instruction load = { |
| 265 | .type = BI_LOAD_UNIFORM, |
Alyssa Rosenzweig | 6f5b788 | 2020-07-31 17:29:50 -0400 | [diff] [blame] | 266 | .segment = BI_SEGMENT_UBO, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 267 | .vector_channels = nr_components, |
Alyssa Rosenzweig | 8bb1613 | 2020-03-20 11:38:21 -0400 | [diff] [blame] | 268 | .src = { BIR_INDEX_CONSTANT, BIR_INDEX_ZERO }, |
Alyssa Rosenzweig | df69304 | 2020-04-14 20:09:00 -0400 | [diff] [blame] | 269 | .src_types = { nir_type_uint32, nir_type_uint32 }, |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 270 | .constant = { (uniform * 16) + offset }, |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 271 | .dest = pan_dest_index(&nir_dest), |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 272 | .dest_type = nir_type_uint32, /* TODO */ |
| 273 | }; |
| 274 | |
| 275 | bi_emit(ctx, load); |
| 276 | } |
| 277 | |
Alyssa Rosenzweig | 47c84ee | 2020-05-01 14:55:04 -0400 | [diff] [blame] | 278 | /* gl_FragCoord.xy = u16_to_f32(R59.xy) + 0.5 |
| 279 | * gl_FragCoord.z = ld_vary(fragz) |
| 280 | * gl_FragCoord.w = ld_vary(fragw) |
| 281 | */ |
| 282 | |
| 283 | static void |
| 284 | bi_emit_ld_frag_coord(bi_context *ctx, nir_intrinsic_instr *instr) |
| 285 | { |
| 286 | /* Future proofing for mediump fragcoord at some point.. */ |
| 287 | nir_alu_type T = nir_type_float32; |
| 288 | |
| 289 | /* First, sketch a combine */ |
| 290 | bi_instruction combine = { |
| 291 | .type = BI_COMBINE, |
| 292 | .dest_type = nir_type_uint32, |
| 293 | .dest = pan_dest_index(&instr->dest), |
| 294 | .src_types = { T, T, T, T }, |
| 295 | }; |
| 296 | |
| 297 | /* Second, handle xy */ |
| 298 | for (unsigned i = 0; i < 2; ++i) { |
| 299 | bi_instruction conv = { |
| 300 | .type = BI_CONVERT, |
| 301 | .dest_type = T, |
| 302 | .dest = bi_make_temp(ctx), |
| 303 | .src = { |
| 304 | /* TODO: RA XXX */ |
| 305 | BIR_INDEX_REGISTER | 59 |
| 306 | }, |
| 307 | .src_types = { nir_type_uint16 }, |
| 308 | .swizzle = { { i } } |
| 309 | }; |
| 310 | |
| 311 | bi_instruction add = { |
| 312 | .type = BI_ADD, |
| 313 | .dest_type = T, |
| 314 | .dest = bi_make_temp(ctx), |
| 315 | .src = { conv.dest, BIR_INDEX_CONSTANT }, |
| 316 | .src_types = { T, T }, |
| 317 | }; |
| 318 | |
| 319 | float half = 0.5; |
| 320 | memcpy(&add.constant.u32, &half, sizeof(float)); |
| 321 | |
| 322 | bi_emit(ctx, conv); |
| 323 | bi_emit(ctx, add); |
| 324 | |
| 325 | combine.src[i] = add.dest; |
| 326 | } |
| 327 | |
| 328 | /* Third, zw */ |
| 329 | for (unsigned i = 0; i < 2; ++i) { |
| 330 | bi_instruction load = { |
| 331 | .type = BI_LOAD_VAR, |
| 332 | .load_vary = { |
| 333 | .interp_mode = BIFROST_INTERP_DEFAULT, |
| 334 | .reuse = false, |
| 335 | .flat = true |
| 336 | }, |
| 337 | .vector_channels = 1, |
| 338 | .dest_type = nir_type_float32, |
Alyssa Rosenzweig | 8dd3a81 | 2020-07-31 18:48:27 -0400 | [diff] [blame] | 339 | .format = nir_type_float32, |
Alyssa Rosenzweig | 47c84ee | 2020-05-01 14:55:04 -0400 | [diff] [blame] | 340 | .dest = bi_make_temp(ctx), |
Alyssa Rosenzweig | 2ff5387 | 2020-08-03 12:48:44 -0400 | [diff] [blame] | 341 | .src = { |
| 342 | BIR_INDEX_CONSTANT, |
| 343 | BIR_INDEX_PASS | BIFROST_SRC_CONST_LO |
| 344 | }, |
Alyssa Rosenzweig | 47c84ee | 2020-05-01 14:55:04 -0400 | [diff] [blame] | 345 | .src_types = { nir_type_uint32, nir_type_uint32 }, |
| 346 | .constant = { |
| 347 | .u32 = (i == 0) ? BIFROST_FRAGZ : BIFROST_FRAGW |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | bi_emit(ctx, load); |
| 352 | |
| 353 | combine.src[i + 2] = load.dest; |
| 354 | } |
| 355 | |
| 356 | /* Finally, emit the combine */ |
| 357 | bi_emit(ctx, combine); |
| 358 | } |
| 359 | |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 360 | static void |
Alyssa Rosenzweig | 7d867f7 | 2020-05-01 18:26:18 -0400 | [diff] [blame] | 361 | bi_emit_discard(bi_context *ctx, nir_intrinsic_instr *instr) |
| 362 | { |
| 363 | /* Goofy lowering */ |
| 364 | bi_instruction discard = { |
| 365 | .type = BI_DISCARD, |
| 366 | .cond = BI_COND_EQ, |
| 367 | .src_types = { nir_type_uint32, nir_type_uint32 }, |
| 368 | .src = { BIR_INDEX_ZERO, BIR_INDEX_ZERO }, |
| 369 | }; |
| 370 | |
| 371 | bi_emit(ctx, discard); |
| 372 | } |
| 373 | |
| 374 | static void |
Alyssa Rosenzweig | 8ab5c97 | 2020-05-01 18:36:42 -0400 | [diff] [blame] | 375 | bi_fuse_cond(bi_instruction *csel, nir_alu_src cond, |
| 376 | unsigned *constants_left, unsigned *constant_shift, |
| 377 | unsigned comps, bool float_only); |
| 378 | |
| 379 | static void |
Alyssa Rosenzweig | c9ab732 | 2020-05-01 18:24:11 -0400 | [diff] [blame] | 380 | bi_emit_discard_if(bi_context *ctx, nir_intrinsic_instr *instr) |
| 381 | { |
| 382 | nir_src cond = instr->src[0]; |
| 383 | nir_alu_type T = nir_type_uint | nir_src_bit_size(cond); |
| 384 | |
| 385 | bi_instruction discard = { |
| 386 | .type = BI_DISCARD, |
| 387 | .cond = BI_COND_NE, |
| 388 | .src_types = { T, T }, |
| 389 | .src = { |
| 390 | pan_src_index(&cond), |
| 391 | BIR_INDEX_ZERO |
| 392 | }, |
| 393 | }; |
| 394 | |
Alyssa Rosenzweig | 8ab5c97 | 2020-05-01 18:36:42 -0400 | [diff] [blame] | 395 | /* Try to fuse in the condition */ |
| 396 | unsigned constants_left = 1, constant_shift = 0; |
| 397 | |
| 398 | /* Scalar so no swizzle */ |
| 399 | nir_alu_src wrap = { |
| 400 | .src = instr->src[0] |
| 401 | }; |
| 402 | |
| 403 | /* May or may not succeed but we're optimistic */ |
| 404 | bi_fuse_cond(&discard, wrap, &constants_left, &constant_shift, 1, true); |
| 405 | |
Alyssa Rosenzweig | c9ab732 | 2020-05-01 18:24:11 -0400 | [diff] [blame] | 406 | bi_emit(ctx, discard); |
| 407 | } |
| 408 | |
| 409 | static void |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 410 | emit_intrinsic(bi_context *ctx, nir_intrinsic_instr *instr) |
| 411 | { |
| 412 | |
| 413 | switch (instr->intrinsic) { |
| 414 | case nir_intrinsic_load_barycentric_pixel: |
| 415 | /* stub */ |
| 416 | break; |
| 417 | case nir_intrinsic_load_interpolated_input: |
Alyssa Rosenzweig | 59b476e | 2020-03-06 09:33:52 -0500 | [diff] [blame] | 418 | case nir_intrinsic_load_input: |
| 419 | if (ctx->stage == MESA_SHADER_FRAGMENT) |
| 420 | bi_emit_ld_vary(ctx, instr); |
| 421 | else if (ctx->stage == MESA_SHADER_VERTEX) |
Alyssa Rosenzweig | 1097c69 | 2020-03-21 15:25:54 -0400 | [diff] [blame] | 422 | bi_emit(ctx, bi_load_with_r61(BI_LOAD_ATTR, instr)); |
Alyssa Rosenzweig | 59b476e | 2020-03-06 09:33:52 -0500 | [diff] [blame] | 423 | else { |
| 424 | unreachable("Unsupported shader stage"); |
| 425 | } |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 426 | break; |
Alyssa Rosenzweig | 59b476e | 2020-03-06 09:33:52 -0500 | [diff] [blame] | 427 | |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 428 | case nir_intrinsic_store_output: |
| 429 | if (ctx->stage == MESA_SHADER_FRAGMENT) |
| 430 | bi_emit_frag_out(ctx, instr); |
Alyssa Rosenzweig | 48910e8 | 2020-03-06 09:44:19 -0500 | [diff] [blame] | 431 | else if (ctx->stage == MESA_SHADER_VERTEX) |
| 432 | bi_emit_st_vary(ctx, instr); |
| 433 | else |
| 434 | unreachable("Unsupported shader stage"); |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 435 | break; |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 436 | |
| 437 | case nir_intrinsic_load_uniform: |
| 438 | bi_emit_ld_uniform(ctx, instr); |
| 439 | break; |
| 440 | |
Alyssa Rosenzweig | 47c84ee | 2020-05-01 14:55:04 -0400 | [diff] [blame] | 441 | case nir_intrinsic_load_frag_coord: |
| 442 | bi_emit_ld_frag_coord(ctx, instr); |
| 443 | break; |
| 444 | |
Alyssa Rosenzweig | 7d867f7 | 2020-05-01 18:26:18 -0400 | [diff] [blame] | 445 | case nir_intrinsic_discard: |
| 446 | bi_emit_discard(ctx, instr); |
| 447 | break; |
| 448 | |
Alyssa Rosenzweig | c9ab732 | 2020-05-01 18:24:11 -0400 | [diff] [blame] | 449 | case nir_intrinsic_discard_if: |
| 450 | bi_emit_discard_if(ctx, instr); |
| 451 | break; |
| 452 | |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 453 | case nir_intrinsic_load_ssbo_address: |
| 454 | bi_emit_sysval(ctx, &instr->instr, 1, 0); |
| 455 | break; |
| 456 | |
Jason Ekstrand | 9750164 | 2020-09-22 03:24:45 -0500 | [diff] [blame] | 457 | case nir_intrinsic_get_ssbo_size: |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 458 | bi_emit_sysval(ctx, &instr->instr, 1, 8); |
| 459 | break; |
| 460 | |
| 461 | case nir_intrinsic_load_viewport_scale: |
| 462 | case nir_intrinsic_load_viewport_offset: |
| 463 | case nir_intrinsic_load_num_work_groups: |
| 464 | case nir_intrinsic_load_sampler_lod_parameters_pan: |
| 465 | bi_emit_sysval(ctx, &instr->instr, 3, 0); |
| 466 | break; |
| 467 | |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 468 | default: |
Alyssa Rosenzweig | c488390 | 2020-05-01 14:13:10 -0400 | [diff] [blame] | 469 | unreachable("Unknown intrinsic"); |
Alyssa Rosenzweig | 0767182 | 2020-03-05 17:50:18 -0500 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static void |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 475 | emit_load_const(bi_context *ctx, nir_load_const_instr *instr) |
| 476 | { |
| 477 | /* Make sure we've been lowered */ |
Alyssa Rosenzweig | 0e73d87 | 2020-06-02 19:30:56 -0400 | [diff] [blame] | 478 | assert(instr->def.num_components <= (32 / instr->def.bit_size)); |
| 479 | |
| 480 | /* Accumulate all the channels of the constant, as if we did an |
| 481 | * implicit SEL over them */ |
| 482 | uint32_t acc = 0; |
| 483 | |
| 484 | for (unsigned i = 0; i < instr->def.num_components; ++i) { |
| 485 | unsigned v = nir_const_value_as_uint(instr->value[i], instr->def.bit_size); |
| 486 | acc |= (v << (i * instr->def.bit_size)); |
| 487 | } |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 488 | |
| 489 | bi_instruction move = { |
| 490 | .type = BI_MOV, |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 491 | .dest = pan_ssa_index(&instr->def), |
Alyssa Rosenzweig | 0e73d87 | 2020-06-02 19:30:56 -0400 | [diff] [blame] | 492 | .dest_type = nir_type_uint32, |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 493 | .src = { |
| 494 | BIR_INDEX_CONSTANT |
| 495 | }, |
Alyssa Rosenzweig | 02ad147 | 2020-03-30 20:54:51 -0400 | [diff] [blame] | 496 | .src_types = { |
Alyssa Rosenzweig | 0e73d87 | 2020-06-02 19:30:56 -0400 | [diff] [blame] | 497 | nir_type_uint32, |
Alyssa Rosenzweig | 02ad147 | 2020-03-30 20:54:51 -0400 | [diff] [blame] | 498 | }, |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 499 | .constant = { |
Alyssa Rosenzweig | 0e73d87 | 2020-06-02 19:30:56 -0400 | [diff] [blame] | 500 | .u32 = acc |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 501 | } |
| 502 | }; |
| 503 | |
| 504 | bi_emit(ctx, move); |
| 505 | } |
| 506 | |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 507 | #define BI_CASE_CMP(op) \ |
| 508 | case op##8: \ |
| 509 | case op##16: \ |
| 510 | case op##32: \ |
| 511 | |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 512 | static enum bi_class |
| 513 | bi_class_for_nir_alu(nir_op op) |
| 514 | { |
| 515 | switch (op) { |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 516 | case nir_op_fadd: |
Alyssa Rosenzweig | acab788 | 2020-03-10 07:56:14 -0400 | [diff] [blame] | 517 | case nir_op_fsub: |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 518 | return BI_ADD; |
Alyssa Rosenzweig | cf3c356 | 2020-05-04 14:04:35 -0400 | [diff] [blame] | 519 | |
| 520 | case nir_op_iadd: |
Alyssa Rosenzweig | 55f0d81 | 2020-03-10 08:03:20 -0400 | [diff] [blame] | 521 | case nir_op_isub: |
Alyssa Rosenzweig | 1a94dae | 2020-05-04 14:00:13 -0400 | [diff] [blame] | 522 | return BI_IMATH; |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 523 | |
Chris Forbes | a0a7087 | 2020-07-26 15:54:14 -0700 | [diff] [blame] | 524 | case nir_op_imul: |
| 525 | return BI_IMUL; |
| 526 | |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 527 | case nir_op_iand: |
| 528 | case nir_op_ior: |
| 529 | case nir_op_ixor: |
Chris Forbes | 539ea08 | 2020-07-26 11:37:42 -0700 | [diff] [blame] | 530 | case nir_op_inot: |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 531 | case nir_op_ishl: |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 532 | return BI_BITWISE; |
| 533 | |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 534 | BI_CASE_CMP(nir_op_flt) |
| 535 | BI_CASE_CMP(nir_op_fge) |
| 536 | BI_CASE_CMP(nir_op_feq) |
Karol Herbst | e5899c1 | 2020-08-18 19:51:57 +0200 | [diff] [blame] | 537 | BI_CASE_CMP(nir_op_fneu) |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 538 | BI_CASE_CMP(nir_op_ilt) |
| 539 | BI_CASE_CMP(nir_op_ige) |
| 540 | BI_CASE_CMP(nir_op_ieq) |
| 541 | BI_CASE_CMP(nir_op_ine) |
Chris Forbes | 718d444 | 2020-07-26 12:41:17 -0700 | [diff] [blame] | 542 | BI_CASE_CMP(nir_op_uge) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 543 | return BI_CMP; |
| 544 | |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 545 | case nir_op_b8csel: |
| 546 | case nir_op_b16csel: |
| 547 | case nir_op_b32csel: |
Alyssa Rosenzweig | d382355 | 2020-03-10 08:32:56 -0400 | [diff] [blame] | 548 | return BI_CSEL; |
| 549 | |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 550 | case nir_op_i2i8: |
| 551 | case nir_op_i2i16: |
| 552 | case nir_op_i2i32: |
| 553 | case nir_op_i2i64: |
| 554 | case nir_op_u2u8: |
| 555 | case nir_op_u2u16: |
| 556 | case nir_op_u2u32: |
| 557 | case nir_op_u2u64: |
| 558 | case nir_op_f2i16: |
| 559 | case nir_op_f2i32: |
| 560 | case nir_op_f2i64: |
| 561 | case nir_op_f2u16: |
| 562 | case nir_op_f2u32: |
| 563 | case nir_op_f2u64: |
| 564 | case nir_op_i2f16: |
| 565 | case nir_op_i2f32: |
| 566 | case nir_op_i2f64: |
| 567 | case nir_op_u2f16: |
| 568 | case nir_op_u2f32: |
| 569 | case nir_op_u2f64: |
Alyssa Rosenzweig | aa77d81 | 2020-03-27 14:40:04 -0400 | [diff] [blame] | 570 | case nir_op_f2f16: |
| 571 | case nir_op_f2f32: |
| 572 | case nir_op_f2f64: |
| 573 | case nir_op_f2fmp: |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 574 | return BI_CONVERT; |
| 575 | |
Alyssa Rosenzweig | e0a51d5 | 2020-03-22 17:31:23 -0400 | [diff] [blame] | 576 | case nir_op_vec2: |
| 577 | case nir_op_vec3: |
| 578 | case nir_op_vec4: |
| 579 | return BI_COMBINE; |
| 580 | |
| 581 | case nir_op_vec8: |
| 582 | case nir_op_vec16: |
| 583 | unreachable("should've been lowered"); |
| 584 | |
Alyssa Rosenzweig | f6d96aa | 2020-03-11 15:15:41 -0400 | [diff] [blame] | 585 | case nir_op_ffma: |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 586 | case nir_op_fmul: |
| 587 | return BI_FMA; |
| 588 | |
| 589 | case nir_op_imin: |
| 590 | case nir_op_imax: |
| 591 | case nir_op_umin: |
| 592 | case nir_op_umax: |
| 593 | case nir_op_fmin: |
| 594 | case nir_op_fmax: |
| 595 | return BI_MINMAX; |
| 596 | |
Alyssa Rosenzweig | 5a5896c | 2020-03-09 21:02:51 -0400 | [diff] [blame] | 597 | case nir_op_fsat: |
Alyssa Rosenzweig | 1216a63 | 2020-03-10 07:52:24 -0400 | [diff] [blame] | 598 | case nir_op_fneg: |
| 599 | case nir_op_fabs: |
Alyssa Rosenzweig | 6b7077e | 2020-03-19 16:58:48 -0400 | [diff] [blame] | 600 | return BI_FMOV; |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 601 | case nir_op_mov: |
| 602 | return BI_MOV; |
| 603 | |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 604 | case nir_op_fround_even: |
| 605 | case nir_op_fceil: |
| 606 | case nir_op_ffloor: |
| 607 | case nir_op_ftrunc: |
| 608 | return BI_ROUND; |
| 609 | |
Alyssa Rosenzweig | 8ed79c9 | 2020-03-09 21:20:20 -0400 | [diff] [blame] | 610 | case nir_op_frcp: |
| 611 | case nir_op_frsq: |
Chris Forbes | 1882b1e | 2020-07-27 11:51:31 -0700 | [diff] [blame] | 612 | case nir_op_iabs: |
Alyssa Rosenzweig | 8ed79c9 | 2020-03-09 21:20:20 -0400 | [diff] [blame] | 613 | return BI_SPECIAL; |
| 614 | |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 615 | default: |
| 616 | unreachable("Unknown ALU op"); |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
Alyssa Rosenzweig | 5a02c871 | 2020-03-21 18:12:31 -0400 | [diff] [blame] | 620 | /* Gets a bi_cond for a given NIR comparison opcode. In soft mode, it will |
| 621 | * return BI_COND_ALWAYS as a sentinel if it fails to do so (when used for |
| 622 | * optimizations). Otherwise it will bail (when used for primary code |
| 623 | * generation). */ |
| 624 | |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 625 | static enum bi_cond |
Alyssa Rosenzweig | 5a02c871 | 2020-03-21 18:12:31 -0400 | [diff] [blame] | 626 | bi_cond_for_nir(nir_op op, bool soft) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 627 | { |
| 628 | switch (op) { |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 629 | BI_CASE_CMP(nir_op_flt) |
| 630 | BI_CASE_CMP(nir_op_ilt) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 631 | return BI_COND_LT; |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 632 | |
| 633 | BI_CASE_CMP(nir_op_fge) |
| 634 | BI_CASE_CMP(nir_op_ige) |
Chris Forbes | 718d444 | 2020-07-26 12:41:17 -0700 | [diff] [blame] | 635 | BI_CASE_CMP(nir_op_uge) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 636 | return BI_COND_GE; |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 637 | |
| 638 | BI_CASE_CMP(nir_op_feq) |
| 639 | BI_CASE_CMP(nir_op_ieq) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 640 | return BI_COND_EQ; |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 641 | |
Karol Herbst | e5899c1 | 2020-08-18 19:51:57 +0200 | [diff] [blame] | 642 | BI_CASE_CMP(nir_op_fneu) |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 643 | BI_CASE_CMP(nir_op_ine) |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 644 | return BI_COND_NE; |
| 645 | default: |
Alyssa Rosenzweig | 5a02c871 | 2020-03-21 18:12:31 -0400 | [diff] [blame] | 646 | if (soft) |
| 647 | return BI_COND_ALWAYS; |
| 648 | else |
| 649 | unreachable("Invalid compare"); |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 653 | static void |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 654 | bi_copy_src(bi_instruction *alu, nir_alu_instr *instr, unsigned i, unsigned to, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 655 | unsigned *constants_left, unsigned *constant_shift, unsigned comps) |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 656 | { |
| 657 | unsigned bits = nir_src_bit_size(instr->src[i].src); |
| 658 | unsigned dest_bits = nir_dest_bit_size(instr->dest.dest); |
| 659 | |
| 660 | alu->src_types[to] = nir_op_infos[instr->op].input_types[i] |
| 661 | | bits; |
| 662 | |
| 663 | /* Try to inline a constant */ |
| 664 | if (nir_src_is_const(instr->src[i].src) && *constants_left && (dest_bits == bits)) { |
Alyssa Rosenzweig | d772bf0 | 2020-04-15 10:39:42 -0400 | [diff] [blame] | 665 | uint64_t mask = (1ull << dest_bits) - 1; |
| 666 | uint64_t cons = nir_src_as_uint(instr->src[i].src); |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 667 | |
Alyssa Rosenzweig | d772bf0 | 2020-04-15 10:39:42 -0400 | [diff] [blame] | 668 | /* Try to reuse a constant */ |
| 669 | for (unsigned i = 0; i < (*constant_shift); i += dest_bits) { |
| 670 | if (((alu->constant.u64 >> i) & mask) == cons) { |
| 671 | alu->src[to] = BIR_INDEX_CONSTANT | i; |
| 672 | return; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | alu->constant.u64 |= cons << *constant_shift; |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 677 | alu->src[to] = BIR_INDEX_CONSTANT | (*constant_shift); |
| 678 | --(*constants_left); |
Alyssa Rosenzweig | 4d0f941 | 2020-04-17 15:52:03 -0400 | [diff] [blame] | 679 | (*constant_shift) += MAX2(dest_bits, 32); /* lo/hi */ |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 683 | alu->src[to] = pan_src_index(&instr->src[i].src); |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 684 | |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 685 | /* Copy swizzle for all vectored components, replicating last component |
| 686 | * to fill undersized */ |
| 687 | |
| 688 | unsigned vec = alu->type == BI_COMBINE ? 1 : |
| 689 | MAX2(1, 32 / dest_bits); |
| 690 | |
| 691 | for (unsigned j = 0; j < vec; ++j) |
| 692 | alu->swizzle[to][j] = instr->src[i].swizzle[MIN2(j, comps - 1)]; |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static void |
Alyssa Rosenzweig | 201a11a | 2020-05-01 18:31:22 -0400 | [diff] [blame] | 696 | bi_fuse_cond(bi_instruction *csel, nir_alu_src cond, |
| 697 | unsigned *constants_left, unsigned *constant_shift, |
| 698 | unsigned comps, bool float_only) |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 699 | { |
| 700 | /* Bail for vector weirdness */ |
| 701 | if (cond.swizzle[0] != 0) |
| 702 | return; |
| 703 | |
| 704 | if (!cond.src.is_ssa) |
| 705 | return; |
| 706 | |
| 707 | nir_ssa_def *def = cond.src.ssa; |
| 708 | nir_instr *parent = def->parent_instr; |
| 709 | |
| 710 | if (parent->type != nir_instr_type_alu) |
| 711 | return; |
| 712 | |
| 713 | nir_alu_instr *alu = nir_instr_as_alu(parent); |
| 714 | |
| 715 | /* Try to match a condition */ |
| 716 | enum bi_cond bcond = bi_cond_for_nir(alu->op, true); |
| 717 | |
| 718 | if (bcond == BI_COND_ALWAYS) |
| 719 | return; |
| 720 | |
Alyssa Rosenzweig | 201a11a | 2020-05-01 18:31:22 -0400 | [diff] [blame] | 721 | /* Some instructions can't compare ints */ |
| 722 | if (float_only) { |
| 723 | nir_alu_type T = nir_op_infos[alu->op].input_types[0]; |
| 724 | T = nir_alu_type_get_base_type(T); |
| 725 | |
| 726 | if (T != nir_type_float) |
| 727 | return; |
| 728 | } |
| 729 | |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 730 | /* We found one, let's fuse it in */ |
Alyssa Rosenzweig | 95fc71e | 2020-04-27 14:15:57 -0400 | [diff] [blame] | 731 | csel->cond = bcond; |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 732 | bi_copy_src(csel, alu, 0, 0, constants_left, constant_shift, comps); |
| 733 | bi_copy_src(csel, alu, 1, 1, constants_left, constant_shift, comps); |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 737 | emit_alu(bi_context *ctx, nir_alu_instr *instr) |
| 738 | { |
Alyssa Rosenzweig | 8e52206 | 2020-04-14 18:52:21 -0400 | [diff] [blame] | 739 | /* Try some special functions */ |
| 740 | switch (instr->op) { |
| 741 | case nir_op_fexp2: |
| 742 | bi_emit_fexp2(ctx, instr); |
| 743 | return; |
Alyssa Rosenzweig | 031ad0e | 2020-04-14 19:50:24 -0400 | [diff] [blame] | 744 | case nir_op_flog2: |
| 745 | bi_emit_flog2(ctx, instr); |
| 746 | return; |
Alyssa Rosenzweig | 8e52206 | 2020-04-14 18:52:21 -0400 | [diff] [blame] | 747 | default: |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | /* Otherwise, assume it's something we can handle normally */ |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 752 | bi_instruction alu = { |
| 753 | .type = bi_class_for_nir_alu(instr->op), |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 754 | .dest = pan_dest_index(&instr->dest.dest), |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 755 | .dest_type = nir_op_infos[instr->op].output_type |
| 756 | | nir_dest_bit_size(instr->dest.dest), |
| 757 | }; |
| 758 | |
Alyssa Rosenzweig | 8ed79c9 | 2020-03-09 21:20:20 -0400 | [diff] [blame] | 759 | /* TODO: Implement lowering of special functions for older Bifrost */ |
| 760 | assert((alu.type != BI_SPECIAL) || !(ctx->quirks & BIFROST_NO_FAST_OP)); |
| 761 | |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 762 | unsigned comps = nir_dest_num_components(instr->dest.dest); |
Alyssa Rosenzweig | e0a51d5 | 2020-03-22 17:31:23 -0400 | [diff] [blame] | 763 | |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 764 | if (alu.type != BI_COMBINE) |
| 765 | assert(comps <= MAX2(1, 32 / comps)); |
Alyssa Rosenzweig | e0a51d5 | 2020-03-22 17:31:23 -0400 | [diff] [blame] | 766 | |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 767 | if (!instr->dest.dest.is_ssa) { |
| 768 | for (unsigned i = 0; i < comps; ++i) |
| 769 | assert(instr->dest.write_mask); |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Alyssa Rosenzweig | 48e50ef | 2020-03-09 20:32:00 -0400 | [diff] [blame] | 772 | /* We inline constants as we go. This tracks how many constants have |
| 773 | * been inlined, since we're limited to 64-bits of constants per |
| 774 | * instruction */ |
| 775 | |
| 776 | unsigned dest_bits = nir_dest_bit_size(instr->dest.dest); |
| 777 | unsigned constants_left = (64 / dest_bits); |
| 778 | unsigned constant_shift = 0; |
| 779 | |
Alyssa Rosenzweig | 02ad147 | 2020-03-30 20:54:51 -0400 | [diff] [blame] | 780 | if (alu.type == BI_COMBINE) |
| 781 | constants_left = 0; |
| 782 | |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 783 | /* Copy sources */ |
| 784 | |
| 785 | unsigned num_inputs = nir_op_infos[instr->op].num_inputs; |
| 786 | assert(num_inputs <= ARRAY_SIZE(alu.src)); |
| 787 | |
Alyssa Rosenzweig | 8eefb27 | 2020-04-05 19:22:01 -0400 | [diff] [blame] | 788 | for (unsigned i = 0; i < num_inputs; ++i) { |
| 789 | unsigned f = 0; |
| 790 | |
| 791 | if (i && alu.type == BI_CSEL) |
| 792 | f++; |
| 793 | |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 794 | bi_copy_src(&alu, instr, i, i + f, &constants_left, &constant_shift, comps); |
Alyssa Rosenzweig | 8eefb27 | 2020-04-05 19:22:01 -0400 | [diff] [blame] | 795 | } |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 796 | |
| 797 | /* Op-specific fixup */ |
| 798 | switch (instr->op) { |
| 799 | case nir_op_fmul: |
| 800 | alu.src[2] = BIR_INDEX_ZERO; /* FMA */ |
Alyssa Rosenzweig | b5148b6 | 2020-03-27 15:53:12 -0400 | [diff] [blame] | 801 | alu.src_types[2] = alu.src_types[1]; |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 802 | break; |
Alyssa Rosenzweig | 5a5896c | 2020-03-09 21:02:51 -0400 | [diff] [blame] | 803 | case nir_op_fsat: |
Alyssa Rosenzweig | 6b7077e | 2020-03-19 16:58:48 -0400 | [diff] [blame] | 804 | alu.outmod = BIFROST_SAT; /* FMOV */ |
Alyssa Rosenzweig | 5a5896c | 2020-03-09 21:02:51 -0400 | [diff] [blame] | 805 | break; |
Alyssa Rosenzweig | 1216a63 | 2020-03-10 07:52:24 -0400 | [diff] [blame] | 806 | case nir_op_fneg: |
Alyssa Rosenzweig | 6b7077e | 2020-03-19 16:58:48 -0400 | [diff] [blame] | 807 | alu.src_neg[0] = true; /* FMOV */ |
Alyssa Rosenzweig | 1216a63 | 2020-03-10 07:52:24 -0400 | [diff] [blame] | 808 | break; |
| 809 | case nir_op_fabs: |
Alyssa Rosenzweig | 6b7077e | 2020-03-19 16:58:48 -0400 | [diff] [blame] | 810 | alu.src_abs[0] = true; /* FMOV */ |
Alyssa Rosenzweig | 1216a63 | 2020-03-10 07:52:24 -0400 | [diff] [blame] | 811 | break; |
Alyssa Rosenzweig | acab788 | 2020-03-10 07:56:14 -0400 | [diff] [blame] | 812 | case nir_op_fsub: |
Alyssa Rosenzweig | 6b7077e | 2020-03-19 16:58:48 -0400 | [diff] [blame] | 813 | alu.src_neg[1] = true; /* FADD */ |
Alyssa Rosenzweig | acab788 | 2020-03-10 07:56:14 -0400 | [diff] [blame] | 814 | break; |
Alyssa Rosenzweig | cf3c356 | 2020-05-04 14:04:35 -0400 | [diff] [blame] | 815 | case nir_op_iadd: |
| 816 | alu.op.imath = BI_IMATH_ADD; |
Alyssa Rosenzweig | 33710ff | 2020-07-31 16:47:05 -0400 | [diff] [blame] | 817 | /* Carry */ |
| 818 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | cf3c356 | 2020-05-04 14:04:35 -0400 | [diff] [blame] | 819 | break; |
| 820 | case nir_op_isub: |
| 821 | alu.op.imath = BI_IMATH_SUB; |
Alyssa Rosenzweig | 33710ff | 2020-07-31 16:47:05 -0400 | [diff] [blame] | 822 | /* Borrow */ |
| 823 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | cf3c356 | 2020-05-04 14:04:35 -0400 | [diff] [blame] | 824 | break; |
Chris Forbes | 1882b1e | 2020-07-27 11:51:31 -0700 | [diff] [blame] | 825 | case nir_op_iabs: |
| 826 | alu.op.special = BI_SPECIAL_IABS; |
| 827 | break; |
Chris Forbes | 539ea08 | 2020-07-26 11:37:42 -0700 | [diff] [blame] | 828 | case nir_op_inot: |
Alyssa Rosenzweig | d2158a5 | 2020-09-09 17:46:58 -0400 | [diff] [blame] | 829 | /* no dedicated bitwise not, but we can invert sources. convert to ~(a | 0) */ |
Chris Forbes | 539ea08 | 2020-07-26 11:37:42 -0700 | [diff] [blame] | 830 | alu.op.bitwise = BI_BITWISE_OR; |
Alyssa Rosenzweig | d2158a5 | 2020-09-09 17:46:58 -0400 | [diff] [blame] | 831 | alu.bitwise.dest_invert = true; |
Chris Forbes | 539ea08 | 2020-07-26 11:37:42 -0700 | [diff] [blame] | 832 | alu.src[1] = BIR_INDEX_ZERO; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 833 | /* zero shift */ |
| 834 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | 08b105d | 2020-09-09 17:40:22 -0400 | [diff] [blame] | 835 | alu.src_types[2] = nir_type_uint8; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 836 | break; |
| 837 | case nir_op_ishl: |
| 838 | alu.op.bitwise = BI_BITWISE_OR; |
| 839 | /* move src1 to src2 and replace with zero. underlying op is (src0 << src2) | src1 */ |
| 840 | alu.src[2] = alu.src[1]; |
Alyssa Rosenzweig | 08b105d | 2020-09-09 17:40:22 -0400 | [diff] [blame] | 841 | alu.src_types[2] = nir_type_uint8; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 842 | alu.src[1] = BIR_INDEX_ZERO; |
Chris Forbes | 539ea08 | 2020-07-26 11:37:42 -0700 | [diff] [blame] | 843 | break; |
Chris Forbes | a0a7087 | 2020-07-26 15:54:14 -0700 | [diff] [blame] | 844 | case nir_op_imul: |
| 845 | alu.op.imul = BI_IMUL_IMUL; |
| 846 | break; |
Alyssa Rosenzweig | c862234 | 2020-03-09 21:10:41 -0400 | [diff] [blame] | 847 | case nir_op_fmax: |
| 848 | case nir_op_imax: |
| 849 | case nir_op_umax: |
| 850 | alu.op.minmax = BI_MINMAX_MAX; /* MINMAX */ |
| 851 | break; |
Alyssa Rosenzweig | 8ed79c9 | 2020-03-09 21:20:20 -0400 | [diff] [blame] | 852 | case nir_op_frcp: |
| 853 | alu.op.special = BI_SPECIAL_FRCP; |
| 854 | break; |
| 855 | case nir_op_frsq: |
| 856 | alu.op.special = BI_SPECIAL_FRSQ; |
| 857 | break; |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 858 | BI_CASE_CMP(nir_op_flt) |
| 859 | BI_CASE_CMP(nir_op_ilt) |
| 860 | BI_CASE_CMP(nir_op_fge) |
| 861 | BI_CASE_CMP(nir_op_ige) |
| 862 | BI_CASE_CMP(nir_op_feq) |
| 863 | BI_CASE_CMP(nir_op_ieq) |
Karol Herbst | e5899c1 | 2020-08-18 19:51:57 +0200 | [diff] [blame] | 864 | BI_CASE_CMP(nir_op_fneu) |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 865 | BI_CASE_CMP(nir_op_ine) |
Chris Forbes | 718d444 | 2020-07-26 12:41:17 -0700 | [diff] [blame] | 866 | BI_CASE_CMP(nir_op_uge) |
Alyssa Rosenzweig | 95fc71e | 2020-04-27 14:15:57 -0400 | [diff] [blame] | 867 | alu.cond = bi_cond_for_nir(instr->op, false); |
Alyssa Rosenzweig | 0541350 | 2020-03-10 08:21:35 -0400 | [diff] [blame] | 868 | break; |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 869 | case nir_op_fround_even: |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 870 | alu.roundmode = BIFROST_RTE; |
| 871 | break; |
| 872 | case nir_op_fceil: |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 873 | alu.roundmode = BIFROST_RTP; |
| 874 | break; |
| 875 | case nir_op_ffloor: |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 876 | alu.roundmode = BIFROST_RTN; |
| 877 | break; |
| 878 | case nir_op_ftrunc: |
Alyssa Rosenzweig | f81b67b | 2020-03-27 20:28:09 -0400 | [diff] [blame] | 879 | alu.roundmode = BIFROST_RTZ; |
| 880 | break; |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 881 | case nir_op_iand: |
| 882 | alu.op.bitwise = BI_BITWISE_AND; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 883 | /* zero shift */ |
| 884 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | 08b105d | 2020-09-09 17:40:22 -0400 | [diff] [blame] | 885 | alu.src_types[2] = nir_type_uint8; |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 886 | break; |
| 887 | case nir_op_ior: |
| 888 | alu.op.bitwise = BI_BITWISE_OR; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 889 | /* zero shift */ |
| 890 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | 08b105d | 2020-09-09 17:40:22 -0400 | [diff] [blame] | 891 | alu.src_types[2] = nir_type_uint8; |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 892 | break; |
| 893 | case nir_op_ixor: |
| 894 | alu.op.bitwise = BI_BITWISE_XOR; |
Chris Forbes | 946ff9b | 2020-07-26 12:18:54 -0700 | [diff] [blame] | 895 | /* zero shift */ |
| 896 | alu.src[2] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | 08b105d | 2020-09-09 17:40:22 -0400 | [diff] [blame] | 897 | alu.src_types[2] = nir_type_uint8; |
Alyssa Rosenzweig | a077da6 | 2020-04-28 14:36:17 -0400 | [diff] [blame] | 898 | break; |
Chris Forbes | f6aa071 | 2020-07-04 15:26:42 -0700 | [diff] [blame] | 899 | case nir_op_f2i32: |
| 900 | alu.roundmode = BIFROST_RTZ; |
| 901 | break; |
Alyssa Rosenzweig | 1b09c69 | 2020-06-02 19:29:25 -0400 | [diff] [blame] | 902 | |
| 903 | case nir_op_f2f16: |
| 904 | case nir_op_i2i16: |
| 905 | case nir_op_u2u16: { |
| 906 | if (nir_src_bit_size(instr->src[0].src) != 32) |
| 907 | break; |
| 908 | |
| 909 | /* Should have been const folded */ |
| 910 | assert(!nir_src_is_const(instr->src[0].src)); |
| 911 | |
| 912 | alu.src_types[1] = alu.src_types[0]; |
| 913 | alu.src[1] = alu.src[0]; |
| 914 | |
| 915 | unsigned last = nir_dest_num_components(instr->dest.dest) - 1; |
| 916 | assert(last <= 1); |
| 917 | |
| 918 | alu.swizzle[1][0] = instr->src[0].swizzle[last]; |
| 919 | break; |
| 920 | } |
| 921 | |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 922 | default: |
| 923 | break; |
| 924 | } |
| 925 | |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 926 | if (alu.type == BI_CSEL) { |
Alyssa Rosenzweig | 5cdc31a | 2020-03-21 21:19:14 -0400 | [diff] [blame] | 927 | /* Default to csel3 */ |
Alyssa Rosenzweig | 95fc71e | 2020-04-27 14:15:57 -0400 | [diff] [blame] | 928 | alu.cond = BI_COND_NE; |
Alyssa Rosenzweig | 8eefb27 | 2020-04-05 19:22:01 -0400 | [diff] [blame] | 929 | alu.src[1] = BIR_INDEX_ZERO; |
| 930 | alu.src_types[1] = alu.src_types[0]; |
Alyssa Rosenzweig | 5cdc31a | 2020-03-21 21:19:14 -0400 | [diff] [blame] | 931 | |
Alyssa Rosenzweig | 31a41bb | 2020-05-01 17:34:47 -0400 | [diff] [blame] | 932 | /* TODO: Reenable cond fusing when we can split up registers |
| 933 | * when scheduling */ |
| 934 | #if 0 |
Alyssa Rosenzweig | 201a11a | 2020-05-01 18:31:22 -0400 | [diff] [blame] | 935 | bi_fuse_cond(&alu, instr->src[0], |
| 936 | &constants_left, &constant_shift, comps, false); |
Alyssa Rosenzweig | 31a41bb | 2020-05-01 17:34:47 -0400 | [diff] [blame] | 937 | #endif |
Alyssa Rosenzweig | 3f786ed | 2020-03-21 18:13:49 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Alyssa Rosenzweig | 929baf3 | 2020-03-09 20:19:51 -0400 | [diff] [blame] | 940 | bi_emit(ctx, alu); |
| 941 | } |
| 942 | |
Alyssa Rosenzweig | 0769036a | 2020-04-21 12:15:29 -0400 | [diff] [blame] | 943 | /* TEX_COMPACT instructions assume normal 2D f32 operation but are more |
| 944 | * space-efficient and with simpler RA/scheduling requirements*/ |
| 945 | |
| 946 | static void |
| 947 | emit_tex_compact(bi_context *ctx, nir_tex_instr *instr) |
| 948 | { |
Alyssa Rosenzweig | cd5fe3b | 2020-04-21 13:00:44 -0400 | [diff] [blame] | 949 | bi_instruction tex = { |
| 950 | .type = BI_TEX, |
| 951 | .op = { .texture = BI_TEX_COMPACT }, |
Alyssa Rosenzweig | 5f35cda | 2020-04-30 16:10:55 -0400 | [diff] [blame] | 952 | .texture = { |
| 953 | .texture_index = instr->texture_index, |
| 954 | .sampler_index = instr->sampler_index, |
Alyssa Rosenzweig | 67d8956 | 2020-08-03 12:47:57 -0400 | [diff] [blame] | 955 | .compute_lod = instr->op == nir_texop_tex, |
Alyssa Rosenzweig | 5f35cda | 2020-04-30 16:10:55 -0400 | [diff] [blame] | 956 | }, |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 957 | .dest = pan_dest_index(&instr->dest), |
Alyssa Rosenzweig | cd5fe3b | 2020-04-21 13:00:44 -0400 | [diff] [blame] | 958 | .dest_type = instr->dest_type, |
| 959 | .src_types = { nir_type_float32, nir_type_float32 }, |
Alyssa Rosenzweig | b2c6cf2 | 2020-04-24 17:20:28 -0400 | [diff] [blame] | 960 | .vector_channels = 4 |
Alyssa Rosenzweig | cd5fe3b | 2020-04-21 13:00:44 -0400 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | for (unsigned i = 0; i < instr->num_srcs; ++i) { |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 964 | int index = pan_src_index(&instr->src[i].src); |
Alyssa Rosenzweig | 6650fa2 | 2020-05-27 11:43:37 -0400 | [diff] [blame] | 965 | |
| 966 | /* We were checked ahead-of-time */ |
| 967 | if (instr->src[i].src_type == nir_tex_src_lod) |
| 968 | continue; |
| 969 | |
Alyssa Rosenzweig | cd5fe3b | 2020-04-21 13:00:44 -0400 | [diff] [blame] | 970 | assert (instr->src[i].src_type == nir_tex_src_coord); |
| 971 | |
| 972 | tex.src[0] = index; |
| 973 | tex.src[1] = index; |
| 974 | tex.swizzle[0][0] = 0; |
| 975 | tex.swizzle[1][0] = 1; |
| 976 | } |
| 977 | |
| 978 | bi_emit(ctx, tex); |
Alyssa Rosenzweig | 0769036a | 2020-04-21 12:15:29 -0400 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void |
| 982 | emit_tex_full(bi_context *ctx, nir_tex_instr *instr) |
| 983 | { |
| 984 | unreachable("stub"); |
| 985 | } |
| 986 | |
Alyssa Rosenzweig | 731dfc6 | 2020-05-27 11:41:42 -0400 | [diff] [blame] | 987 | /* Normal textures ops are tex for frag shaders and txl for vertex shaders with |
| 988 | * lod a constant 0. Anything else needs a full texture op. */ |
| 989 | |
| 990 | static bool |
| 991 | bi_is_normal_tex(gl_shader_stage stage, nir_tex_instr *instr) |
| 992 | { |
| 993 | if (stage == MESA_SHADER_FRAGMENT) |
| 994 | return instr->op == nir_texop_tex; |
| 995 | |
| 996 | if (instr->op != nir_texop_txl) |
| 997 | return false; |
| 998 | |
| 999 | for (unsigned i = 0; i < instr->num_srcs; ++i) { |
| 1000 | if (instr->src[i].src_type != nir_tex_src_lod) |
| 1001 | continue; |
| 1002 | |
| 1003 | nir_src src = instr->src[i].src; |
| 1004 | |
| 1005 | if (!nir_src_is_const(src)) |
| 1006 | continue; |
| 1007 | |
| 1008 | if (nir_src_as_uint(src) != 0) |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Alyssa Rosenzweig | 0769036a | 2020-04-21 12:15:29 -0400 | [diff] [blame] | 1015 | static void |
| 1016 | emit_tex(bi_context *ctx, nir_tex_instr *instr) |
| 1017 | { |
| 1018 | nir_alu_type base = nir_alu_type_get_base_type(instr->dest_type); |
| 1019 | unsigned sz = nir_dest_bit_size(instr->dest); |
| 1020 | instr->dest_type = base | sz; |
| 1021 | |
Alyssa Rosenzweig | 731dfc6 | 2020-05-27 11:41:42 -0400 | [diff] [blame] | 1022 | bool is_normal = bi_is_normal_tex(ctx->stage, instr); |
Alyssa Rosenzweig | 0769036a | 2020-04-21 12:15:29 -0400 | [diff] [blame] | 1023 | bool is_2d = instr->sampler_dim == GLSL_SAMPLER_DIM_2D || |
| 1024 | instr->sampler_dim == GLSL_SAMPLER_DIM_EXTERNAL; |
| 1025 | bool is_f = base == nir_type_float && (sz == 16 || sz == 32); |
| 1026 | |
| 1027 | bool is_compact = is_normal && is_2d && is_f && !instr->is_shadow; |
| 1028 | |
| 1029 | if (is_compact) |
| 1030 | emit_tex_compact(ctx, instr); |
| 1031 | else |
| 1032 | emit_tex_full(ctx, instr); |
| 1033 | } |
| 1034 | |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 1035 | static void |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 1036 | emit_instr(bi_context *ctx, struct nir_instr *instr) |
| 1037 | { |
| 1038 | switch (instr->type) { |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 1039 | case nir_instr_type_load_const: |
| 1040 | emit_load_const(ctx, nir_instr_as_load_const(instr)); |
| 1041 | break; |
| 1042 | |
| 1043 | case nir_instr_type_intrinsic: |
| 1044 | emit_intrinsic(ctx, nir_instr_as_intrinsic(instr)); |
| 1045 | break; |
| 1046 | |
| 1047 | case nir_instr_type_alu: |
| 1048 | emit_alu(ctx, nir_instr_as_alu(instr)); |
| 1049 | break; |
| 1050 | |
| 1051 | case nir_instr_type_tex: |
| 1052 | emit_tex(ctx, nir_instr_as_tex(instr)); |
| 1053 | break; |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 1054 | |
| 1055 | case nir_instr_type_jump: |
| 1056 | emit_jump(ctx, nir_instr_as_jump(instr)); |
| 1057 | break; |
| 1058 | |
| 1059 | case nir_instr_type_ssa_undef: |
| 1060 | /* Spurious */ |
| 1061 | break; |
| 1062 | |
| 1063 | default: |
Alyssa Rosenzweig | 0769036a | 2020-04-21 12:15:29 -0400 | [diff] [blame] | 1064 | unreachable("Unhandled instruction type"); |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1070 | |
| 1071 | static bi_block * |
| 1072 | create_empty_block(bi_context *ctx) |
| 1073 | { |
| 1074 | bi_block *blk = rzalloc(ctx, bi_block); |
| 1075 | |
Alyssa Rosenzweig | 9b75f41 | 2020-03-11 14:35:38 -0400 | [diff] [blame] | 1076 | blk->base.predecessors = _mesa_set_create(blk, |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1077 | _mesa_hash_pointer, |
| 1078 | _mesa_key_pointer_equal); |
| 1079 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1080 | return blk; |
| 1081 | } |
| 1082 | |
| 1083 | static bi_block * |
| 1084 | emit_block(bi_context *ctx, nir_block *block) |
| 1085 | { |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1086 | if (ctx->after_block) { |
| 1087 | ctx->current_block = ctx->after_block; |
| 1088 | ctx->after_block = NULL; |
| 1089 | } else { |
| 1090 | ctx->current_block = create_empty_block(ctx); |
| 1091 | } |
| 1092 | |
Alyssa Rosenzweig | 9b75f41 | 2020-03-11 14:35:38 -0400 | [diff] [blame] | 1093 | list_addtail(&ctx->current_block->base.link, &ctx->blocks); |
| 1094 | list_inithead(&ctx->current_block->base.instructions); |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1095 | |
| 1096 | nir_foreach_instr(instr, block) { |
Alyssa Rosenzweig | 65c8dcc | 2020-03-05 17:10:46 -0500 | [diff] [blame] | 1097 | emit_instr(ctx, instr); |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1098 | ++ctx->instruction_count; |
| 1099 | } |
| 1100 | |
| 1101 | return ctx->current_block; |
| 1102 | } |
| 1103 | |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1104 | /* Emits an unconditional branch to the end of the current block, returning a |
| 1105 | * pointer so the user can fill in details */ |
| 1106 | |
| 1107 | static bi_instruction * |
| 1108 | bi_emit_branch(bi_context *ctx) |
| 1109 | { |
| 1110 | bi_instruction branch = { |
| 1111 | .type = BI_BRANCH, |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1112 | .cond = BI_COND_ALWAYS |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1113 | }; |
| 1114 | |
| 1115 | return bi_emit(ctx, branch); |
| 1116 | } |
| 1117 | |
| 1118 | /* Sets a condition for a branch by examing the NIR condition. If we're |
| 1119 | * familiar with the condition, we unwrap it to fold it into the branch |
| 1120 | * instruction. Otherwise, we consume the condition directly. We |
| 1121 | * generally use 1-bit booleans which allows us to use small types for |
| 1122 | * the conditions. |
| 1123 | */ |
| 1124 | |
| 1125 | static void |
| 1126 | bi_set_branch_cond(bi_instruction *branch, nir_src *cond, bool invert) |
| 1127 | { |
| 1128 | /* TODO: Try to unwrap instead of always bailing */ |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 1129 | branch->src[0] = pan_src_index(cond); |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1130 | branch->src[1] = BIR_INDEX_ZERO; |
Alyssa Rosenzweig | d619ff0 | 2020-05-28 12:39:14 -0400 | [diff] [blame] | 1131 | branch->src_types[0] = branch->src_types[1] = nir_type_uint | |
| 1132 | nir_src_bit_size(*cond); |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1133 | branch->cond = invert ? BI_COND_EQ : BI_COND_NE; |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static void |
| 1137 | emit_if(bi_context *ctx, nir_if *nif) |
| 1138 | { |
| 1139 | bi_block *before_block = ctx->current_block; |
| 1140 | |
| 1141 | /* Speculatively emit the branch, but we can't fill it in until later */ |
| 1142 | bi_instruction *then_branch = bi_emit_branch(ctx); |
| 1143 | bi_set_branch_cond(then_branch, &nif->condition, true); |
| 1144 | |
| 1145 | /* Emit the two subblocks. */ |
| 1146 | bi_block *then_block = emit_cf_list(ctx, &nif->then_list); |
| 1147 | bi_block *end_then_block = ctx->current_block; |
| 1148 | |
| 1149 | /* Emit a jump from the end of the then block to the end of the else */ |
| 1150 | bi_instruction *then_exit = bi_emit_branch(ctx); |
| 1151 | |
| 1152 | /* Emit second block, and check if it's empty */ |
| 1153 | |
| 1154 | int count_in = ctx->instruction_count; |
| 1155 | bi_block *else_block = emit_cf_list(ctx, &nif->else_list); |
| 1156 | bi_block *end_else_block = ctx->current_block; |
| 1157 | ctx->after_block = create_empty_block(ctx); |
| 1158 | |
| 1159 | /* Now that we have the subblocks emitted, fix up the branches */ |
| 1160 | |
| 1161 | assert(then_block); |
| 1162 | assert(else_block); |
| 1163 | |
| 1164 | if (ctx->instruction_count == count_in) { |
| 1165 | /* The else block is empty, so don't emit an exit jump */ |
| 1166 | bi_remove_instruction(then_exit); |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1167 | then_branch->branch_target = ctx->after_block; |
Alyssa Rosenzweig | e42a5df | 2020-05-27 18:27:08 -0400 | [diff] [blame] | 1168 | pan_block_add_successor(&end_then_block->base, &ctx->after_block->base); /* fallthrough */ |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1169 | } else { |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1170 | then_branch->branch_target = else_block; |
| 1171 | then_exit->branch_target = ctx->after_block; |
| 1172 | pan_block_add_successor(&end_then_block->base, &then_exit->branch_target->base); |
Alyssa Rosenzweig | e42a5df | 2020-05-27 18:27:08 -0400 | [diff] [blame] | 1173 | pan_block_add_successor(&end_else_block->base, &ctx->after_block->base); /* fallthrough */ |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1174 | } |
| 1175 | |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1176 | pan_block_add_successor(&before_block->base, &then_branch->branch_target->base); /* then_branch */ |
Alyssa Rosenzweig | 9b75f41 | 2020-03-11 14:35:38 -0400 | [diff] [blame] | 1177 | pan_block_add_successor(&before_block->base, &then_block->base); /* fallthrough */ |
Alyssa Rosenzweig | 9a00cf3 | 2020-03-05 16:45:16 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
Alyssa Rosenzweig | 987aea1 | 2020-03-05 17:03:53 -0500 | [diff] [blame] | 1180 | static void |
| 1181 | emit_loop(bi_context *ctx, nir_loop *nloop) |
| 1182 | { |
| 1183 | /* Remember where we are */ |
| 1184 | bi_block *start_block = ctx->current_block; |
| 1185 | |
| 1186 | bi_block *saved_break = ctx->break_block; |
| 1187 | bi_block *saved_continue = ctx->continue_block; |
| 1188 | |
| 1189 | ctx->continue_block = create_empty_block(ctx); |
| 1190 | ctx->break_block = create_empty_block(ctx); |
| 1191 | ctx->after_block = ctx->continue_block; |
| 1192 | |
| 1193 | /* Emit the body itself */ |
| 1194 | emit_cf_list(ctx, &nloop->body); |
| 1195 | |
| 1196 | /* Branch back to loop back */ |
| 1197 | bi_instruction *br_back = bi_emit_branch(ctx); |
Alyssa Rosenzweig | 6627b20 | 2020-05-01 18:13:54 -0400 | [diff] [blame] | 1198 | br_back->branch_target = ctx->continue_block; |
Alyssa Rosenzweig | 9b75f41 | 2020-03-11 14:35:38 -0400 | [diff] [blame] | 1199 | pan_block_add_successor(&start_block->base, &ctx->continue_block->base); |
| 1200 | pan_block_add_successor(&ctx->current_block->base, &ctx->continue_block->base); |
Alyssa Rosenzweig | 987aea1 | 2020-03-05 17:03:53 -0500 | [diff] [blame] | 1201 | |
| 1202 | ctx->after_block = ctx->break_block; |
| 1203 | |
| 1204 | /* Pop off */ |
| 1205 | ctx->break_block = saved_break; |
| 1206 | ctx->continue_block = saved_continue; |
| 1207 | ++ctx->loop_count; |
| 1208 | } |
| 1209 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1210 | static bi_block * |
| 1211 | emit_cf_list(bi_context *ctx, struct exec_list *list) |
| 1212 | { |
| 1213 | bi_block *start_block = NULL; |
| 1214 | |
| 1215 | foreach_list_typed(nir_cf_node, node, node, list) { |
| 1216 | switch (node->type) { |
| 1217 | case nir_cf_node_block: { |
| 1218 | bi_block *block = emit_block(ctx, nir_cf_node_as_block(node)); |
| 1219 | |
| 1220 | if (!start_block) |
| 1221 | start_block = block; |
| 1222 | |
| 1223 | break; |
| 1224 | } |
| 1225 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1226 | case nir_cf_node_if: |
| 1227 | emit_if(ctx, nir_cf_node_as_if(node)); |
| 1228 | break; |
| 1229 | |
| 1230 | case nir_cf_node_loop: |
| 1231 | emit_loop(ctx, nir_cf_node_as_loop(node)); |
| 1232 | break; |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1233 | |
| 1234 | default: |
| 1235 | unreachable("Unknown control flow"); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | return start_block; |
| 1240 | } |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1241 | |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1242 | static int |
| 1243 | glsl_type_size(const struct glsl_type *type, bool bindless) |
| 1244 | { |
| 1245 | return glsl_count_attribute_slots(type, false); |
| 1246 | } |
| 1247 | |
| 1248 | static void |
| 1249 | bi_optimize_nir(nir_shader *nir) |
| 1250 | { |
| 1251 | bool progress; |
| 1252 | unsigned lower_flrp = 16 | 32 | 64; |
| 1253 | |
| 1254 | NIR_PASS(progress, nir, nir_lower_regs_to_ssa); |
| 1255 | NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast); |
| 1256 | |
| 1257 | nir_lower_tex_options lower_tex_options = { |
| 1258 | .lower_txs_lod = true, |
| 1259 | .lower_txp = ~0, |
| 1260 | .lower_tex_without_implicit_lod = true, |
| 1261 | .lower_txd = true, |
| 1262 | }; |
| 1263 | |
| 1264 | NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options); |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 1265 | NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL); |
| 1266 | NIR_PASS(progress, nir, nir_lower_load_const_to_scalar); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1267 | |
| 1268 | do { |
| 1269 | progress = false; |
| 1270 | |
| 1271 | NIR_PASS(progress, nir, nir_lower_var_copies); |
| 1272 | NIR_PASS(progress, nir, nir_lower_vars_to_ssa); |
| 1273 | |
| 1274 | NIR_PASS(progress, nir, nir_copy_prop); |
| 1275 | NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 1276 | NIR_PASS(progress, nir, nir_opt_dce); |
| 1277 | NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 1278 | NIR_PASS(progress, nir, nir_opt_cse); |
| 1279 | NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true); |
| 1280 | NIR_PASS(progress, nir, nir_opt_algebraic); |
| 1281 | NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 1282 | |
| 1283 | if (lower_flrp != 0) { |
| 1284 | bool lower_flrp_progress = false; |
| 1285 | NIR_PASS(lower_flrp_progress, |
| 1286 | nir, |
| 1287 | nir_lower_flrp, |
| 1288 | lower_flrp, |
Marek Olšák | ac55b1a | 2020-07-22 22:13:16 -0400 | [diff] [blame] | 1289 | false /* always_precise */); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1290 | if (lower_flrp_progress) { |
| 1291 | NIR_PASS(progress, nir, |
| 1292 | nir_opt_constant_folding); |
| 1293 | progress = true; |
| 1294 | } |
| 1295 | |
| 1296 | /* Nothing should rematerialize any flrps, so we only |
| 1297 | * need to do this lowering once. |
| 1298 | */ |
| 1299 | lower_flrp = 0; |
| 1300 | } |
| 1301 | |
| 1302 | NIR_PASS(progress, nir, nir_opt_undef); |
| 1303 | NIR_PASS(progress, nir, nir_opt_loop_unroll, |
| 1304 | nir_var_shader_in | |
| 1305 | nir_var_shader_out | |
| 1306 | nir_var_function_temp); |
| 1307 | } while (progress); |
| 1308 | |
| 1309 | NIR_PASS(progress, nir, nir_opt_algebraic_late); |
Alyssa Rosenzweig | 12299de | 2020-03-21 17:37:47 -0400 | [diff] [blame] | 1310 | NIR_PASS(progress, nir, nir_lower_bool_to_int32); |
Alyssa Rosenzweig | 3a1baaf | 2020-03-10 08:20:59 -0400 | [diff] [blame] | 1311 | NIR_PASS(progress, nir, bifrost_nir_lower_algebraic_late); |
Alyssa Rosenzweig | 51e537c | 2020-03-06 16:29:35 -0500 | [diff] [blame] | 1312 | NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL); |
| 1313 | NIR_PASS(progress, nir, nir_lower_load_const_to_scalar); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1314 | |
| 1315 | /* Take us out of SSA */ |
| 1316 | NIR_PASS(progress, nir, nir_lower_locals_to_regs); |
Alyssa Rosenzweig | 330e9a6 | 2020-03-09 19:56:35 -0400 | [diff] [blame] | 1317 | NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest); |
Alyssa Rosenzweig | e0a51d5 | 2020-03-22 17:31:23 -0400 | [diff] [blame] | 1318 | NIR_PASS(progress, nir, nir_convert_from_ssa, true); |
Alyssa Rosenzweig | 50d3f4d | 2020-03-19 17:21:49 -0400 | [diff] [blame] | 1319 | } |
| 1320 | |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1321 | void |
Alyssa Rosenzweig | e6f5ae8 | 2020-03-10 16:09:44 -0400 | [diff] [blame] | 1322 | bifrost_compile_shader_nir(nir_shader *nir, panfrost_program *program, unsigned product_id) |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1323 | { |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 1324 | bifrost_debug = debug_get_option_bifrost_debug(); |
| 1325 | |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1326 | bi_context *ctx = rzalloc(NULL, bi_context); |
| 1327 | ctx->nir = nir; |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1328 | ctx->stage = nir->info.stage; |
Alyssa Rosenzweig | 0b26cb1 | 2020-03-03 14:27:05 -0500 | [diff] [blame] | 1329 | ctx->quirks = bifrost_get_quirks(product_id); |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1330 | list_inithead(&ctx->blocks); |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1331 | |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1332 | /* Lower gl_Position pre-optimisation, but after lowering vars to ssa |
| 1333 | * (so we don't accidentally duplicate the epilogue since mesa/st has |
| 1334 | * messed with our I/O quite a bit already) */ |
| 1335 | |
| 1336 | NIR_PASS_V(nir, nir_lower_vars_to_ssa); |
| 1337 | |
| 1338 | if (ctx->stage == MESA_SHADER_VERTEX) { |
| 1339 | NIR_PASS_V(nir, nir_lower_viewport_transform); |
| 1340 | NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0); |
| 1341 | } |
| 1342 | |
| 1343 | NIR_PASS_V(nir, nir_split_var_copies); |
| 1344 | NIR_PASS_V(nir, nir_lower_global_vars_to_local); |
| 1345 | NIR_PASS_V(nir, nir_lower_var_copies); |
| 1346 | NIR_PASS_V(nir, nir_lower_vars_to_ssa); |
Jason Ekstrand | b019b22 | 2020-06-10 17:54:25 -0500 | [diff] [blame] | 1347 | NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out, |
| 1348 | glsl_type_size, 0); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1349 | NIR_PASS_V(nir, nir_lower_ssbo); |
Alyssa Rosenzweig | 9c7d30f | 2020-04-30 09:27:36 +0200 | [diff] [blame] | 1350 | NIR_PASS_V(nir, nir_lower_mediump_outputs); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1351 | |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1352 | bi_optimize_nir(nir); |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 1353 | |
| 1354 | if (bifrost_debug & BIFROST_DBG_SHADERS) { |
| 1355 | nir_print_shader(nir, stdout); |
| 1356 | } |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1357 | |
Alyssa Rosenzweig | 680fb05 | 2020-08-18 08:31:42 -0400 | [diff] [blame] | 1358 | panfrost_nir_assign_sysvals(&ctx->sysvals, ctx, nir); |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 1359 | program->sysval_count = ctx->sysvals.sysval_count; |
| 1360 | memcpy(program->sysvals, ctx->sysvals.sysvals, sizeof(ctx->sysvals.sysvals[0]) * ctx->sysvals.sysval_count); |
Alyssa Rosenzweig | 1a8f1a3 | 2020-04-23 19:26:01 -0400 | [diff] [blame] | 1361 | ctx->blend_types = program->blend_types; |
Alyssa Rosenzweig | 218785c | 2020-03-10 16:20:18 -0400 | [diff] [blame] | 1362 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1363 | nir_foreach_function(func, nir) { |
| 1364 | if (!func->impl) |
| 1365 | continue; |
| 1366 | |
Alyssa Rosenzweig | d86659c | 2020-03-06 09:43:43 -0500 | [diff] [blame] | 1367 | ctx->impl = func->impl; |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 1368 | emit_cf_list(ctx, &func->impl->body); |
| 1369 | break; /* TODO: Multi-function shaders */ |
| 1370 | } |
| 1371 | |
Alyssa Rosenzweig | c697992 | 2020-05-28 14:44:33 -0400 | [diff] [blame] | 1372 | unsigned block_source_count = 0; |
| 1373 | |
Alyssa Rosenzweig | 50d3f4d | 2020-03-19 17:21:49 -0400 | [diff] [blame] | 1374 | bi_foreach_block(ctx, _block) { |
| 1375 | bi_block *block = (bi_block *) _block; |
Alyssa Rosenzweig | c697992 | 2020-05-28 14:44:33 -0400 | [diff] [blame] | 1376 | |
| 1377 | /* Name blocks now that we're done emitting so the order is |
| 1378 | * consistent */ |
| 1379 | block->base.name = block_source_count++; |
| 1380 | |
Alyssa Rosenzweig | e0a51d5 | 2020-03-22 17:31:23 -0400 | [diff] [blame] | 1381 | bi_lower_combine(ctx, block); |
Alyssa Rosenzweig | 50d3f4d | 2020-03-19 17:21:49 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
Alyssa Rosenzweig | 58f9171 | 2020-03-11 15:10:32 -0400 | [diff] [blame] | 1384 | bool progress = false; |
| 1385 | |
| 1386 | do { |
| 1387 | progress = false; |
| 1388 | |
| 1389 | bi_foreach_block(ctx, _block) { |
| 1390 | bi_block *block = (bi_block *) _block; |
| 1391 | progress |= bi_opt_dead_code_eliminate(ctx, block); |
| 1392 | } |
| 1393 | } while(progress); |
| 1394 | |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 1395 | if (bifrost_debug & BIFROST_DBG_SHADERS) |
| 1396 | bi_print_shader(ctx, stdout); |
Alyssa Rosenzweig | b329f8c | 2020-03-06 19:25:00 -0500 | [diff] [blame] | 1397 | bi_schedule(ctx); |
Alyssa Rosenzweig | e8139ef | 2020-03-11 20:39:36 -0400 | [diff] [blame] | 1398 | bi_register_allocate(ctx); |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 1399 | if (bifrost_debug & BIFROST_DBG_SHADERS) |
| 1400 | bi_print_shader(ctx, stdout); |
Alyssa Rosenzweig | 9269c85 | 2020-03-12 14:16:22 -0400 | [diff] [blame] | 1401 | bi_pack(ctx, &program->compiled); |
Tomeu Vizoso | 07b31f3 | 2020-04-30 09:29:10 +0200 | [diff] [blame] | 1402 | |
| 1403 | if (bifrost_debug & BIFROST_DBG_SHADERS) |
| 1404 | disassemble_bifrost(stdout, program->compiled.data, program->compiled.size, true); |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 1405 | |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 1406 | ralloc_free(ctx); |
| 1407 | } |