1 # Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
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:
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
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
22 #!deps/python32/python.exe
29 header
= r
"""/****************************************************************************
30 * Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice (including the next
40 * paragraph) shall be included in all copies or substantial portions of the
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
53 * @brief auto-generated file
57 ******************************************************************************/
63 def gen_file_header(filename
):
65 headerStr
= header
% filename
66 return headerStr
.splitlines()
70 'SHUFFLE_VECTOR': 'VSHUFFLE',
71 'INSERT_ELEMENT': 'VINSERT',
72 'EXTRACT_ELEMENT': 'VEXTRACT',
75 'MEM_MOVE': 'MEMMOVE',
78 'BIT_CAST': 'BITCAST',
87 ["VGATHERPS", "x86_avx2_gather_d_ps_256", ["src", "pBase", "indices", "mask", "scale"]],
88 ["VGATHERDD", "x86_avx2_gather_d_d_256", ["src", "pBase", "indices", "mask", "scale"]],
89 ["VSQRTPS", "x86_avx_sqrt_ps_256", ["a"]],
90 ["VRSQRTPS", "x86_avx_rsqrt_ps_256", ["a"]],
91 ["VRCPPS", "x86_avx_rcp_ps_256", ["a"]],
92 ["VMINPS", "x86_avx_min_ps_256", ["a", "b"]],
93 ["VMAXPS", "x86_avx_max_ps_256", ["a", "b"]],
94 ["VROUND", "x86_avx_round_ps_256", ["a", "rounding"]],
95 ["VCMPPS", "x86_avx_cmp_ps_256", ["a", "b", "cmpop"]],
96 ["VBLENDVPS", "x86_avx_blendv_ps_256", ["a", "b", "mask"]],
97 ["BEXTR_32", "x86_bmi_bextr_32", ["src", "control"]],
98 ["VMASKLOADD", "x86_avx2_maskload_d_256", ["src", "mask"]],
99 ["VMASKMOVPS", "x86_avx_maskload_ps_256", ["src", "mask"]],
100 ["VMASKSTOREPS", "x86_avx_maskstore_ps_256", ["src", "mask", "val"]],
101 ["VPSHUFB", "x86_avx2_pshuf_b", ["a", "b"]],
102 ["VPERMD", "x86_avx2_permd", ["a", "idx"]],
103 ["VPERMPS", "x86_avx2_permps", ["idx", "a"]],
104 ["VCVTPH2PS", "x86_vcvtph2ps_256", ["a"]],
105 ["VCVTPS2PH", "x86_vcvtps2ph_256", ["a", "round"]],
106 ["VHSUBPS", "x86_avx_hsub_ps_256", ["a", "b"]],
107 ["VPTESTC", "x86_avx_ptestc_256", ["a", "b"]],
108 ["VPTESTZ", "x86_avx_ptestz_256", ["a", "b"]],
109 ["VFMADDPS", "x86_fma_vfmadd_ps_256", ["a", "b", "c"]],
110 ["VMOVMSKPS", "x86_avx_movmsk_ps_256", ["a"]],
111 ["INTERRUPT", "x86_int", ["a"]],
114 def convert_uppercamel(name
):
115 s1
= re
.sub('(.)([A-Z][a-z]+)', r
'\1_\2', name
)
116 return re
.sub('([a-z0-9])([A-Z])', r
'\1_\2', s1
).upper()
119 Given an input file (e.g. IRBuilder.h) generates function dictionary.
121 def parse_ir_builder(input_file
):
125 lines
= input_file
.readlines()
128 while idx
< len(lines
) - 1:
129 line
= lines
[idx
].rstrip()
132 #match = re.search(r"\*Create", line)
133 match
= re
.search(r
"[\*\s]Create(\w*)\(", line
)
134 if match
is not None:
135 #print("Line: %s" % match.group(1))
137 if re
.search(r
"^\s*Create", line
) is not None:
138 func_sig
= lines
[idx
-2].rstrip() + line
143 while not end_of_args
:
144 end_paren
= re
.search(r
"\)", line
)
145 if end_paren
is not None:
148 line
= lines
[idx
].rstrip()
152 delfunc
= re
.search(r
"LLVM_DELETED_FUNCTION|= delete;", func_sig
)
155 func
= re
.search(r
"(.*?)\*[\n\s]*(Create\w*)\((.*?)\)", func_sig
)
158 return_type
= func
.group(1).lstrip() + '*'
159 func_name
= func
.group(2)
160 arguments
= func
.group(3)
163 func_args_nodefs
= ''
165 num_args
= arguments
.count(',')
169 args
= arguments
.split(',')
175 func_args_nodefs
+= ', '
177 func_args_nodefs
+= arg
.split(' =')[0]
179 split_args
= arg
.split('=')
180 arg_name
= split_args
[0].rsplit(None, 1)[-1]
182 #print("Before ArgName = %s" % arg_name)
184 reg_arg
= re
.search(r
"[\&\*]*(\w*)", arg_name
)
186 #print("Arg Name = %s" % reg_arg.group(1))
187 arg_names
+= [reg_arg
.group(1)]
193 # The following functions need to be ignored.
194 if func_name
== 'CreateInsertNUWNSWBinOp':
197 if func_name
== 'CreateMaskedIntrinsic':
200 # Convert CamelCase to CAMEL_CASE
201 func_mod
= re
.search(r
"Create(\w*)", func_name
)
203 func_mod
= func_mod
.group(1)
204 func_mod
= convert_uppercamel(func_mod
)
205 if func_mod
[0:2] == 'F_' or func_mod
[0:2] == 'I_':
206 func_mod
= func_mod
[0] + func_mod
[2:]
208 # Substitute alias based on CAMEL_CASE name.
209 func_alias
= inst_aliases
.get(func_mod
)
211 func_alias
= func_mod
213 if func_name
== 'CreateCall' or func_name
== 'CreateGEP':
214 arglist
= re
.search(r
'ArrayRef', func_args
)
216 func_alias
= func_alias
+ 'A'
222 "return": return_type
,
224 "args_nodefs": func_args_nodefs
,
225 "arg_names": arg_names
231 Auto-generates macros for LLVM IR
233 def generate_gen_h(functions
, output_file
):
234 output_lines
= gen_file_header(os
.path
.basename(output_file
.name
))
239 '//////////////////////////////////////////////////////////////////////////',
240 '/// Auto-generated Builder IR declarations',
241 '//////////////////////////////////////////////////////////////////////////',
244 for func
in functions
:
249 '%s%s(%s);' % (func
['return'], name
, func
['args'])
252 output_file
.write('\n'.join(output_lines
) + '\n')
255 Auto-generates macros for LLVM IR
257 def generate_gen_cpp(functions
, output_file
):
258 output_lines
= gen_file_header(os
.path
.basename(output_file
.name
))
261 '#include \"builder.h\"',
265 for func
in functions
:
270 args
= func
['arg_names']
280 '//////////////////////////////////////////////////////////////////////////',
281 '%sBuilder::%s(%s)' % (func
['return'], name
, func
['args_nodefs']),
283 ' return IRB()->%s(%s);' % (func
['name'], func_args
),
288 output_file
.write('\n'.join(output_lines
) + '\n')
291 Auto-generates macros for LLVM IR
293 def generate_x86_h(output_file
):
294 output_lines
= gen_file_header(os
.path
.basename(output_file
.name
))
299 '//////////////////////////////////////////////////////////////////////////',
300 '/// Auto-generated x86 intrinsics',
301 '//////////////////////////////////////////////////////////////////////////',
304 for inst
in intrinsics
:
305 #print("Inst: %s, x86: %s numArgs: %d" % (inst[0], inst[1], len(inst[2])))
312 args
+= ("Value* %s" % arg
)
316 'Value *%s(%s);' % (inst
[0], args
)
319 output_file
.write('\n'.join(output_lines
) + '\n')
322 Auto-generates macros for LLVM IR
324 def generate_x86_cpp(output_file
):
325 output_lines
= gen_file_header(os
.path
.basename(output_file
.name
))
328 '#include \"builder.h\"',
332 for inst
in intrinsics
:
333 #print("Inst: %s, x86: %s numArgs: %d" % (inst[0], inst[1], len(inst[2])))
342 args
+= ("Value* %s" % arg
)
347 '//////////////////////////////////////////////////////////////////////////',
348 'Value *Builder::%s(%s)' % (inst
[0], args
),
350 ' Function *func = Intrinsic::getDeclaration(JM()->mpCurrentModule, Intrinsic::%s);' % inst
[1],
352 if inst
[0] == "VPERMD":
355 for arg
in reversed(inst
[2]):
362 '#if (HAVE_LLVM == 0x306) && (LLVM_VERSION_PATCH == 0)',
363 ' return CALL(func, std::initializer_list<Value*>{%s});' % rev_args
,
367 ' return CALL(func, std::initializer_list<Value*>{%s});' % pass_args
,
369 if inst
[0] == "VPERMD":
378 output_file
.write('\n'.join(output_lines
) + '\n')
381 Function which is invoked when this script is started from a command line.
382 Will present and consume a set of arguments which will tell this script how
388 parser
= argparse
.ArgumentParser()
389 parser
.add_argument("--input", "-i", type=argparse
.FileType('r'), help="Path to IRBuilder.h", required
=False)
390 parser
.add_argument("--output", "-o", type=argparse
.FileType('w'), help="Path to output file", required
=True)
391 parser
.add_argument("--gen_h", "-gen_h", help="Generate builder_gen.h", action
="store_true", default
=False)
392 parser
.add_argument("--gen_cpp", "-gen_cpp", help="Generate builder_gen.cpp", action
="store_true", default
=False)
393 parser
.add_argument("--gen_x86_h", "-gen_x86_h", help="Generate x86 intrinsics. No input is needed.", action
="store_true", default
=False)
394 parser
.add_argument("--gen_x86_cpp", "-gen_x86_cpp", help="Generate x86 intrinsics. No input is needed.", action
="store_true", default
=False)
395 args
= parser
.parse_args()
398 functions
= parse_ir_builder(args
.input)
401 generate_gen_h(functions
, args
.output
)
404 generate_gen_cpp(functions
, args
.output
)
407 generate_x86_h(args
.output
)
410 generate_x86_cpp(args
.output
)
413 print("Need to specify --input for --gen_h!")
416 print("Need to specify --input for --gen_cpp!")
418 if __name__
== '__main__':