-
Notifications
You must be signed in to change notification settings - Fork 25
/
templates.py
393 lines (338 loc) · 13.6 KB
/
templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import os
import pathlib
import shutil
import fire
import jinja2
def render_all(path: pathlib.Path, **args):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(path))
for template_filename in os.listdir(path):
template = env.get_template(template_filename)
content = template.render(**args)
with open(path / template_filename, mode="w") as outfile:
outfile.write(content)
print(f"Rendered template for {path / template_filename}")
def try_create_dirs(include_path, lib_path, force=False):
print(f"Creating dirs:\n {include_path}\n {lib_path}")
try:
os.makedirs(include_path)
os.makedirs(lib_path)
except FileExistsError:
if force:
shutil.rmtree(include_path)
shutil.rmtree(lib_path)
os.mkdir(include_path)
os.mkdir(lib_path)
else:
raise
def copy_all(filepath_mapping):
for src, dest in filepath_mapping.items():
shutil.copy(src, dest)
class CLI:
"""A helper CLI for generating boilerplate MLIR code in HEIR.
Available subcommands:
new_conversion_pass: Create a conversion pass from one dialect to another.
new_dialect_transform: Create a pass for a dialect-specific transform.
new_transform: Create a pass for a non-dialect-specific transform.
To see the help for a subcommand, run
python templates/templates.py <subcommand> --help
"""
def __init__(self):
git_root = pathlib.Path(__file__).parent.parent.parent
if not os.path.isdir(git_root / ".git"):
raise RuntimeError(f"Could not find git root, looked at {git_root}")
self.root = git_root
def new_conversion_pass(
self,
pass_name: str = None,
source_dialect_name: str = None,
source_dialect_namespace: str = None,
source_dialect_mnemonic: str = None,
target_dialect_name: str = None,
target_dialect_namespace: str = None,
target_dialect_mnemonic: str = None,
force: bool = False,
):
"""Create a new conversion pass.
Args:
pass_name: The CPP class name and directory name for the conversion
pass, e.g., BGVToPolynomial
source_dialect_name: The source dialect's CPP class name prefix and
directory name, e.g., CGGI (for CGGIDialect)
source_dialect_namespace: The source dialect's CPP namespace, e.g.,
tfhe_rust for TfheRustDialect
source_dialect_mnemonic: The source dialect's mnemonic, e.g., cggi
target_dialect_name: The target dialect's CPP class name prefix and
directory name, e.g., CGGI (for CGGIDialect)
target_dialect_namespace: The target dialect's CPP namespace, e.g.,
tfhe_rust for TfheRustDialect
target_dialect_mnemonic: The target dialect's mnemonic, e.g., cggi
force: If True, overwrite existing files. If False, raise an error if
any files already exist.
"""
if not source_dialect_name:
raise ValueError("source_dialect_name must be provided")
if not target_dialect_name:
raise ValueError("target_dialect_name must be provided")
if not pass_name:
pass_name = f"{source_dialect_name}To{target_dialect_name}"
# These defaults could be smarter: look up the name in the actual
# tablegen for the dialect or quit if it can't be found
if not source_dialect_mnemonic:
source_dialect_mnemonic = source_dialect_name.lower()
if not source_dialect_namespace:
source_dialect_namespace = source_dialect_mnemonic
if not target_dialect_mnemonic:
target_dialect_mnemonic = target_dialect_name.lower()
if not target_dialect_namespace:
target_dialect_namespace = target_dialect_mnemonic
include_path = self.root / "include" / "Conversion" / pass_name
lib_path = self.root / "lib" / "Conversion" / pass_name
if not force and (os.path.isdir(include_path) or os.path.isdir(lib_path)):
raise ValueError(
f"Conversion pass directories already exist at {include_path} or"
f" {lib_path}")
templates_path = self.root / "templates" / "Conversion"
templ_include = templates_path / "include"
templ_lib = templates_path / "lib"
path_mapping = {
templ_include / "BUILD.jinja":
include_path / "BUILD",
templ_include / "ConversionPass.h.jinja":
include_path / f"{pass_name}.h",
templ_include / "ConversionPass.td.jinja":
include_path / f"{pass_name}.td",
templ_lib / "BUILD.jinja":
lib_path / "BUILD",
templ_lib / "ConversionPass.cpp.jinja":
lib_path / f"{pass_name}.cpp",
}
try:
try_create_dirs(include_path, lib_path, force)
copy_all(path_mapping)
render_all(
include_path,
pass_name=pass_name,
source_dialect_name=source_dialect_name,
source_dialect_namespace=source_dialect_namespace,
source_dialect_mnemonic=source_dialect_mnemonic,
target_dialect_name=target_dialect_name,
target_dialect_namespace=target_dialect_namespace,
target_dialect_mnemonic=target_dialect_mnemonic,
)
render_all(
lib_path,
pass_name=pass_name,
source_dialect_name=source_dialect_name,
source_dialect_namespace=source_dialect_namespace,
source_dialect_mnemonic=source_dialect_mnemonic,
target_dialect_name=target_dialect_name,
target_dialect_namespace=target_dialect_namespace,
target_dialect_mnemonic=target_dialect_mnemonic,
)
except:
print("Hit unrecoverable error, cleaning up")
shutil.rmtree(include_path)
shutil.rmtree(lib_path)
raise
def new_dialect_transform(
self,
pass_name: str = None,
pass_flag: str = None,
dialect_name: str = None,
dialect_namespace: str = None,
force: bool = False,
):
"""Create a new pass for a dialect-specific transform.
Args:
pass_name: The CPP class name for the pass, e.g., ForgetSecrets.
pass_flag: The CLI flag to use for the pass (optional).
dialect_name: The dialect's CPP class name prefix and directory name,
e.g., CGGI (for CGGIDialect).
dialect_namespace: The dialect's CPP namespace, e.g., tfhe_rust for
TfheRustDialect.
force: If True, overwrite existing files. If False, raise an error if
any files already exist.
"""
if not pass_name:
raise ValueError("pass_name must be provided")
if not dialect_name:
raise ValueError("dialect_name must be provided")
if not pass_flag:
pass_name = f"{dialect_name.lower()}-{pass_name.lower()}"
# Default could be smarter: look up the name in the actual tablegen for the
# dialect or quit if it can't be found
if not dialect_namespace:
dialect_namespace = dialect_name.lower()
include_path = self.root / "include" / "Dialect" / dialect_name / "Transforms"
lib_path = self.root / "lib" / "Dialect" / dialect_name / "Transforms"
if not force and (os.path.isdir(include_path) or os.path.isdir(lib_path)):
raise ValueError(
f"Pass directories already exist at {include_path} or {lib_path}")
templates_path = self.root / "templates" / "DialectTransforms"
templ_include = templates_path / "include"
templ_lib = templates_path / "lib"
path_mapping = {
templ_include / "BUILD.jinja": include_path / "BUILD",
templ_include / "Pass.h.jinja": include_path / f"{pass_name}.h",
templ_include / "Passes.h.jinja": include_path / f"Passes.h",
templ_include / "Passes.td.jinja": include_path / f"Passes.td",
templ_lib / "BUILD.jinja": lib_path / "BUILD",
templ_lib / "Pass.cpp.jinja": lib_path / f"{pass_name}.cpp",
}
try:
try_create_dirs(include_path, lib_path, force)
copy_all(path_mapping)
render_all(
include_path,
pass_name=pass_name,
pass_flag=pass_flag,
dialect_name=dialect_name,
dialect_namespace=dialect_namespace,
)
render_all(
lib_path,
pass_name=pass_name,
pass_flag=pass_flag,
dialect_name=dialect_name,
dialect_namespace=dialect_namespace,
)
except:
print("Hit unrecoverable error, cleaning up")
shutil.rmtree(include_path)
shutil.rmtree(lib_path)
raise
def new_transform(
self,
pass_name: str = None,
pass_flag: str = None,
force: bool = False,
):
"""Create a new pass for a dialect-specific transform.
Args:
pass_name: The CPP class name for the pass, e.g., ForgetSecrets.
pass_flag: The CLI flag to use for the pass (optional).
force: If True, overwrite existing files. If False, raise an error if
any files already exist.
"""
if not pass_name:
raise ValueError("pass_name must be provided")
if not pass_flag:
pass_name = f"{pass_name.lower()}"
include_path = self.root / "include" / "Transforms" / pass_name
lib_path = self.root / "lib" / "Transforms" / pass_name
if not force and (os.path.isdir(include_path) or os.path.isdir(lib_path)):
raise ValueError(
f"Pass directories already exist at {include_path} or {lib_path}")
templates_path = self.root / "templates" / "Transforms"
templ_include = templates_path / "include"
templ_lib = templates_path / "lib"
path_mapping = {
templ_include / "BUILD.jinja": include_path / "BUILD",
templ_include / "Pass.h.jinja": include_path / f"{pass_name}.h",
templ_include / "Pass.td.jinja": include_path / f"{pass_name}.td",
templ_lib / "BUILD.jinja": lib_path / "BUILD",
templ_lib / "Pass.cpp.jinja": lib_path / f"{pass_name}.cpp",
}
try:
try_create_dirs(include_path, lib_path, force)
copy_all(path_mapping)
render_all(
include_path,
pass_name=pass_name,
pass_flag=pass_flag,
)
render_all(
lib_path,
pass_name=pass_name,
pass_flag=pass_flag,
)
except:
print("Hit unrecoverable error, cleaning up")
shutil.rmtree(include_path)
shutil.rmtree(lib_path)
raise
def new_dialect(
self,
dialect_name: str = None,
dialect_namespace: str = None,
enable_attributes: bool = True,
enable_types: bool = True,
enable_ops: bool = True,
force: bool = False,
):
"""Create a new dialect.
Args:
dialect_name: The dialect's CPP class name prefix and directory name,
e.g., CGGI (for CGGIDialect).
dialect_namespace: The dialect's CPP namespace, e.g., tfhe_rust for
TfheRustDialect.
enable_attributes: Generate a separate tablegen and includes for
attributes.
enable_types: Generate a separate tablegen and includes for types.
enable_ops: Generate a separate tablegen and includes for ops.
force: If True, overwrite existing files. If False, raise an error if
any files already exist.
"""
if not dialect_name:
raise ValueError("dialect_name must be provided")
if not dialect_namespace:
dialect_namespace = dialect_name.lower()
include_path = self.root / "include" / "Dialect" / dialect_name / "IR"
lib_path = self.root / "lib" / "Dialect" / dialect_name / "IR"
if not force and (os.path.isdir(include_path) or os.path.isdir(lib_path)):
raise ValueError(
f"Dialect directories already exist at {include_path} or {lib_path}")
templates_path = self.root / "templates" / "Dialect"
templ_include = templates_path / "include"
templ_lib = templates_path / "lib"
path_mapping = {
templ_include / "BUILD.jinja": include_path / "BUILD",
templ_include / "Dialect.h.jinja": include_path / f"{dialect_name}Dialect.h",
templ_include / "Dialect.td.jinja": include_path / f"{dialect_name}Dialect.td",
templ_lib / "BUILD.jinja": lib_path / "BUILD",
templ_lib / "Dialect.cpp.jinja": lib_path / f"{dialect_name}Dialect.cpp",
}
if enable_attributes:
path_mapping.update({
templ_include / "Attributes.h.jinja": include_path / f"{dialect_name}Attributes.h",
templ_include / "Attributes.td.jinja": include_path / f"{dialect_name}Attributes.td",
templ_lib / "Attributes.cpp.jinja": lib_path / f"{dialect_name}Attributes.cpp",
})
if enable_types:
path_mapping.update({
templ_include / "Types.h.jinja": include_path / f"{dialect_name}Types.h",
templ_include / "Types.td.jinja": include_path / f"{dialect_name}Types.td",
templ_lib / "Types.cpp.jinja": lib_path / f"{dialect_name}Types.cpp",
})
if enable_ops:
path_mapping.update({
templ_include / "Ops.h.jinja": include_path / f"{dialect_name}Ops.h",
templ_include / "Ops.td.jinja": include_path / f"{dialect_name}Ops.td",
templ_lib / "Ops.cpp.jinja": lib_path / f"{dialect_name}Ops.cpp",
})
try:
try_create_dirs(include_path, lib_path, force)
copy_all(path_mapping)
render_all(
include_path,
dialect_name=dialect_name,
dialect_namespace=dialect_namespace,
enable_attributes=enable_attributes,
enable_types=enable_types,
enable_ops=enable_ops,
)
render_all(
lib_path,
dialect_name=dialect_name,
dialect_namespace=dialect_namespace,
enable_attributes=enable_attributes,
enable_types=enable_types,
enable_ops=enable_ops,
)
except:
print("Hit unrecoverable error, cleaning up")
shutil.rmtree(include_path)
shutil.rmtree(lib_path)
raise
if __name__ == "__main__":
fire.Fire(CLI)