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