swr/rast: ignore CreateElementUnorderedAtomicMemCpy
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / gen_llvm_ir_macros.py
1 # Copyright (C) 2014-2018 Intel Corporation. All Rights Reserved.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
12 # Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE.
21
22 from __future__ import print_function
23 import os, sys, re
24 from gen_common import *
25 from argparse import FileType
26
27 inst_aliases = {
28 'SHUFFLE_VECTOR': 'VSHUFFLE',
29 'INSERT_ELEMENT': 'VINSERT',
30 'EXTRACT_ELEMENT': 'VEXTRACT',
31 'MEM_SET': 'MEMSET',
32 'MEM_CPY': 'MEMCOPY',
33 'MEM_MOVE': 'MEMMOVE',
34 'L_SHR': 'LSHR',
35 'A_SHR': 'ASHR',
36 'BIT_CAST': 'BITCAST',
37 'U_DIV': 'UDIV',
38 'S_DIV': 'SDIV',
39 'U_REM': 'UREM',
40 'S_REM': 'SREM',
41 'BIN_OP': 'BINOP',
42 }
43
44 intrinsics = [
45 ['VGATHERPD', ['src', 'pBase', 'indices', 'mask', 'scale'], 'src'],
46 ['VGATHERPS', ['src', 'pBase', 'indices', 'mask', 'scale'], 'src'],
47 ['VGATHERDD', ['src', 'pBase', 'indices', 'mask', 'scale'], 'src'],
48 ['VRCPPS', ['a'], 'a'],
49 ['VROUND', ['a', 'rounding'], 'a'],
50 ['BEXTR_32', ['src', 'control'], 'src'],
51 ['VPSHUFB', ['a', 'b'], 'a'],
52 ['VPERMD', ['a', 'idx'], 'a'],
53 ['VPERMPS', ['idx', 'a'], 'a'],
54 ['VCVTPD2PS', ['a'], 'VectorType::get(mFP32Ty, a->getType()->getVectorNumElements())'],
55 ['VCVTPH2PS', ['a'], 'VectorType::get(mFP32Ty, a->getType()->getVectorNumElements())'],
56 ['VCVTPS2PH', ['a', 'round'], 'mSimdInt16Ty'],
57 ['VHSUBPS', ['a', 'b'], 'a'],
58 ['VPTESTC', ['a', 'b'], 'mInt32Ty'],
59 ['VPTESTZ', ['a', 'b'], 'mInt32Ty'],
60 ['VPHADDD', ['a', 'b'], 'a'],
61 ['PDEP32', ['a', 'b'], 'a'],
62 ['RDTSC', [], 'mInt64Ty'],
63 ]
64
65 llvm_intrinsics = [
66 ['CTTZ', 'cttz', ['a', 'flag'], ['a']],
67 ['CTLZ', 'ctlz', ['a', 'flag'], ['a']],
68 ['VSQRTPS', 'sqrt', ['a'], ['a']],
69 ['STACKSAVE', 'stacksave', [], []],
70 ['STACKRESTORE', 'stackrestore', ['a'], []],
71 ['VMINPS', 'minnum', ['a', 'b'], ['a']],
72 ['VMAXPS', 'maxnum', ['a', 'b'], ['a']],
73 ['VFMADDPS', 'fmuladd', ['a', 'b', 'c'], ['a']],
74 ['DEBUGTRAP', 'debugtrap', [], []],
75 ['POPCNT', 'ctpop', ['a'], ['a']],
76 ['LOG2', 'log2', ['a'], ['a']],
77 ['FABS', 'fabs', ['a'], ['a']],
78 ['EXP2', 'exp2', ['a'], ['a']],
79 ['POW', 'pow', ['a', 'b'], ['a']]
80 ]
81
82 this_dir = os.path.dirname(os.path.abspath(__file__))
83 template = os.path.join(this_dir, 'templates', 'gen_builder.hpp')
84
85 def convert_uppercamel(name):
86 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
87 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
88
89 '''
90 Given an input file (e.g. IRBuilder.h) generates function dictionary.
91 '''
92 def parse_ir_builder(input_file):
93
94 functions = []
95
96 lines = input_file.readlines()
97
98 idx = 0
99 while idx < len(lines) - 1:
100 line = lines[idx].rstrip()
101 idx += 1
102
103 #match = re.search(r'\*Create', line)
104 match = re.search(r'[\*\s]Create(\w*)\(', line)
105 if match is not None:
106 #print('Line: %s' % match.group(1))
107
108 if re.search(r'^\s*Create', line) is not None:
109 func_sig = lines[idx-2].rstrip() + line
110 else:
111 func_sig = line
112
113 end_of_args = False
114 while not end_of_args:
115 end_paren = re.search(r'\)', line)
116 if end_paren is not None:
117 end_of_args = True
118 else:
119 line = lines[idx].rstrip()
120 func_sig += line
121 idx += 1
122
123 delfunc = re.search(r'LLVM_DELETED_FUNCTION|= delete;', func_sig)
124
125 if not delfunc:
126 func = re.search(r'(.*?)\*[\n\s]*(Create\w*)\((.*?)\)', func_sig)
127 if func is not None:
128
129 return_type = func.group(1).strip() + '*'
130 func_name = func.group(2)
131 arguments = func.group(3)
132
133 func_args = []
134 arg_names = []
135 args = arguments.split(',')
136 for arg in args:
137 arg = arg.strip()
138 if arg:
139 func_args.append(arg)
140
141 split_args = arg.split('=')
142 arg_name = split_args[0].rsplit(None, 1)[-1]
143
144 reg_arg = re.search(r'[\&\*]*(\w*)', arg_name)
145 if reg_arg:
146 arg_names += [reg_arg.group(1)]
147
148 ignore = False
149
150 # The following functions need to be ignored in openswr.
151 # API change in llvm-5.0 breaks baked autogen files
152 if (
153 (func_name == 'CreateFence' or
154 func_name == 'CreateAtomicCmpXchg' or
155 func_name == 'CreateAtomicRMW')):
156 ignore = True
157
158 # The following functions need to be ignored.
159 if (func_name == 'CreateInsertNUWNSWBinOp' or
160 func_name == 'CreateMaskedIntrinsic' or
161 func_name == 'CreateAlignmentAssumptionHelper' or
162 func_name == 'CreateGEP' or
163 func_name == 'CreateLoad' or
164 func_name == 'CreateMaskedLoad' or
165 func_name == 'CreateElementUnorderedAtomicMemCpy'):
166 ignore = True
167
168 # Convert CamelCase to CAMEL_CASE
169 func_mod = re.search(r'Create(\w*)', func_name)
170 if func_mod:
171 func_mod = func_mod.group(1)
172 func_mod = convert_uppercamel(func_mod)
173 if func_mod[0:2] == 'F_' or func_mod[0:2] == 'I_':
174 func_mod = func_mod[0] + func_mod[2:]
175
176 # Substitute alias based on CAMEL_CASE name.
177 func_alias = inst_aliases.get(func_mod)
178 if not func_alias:
179 func_alias = func_mod
180
181 if func_name == 'CreateCall' or func_name == 'CreateGEP':
182 arglist = re.search(r'ArrayRef', ', '.join(func_args))
183 if arglist:
184 func_alias = func_alias + 'A'
185
186 if not ignore:
187 functions.append({
188 'name' : func_name,
189 'alias' : func_alias,
190 'return' : return_type,
191 'args' : ', '.join(func_args),
192 'arg_names' : arg_names,
193 })
194
195 return functions
196
197 '''
198 Auto-generates macros for LLVM IR
199 '''
200 def generate_gen_h(functions, output_dir):
201 filename = 'gen_builder.hpp'
202 output_filename = os.path.join(output_dir, filename)
203
204 templfuncs = []
205 for func in functions:
206 decl = '%s %s(%s)' % (func['return'], func['alias'], func['args'])
207
208 templfuncs.append({
209 'decl' : decl,
210 'intrin' : func['name'],
211 'args' : func['arg_names'],
212 })
213
214 MakoTemplateWriter.to_file(
215 template,
216 output_filename,
217 cmdline=sys.argv,
218 comment='Builder IR Wrappers',
219 filename=filename,
220 functions=templfuncs,
221 isX86=False, isIntrin=False)
222
223 '''
224 Auto-generates macros for LLVM IR
225 '''
226 def generate_meta_h(output_dir):
227 filename = 'gen_builder_meta.hpp'
228 output_filename = os.path.join(output_dir, filename)
229
230 functions = []
231 for inst in intrinsics:
232 name = inst[0]
233 args = inst[1]
234 ret = inst[2]
235
236 #print('Inst: %s, x86: %s numArgs: %d' % (inst[0], inst[1], len(inst[2])))
237 if len(args) != 0:
238 declargs = 'Value* ' + ', Value* '.join(args)
239 decl = 'Value* %s(%s, const llvm::Twine& name = "")' % (name, declargs)
240 else:
241 decl = 'Value* %s(const llvm::Twine& name = "")' % (name)
242
243 # determine the return type of the intrinsic. It can either be:
244 # - type of one of the input arguments
245 # - snippet of code to set the return type
246
247 if ret in args:
248 returnTy = ret + '->getType()'
249 else:
250 returnTy = ret
251
252 functions.append({
253 'decl' : decl,
254 'name' : name,
255 'args' : args,
256 'returnType': returnTy
257 })
258
259 MakoTemplateWriter.to_file(
260 template,
261 output_filename,
262 cmdline=sys.argv,
263 comment='meta intrinsics',
264 filename=filename,
265 functions=functions,
266 isX86=True, isIntrin=False)
267
268 def generate_intrin_h(output_dir):
269 filename = 'gen_builder_intrin.hpp'
270 output_filename = os.path.join(output_dir, filename)
271
272 functions = []
273 for inst in llvm_intrinsics:
274 #print('Inst: %s, x86: %s numArgs: %d' % (inst[0], inst[1], len(inst[2])))
275 if len(inst[2]) != 0:
276 declargs = 'Value* ' + ', Value* '.join(inst[2])
277 decl = 'Value* %s(%s, const llvm::Twine& name = "")' % (inst[0], declargs)
278 else:
279 decl = 'Value* %s(const llvm::Twine& name = "")' % (inst[0])
280
281 functions.append({
282 'decl' : decl,
283 'intrin' : inst[1],
284 'args' : inst[2],
285 'types' : inst[3],
286 })
287
288 MakoTemplateWriter.to_file(
289 template,
290 output_filename,
291 cmdline=sys.argv,
292 comment='llvm intrinsics',
293 filename=filename,
294 functions=functions,
295 isX86=False, isIntrin=True)
296 '''
297 Function which is invoked when this script is started from a command line.
298 Will present and consume a set of arguments which will tell this script how
299 to behave
300 '''
301 def main():
302
303 # Parse args...
304 parser = ArgumentParser()
305 parser.add_argument('--input', '-i', type=FileType('r'), help='Path to IRBuilder.h', required=False)
306 parser.add_argument('--output-dir', '-o', action='store', dest='output', help='Path to output directory', required=True)
307 parser.add_argument('--gen_h', help='Generate builder_gen.h', action='store_true', default=False)
308 parser.add_argument('--gen_meta_h', help='Generate meta intrinsics. No input is needed.', action='store_true', default=False)
309 parser.add_argument('--gen_intrin_h', help='Generate llvm intrinsics. No input is needed.', action='store_true', default=False)
310 args = parser.parse_args()
311
312 if not os.path.exists(args.output):
313 os.makedirs(args.output)
314
315 final_output_dir = args.output
316 args.output = MakeTmpDir('_codegen')
317
318 rval = 0
319 try:
320 if args.input:
321 functions = parse_ir_builder(args.input)
322
323 if args.gen_h:
324 generate_gen_h(functions, args.output)
325
326 elif args.gen_h:
327 print('Need to specify --input for --gen_h!')
328
329 if args.gen_meta_h:
330 generate_meta_h(args.output)
331
332 if args.gen_intrin_h:
333 generate_intrin_h(args.output)
334
335 rval = CopyDirFilesIfDifferent(args.output, final_output_dir)
336
337 except:
338 print('ERROR: Could not generate llvm_ir_macros', file=sys.stderr)
339 rval = 1
340
341 finally:
342 DeleteDirTree(args.output)
343
344 return rval
345
346 if __name__ == '__main__':
347 sys.exit(main())
348 # END OF FILE