Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/nir/nir_builder_opcodes_h.py
4545 views
1
from __future__ import print_function
2
3
template = """\
4
/* Copyright (C) 2015 Broadcom
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice (including the next
14
* paragraph) shall be included in all copies or substantial portions of the
15
* Software.
16
*
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
* IN THE SOFTWARE.
24
*/
25
26
#ifndef _NIR_BUILDER_OPCODES_
27
#define _NIR_BUILDER_OPCODES_
28
29
<%
30
def src_decl_list(num_srcs):
31
return ', '.join('nir_ssa_def *src' + str(i) for i in range(num_srcs))
32
33
def src_list(num_srcs):
34
if num_srcs <= 4:
35
return ', '.join('src' + str(i) if i < num_srcs else 'NULL' for i in range(4))
36
else:
37
return ', '.join('src' + str(i) for i in range(num_srcs))
38
%>
39
40
% for name, opcode in sorted(opcodes.items()):
41
static inline nir_ssa_def *
42
nir_${name}(nir_builder *build, ${src_decl_list(opcode.num_inputs)})
43
{
44
% if opcode.num_inputs <= 4:
45
return nir_build_alu(build, nir_op_${name}, ${src_list(opcode.num_inputs)});
46
% else:
47
nir_ssa_def *srcs[${opcode.num_inputs}] = {${src_list(opcode.num_inputs)}};
48
return nir_build_alu_src_arr(build, nir_op_${name}, srcs);
49
% endif
50
}
51
% endfor
52
53
% for name, opcode in sorted(INTR_OPCODES.items()):
54
struct _nir_${name}_indices {
55
int _; /* exists to avoid empty initializers */
56
% for index in opcode.indices:
57
${index.c_data_type} ${index.name};
58
% endfor
59
};
60
% endfor
61
62
<%
63
def intrinsic_decl_list(opcode):
64
need_components = opcode.dest_components == 0 and \
65
0 not in opcode.src_components
66
67
res = ''
68
if (opcode.has_dest or opcode.num_srcs) and need_components:
69
res += ', unsigned num_components'
70
if opcode.has_dest and len(opcode.bit_sizes) != 1 and opcode.bit_size_src == -1:
71
res += ', unsigned bit_size'
72
for i in range(opcode.num_srcs):
73
res += ', nir_ssa_def *src' + str(i)
74
if opcode.indices:
75
res += ', struct _nir_' + opcode.name + '_indices indices'
76
return res
77
78
def intrinsic_macro_list(opcode):
79
need_components = opcode.dest_components == 0 and \
80
0 not in opcode.src_components
81
82
res = ''
83
if (opcode.has_dest or opcode.num_srcs) and need_components:
84
res += ', num_components'
85
if opcode.has_dest and len(opcode.bit_sizes) != 1 and opcode.bit_size_src == -1:
86
res += ', bit_size'
87
for i in range(opcode.num_srcs):
88
res += ', src' + str(i)
89
return res
90
91
def get_intrinsic_bitsize(opcode):
92
if len(opcode.bit_sizes) == 1:
93
return str(opcode.bit_sizes[0])
94
elif opcode.bit_size_src != -1:
95
return 'src' + str(opcode.bit_size_src) + '->bit_size'
96
else:
97
return 'bit_size'
98
%>
99
100
% for name, opcode in sorted(INTR_OPCODES.items()):
101
% if opcode.has_dest:
102
static inline nir_ssa_def *
103
% else:
104
static inline nir_intrinsic_instr *
105
% endif
106
_nir_build_${name}(nir_builder *build${intrinsic_decl_list(opcode)})
107
{
108
nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(
109
build->shader, nir_intrinsic_${name});
110
111
% if 0 in opcode.src_components:
112
intrin->num_components = src${opcode.src_components.index(0)}->num_components;
113
% elif opcode.dest_components == 0:
114
intrin->num_components = num_components;
115
% endif
116
% if opcode.has_dest:
117
% if opcode.dest_components == 0:
118
nir_ssa_dest_init(&intrin->instr, &intrin->dest, intrin->num_components, ${get_intrinsic_bitsize(opcode)}, NULL);
119
% else:
120
nir_ssa_dest_init(&intrin->instr, &intrin->dest, ${opcode.dest_components}, ${get_intrinsic_bitsize(opcode)}, NULL);
121
% endif
122
% endif
123
% for i in range(opcode.num_srcs):
124
intrin->src[${i}] = nir_src_for_ssa(src${i});
125
% endfor
126
% for index in opcode.indices:
127
nir_intrinsic_set_${index.name}(intrin, indices.${index.name});
128
% endfor
129
130
nir_builder_instr_insert(build, &intrin->instr);
131
% if opcode.has_dest:
132
return &intrin->dest.ssa;
133
% else:
134
return intrin;
135
% endif
136
}
137
% endfor
138
139
% for name, opcode in sorted(INTR_OPCODES.items()):
140
% if opcode.indices:
141
#ifdef __cplusplus
142
#define nir_build_${name}(build${intrinsic_macro_list(opcode)}, ...) ${'\\\\'}
143
_nir_build_${name}(build${intrinsic_macro_list(opcode)}, _nir_${name}_indices{0, __VA_ARGS__})
144
#else
145
#define nir_build_${name}(build${intrinsic_macro_list(opcode)}, ...) ${'\\\\'}
146
_nir_build_${name}(build${intrinsic_macro_list(opcode)}, (struct _nir_${name}_indices){0, __VA_ARGS__})
147
#endif
148
% else:
149
#define nir_build_${name} _nir_build_${name}
150
% endif
151
#define nir_${name} nir_build_${name}
152
% endfor
153
154
#endif /* _NIR_BUILDER_OPCODES_ */"""
155
156
from nir_opcodes import opcodes
157
from nir_intrinsics import INTR_OPCODES
158
from mako.template import Template
159
160
print(Template(template).render(opcodes=opcodes, INTR_OPCODES=INTR_OPCODES))
161
162