xref: /third_party/mesa3d/src/panfrost/util/pan_ir.c (revision bf215546)
1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2020 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors (Collabora):
24bf215546Sopenharmony_ci *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pan_ir.h"
28bf215546Sopenharmony_ci#include "util/macros.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/* Converts a per-component mask to a byte mask */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ciuint16_t
33bf215546Sopenharmony_cipan_to_bytemask(unsigned bytes, unsigned mask)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci        switch (bytes) {
36bf215546Sopenharmony_ci        case 0:
37bf215546Sopenharmony_ci                assert(mask == 0);
38bf215546Sopenharmony_ci                return 0;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci        case 8:
41bf215546Sopenharmony_ci                return mask;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci        case 16: {
44bf215546Sopenharmony_ci                unsigned space =
45bf215546Sopenharmony_ci                        (mask & 0x1) |
46bf215546Sopenharmony_ci                        ((mask & 0x2) << (2 - 1)) |
47bf215546Sopenharmony_ci                        ((mask & 0x4) << (4 - 2)) |
48bf215546Sopenharmony_ci                        ((mask & 0x8) << (6 - 3)) |
49bf215546Sopenharmony_ci                        ((mask & 0x10) << (8 - 4)) |
50bf215546Sopenharmony_ci                        ((mask & 0x20) << (10 - 5)) |
51bf215546Sopenharmony_ci                        ((mask & 0x40) << (12 - 6)) |
52bf215546Sopenharmony_ci                        ((mask & 0x80) << (14 - 7));
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci                return space | (space << 1);
55bf215546Sopenharmony_ci        }
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci        case 32: {
58bf215546Sopenharmony_ci                unsigned space =
59bf215546Sopenharmony_ci                        (mask & 0x1) |
60bf215546Sopenharmony_ci                        ((mask & 0x2) << (4 - 1)) |
61bf215546Sopenharmony_ci                        ((mask & 0x4) << (8 - 2)) |
62bf215546Sopenharmony_ci                        ((mask & 0x8) << (12 - 3));
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci                return space | (space << 1) | (space << 2) | (space << 3);
65bf215546Sopenharmony_ci        }
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci        case 64: {
68bf215546Sopenharmony_ci                unsigned A = (mask & 0x1) ? 0xFF : 0x00;
69bf215546Sopenharmony_ci                unsigned B = (mask & 0x2) ? 0xFF : 0x00;
70bf215546Sopenharmony_ci                return A | (B << 8);
71bf215546Sopenharmony_ci        }
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci        default:
74bf215546Sopenharmony_ci                unreachable("Invalid register mode");
75bf215546Sopenharmony_ci        }
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_civoid
79bf215546Sopenharmony_cipan_block_add_successor(pan_block *block, pan_block *successor)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci        assert(block);
82bf215546Sopenharmony_ci        assert(successor);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci        /* Cull impossible edges */
85bf215546Sopenharmony_ci        if (block->unconditional_jumps)
86bf215546Sopenharmony_ci                return;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci        for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
89bf215546Sopenharmony_ci                if (block->successors[i]) {
90bf215546Sopenharmony_ci                       if (block->successors[i] == successor)
91bf215546Sopenharmony_ci                               return;
92bf215546Sopenharmony_ci                       else
93bf215546Sopenharmony_ci                               continue;
94bf215546Sopenharmony_ci                }
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci                block->successors[i] = successor;
97bf215546Sopenharmony_ci                _mesa_set_add(successor->predecessors, block);
98bf215546Sopenharmony_ci                return;
99bf215546Sopenharmony_ci        }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci        unreachable("Too many successors");
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci/* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_civoid
107bf215546Sopenharmony_cipan_print_alu_type(nir_alu_type t, FILE *fp)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci        unsigned size = nir_alu_type_get_type_size(t);
110bf215546Sopenharmony_ci        nir_alu_type base = nir_alu_type_get_base_type(t);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci        switch (base) {
113bf215546Sopenharmony_ci        case nir_type_int:
114bf215546Sopenharmony_ci                fprintf(fp, ".i");
115bf215546Sopenharmony_ci                break;
116bf215546Sopenharmony_ci        case nir_type_uint:
117bf215546Sopenharmony_ci                fprintf(fp, ".u");
118bf215546Sopenharmony_ci                break;
119bf215546Sopenharmony_ci        case nir_type_bool:
120bf215546Sopenharmony_ci                fprintf(fp, ".b");
121bf215546Sopenharmony_ci                break;
122bf215546Sopenharmony_ci        case nir_type_float:
123bf215546Sopenharmony_ci                fprintf(fp, ".f");
124bf215546Sopenharmony_ci                break;
125bf215546Sopenharmony_ci        default:
126bf215546Sopenharmony_ci                fprintf(fp, ".unknown");
127bf215546Sopenharmony_ci                break;
128bf215546Sopenharmony_ci        }
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci        fprintf(fp, "%u", size);
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci/* Could optimize with a better data structure if anyone cares, TODO: profile */
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ciunsigned
136bf215546Sopenharmony_cipan_lookup_pushed_ubo(struct panfrost_ubo_push *push, unsigned ubo, unsigned offs)
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci        struct panfrost_ubo_word word = {
139bf215546Sopenharmony_ci                .ubo = ubo,
140bf215546Sopenharmony_ci                .offset = offs
141bf215546Sopenharmony_ci        };
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci        for (unsigned i = 0; i < push->count; ++i) {
144bf215546Sopenharmony_ci                if (memcmp(push->words + i, &word, sizeof(word)) == 0)
145bf215546Sopenharmony_ci                        return i;
146bf215546Sopenharmony_ci        }
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci        unreachable("UBO not pushed");
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci}
151