xref: /aosp_15_r20/external/mesa3d/src/microsoft/compiler/dxil_function.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 #include "dxil_function.h"
25 #include "dxil_module.h"
26 
27 #define MAX_FUNC_PARAMS 18
28 
29 struct predefined_func_descr {
30    const char *base_name;
31    const char *retval_descr;
32    const char *param_descr;
33    enum dxil_attr_kind attr;
34 };
35 
36 static struct  predefined_func_descr predefined_funcs[] = {
37 {"dx.op.atomicBinOp", "O", "i@iiiii", DXIL_ATTR_KIND_READ_NONE},
38 {"dx.op.cbufferLoad", "O", "i@ii", DXIL_ATTR_KIND_READ_ONLY},
39 {"dx.op.cbufferLoadLegacy", "B", "i@i", DXIL_ATTR_KIND_READ_ONLY},
40 {"dx.op.createHandle", "@", "iciib", DXIL_ATTR_KIND_READ_ONLY},
41 {"dx.op.storeOutput", "v", "iiicO", DXIL_ATTR_KIND_NO_UNWIND},
42 {"dx.op.loadInput", "O", "iiici", DXIL_ATTR_KIND_READ_NONE},
43 {"dx.op.tertiary", "O", "iOOO", DXIL_ATTR_KIND_READ_NONE},
44 {"dx.op.quaternary", "O", "iOOOO", DXIL_ATTR_KIND_READ_NONE},
45 {"dx.op.threadId", "i", "ii", DXIL_ATTR_KIND_READ_NONE},
46 {"dx.op.threadIdInGroup", "i", "ii", DXIL_ATTR_KIND_READ_NONE},
47 {"dx.op.flattenedThreadIdInGroup", "i", "i", DXIL_ATTR_KIND_READ_NONE},
48 {"dx.op.groupId", "i", "ii", DXIL_ATTR_KIND_READ_NONE},
49 {"dx.op.unary", "O", "iO", DXIL_ATTR_KIND_READ_NONE},
50 {"dx.op.unaryBits", "i", "iO", DXIL_ATTR_KIND_READ_NONE},
51 {"dx.op.isSpecialFloat", "b", "iO", DXIL_ATTR_KIND_READ_NONE},
52 {"dx.op.binary", "O", "iOO", DXIL_ATTR_KIND_READ_NONE},
53 {"dx.op.bufferStore", "v", "i@iiOOOOc", DXIL_ATTR_KIND_NONE},
54 {"dx.op.rawBufferStore", "v", "i@iiOOOOci", DXIL_ATTR_KIND_NONE},
55 {"dx.op.bufferLoad", "R", "i@ii", DXIL_ATTR_KIND_READ_ONLY},
56 {"dx.op.rawBufferLoad", "R", "i@iici", DXIL_ATTR_KIND_READ_ONLY},
57 {"dx.op.attributeAtVertex", "O", "iiicc", DXIL_ATTR_KIND_READ_NONE},
58 {"dx.op.sample", "R", "i@@ffffiiif", DXIL_ATTR_KIND_READ_ONLY},
59 {"dx.op.sampleBias", "R", "i@@ffffiiiff", DXIL_ATTR_KIND_READ_ONLY},
60 {"dx.op.sampleLevel", "R", "i@@ffffiiif", DXIL_ATTR_KIND_READ_ONLY},
61 {"dx.op.sampleGrad", "R", "i@@ffffiiifffffff", DXIL_ATTR_KIND_READ_ONLY},
62 {"dx.op.sampleCmp", "R", "i@@ffffiiiff", DXIL_ATTR_KIND_READ_ONLY},
63 {"dx.op.sampleCmpLevel", "R", "i@@ffffiiiff", DXIL_ATTR_KIND_READ_ONLY},
64 {"dx.op.sampleCmpLevelZero", "R", "i@@ffffiiif", DXIL_ATTR_KIND_READ_ONLY},
65 {"dx.op.sampleCmpBias", "R", "i@@ffffiiifff", DXIL_ATTR_KIND_READ_ONLY},
66 {"dx.op.sampleCmpGrad", "R", "i@@ffffiiiffffffff", DXIL_ATTR_KIND_READ_ONLY},
67 {"dx.op.textureLoad", "R", "i@iiiiiii", DXIL_ATTR_KIND_READ_ONLY},
68 {"dx.op.textureGather", "R", "i@@ffffiii", DXIL_ATTR_KIND_READ_ONLY},
69 {"dx.op.textureGatherCmp", "R", "i@@ffffiiif", DXIL_ATTR_KIND_READ_ONLY},
70 {"dx.op.discard", "v", "ib", DXIL_ATTR_KIND_NO_UNWIND},
71 {"dx.op.sampleIndex", "i", "i", DXIL_ATTR_KIND_READ_NONE},
72 {"dx.op.emitStream", "v", "ic", DXIL_ATTR_KIND_NONE},
73 {"dx.op.cutStream", "v", "ic", DXIL_ATTR_KIND_NONE},
74 {"dx.op.getDimensions", "D", "i@i", DXIL_ATTR_KIND_READ_ONLY},
75 {"dx.op.calculateLOD", "f", "i@@fffb", DXIL_ATTR_KIND_READ_ONLY},
76 {"dx.op.barrier", "v", "ii", DXIL_ATTR_KIND_NO_DUPLICATE},
77 {"dx.op.atomicCompareExchange", "O", "i@iiiii", DXIL_ATTR_KIND_READ_NONE},
78 {"dx.op.textureStore", "v", "i@iiiOOOOc", DXIL_ATTR_KIND_NONE},
79 {"dx.op.primitiveID", "i", "i", DXIL_ATTR_KIND_READ_NONE},
80 {"dx.op.outputControlPointID", "i", "i", DXIL_ATTR_KIND_READ_NONE},
81 {"dx.op.gsInstanceID", "i", "i", DXIL_ATTR_KIND_READ_NONE},
82 {"dx.op.viewID", "i", "i", DXIL_ATTR_KIND_READ_NONE},
83 {"dx.op.domainLocation", "f", "ic", DXIL_ATTR_KIND_READ_NONE},
84 {"dx.op.legacyF16ToF32", "f", "ii", DXIL_ATTR_KIND_READ_ONLY},
85 {"dx.op.legacyF32ToF16", "i", "if", DXIL_ATTR_KIND_READ_ONLY},
86 {"dx.op.makeDouble", "g", "iii", DXIL_ATTR_KIND_READ_NONE},
87 {"dx.op.splitDouble", "G", "ig", DXIL_ATTR_KIND_READ_NONE},
88 {"dx.op.texture2DMSGetSamplePosition", "S", "i@i", DXIL_ATTR_KIND_READ_ONLY},
89 {"dx.op.renderTargetGetSamplePosition", "S", "ii", DXIL_ATTR_KIND_READ_ONLY},
90 {"dx.op.evalSnapped", "O", "iiicii", DXIL_ATTR_KIND_READ_NONE},
91 {"dx.op.evalCentroid", "O", "iiic", DXIL_ATTR_KIND_READ_NONE},
92 {"dx.op.evalSampleIndex", "O", "iiici", DXIL_ATTR_KIND_READ_NONE},
93 {"dx.op.coverage", "i", "i", DXIL_ATTR_KIND_READ_NONE},
94 {"dx.op.storePatchConstant", "v", "iiicO", DXIL_ATTR_KIND_NO_UNWIND},
95 {"dx.op.loadPatchConstant", "O", "iiic", DXIL_ATTR_KIND_READ_NONE},
96 {"dx.op.loadOutputControlPoint", "O", "iiici", DXIL_ATTR_KIND_READ_NONE},
97 {"dx.op.createHandleFromBinding", "@", "i#ib", DXIL_ATTR_KIND_READ_NONE},
98 {"dx.op.createHandleFromHeap", "@", "iibb", DXIL_ATTR_KIND_READ_NONE},
99 {"dx.op.annotateHandle", "@", "i@P", DXIL_ATTR_KIND_READ_NONE},
100 {"dx.op.isHelperLane", "b", "i", DXIL_ATTR_KIND_READ_ONLY},
101 {"dx.op.waveIsFirstLane", "b", "i", DXIL_ATTR_KIND_NO_UNWIND},
102 {"dx.op.waveGetLaneIndex", "i", "i", DXIL_ATTR_KIND_READ_NONE},
103 {"dx.op.waveGetLaneCount", "i", "i", DXIL_ATTR_KIND_READ_NONE},
104 {"dx.op.waveReadLaneFirst", "O", "iO", DXIL_ATTR_KIND_NO_UNWIND},
105 {"dx.op.waveReadLaneAt", "O", "iOi", DXIL_ATTR_KIND_NO_UNWIND},
106 {"dx.op.waveAnyTrue", "b", "ib", DXIL_ATTR_KIND_NO_UNWIND},
107 {"dx.op.waveAllTrue", "b", "ib", DXIL_ATTR_KIND_NO_UNWIND},
108 {"dx.op.waveActiveAllEqual", "b", "iO", DXIL_ATTR_KIND_NO_UNWIND},
109 {"dx.op.waveActiveBallot", "F", "ib", DXIL_ATTR_KIND_NO_UNWIND},
110 {"dx.op.waveActiveOp", "O", "iOcc", DXIL_ATTR_KIND_NO_UNWIND},
111 {"dx.op.waveActiveBit", "O", "iOc", DXIL_ATTR_KIND_NO_UNWIND},
112 {"dx.op.wavePrefixOp", "O", "iOcc", DXIL_ATTR_KIND_NO_UNWIND},
113 {"dx.op.quadReadLaneAt", "O", "iOi", DXIL_ATTR_KIND_NO_UNWIND},
114 {"dx.op.quadOp", "O", "iOc", DXIL_ATTR_KIND_NO_UNWIND},
115 {"dx.op.dot4AddPacked", "i", "iiii", DXIL_ATTR_KIND_READ_NONE},
116 {"dx.op.startVertexLocation", "i", "i", DXIL_ATTR_KIND_READ_NONE},
117 {"dx.op.startInstanceLocation", "i", "i", DXIL_ATTR_KIND_READ_NONE},
118 };
119 
120 struct func_descr {
121    const char *name;
122    enum overload_type overload;
123 };
124 
125 struct func_rb_node {
126    struct rb_node node;
127    const struct dxil_func *func;
128    struct func_descr descr;
129 };
130 
131 static inline
132 const struct func_rb_node *
func_rb_node(const struct rb_node * n)133 func_rb_node(const struct rb_node *n)
134 {
135    return (const struct func_rb_node *)n;
136 }
137 
138 static int
func_compare_to_name_and_overload(const struct rb_node * node,const void * data)139 func_compare_to_name_and_overload(const struct rb_node *node, const void *data)
140 {
141    const struct func_descr *descr = (const struct func_descr *)data;
142    const struct func_rb_node *f = func_rb_node(node);
143    if (f->descr.overload < descr->overload)
144       return -1;
145    if (f->descr.overload > descr->overload)
146       return 1;
147 
148    return strcmp(f->descr.name, descr->name);
149 }
150 
151 static const struct dxil_func *
allocate_function_from_predefined(struct dxil_module * mod,const char * name,enum overload_type overload)152 allocate_function_from_predefined(struct dxil_module *mod,
153                                        const char *name,
154                                        enum overload_type overload)
155 {
156    for (unsigned i = 0; i < ARRAY_SIZE(predefined_funcs); ++i) {
157       if (!strcmp(predefined_funcs[i].base_name, name)) {
158          return dxil_alloc_func(mod, name, overload,
159                                 predefined_funcs[i].retval_descr,
160                                 predefined_funcs[i].param_descr,
161                                 predefined_funcs[i].attr);
162       }
163    }
164    unreachable("Invalid function name");
165 }
166 
167 const struct dxil_func *
dxil_get_function(struct dxil_module * mod,const char * name,enum overload_type overload)168 dxil_get_function(struct dxil_module *mod,
169                   const char *name, enum overload_type overload)
170 {
171    struct func_descr descr = { name, overload };
172    const struct rb_node *node = rb_tree_search(mod->functions, &descr,
173                                                func_compare_to_name_and_overload);
174    if (node)
175       return func_rb_node(node)->func;
176 
177    return allocate_function_from_predefined(mod, name, overload);
178 }
179 
func_compare_name(const struct rb_node * lhs,const struct rb_node * rhs)180 static int func_compare_name(const struct rb_node *lhs, const struct rb_node *rhs)
181 {
182    const struct func_rb_node *node = func_rb_node(rhs);
183    return func_compare_to_name_and_overload(lhs, &node->descr);
184 }
185 
186 static void
dxil_add_function(struct rb_tree * functions,const struct dxil_func * func,const char * name,enum overload_type overload)187 dxil_add_function(struct rb_tree *functions, const struct dxil_func *func,
188                   const char *name, enum overload_type overload)
189 {
190    struct func_rb_node *f = rzalloc(functions, struct func_rb_node);
191    f->func = func;
192    f->descr.name = name;
193    f->descr.overload = overload;
194    rb_tree_insert(functions, &f->node, func_compare_name);
195 }
196 
197 static const struct dxil_type *
get_type_from_string(struct dxil_module * mod,const char * param_descr,enum overload_type overload,int * idx)198 get_type_from_string(struct dxil_module *mod, const char *param_descr,
199                      enum overload_type overload,  int *idx)
200 {
201    assert(param_descr);
202    char type_id = param_descr[(*idx)++];
203    assert(*idx <= (int)strlen(param_descr));
204 
205    switch (type_id) {
206    case DXIL_FUNC_PARAM_INT64: return dxil_module_get_int_type(mod, 64);
207    case DXIL_FUNC_PARAM_INT32: return dxil_module_get_int_type(mod, 32);
208    case DXIL_FUNC_PARAM_INT16: return dxil_module_get_int_type(mod, 16);
209    case DXIL_FUNC_PARAM_INT8: return dxil_module_get_int_type(mod, 8);
210    case DXIL_FUNC_PARAM_BOOL: return dxil_module_get_int_type(mod, 1);
211    case DXIL_FUNC_PARAM_FLOAT64: return dxil_module_get_float_type(mod, 64);
212    case DXIL_FUNC_PARAM_FLOAT32: return dxil_module_get_float_type(mod, 32);
213    case DXIL_FUNC_PARAM_FLOAT16: return dxil_module_get_float_type(mod, 16);
214    case DXIL_FUNC_PARAM_HANDLE: return dxil_module_get_handle_type(mod);
215    case DXIL_FUNC_PARAM_VOID: return dxil_module_get_void_type(mod);
216    case DXIL_FUNC_PARAM_FROM_OVERLOAD:  return dxil_get_overload_type(mod, overload);
217    case DXIL_FUNC_PARAM_RESRET: return dxil_module_get_resret_type(mod, overload);
218    case DXIL_FUNC_PARAM_DIM: return dxil_module_get_dimret_type(mod);
219    case DXIL_FUNC_PARAM_SAMPLE_POS: return dxil_module_get_samplepos_type(mod);
220    case DXIL_FUNC_PARAM_CBUF_RET: return dxil_module_get_cbuf_ret_type(mod, overload);
221    case DXIL_FUNC_PARAM_SPLIT_DOUBLE: return dxil_module_get_split_double_ret_type(mod);
222    case DXIL_FUNC_PARAM_RES_BIND: return dxil_module_get_res_bind_type(mod);
223    case DXIL_FUNC_PARAM_RES_PROPS: return dxil_module_get_res_props_type(mod);
224    case DXIL_FUNC_PARAM_POINTER: {
225          const struct dxil_type *target = get_type_from_string(mod, param_descr, overload, idx);
226          return dxil_module_get_pointer_type(mod, target);
227       }
228    case DXIL_FUNC_PARAM_FOURI32: return dxil_module_get_fouri32_type(mod);
229    default:
230       assert(0 && "unknown type identifier");
231    }
232    return NULL;
233 }
234 
235 const struct dxil_func *
dxil_alloc_func_with_rettype(struct dxil_module * mod,const char * name,enum overload_type overload,const struct dxil_type * retval_type,const char * param_descr,enum dxil_attr_kind attr)236 dxil_alloc_func_with_rettype(struct dxil_module *mod, const char *name,
237                              enum overload_type overload,
238                              const struct dxil_type *retval_type,
239                              const char *param_descr,
240                              enum dxil_attr_kind attr)
241 {
242    assert(param_descr);
243    const struct dxil_type *arg_types[MAX_FUNC_PARAMS];
244 
245    int index = 0;
246    unsigned num_params = 0;
247 
248    while (param_descr[num_params]) {
249       const struct dxil_type *t = get_type_from_string(mod, param_descr, overload, &index);
250       if (!t)
251          return false;
252       assert(num_params < MAX_FUNC_PARAMS);
253       arg_types[num_params++] = t;
254    }
255 
256    const struct dxil_type *func_type =
257       dxil_module_add_function_type(mod, retval_type,
258                                     arg_types, num_params);
259    if (!func_type) {
260       fprintf(stderr, "%s: Func type allocation failed\n", __func__);
261       return false;
262    }
263 
264    char full_name[100];
265    snprintf(full_name, sizeof (full_name), "%s%s%s", name,
266             overload == DXIL_NONE ? "" : ".", dxil_overload_suffix(overload));
267    const struct dxil_func *func = dxil_add_function_decl(mod, full_name, func_type, attr);
268 
269    if (func)
270       dxil_add_function(mod->functions, func, name, overload);
271 
272    return func;
273 }
274 
275 const struct dxil_func *
dxil_alloc_func(struct dxil_module * mod,const char * name,enum overload_type overload,const char * retval_type_descr,const char * param_descr,enum dxil_attr_kind attr)276 dxil_alloc_func(struct dxil_module *mod, const char *name, enum overload_type overload,
277                 const char *retval_type_descr,
278                 const char *param_descr, enum dxil_attr_kind attr)
279 {
280 
281    int index = 0;
282    const struct dxil_type *retval_type = get_type_from_string(mod, retval_type_descr, overload, &index);
283    assert(retval_type_descr[index] == 0);
284 
285    return dxil_alloc_func_with_rettype(mod, name, overload, retval_type,
286                                        param_descr, attr);
287 }
288