1 # Copyright (C) 2014-2017 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 from __future__
import print_function
24 from gen_common
import MakoTemplateWriter
, ArgumentParser
25 from argparse
import FileType
28 'SHUFFLE_VECTOR': 'VSHUFFLE',
29 'INSERT_ELEMENT': 'VINSERT',
30 'EXTRACT_ELEMENT': 'VEXTRACT',
33 'MEM_MOVE': 'MEMMOVE',
36 'BIT_CAST': 'BITCAST',
45 ['VGATHERPD', 'x86_avx2_gather_d_pd_256', ['src', 'pBase', 'indices', 'mask', 'scale']],
46 ['VGATHERPS', 'x86_avx2_gather_d_ps_256', ['src', 'pBase', 'indices', 'mask', 'scale']],
47 ['VGATHERPS_16', 'x86_avx512_gather_dps_512', ['src', 'pBase', 'indices', 'mask', 'scale']],
48 ['VGATHERDD', 'x86_avx2_gather_d_d_256', ['src', 'pBase', 'indices', 'mask', 'scale']],
49 ['VPSRLI', 'x86_avx2_psrli_d', ['src', 'imm']],
50 ['VPSRLI_16', 'x86_avx512_psrli_d_512', ['src', 'imm']],
51 ['VSQRTPS', 'x86_avx_sqrt_ps_256', ['a']],
52 ['VRSQRTPS', 'x86_avx_rsqrt_ps_256', ['a']],
53 ['VRCPPS', 'x86_avx_rcp_ps_256', ['a']],
54 ['VMINPS', 'x86_avx_min_ps_256', ['a', 'b']],
55 ['VMAXPS', 'x86_avx_max_ps_256', ['a', 'b']],
56 ['VROUND', 'x86_avx_round_ps_256', ['a', 'rounding']],
57 ['VCMPPS', 'x86_avx_cmp_ps_256', ['a', 'b', 'cmpop']],
58 ['VBLENDVPS', 'x86_avx_blendv_ps_256', ['a', 'b', 'mask']],
59 ['BEXTR_32', 'x86_bmi_bextr_32', ['src', 'control']],
60 ['VMASKLOADD', 'x86_avx2_maskload_d_256', ['src', 'mask']],
61 ['VMASKMOVPS', 'x86_avx_maskload_ps_256', ['src', 'mask']],
62 ['VMASKSTOREPS', 'x86_avx_maskstore_ps_256', ['src', 'mask', 'val']],
63 ['VPSHUFB', 'x86_avx2_pshuf_b', ['a', 'b']],
64 ['VPERMD', 'x86_avx2_permd', ['a', 'idx']],
65 ['VPERMPS', 'x86_avx2_permps', ['idx', 'a']],
66 ['VCVTPD2PS', 'x86_avx_cvt_pd2_ps_256', ['a']],
67 ['VCVTPH2PS', 'x86_vcvtph2ps_256', ['a']],
68 ['VCVTPS2PH', 'x86_vcvtps2ph_256', ['a', 'round']],
69 ['VHSUBPS', 'x86_avx_hsub_ps_256', ['a', 'b']],
70 ['VPTESTC', 'x86_avx_ptestc_256', ['a', 'b']],
71 ['VPTESTZ', 'x86_avx_ptestz_256', ['a', 'b']],
72 ['VFMADDPS', 'x86_fma_vfmadd_ps_256', ['a', 'b', 'c']],
73 ['VMOVMSKPS', 'x86_avx_movmsk_ps_256', ['a']],
74 ['INTERRUPT', 'x86_int', ['a']],
77 this_dir
= os
.path
.dirname(os
.path
.abspath(__file__
))
78 template
= os
.path
.join(this_dir
, 'templates', 'gen_builder.hpp')
80 def convert_uppercamel(name
):
81 s1
= re
.sub('(.)([A-Z][a-z]+)', r
'\1_\2', name
)
82 return re
.sub('([a-z0-9])([A-Z])', r
'\1_\2', s1
).upper()
85 Given an input file (e.g. IRBuilder.h) generates function dictionary.
87 def parse_ir_builder(input_file
):
91 lines
= input_file
.readlines()
94 while idx
< len(lines
) - 1:
95 line
= lines
[idx
].rstrip()
98 #match = re.search(r'\*Create', line)
99 match
= re
.search(r
'[\*\s]Create(\w*)\(', line
)
100 if match
is not None:
101 #print('Line: %s' % match.group(1))
103 if re
.search(r
'^\s*Create', line
) is not None:
104 func_sig
= lines
[idx
-2].rstrip() + line
109 while not end_of_args
:
110 end_paren
= re
.search(r
'\)', line
)
111 if end_paren
is not None:
114 line
= lines
[idx
].rstrip()
118 delfunc
= re
.search(r
'LLVM_DELETED_FUNCTION|= delete;', func_sig
)
121 func
= re
.search(r
'(.*?)\*[\n\s]*(Create\w*)\((.*?)\)', func_sig
)
124 return_type
= func
.group(1).strip() + '*'
125 func_name
= func
.group(2)
126 arguments
= func
.group(3)
130 args
= arguments
.split(',')
134 func_args
.append(arg
)
136 split_args
= arg
.split('=')
137 arg_name
= split_args
[0].rsplit(None, 1)[-1]
139 reg_arg
= re
.search(r
'[\&\*]*(\w*)', arg_name
)
141 arg_names
+= [reg_arg
.group(1)]
145 # The following functions need to be ignored in openswr.
146 # API change in llvm-5.0 breaks baked autogen files
148 (func_name
== 'CreateFence' or
149 func_name
== 'CreateAtomicCmpXchg' or
150 func_name
== 'CreateAtomicRMW')):
153 # The following functions need to be ignored.
154 if (func_name
== 'CreateInsertNUWNSWBinOp' or
155 func_name
== 'CreateMaskedIntrinsic' or
156 func_name
== 'CreateAlignmentAssumptionHelper'):
159 # Convert CamelCase to CAMEL_CASE
160 func_mod
= re
.search(r
'Create(\w*)', func_name
)
162 func_mod
= func_mod
.group(1)
163 func_mod
= convert_uppercamel(func_mod
)
164 if func_mod
[0:2] == 'F_' or func_mod
[0:2] == 'I_':
165 func_mod
= func_mod
[0] + func_mod
[2:]
167 # Substitute alias based on CAMEL_CASE name.
168 func_alias
= inst_aliases
.get(func_mod
)
170 func_alias
= func_mod
172 if func_name
== 'CreateCall' or func_name
== 'CreateGEP':
173 arglist
= re
.search(r
'ArrayRef', ', '.join(func_args
))
175 func_alias
= func_alias
+ 'A'
180 'alias' : func_alias
,
181 'return' : return_type
,
182 'args' : ', '.join(func_args
),
183 'arg_names' : arg_names
,
189 Auto-generates macros for LLVM IR
191 def generate_gen_h(functions
, output_dir
):
192 filename
= 'gen_builder.hpp'
193 output_filename
= os
.path
.join(output_dir
, filename
)
196 for func
in functions
:
197 decl
= '%s %s(%s)' % (func
['return'], func
['alias'], func
['args'])
201 'intrin' : func
['name'],
202 'args' : ', '.join(func
['arg_names']),
205 MakoTemplateWriter
.to_file(
209 comment
='Builder IR Wrappers',
211 functions
=templfuncs
,
215 Auto-generates macros for LLVM IR
217 def generate_x86_h(output_dir
):
218 filename
= 'gen_builder_x86.hpp'
219 output_filename
= os
.path
.join(output_dir
, filename
)
222 for inst
in intrinsics
:
223 #print('Inst: %s, x86: %s numArgs: %d' % (inst[0], inst[1], len(inst[2])))
224 declargs
= 'Value* ' + ', Value* '.join(inst
[2])
227 'decl' : 'Value* %s(%s)' % (inst
[0], declargs
),
228 'args' : ', '.join(inst
[2]),
232 MakoTemplateWriter
.to_file(
236 comment
='x86 intrinsics',
242 Function which is invoked when this script is started from a command line.
243 Will present and consume a set of arguments which will tell this script how
249 parser
= ArgumentParser()
250 parser
.add_argument('--input', '-i', type=FileType('r'), help='Path to IRBuilder.h', required
=False)
251 parser
.add_argument('--output-dir', '-o', action
='store', dest
='output', help='Path to output directory', required
=True)
252 parser
.add_argument('--gen_h', help='Generate builder_gen.h', action
='store_true', default
=False)
253 parser
.add_argument('--gen_x86_h', help='Generate x86 intrinsics. No input is needed.', action
='store_true', default
=False)
254 args
= parser
.parse_args()
256 if not os
.path
.exists(args
.output
):
257 os
.makedirs(args
.output
)
260 functions
= parse_ir_builder(args
.input)
263 generate_gen_h(functions
, args
.output
)
266 print('Need to specify --input for --gen_h!')
269 generate_x86_h(args
.output
)
271 if __name__
== '__main__':