2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
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:
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
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
30 #include "instr-a3xx.h"
31 #include "disasm.h" /* TODO move 'enum shader_t' somewhere else.. */
33 /* low level intermediate representation of an adreno shader program */
36 struct ir3_instruction
;
39 struct ir3_shader
* fd_asm_parse(const char *src
);
41 struct ir3_shader_info
{
43 uint16_t instrs_count
; /* expanded to account for rpt's */
44 /* NOTE: max_reg, etc, does not include registers not touched
45 * by the shader (ie. vertex fetched via VFD_DECODE but not
48 int8_t max_reg
; /* highest GPR # used by shader */
55 IR3_REG_CONST
= 0x001,
56 IR3_REG_IMMED
= 0x002,
58 IR3_REG_RELATIV
= 0x008,
60 IR3_REG_NEGATE
= 0x020,
63 IR3_REG_POS_INF
= 0x100,
64 /* (ei) flag, end-input? Set on last bary, presumably to signal
65 * that the shader needs no more input:
71 * the component is in the low two bits of the reg #, so
72 * rN.x becomes: (N << 2) | x
82 /* used for cat5 instructions, but also for internal/IR level
83 * tracking of what registers are read/written by an instruction.
84 * wrmask may be a bad name since it is used to represent both
85 * src and dst that touch multiple adjacent registers.
90 struct ir3_instruction
{
91 struct ir3_block
*block
;
95 /* (sy) flag is set on first instruction, and after sample
96 * instructions (probably just on RAW hazard).
99 /* (ss) flag is set on first instruction, and first instruction
100 * to depend on the result of "long" instructions (RAW hazard):
102 * rcp, rsq, log2, exp2, sin, cos, sqrt
104 * It seems to synchronize until all in-flight instructions are
105 * completed, for example:
108 * add.f hr2.z, (neg)hr2.z, hc0.y
109 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
112 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
114 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
115 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
116 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
118 * The last mul.f does not have (ss) set, presumably because the
119 * (ss) on the previous instruction does the job.
121 * The blob driver also seems to set it on WAR hazards, although
122 * not really clear if this is needed or just blob compiler being
123 * sloppy. So far I haven't found a case where removing the (ss)
124 * causes problems for WAR hazard, but I could just be getting
128 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
131 IR3_INSTR_SS
= 0x002,
132 /* (jp) flag is set on jump targets:
134 IR3_INSTR_JP
= 0x004,
135 IR3_INSTR_UL
= 0x008,
136 IR3_INSTR_3D
= 0x010,
141 IR3_INSTR_S2EN
= 0x200,
145 struct ir3_register
*regs
[5];
153 type_t src_type
, dst_type
;
180 #define MAX_INSTRS 1024
183 unsigned instrs_count
;
184 struct ir3_instruction
*instrs
[MAX_INSTRS
];
185 uint32_t heap
[128 * MAX_INSTRS
];
190 struct ir3_shader
*shader
;
191 unsigned ntemporaries
, ninputs
, noutputs
;
192 /* maps TGSI_FILE_TEMPORARY index back to the assigning instruction: */
193 struct ir3_instruction
**temporaries
;
194 struct ir3_instruction
**inputs
;
195 struct ir3_instruction
**outputs
;
196 struct ir3_block
*parent
;
197 struct ir3_instruction
*head
;
200 struct ir3_shader
* ir3_shader_create(void);
201 void ir3_shader_destroy(struct ir3_shader
*shader
);
202 void * ir3_shader_assemble(struct ir3_shader
*shader
,
203 struct ir3_shader_info
*info
);
205 struct ir3_block
* ir3_block_create(struct ir3_shader
*shader
,
206 unsigned ntmp
, unsigned nin
, unsigned nout
);
208 struct ir3_instruction
* ir3_instr_create(struct ir3_block
*block
,
209 int category
, opc_t opc
);
210 struct ir3_instruction
* ir3_instr_clone(struct ir3_instruction
*instr
);
212 struct ir3_register
* ir3_reg_create(struct ir3_instruction
*instr
,
222 static inline uint32_t regid(int num
, int comp
)
224 return (num
<< 2) | (comp
& 0x3);
227 static inline uint32_t reg_num(struct ir3_register
*reg
)
229 return reg
->num
>> 2;
232 static inline uint32_t reg_comp(struct ir3_register
*reg
)
234 return reg
->num
& 0x3;
237 static inline bool is_alu(struct ir3_instruction
*instr
)
239 return (1 <= instr
->category
) && (instr
->category
<= 3);
242 static inline bool is_sfu(struct ir3_instruction
*instr
)
244 return (instr
->category
== 4);
247 static inline bool is_tex(struct ir3_instruction
*instr
)
249 return (instr
->category
== 5);
252 static inline bool is_input(struct ir3_instruction
*instr
)
254 return (instr
->category
== 2) && (instr
->opc
== OPC_BARY_F
);
257 static inline bool is_gpr(struct ir3_register
*reg
)
259 return !(reg
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
));
262 /* TODO combine is_gpr()/reg_gpr().. */
263 static inline bool reg_gpr(struct ir3_register
*r
)
265 if (r
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
| IR3_REG_RELATIV
))
267 if ((reg_num(r
) == REG_A0
) || (reg_num(r
) == REG_P0
))
273 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
276 /* ************************************************************************* */
277 /* split this out or find some helper to use.. like main/bitset.h.. */
283 typedef uint8_t regmask_t
[2 * MAX_REG
/ 8];
285 static inline unsigned regmask_idx(struct ir3_register
*reg
)
287 unsigned num
= reg
->num
;
288 assert(num
< MAX_REG
);
289 if (reg
->flags
& IR3_REG_HALF
)
294 static inline void regmask_init(regmask_t
*regmask
)
296 memset(regmask
, 0, sizeof(*regmask
));
299 static inline void regmask_set(regmask_t
*regmask
, struct ir3_register
*reg
)
301 unsigned idx
= regmask_idx(reg
);
303 for (i
= 0; i
< 4; i
++, idx
++)
304 if (reg
->wrmask
& (1 << i
))
305 (*regmask
)[idx
/ 8] |= 1 << (idx
% 8);
308 /* set bits in a if not set in b, conceptually:
311 static inline void regmask_set_if_not(regmask_t
*a
,
312 struct ir3_register
*reg
, regmask_t
*b
)
314 unsigned idx
= regmask_idx(reg
);
316 for (i
= 0; i
< 4; i
++, idx
++)
317 if (reg
->wrmask
& (1 << i
))
318 if (!((*b
)[idx
/ 8] & (1 << (idx
% 8))))
319 (*a
)[idx
/ 8] |= 1 << (idx
% 8);
322 static inline unsigned regmask_get(regmask_t
*regmask
,
323 struct ir3_register
*reg
)
325 unsigned idx
= regmask_idx(reg
);
327 for (i
= 0; i
< 4; i
++, idx
++)
328 if (reg
->wrmask
& (1 << i
))
329 if ((*regmask
)[idx
/ 8] & (1 << (idx
% 8)))
334 /* ************************************************************************* */