Debian Codegraph @

src/panfrost/compiler/bifrost/bi_printer.c.py

raw
1 # Copyright (C) 2020 Collabora, Ltd.
2 # Copyright (C) 2026 Arm Ltd.
3 # SPDX-License-Identifier: MIT
4
5 TEMPLATE = """#include <stdio.h>
6 #include "compiler.h"
7
8 static const char *
9 bi_swizzle_as_str(enum bi_swizzle swz)
10 {
11 switch (swz) {
12 case BI_SWIZZLE_H00: return ".h00";
13 case BI_SWIZZLE_H01: return "";
14 case BI_SWIZZLE_H10: return ".h10";
15 case BI_SWIZZLE_H11: return ".h11";
16 case BI_SWIZZLE_B0000: return ".b0";
17 case BI_SWIZZLE_B1111: return ".b1";
18 case BI_SWIZZLE_B2222: return ".b2";
19 case BI_SWIZZLE_B3333: return ".b3";
20 case BI_SWIZZLE_B0011: return ".b0011";
21 case BI_SWIZZLE_B2233: return ".b2233";
22 case BI_SWIZZLE_B1032: return ".b1032";
23 case BI_SWIZZLE_B3210: return ".b3210";
24 case BI_SWIZZLE_B0022: return ".b0022";
25 case BI_SWIZZLE_B1100: return ".b1100";
26 case BI_SWIZZLE_B2200: return ".b2200";
27 case BI_SWIZZLE_B3300: return ".b3300";
28 case BI_SWIZZLE_B2211: return ".b2211";
29 case BI_SWIZZLE_B3311: return ".b3311";
30 case BI_SWIZZLE_B1122: return ".b1122";
31 case BI_SWIZZLE_B3322: return ".b3322";
32 case BI_SWIZZLE_B0033: return ".b0033";
33 case BI_SWIZZLE_B1133: return ".b1133";
34 }
35
36 UNREACHABLE("Invalid swizzle");
37 }
38
39 static const char *
40 bir_fau_name(unsigned fau_idx)
41 {
42 const char *names[] = {
43 "zero", "lane-id", "warp-id", "core-id", "fb-extent",
44 "atest-param", "sample-pos", "reserved",
45 "blend_descriptor_0", "blend_descriptor_1",
46 "blend_descriptor_2", "blend_descriptor_3",
47 "blend_descriptor_4", "blend_descriptor_5",
48 "blend_descriptor_6", "blend_descriptor_7",
49 "tls_ptr", "wls_ptr", "program_counter",
50 };
51
52 assert(fau_idx < ARRAY_SIZE(names));
53 return names[fau_idx];
54 }
55
56 static const char *
57 bir_passthrough_name(unsigned idx)
58 {
59 const char *names[] = {
60 "s0", "s1", "s2", "t", "fau.x", "fau.y", "t0", "t1"
61 };
62
63 assert(idx < ARRAY_SIZE(names));
64 return names[idx];
65 }
66
67 static void
68 bi_print_index(FILE *fp, bi_index index, unsigned nr_regs)
69 {
70 if (bi_is_null(index))
71 fprintf(fp, "_");
72 else if (index.type == BI_INDEX_CONSTANT)
73 fprintf(fp, "#0x%x", index.value);
74 else if (index.type == BI_INDEX_FAU && index.memory)
75 fprintf(fp, "m%u", index.value);
76 else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM)
77 fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM);
78 else if (index.type == BI_INDEX_FAU)
79 fprintf(fp, "%s", bir_fau_name(index.value));
80 else if (index.type == BI_INDEX_PASS)
81 fprintf(fp, "%s", bir_passthrough_name(index.value));
82 else if (index.type == BI_INDEX_REGISTER) {
83 for (int i = 0; i < nr_regs; i++) {
84 if (i > 0) fputs(":", fp);
85 fprintf(fp, "%s%u", index.memory ? "m" : "r", index.value + i);
86 }
87 } else if (index.type == BI_INDEX_NORMAL)
88 fprintf(fp, "%%%s%u", index.memory ? "m" : "", index.value);
89 else
90 UNREACHABLE("Invalid index");
91
92 if (index.discard || index.kill_ssa)
93 fputs("^", fp);
94
95 if (index.offset)
96 fprintf(fp, "[%u]", index.offset);
97
98 if (index.abs)
99 fputs(".abs", fp);
100
101 if (index.neg)
102 fputs(".neg", fp);
103
104 fputs(bi_swizzle_as_str(index.swizzle), fp);
105 }
106
107 % for mod in sorted(modifiers):
108 % if len(modifiers[mod]) > 2: # otherwise just boolean
109
110 UNUSED static inline const char *
111 bi_${mod}_as_str(enum bi_${mod} ${mod})
112 {
113 switch (${mod}) {
114 % for i, state in enumerate(sorted(modifiers[mod])):
115 % if state == "none":
116 case BI_${mod.upper()}_NONE: return "";
117 % elif state != "reserved":
118 case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}";
119 % endif
120 % endfor
121 }
122
123 UNREACHABLE("Invalid ${mod}");
124 };
125 % endif
126 % endfor
127
128 <%def name="print_modifiers(mods, table)">
129 % for mod in mods:
130 % if mod not in ["lane_dest"]:
131 % if len(table[mod]) > 2:
132 fputs(bi_${mod}_as_str(I->${mod}), fp);
133 % else:
134 if (I->${mod}) fputs(".${mod}", fp);
135 % endif
136 % endif
137 % endfor
138 </%def>
139
140 <%def name="print_source_modifiers(mods, src, table)">
141 % for mod in mods:
142 % if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]:
143 % if len(table[mod[0:-1]]) > 2:
144 fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp);
145 % elif mod == "bytes2":
146 if (I->bytes2) fputs(".bytes", fp);
147 % else:
148 if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp);
149 % endif
150 %endif
151 % endfor
152 </%def>
153
154 void
155 bi_print_instr_impl(const bi_instr *I, FILE *fp)
156 {
157 char buf[128];
158 size_t len = 0;
159 unsigned indent = 8;
160
161 /* Print destination width in SSA-form. For SPLIT, only print one width as
162 * it is assumed all destinations will have the same width.
163 */
164 bi_foreach_ssa_dest(I, d) {
165 if (I->op == BI_OPCODE_SPLIT_I32 && d > 0)
166 continue;
167 if (len > 0)
168 len += snprintf(buf + len, sizeof(buf) - len, ",");
169
170 unsigned nr_regs = bi_count_write_registers(I, d);
171 if (nr_regs > 1)
172 len += snprintf(buf + len, sizeof(buf) - len, "32x%u", nr_regs);
173 else if (nr_regs == 1)
174 len += snprintf(buf + len, sizeof(buf) - len, "32");
175 }
176
177 if (len > 0)
178 fputs(buf, fp);
179 if (len >= indent)
180 fputc(' ', fp);
181 else
182 fprintf(fp, "%*s", (int)(indent - len), "");
183
184 bi_foreach_dest(I, d) {
185 if (d > 0) fprintf(fp, ", ");
186
187 unsigned nr_regs = bi_count_write_registers(I, d);
188 bi_print_index(fp, I->dest[d], nr_regs);
189 }
190
191 if (I->nr_dests > 0)
192 fputs(" = ", fp);
193
194 fprintf(fp, "%s", bi_get_opcode_props(I)->name);
195
196 if (I->table)
197 fprintf(fp, ".table%u", I->table);
198
199 if (I->flow)
200 fprintf(fp, ".flow%u", I->flow);
201
202 if (I->op == BI_OPCODE_COLLECT_I32 || I->op == BI_OPCODE_PHI) {
203 for (unsigned i = 0; i < I->nr_srcs; ++i) {
204 if (i > 0)
205 fputs(", ", fp);
206 else
207 fputs(" ", fp);
208
209 unsigned nr_regs = bi_count_read_registers(I, i);
210 bi_print_index(fp, I->src[i], nr_regs);
211 }
212 }
213
214 switch (I->op) {
215 % for opcode in ops:
216 <%
217 # Extract modifiers that are not per-source
218 root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"]
219 %>
220 case BI_OPCODE_${opcode.replace('.', '_').upper()}:
221 ${print_modifiers(root_modifiers, modifiers)}
222 fputs(" ", fp);
223 % for src in range(src_count(ops[opcode])):
224 % if src > 0:
225 fputs(", ", fp);
226 % endif
227 bi_print_index(fp, I->src[${src}], bi_count_read_registers(I, ${src}));
228 ${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)}
229 % endfor
230 % for imm in ops[opcode]["immediates"]:
231 fprintf(fp, ", ${imm}:%u", I->${imm});
232 % endfor
233 break;
234 % endfor
235 default:
236 UNREACHABLE("Invalid opcode");
237 }
238
239 if (I->branch_target)
240 fprintf(fp, " -> block%u", I->branch_target->index);
241
242 fputs("\\n", fp);
243
244 }"""
245
246 import sys
247 from bifrost_isa import *
248 from mako.template import Template
249
250 instructions = {}
251 for arg in sys.argv[1:]:
252 new_instructions = parse_instructions(arg, include_pseudo = True)
253 instructions.update(new_instructions)
254
255 ir_instructions = partition_mnemonics(instructions)
256 modifier_lists = order_modifiers(ir_instructions)
257
258 print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count))