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 <[email protected]>
25 */
26
27 #include "pan_ir.h"
28 #include "util/macros.h"
29
30 /* Converts a per-component mask to a byte mask */
31
32 uint16_t
pan_to_bytemask(unsigned bytes,unsigned mask)33 pan_to_bytemask(unsigned bytes, unsigned mask)
34 {
35 switch (bytes) {
36 case 0:
37 assert(mask == 0);
38 return 0;
39
40 case 8:
41 return mask;
42
43 case 16: {
44 unsigned space =
45 (mask & 0x1) | ((mask & 0x2) << (2 - 1)) | ((mask & 0x4) << (4 - 2)) |
46 ((mask & 0x8) << (6 - 3)) | ((mask & 0x10) << (8 - 4)) |
47 ((mask & 0x20) << (10 - 5)) | ((mask & 0x40) << (12 - 6)) |
48 ((mask & 0x80) << (14 - 7));
49
50 return space | (space << 1);
51 }
52
53 case 32: {
54 unsigned space = (mask & 0x1) | ((mask & 0x2) << (4 - 1)) |
55 ((mask & 0x4) << (8 - 2)) | ((mask & 0x8) << (12 - 3));
56
57 return space | (space << 1) | (space << 2) | (space << 3);
58 }
59
60 case 64: {
61 unsigned A = (mask & 0x1) ? 0xFF : 0x00;
62 unsigned B = (mask & 0x2) ? 0xFF : 0x00;
63 return A | (B << 8);
64 }
65
66 default:
67 unreachable("Invalid register mode");
68 }
69 }
70
71 void
pan_block_add_successor(pan_block * block,pan_block * successor)72 pan_block_add_successor(pan_block *block, pan_block *successor)
73 {
74 assert(block);
75 assert(successor);
76
77 /* Cull impossible edges */
78 if (block->unconditional_jumps)
79 return;
80
81 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
82 if (block->successors[i]) {
83 if (block->successors[i] == successor)
84 return;
85 else
86 continue;
87 }
88
89 block->successors[i] = successor;
90 _mesa_set_add(successor->predecessors, block);
91 return;
92 }
93
94 unreachable("Too many successors");
95 }
96
97 /* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
98
99 void
pan_print_alu_type(nir_alu_type t,FILE * fp)100 pan_print_alu_type(nir_alu_type t, FILE *fp)
101 {
102 unsigned size = nir_alu_type_get_type_size(t);
103 nir_alu_type base = nir_alu_type_get_base_type(t);
104
105 switch (base) {
106 case nir_type_int:
107 fprintf(fp, ".i");
108 break;
109 case nir_type_uint:
110 fprintf(fp, ".u");
111 break;
112 case nir_type_bool:
113 fprintf(fp, ".b");
114 break;
115 case nir_type_float:
116 fprintf(fp, ".f");
117 break;
118 default:
119 fprintf(fp, ".unknown");
120 break;
121 }
122
123 fprintf(fp, "%u", size);
124 }
125
126 /* Could optimize with a better data structure if anyone cares, TODO: profile */
127
128 unsigned
pan_lookup_pushed_ubo(struct panfrost_ubo_push * push,unsigned ubo,unsigned offs)129 pan_lookup_pushed_ubo(struct panfrost_ubo_push *push, unsigned ubo,
130 unsigned offs)
131 {
132 struct panfrost_ubo_word word = {.ubo = ubo, .offset = offs};
133
134 for (unsigned i = 0; i < push->count; ++i) {
135 if (memcmp(push->words + i, &word, sizeof(word)) == 0)
136 return i;
137 }
138
139 unreachable("UBO not pushed");
140 }
141