Debian Codegraph @

src/asahi/compiler/agx_builder.h.py

raw
1 template = """/*
2 * Copyright 2021 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef _AGX_BUILDER_
7 #define _AGX_BUILDER_
8
9 #include "agx_compiler.h"
10 #include "util/lut.h"
11
12 static inline agx_instr *
13 agx_alloc_instr(agx_builder *b, enum agx_opcode op, uint8_t nr_dests, uint8_t nr_srcs)
14 {
15 size_t size = sizeof(agx_instr);
16 size += sizeof(agx_index) * nr_dests;
17 size += sizeof(agx_index) * nr_srcs;
18
19 agx_instr *I = (agx_instr *) rzalloc_size(b->shader, size);
20 I->dest = (agx_index *) (I + 1);
21 I->src = I->dest + nr_dests;
22
23 I->op = op;
24 I->nr_dests = nr_dests;
25 I->nr_srcs = nr_srcs;
26 return I;
27 }
28
29 % for opcode in opcodes:
30 <%
31 op = opcodes[opcode]
32 dests = op.dests
33 srcs = op.srcs
34 imms = [x for x in op.imms if (x.name != 'scoreboard' or opcode == 'wait')]
35 suffix = "_to" if dests > 0 else ""
36 nr_dests = "nr_dests" if op.variable_dests else str(dests)
37 nr_srcs = "nr_srcs" if op.variable_srcs else str(srcs)
38 %>
39
40 static inline agx_instr *
41 agx_${opcode}${suffix}(agx_builder *b
42
43 % if op.variable_dests:
44 , unsigned nr_dests
45 % endif
46
47 % for dest in range(dests):
48 , agx_index dst${dest}
49 % endfor
50
51 % if op.variable_srcs:
52 , unsigned nr_srcs
53 % endif
54
55 % for src in range(srcs):
56 , agx_index src${src}
57 % endfor
58
59 % for imm in imms:
60 , ${imm.ctype} ${imm.name}
61 % endfor
62
63 ) {
64 agx_instr *I = agx_alloc_instr(b, AGX_OPCODE_${opcode.upper()}, ${nr_dests}, ${nr_srcs});
65
66 % for dest in range(dests):
67 I->dest[${dest}] = dst${dest};
68 % endfor
69
70 % for src in range(srcs):
71 I->src[${src}] = src${src};
72 % endfor
73
74 % for imm in imms:
75 I->${imm.name} = ${imm.name};
76 % endfor
77
78 agx_builder_insert(&b->cursor, I);
79 return I;
80 }
81
82 % if dests == 1 and not op.variable_srcs and not op.variable_dests:
83 static inline agx_index
84 agx_${opcode}(agx_builder *b
85
86 % if srcs == 0:
87 , unsigned size
88 % endif
89
90 % for src in range(srcs):
91 , agx_index src${src}
92 % endfor
93
94 % for imm in imms:
95 , ${imm.ctype} ${imm.name}
96 % endfor
97
98 ) {
99 <%
100 args = ["tmp"]
101 args += ["src" + str(i) for i in range(srcs)]
102 args += [imm.name for imm in imms]
103 %>
104 % if srcs == 0:
105 agx_index tmp = agx_temp(b->shader, agx_size_for_bits(size));
106 % else:
107 agx_index tmp = agx_temp(b->shader, src0.size);
108 % endif
109 agx_${opcode}_to(b, ${", ".join(args)});
110 return tmp;
111 }
112 % endif
113
114 % endfor
115
116 /* Convenience methods */
117 #define BINOP_BITOP(name, expr) \
118 static inline agx_instr * \
119 agx_## name ##_to(agx_builder *b, agx_index dst0, agx_index src0, agx_index src1) \
120 { \
121 return agx_bitop_to(b, dst0, src0, src1, UTIL_LUT2(expr)); \
122 } \
123 \
124 static inline agx_index \
125 agx_## name (agx_builder *b, agx_index src0, agx_index src1) \
126 { \
127 agx_index tmp = agx_temp(b->shader, src0.size); \
128 agx_##name##_to(b, tmp, src0, src1); \
129 return tmp; \
130 }
131
132 BINOP_BITOP(nor, ~(a | b))
133 BINOP_BITOP(andn2, a & ~b)
134 BINOP_BITOP(andn1, ~a & b)
135 BINOP_BITOP(xor, a ^ b)
136 BINOP_BITOP(nand, ~(a & b))
137 BINOP_BITOP(and, a & b)
138 BINOP_BITOP(xnor, ~a ^ b)
139 BINOP_BITOP(orn2, a | ~b)
140 BINOP_BITOP(orn1, ~a | b)
141 BINOP_BITOP(or, a | b)
142
143 #undef BINOP_BITOP
144
145 static inline agx_instr *
146 agx_fmov_to(agx_builder *b, agx_index dst0, agx_index src0)
147 {
148 return agx_fadd_to(b, dst0, src0, agx_negzero());
149 }
150
151 static inline agx_instr *
152 agx_push_exec(agx_builder *b, unsigned n)
153 {
154 return agx_if_fcmp(b, agx_zero(), agx_zero(), n, AGX_FCOND_EQ, false, NULL);
155 }
156
157 static inline agx_instr *
158 agx_ushr_to(agx_builder *b, agx_index dst, agx_index s0, agx_index s1)
159 {
160 return agx_bfeil_to(b, dst, agx_zero(), s0, s1, 0);
161 }
162
163 static inline agx_index
164 agx_ushr(agx_builder *b, agx_index s0, agx_index s1)
165 {
166 agx_index tmp = agx_temp(b->shader, s0.size);
167 agx_ushr_to(b, tmp, s0, s1);
168 return tmp;
169 }
170
171 #endif
172 """
173
174 from mako.template import Template
175 from agx_opcodes import opcodes
176
177 print(Template(template).render(opcodes=opcodes))