1 /*
2  * Copyright © Microsoft Corporation
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
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24  /*
25   * See the DirectX Shader Compiler for documentation for DXIL details:
26   * https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst
27   */
28 
29 #ifndef DXIL_MODULE_H
30 #define DXIL_MODULE_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include "dxil_buffer.h"
37 #include "dxil_signature.h"
38 
39 #include "util/list.h"
40 
41 
42 #define DXIL_SHADER_MAX_IO_ROWS 80
43 
44 enum dxil_shader_kind {
45    DXIL_PIXEL_SHADER = 0,
46    DXIL_VERTEX_SHADER = 1,
47    DXIL_GEOMETRY_SHADER = 2,
48    DXIL_HULL_SHADER = 3,
49    DXIL_DOMAIN_SHADER = 4,
50    DXIL_COMPUTE_SHADER = 5,
51 };
52 
53 extern int debug_dxil;
54 
55 enum dxil_debug_flags {
56    DXIL_DEBUG_VERBOSE    = 1 << 0,
57    DXIL_DEBUG_DUMP_BLOB  = 1 << 1,
58    DXIL_DEBUG_TRACE      = 1 << 2,
59    DXIL_DEBUG_DUMP_MODULE = 1 << 3,
60 };
61 
62 enum dxil_bin_opcode {
63    DXIL_BINOP_ADD = 0,
64    DXIL_BINOP_SUB = 1,
65    DXIL_BINOP_MUL = 2,
66    DXIL_BINOP_UDIV = 3,
67    DXIL_BINOP_SDIV = 4,
68    DXIL_BINOP_UREM = 5,
69    DXIL_BINOP_SREM = 6,
70    DXIL_BINOP_SHL = 7,
71    DXIL_BINOP_LSHR = 8,
72    DXIL_BINOP_ASHR = 9,
73    DXIL_BINOP_AND = 10,
74    DXIL_BINOP_OR = 11,
75    DXIL_BINOP_XOR = 12,
76    DXIL_BINOP_INSTR_COUNT
77 };
78 
79 enum dxil_cast_opcode {
80    DXIL_CAST_TRUNC = 0,
81    DXIL_CAST_ZEXT = 1,
82    DXIL_CAST_SEXT = 2,
83    DXIL_CAST_FPTOUI = 3,
84    DXIL_CAST_FPTOSI = 4,
85    DXIL_CAST_UITOFP = 5,
86    DXIL_CAST_SITOFP = 6,
87    DXIL_CAST_FPTRUNC = 7,
88    DXIL_CAST_FPEXT = 8,
89    DXIL_CAST_PTRTOINT = 9,
90    DXIL_CAST_INTTOPTR = 10,
91    DXIL_CAST_BITCAST = 11,
92    DXIL_CAST_ADDRSPACECAST = 12,
93    DXIL_CAST_INSTR_COUNT
94 };
95 
96 enum dxil_cmp_pred {
97    DXIL_FCMP_FALSE = 0,
98    DXIL_FCMP_OEQ = 1,
99    DXIL_FCMP_OGT = 2,
100    DXIL_FCMP_OGE = 3,
101    DXIL_FCMP_OLT = 4,
102    DXIL_FCMP_OLE = 5,
103    DXIL_FCMP_ONE = 6,
104    DXIL_FCMP_ORD = 7,
105    DXIL_FCMP_UNO = 8,
106    DXIL_FCMP_UEQ = 9,
107    DXIL_FCMP_UGT = 10,
108    DXIL_FCMP_UGE = 11,
109    DXIL_FCMP_ULT = 12,
110    DXIL_FCMP_ULE = 13,
111    DXIL_FCMP_UNE = 14,
112    DXIL_FCMP_TRUE = 15,
113    DXIL_ICMP_EQ = 32,
114    DXIL_ICMP_NE = 33,
115    DXIL_ICMP_UGT = 34,
116    DXIL_ICMP_UGE = 35,
117    DXIL_ICMP_ULT = 36,
118    DXIL_ICMP_ULE = 37,
119    DXIL_ICMP_SGT = 38,
120    DXIL_ICMP_SGE = 39,
121    DXIL_ICMP_SLT = 40,
122    DXIL_ICMP_SLE = 41,
123    DXIL_CMP_INSTR_COUNT
124 };
125 
126 enum dxil_opt_flags {
127   DXIL_UNSAFE_ALGEBRA = (1 << 0),
128   DXIL_NO_NANS = (1 << 1),
129   DXIL_NO_INFS = (1 << 2),
130   DXIL_NO_SIGNED_ZEROS = (1 << 3),
131   DXIL_ALLOW_RECIPROCAL = (1 << 4)
132 };
133 
134 struct dxil_features {
135    unsigned doubles : 1,
136             cs_4x_raw_sb : 1,
137             uavs_at_every_stage : 1,
138             use_64uavs : 1,
139             min_precision : 1,
140             dx11_1_double_extensions : 1,
141             dx11_1_shader_extensions : 1,
142             dx9_comparison_filtering : 1,
143             tiled_resources : 1,
144             stencil_ref : 1,
145             inner_coverage : 1,
146             typed_uav_load_additional_formats : 1,
147             rovs : 1,
148             array_layer_from_vs_or_ds : 1,
149             wave_ops : 1,
150             int64_ops : 1,
151             view_id : 1,
152             barycentrics : 1,
153             native_low_precision : 1,
154             shading_rate : 1,
155             raytracing_tier_1_1 : 1,
156             sampler_feedback : 1;
157 };
158 
159 struct dxil_shader_info {
160    unsigned has_out_position:1;
161    unsigned has_out_depth:1;
162    unsigned has_per_sample_input:1;
163 };
164 
165 struct dxil_module {
166    void *ralloc_ctx;
167    enum dxil_shader_kind shader_kind;
168    unsigned major_version, minor_version;
169    struct dxil_features feats;
170    unsigned raw_and_structured_buffers : 1;
171    struct dxil_shader_info info;
172 
173    struct dxil_buffer buf;
174 
175    unsigned num_sig_inputs;
176    unsigned num_sig_outputs;
177    unsigned num_psv_inputs;
178    unsigned num_psv_outputs;
179 
180    struct dxil_signature_record inputs[DXIL_SHADER_MAX_IO_ROWS];
181    struct dxil_signature_record outputs[DXIL_SHADER_MAX_IO_ROWS];
182 
183    struct dxil_psv_signature_element psv_inputs[DXIL_SHADER_MAX_IO_ROWS];
184    struct dxil_psv_signature_element psv_outputs[DXIL_SHADER_MAX_IO_ROWS];
185 
186    struct _mesa_string_buffer *sem_string_table;
187    struct dxil_psv_sem_index_table sem_index_table;
188 
189    struct {
190       unsigned abbrev_width;
191       intptr_t offset;
192    } blocks[16];
193    size_t num_blocks;
194 
195    struct list_head type_list;
196    struct list_head gvar_list;
197    struct list_head func_list;
198    struct list_head attr_set_list;
199    struct list_head instr_list;
200    struct list_head const_list;
201    struct list_head mdnode_list;
202    struct list_head md_named_node_list;
203    const struct dxil_type *void_type;
204    const struct dxil_type *int1_type, *int8_type, *int16_type,
205                           *int32_type, *int64_type;
206    const struct dxil_type *float16_type, *float32_type, *float64_type;
207 
208    struct rb_tree *functions;
209 
210    int *basic_block_ids; /* maps from "user" ids to LLVM ids */
211    size_t num_basic_block_ids;
212    unsigned curr_block;
213 };
214 
215 struct dxil_instr;
216 struct dxil_value;
217 
218 void
219 dxil_module_init(struct dxil_module *m, void *ralloc_ctx);
220 
221 void
222 dxil_module_release(struct dxil_module *m);
223 
224 const struct dxil_value *
225 dxil_add_global_var(struct dxil_module *m, const char *name,
226                     const struct dxil_type *type,
227                     enum dxil_address_space as, int align,
228                     const struct dxil_value *value);
229 
230 const struct dxil_value *
231 dxil_add_global_ptr_var(struct dxil_module *m, const char *name,
232                         const struct dxil_type *type,
233                         enum dxil_address_space as, int align,
234                         const struct dxil_value *value);
235 
236 const struct dxil_func *
237 dxil_add_function_def(struct dxil_module *m, const char *name,
238                       const struct dxil_type *type);
239 
240 const struct dxil_func *
241 dxil_add_function_decl(struct dxil_module *m, const char *name,
242                        const struct dxil_type *type,
243                        enum dxil_attr_kind attr);
244 
245 const struct dxil_type *
246 dxil_module_get_void_type(struct dxil_module *m);
247 
248 const struct dxil_type *
249 dxil_module_get_int_type(struct dxil_module *m, unsigned bit_size);
250 
251 const struct dxil_type *
252 dxil_module_get_float_type(struct dxil_module *m, unsigned bit_size);
253 
254 const struct dxil_type *
255 dxil_module_get_pointer_type(struct dxil_module *m,
256                              const struct dxil_type *target);
257 
258 const struct dxil_type *
259 dxil_get_overload_type(struct dxil_module *mod, enum overload_type overload);
260 
261 const struct dxil_type *
262 dxil_module_get_handle_type(struct dxil_module *m);
263 
264 const struct dxil_type *
265 dxil_module_get_cbuf_ret_type(struct dxil_module *mod, enum overload_type overload);
266 
267 const struct dxil_type *
268 dxil_module_get_split_double_ret_type(struct dxil_module *mod);
269 
270 const struct dxil_type *
271 dxil_module_get_res_type(struct dxil_module *m, enum dxil_resource_kind kind,
272                          enum dxil_component_type comp_type, bool readwrite);
273 
274 const struct dxil_type *
275 dxil_module_get_resret_type(struct dxil_module *m, enum overload_type overload);
276 
277 const struct dxil_type *
278 dxil_module_get_dimret_type(struct dxil_module *m);
279 
280 const struct dxil_type *
281 dxil_module_get_struct_type(struct dxil_module *m,
282                             const char *name,
283                             const struct dxil_type **elem_types,
284                             size_t num_elem_types);
285 
286 const struct dxil_type *
287 dxil_module_get_array_type(struct dxil_module *m,
288                            const struct dxil_type *elem_type,
289                            size_t num_elems);
290 
291 const struct dxil_type *
292 dxil_module_get_vector_type(struct dxil_module *m,
293                             const struct dxil_type *elem_type,
294                             size_t num_elems);
295 
296 const struct dxil_type *
297 dxil_module_add_function_type(struct dxil_module *m,
298                               const struct dxil_type *ret_type,
299                               const struct dxil_type **arg_types,
300                               size_t num_arg_types);
301 
302 nir_alu_type
303 dxil_type_to_nir_type(const struct dxil_type *type);
304 
305 bool
306 dxil_value_type_equal_to(const struct dxil_value *value,
307                          const struct dxil_type *lhs);
308 
309 bool
310 dxil_value_type_bitsize_equal_to(const struct dxil_value *value, unsigned bitsize);
311 
312 const struct dxil_type *
313 dxil_value_get_type(const struct dxil_value *value);
314 
315 const struct dxil_value *
316 dxil_module_get_int1_const(struct dxil_module *m, bool value);
317 
318 const struct dxil_value *
319 dxil_module_get_int8_const(struct dxil_module *m, int8_t value);
320 
321 const struct dxil_value *
322 dxil_module_get_int16_const(struct dxil_module *m, int16_t value);
323 
324 const struct dxil_value *
325 dxil_module_get_int32_const(struct dxil_module *m, int32_t value);
326 
327 const struct dxil_value *
328 dxil_module_get_int64_const(struct dxil_module *m, int64_t value);
329 
330 const struct dxil_value *
331 dxil_module_get_int_const(struct dxil_module *m, intmax_t value,
332                           unsigned bit_size);
333 
334 const struct dxil_value *
335 dxil_module_get_float16_const(struct dxil_module *m, uint16_t);
336 
337 const struct dxil_value *
338 dxil_module_get_float_const(struct dxil_module *m, float value);
339 
340 const struct dxil_value *
341 dxil_module_get_double_const(struct dxil_module *m, double value);
342 
343 const struct dxil_value *
344 dxil_module_get_array_const(struct dxil_module *m, const struct dxil_type *type,
345                             const struct dxil_value **values);
346 
347 const struct dxil_value *
348 dxil_module_get_undef(struct dxil_module *m, const struct dxil_type *type);
349 
350 const struct dxil_mdnode *
351 dxil_get_metadata_string(struct dxil_module *m, const char *str);
352 
353 const struct dxil_mdnode *
354 dxil_get_metadata_value(struct dxil_module *m, const struct dxil_type *type,
355                         const struct dxil_value *value);
356 
357 const struct dxil_mdnode *
358 dxil_get_metadata_func(struct dxil_module *m, const struct dxil_func *func);
359 
360 const struct dxil_mdnode *
361 dxil_get_metadata_int1(struct dxil_module *m, bool value);
362 
363 const struct dxil_mdnode *
364 dxil_get_metadata_int8(struct dxil_module *m, int8_t value);
365 
366 const struct dxil_mdnode *
367 dxil_get_metadata_int32(struct dxil_module *m, int32_t value);
368 
369 const struct dxil_mdnode *
370 dxil_get_metadata_int64(struct dxil_module *m, int64_t value);
371 
372 const struct dxil_mdnode *
373 dxil_get_metadata_node(struct dxil_module *m,
374                        const struct dxil_mdnode *subnodes[],
375                        size_t num_subnodes);
376 
377 bool
378 dxil_add_metadata_named_node(struct dxil_module *m, const char *name,
379                              const struct dxil_mdnode *subnodes[],
380                              size_t num_subnodes);
381 
382 const struct dxil_value *
383 dxil_emit_binop(struct dxil_module *m, enum dxil_bin_opcode opcode,
384                 const struct dxil_value *op0, const struct dxil_value *op1,
385                 enum dxil_opt_flags flags);
386 
387 const struct dxil_value *
388 dxil_emit_cmp(struct dxil_module *m, enum dxil_cmp_pred pred,
389               const struct dxil_value *op0, const struct dxil_value *op1);
390 
391 const struct dxil_value *
392 dxil_emit_select(struct dxil_module *m,
393                 const struct dxil_value *op0,
394                 const struct dxil_value *op1,
395                 const struct dxil_value *op2);
396 
397 const struct dxil_value *
398 dxil_emit_extractval(struct dxil_module *m, const struct dxil_value *src,
399                      const unsigned int index);
400 
401 const struct dxil_value *
402 dxil_emit_cast(struct dxil_module *m, enum dxil_cast_opcode opcode,
403                const struct dxil_type *type,
404                const struct dxil_value *value);
405 
406 bool
407 dxil_emit_branch(struct dxil_module *m, const struct dxil_value *cond,
408                  unsigned true_block, unsigned false_block);
409 
410 const struct dxil_value *
411 dxil_instr_get_return_value(struct dxil_instr *instr);
412 
413 struct dxil_instr *
414 dxil_emit_phi(struct dxil_module *m, const struct dxil_type *type);
415 
416 void
417 dxil_phi_set_incoming(struct dxil_instr *instr,
418                       const struct dxil_value *incoming_values[],
419                       const unsigned incoming_blocks[],
420                       size_t num_incoming);
421 
422 const struct dxil_value *
423 dxil_emit_call(struct dxil_module *m,
424                const struct dxil_func *func,
425                const struct dxil_value **args, size_t num_args);
426 
427 bool
428 dxil_emit_call_void(struct dxil_module *m,
429                     const struct dxil_func *func,
430                     const struct dxil_value **args, size_t num_args);
431 
432 bool
433 dxil_emit_ret_void(struct dxil_module *m);
434 
435 const struct dxil_value *
436 dxil_emit_alloca(struct dxil_module *m, const struct dxil_type *alloc_type,
437                  const struct dxil_type *size_type,
438                  const struct dxil_value *size,
439                  unsigned int align);
440 
441 const struct dxil_value *
442 dxil_emit_gep_inbounds(struct dxil_module *m,
443                        const struct dxil_value **operands,
444                        size_t num_operands);
445 
446 const struct dxil_value *
447 dxil_emit_load(struct dxil_module *m, const struct dxil_value *ptr,
448                unsigned align,
449                bool is_volatile);
450 
451 bool
452 dxil_emit_store(struct dxil_module *m, const struct dxil_value *value,
453                 const struct dxil_value *ptr, unsigned align,
454                 bool is_volatile);
455 
456 const struct dxil_value *
457 dxil_emit_cmpxchg(struct dxil_module *m, const struct dxil_value *cmpval,
458                   const struct dxil_value *newval,
459                   const struct dxil_value *ptr, bool is_volatile,
460                   enum dxil_atomic_ordering ordering,
461                   enum dxil_sync_scope syncscope);
462 
463 const struct dxil_value *
464 dxil_emit_atomicrmw(struct dxil_module *m, const struct dxil_value *value,
465                     const struct dxil_value *ptr, enum dxil_rmw_op op,
466                     bool is_volatile, enum dxil_atomic_ordering ordering,
467                     enum dxil_sync_scope syncscope);
468 
469 bool
470 dxil_emit_module(struct dxil_module *m);
471 
472 #ifdef __cplusplus
473 }
474 #endif
475 
476 #endif
477