Debian Codegraph @

utils/bazel/llvm-project-overlay/mlir/tblgen.bzl

raw
1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4 """BUILD extensions for MLIR table generation."""
5
6 load("@bazel_skylib//lib:paths.bzl", "paths")
7
8 TdInfo = provider(
9 "Holds TableGen files and the dependencies and include paths necessary to" +
10 " build them.",
11 fields = {
12 "transitive_sources": "td files transitively used by this rule.",
13 "transitive_includes": (
14 "include arguments to add to the final TableGen invocation. These" +
15 " are the absolute directory paths that will be added with '-I'."
16 ),
17 },
18 )
19
20 # For now we allow anything that provides DefaultInfo to just forward its files.
21 # In particular, this allows filegroups to be used. This is mostly to ease
22 # transition. In the future, the TdInfo provider will be required.
23 # TODO(gcmn): Switch to enforcing TdInfo provider.
24 def _get_dep_transitive_srcs(dep):
25 """Extract TdInfo.transitive_sources, falling back to DefaultInfo.files."""
26 if TdInfo in dep:
27 return dep[TdInfo].transitive_sources
28 return dep[DefaultInfo].files
29
30 def _get_dep_transitive_includes(dep):
31 """Extract TdInfo.transitive_includes, falling back to an empty depset()."""
32 if TdInfo in dep:
33 return dep[TdInfo].transitive_includes
34 return depset()
35
36 def _get_transitive_srcs(srcs, deps):
37 """Obtain the source files for a target and its transitive dependencies.
38
39 Args:
40 srcs: a list of source files
41 deps: a list of targets that are direct dependencies
42 Returns:
43 a collection of the transitive sources
44 """
45 return depset(
46 direct = srcs,
47 transitive = [_get_dep_transitive_srcs(dep) for dep in deps],
48 )
49
50 def _get_transitive_includes(includes, deps):
51 """Obtain the includes paths for a target and its transitive dependencies.
52
53 Args:
54 includes: a list of include paths
55 deps: a list of targets that are direct dependencies
56 Returns:
57 a collection of the transitive include paths
58 """
59 return depset(
60 direct = includes,
61 transitive = [_get_dep_transitive_includes(dep) for dep in deps],
62 )
63
64 def _prefix_roots(ctx, includes):
65 """Map the given includes to be relative to all root directories.
66
67 This will expand them to be relative to all the root directories available
68 in the execution environment for ctx.run (bin and genfiles in addition to
69 the normal source root)
70 """
71 prefixed_includes = []
72 for include in includes:
73 prefixed_includes.append(include)
74 prefixed_includes.append(paths.join(ctx.genfiles_dir.path, include))
75 prefixed_includes.append(paths.join(ctx.bin_dir.path, include))
76 return prefixed_includes
77
78 def _resolve_includes(ctx, includes):
79 """Resolves include paths to paths relative to the execution root.
80
81 Relative paths are interpreted as relative to the current label's package.
82 Absolute paths are interpreted as relative to the current label's workspace
83 root."""
84 package = ctx.label.package
85 workspace_root = ctx.label.workspace_root
86 workspace_root = workspace_root if workspace_root else "."
87 resolved_includes = []
88 for include in includes:
89 if paths.is_absolute(include):
90 include = include.lstrip("/")
91 else:
92 include = paths.join(package, include)
93 include = paths.join(workspace_root, include)
94 resolved_includes.extend(_prefix_roots(ctx, [include]))
95 return resolved_includes
96
97 def _td_library_impl(ctx):
98 trans_srcs = _get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
99 trans_includes = _get_transitive_includes(
100 _resolve_includes(ctx, ctx.attr.includes),
101 ctx.attr.deps,
102 )
103
104 # Note that we include srcs in runfiles. A td_library doesn't compile to
105 # produce an output: it's just a depset of source files and include
106 # directories. So if it is needed for execution of some rule (likely
107 # something running tblgen as a test action), the files needed are the same
108 # as the source files.
109 # Note: not using merge_all, as that is not available in Bazel 4.0
110 runfiles = ctx.runfiles(ctx.files.srcs)
111 for src in ctx.attr.srcs:
112 runfiles = runfiles.merge(src[DefaultInfo].default_runfiles)
113 for dep in ctx.attr.deps:
114 runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)
115
116 return [
117 DefaultInfo(files = trans_srcs, runfiles = runfiles),
118 TdInfo(
119 transitive_sources = trans_srcs,
120 transitive_includes = trans_includes,
121 ),
122 ]
123
124 td_library = rule(
125 _td_library_impl,
126 attrs = {
127 "srcs": attr.label_list(allow_files = True),
128 "includes": attr.string_list(
129 doc = "Include paths to be added to the final TableGen tool" +
130 " invocation. Relative paths are interpreted as relative to" +
131 " the current label's package. Absolute paths are" +
132 " interpreted as relative to the current label's workspace",
133 ),
134 # TODO(gcmn): limit to TdInfo providers.
135 "deps": attr.label_list(
136 doc = "Dependencies providing TableGen source files and include" +
137 " paths.",
138 ),
139 },
140 )
141
142 def _gentbl_rule_impl(ctx):
143 td_file = ctx.file.td_file
144
145 trans_srcs = _get_transitive_srcs(
146 ctx.files.td_srcs + [td_file],
147 ctx.attr.deps,
148 )
149
150 # Note that the td_file.dirname is already relative to the execution root,
151 # i.e. may contain an `external/<workspace_name>` prefix if the current
152 # workspace is not the main workspace. Therefore it is not included in the
153 # _resolve_includes call that prepends this prefix.
154 trans_includes = _get_transitive_includes(
155 _resolve_includes(ctx, ctx.attr.includes + ["/"]) +
156 _prefix_roots(ctx, [td_file.dirname]),
157 ctx.attr.deps,
158 )
159
160 args = ctx.actions.args()
161 args.add_all(ctx.attr.opts)
162 args.add(td_file)
163 args.add_all(trans_includes, before_each = "-I")
164
165 args.add("-o", ctx.outputs.out.path)
166
167 ctx.actions.run(
168 outputs = [ctx.outputs.out],
169 inputs = trans_srcs,
170 executable = ctx.executable.tblgen,
171 arguments = [args],
172 # Make sure action_env settings are honored so the env is the same as
173 # when the tool was built. Important for locating shared libraries with
174 # a custom LD_LIBRARY_PATH.
175 use_default_shell_env = True,
176 mnemonic = "TdGenerate",
177 )
178
179 return [DefaultInfo()]
180
181 gentbl_rule = rule(
182 _gentbl_rule_impl,
183 doc = "Generates tabular code from a table definition file.",
184 attrs = {
185 "tblgen": attr.label(
186 doc = "The TableGen executable with which to generate `out`.",
187 executable = True,
188 cfg = "exec",
189 ),
190 "td_file": attr.label(
191 doc = "The TableGen file to run through `tblgen`.",
192 allow_single_file = True,
193 mandatory = True,
194 ),
195 "td_srcs": attr.label_list(
196 doc = "Additional TableGen files included by `td_file`. It is not" +
197 " necessary to list td_file here (though not an error).",
198 allow_files = True,
199 ),
200 # TODO(gcmn): limit to TdInfo providers.
201 "deps": attr.label_list(
202 doc = "Dependencies providing TableGen source files and include" +
203 " paths.",
204 ),
205 "out": attr.output(
206 doc = "The output file for the TableGen invocation.",
207 mandatory = True,
208 ),
209 "opts": attr.string_list(
210 doc = "Additional command line options to add to the TableGen" +
211 " invocation. For include arguments, prefer to use" +
212 " `includes`.",
213 ),
214 "includes": attr.string_list(
215 doc = "Include paths to be added to the final TableGen tool" +
216 " invocation. Relative paths are interpreted as relative to" +
217 " the current label's package. Absolute paths are" +
218 " interpreted as relative to the current label's workspace." +
219 " Includes are applied from all roots available in the" +
220 " execution environment (source, genfiles, and bin" +
221 " directories). The execution roots themselves and the " +
222 " directory of td_file are always added.",
223 ),
224 },
225 )
226
227 # TODO(gcmn): Figure out how to reduce duplication with _gentbl_rule_impl
228 def _gentbl_test_impl(ctx):
229 td_file = ctx.file.td_file
230
231 # Note that the td_file.dirname is already relative to the execution root,
232 # i.e. may contain an `external/<workspace_name>` prefix if the current
233 # workspace is not the main workspace. Therefore it is not included in the
234 # _resolve_includes call that prepends this prefix.
235 trans_includes = _get_transitive_includes(
236 _resolve_includes(ctx, ctx.attr.includes + ["/"]) +
237 _prefix_roots(ctx, [td_file.dirname]),
238 ctx.attr.deps,
239 )
240
241 test_args = [ctx.executable.tblgen.short_path]
242 test_args.extend(ctx.attr.opts)
243 test_args.append(td_file.path)
244 test_args.extend(["-I " + include for include in trans_includes.to_list()])
245
246 test_args.extend(["-o", "/dev/null"])
247
248 ctx.actions.write(
249 ctx.outputs.executable,
250 content = " ".join(test_args),
251 is_executable = True,
252 )
253
254 # Note: not using merge_all, as that is not available in Bazel 4.0
255 runfiles = ctx.runfiles(
256 files = [ctx.executable.tblgen],
257 transitive_files = _get_transitive_srcs(
258 ctx.files.td_srcs + [td_file],
259 ctx.attr.deps,
260 ),
261 )
262 for src in ctx.attr.td_srcs:
263 runfiles = runfiles.merge(src[DefaultInfo].default_runfiles)
264 for dep in ctx.attr.deps:
265 runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)
266
267 return [
268 coverage_common.instrumented_files_info(
269 ctx,
270 source_attributes = ["td_file", "td_srcs"],
271 dependency_attributes = ["tblgen", "deps"],
272 ),
273 DefaultInfo(runfiles = runfiles),
274 ]
275
276 gentbl_test = rule(
277 _gentbl_test_impl,
278 test = True,
279 doc = "A shell test that tests the given TablegGen invocation. Note" +
280 " that unlike gentbl_rule, this builds and invokes `tblgen` in the" +
281 " target configuration. Takes all the same arguments as gentbl_rule" +
282 " except for `out` (as it does not generate any output)",
283 attrs = {
284 "tblgen": attr.label(
285 doc = "The TableGen executable run in the shell command. Note" +
286 " that this is built in the target configuration.",
287 executable = True,
288 cfg = "target",
289 ),
290 "td_file": attr.label(
291 doc = "See gentbl_rule.td_file",
292 allow_single_file = True,
293 mandatory = True,
294 ),
295 "td_srcs": attr.label_list(
296 doc = "See gentbl_rule.td_srcs",
297 allow_files = True,
298 ),
299 "deps": attr.label_list(doc = "See gentbl_rule.deps"),
300 "opts": attr.string_list(doc = "See gentbl_rule.opts"),
301 "includes": attr.string_list(doc = "See gentbl_rule.includes"),
302 },
303 )
304
305 def gentbl_filegroup(
306 name,
307 tblgen,
308 td_file,
309 tbl_outs,
310 td_srcs = [],
311 includes = [],
312 deps = [],
313 test = False,
314 skip_opts = [],
315 **kwargs):
316 """Create multiple TableGen generated files using the same tool and input.
317
318 All generated outputs are bundled in a file group with the given name.
319
320 Args:
321 name: The name of the generated filegroup rule for use in dependencies.
322 tblgen: The binary used to produce the output.
323 td_file: The primary table definitions file.
324 tbl_outs: Either a dict {out: [opts]} or a list of tuples ([opts], out),
325 where each 'opts' is a list of options passed to tblgen, each option
326 being a string, and 'out' is the corresponding output file produced.
327 td_srcs: See gentbl_rule.td_srcs
328 includes: See gentbl_rule.includes
329 deps: See gentbl_rule.deps
330 test: Whether to create a shell test that invokes the tool too.
331 skip_opts: Files generated using these opts in tbl_outs will be excluded
332 from the generated filegroup.
333 **kwargs: Extra keyword arguments to pass to all generated rules.
334 """
335
336 if type(tbl_outs) == type({}):
337 tbl_outs = [(v, k) for k, v in tbl_outs.items()]
338 for (opts, out) in tbl_outs:
339 first_opt = opts[0] if opts else ""
340 rule_suffix = "_{}_{}".format(
341 first_opt.replace("-", "_").replace("=", "_"),
342 str(hash(" ".join(opts))),
343 )
344 gentbl_name = "%s_%s_genrule" % (name, rule_suffix)
345 gentbl_rule(
346 name = gentbl_name,
347 td_file = td_file,
348 tblgen = tblgen,
349 opts = opts,
350 td_srcs = td_srcs,
351 deps = deps,
352 includes = includes,
353 out = out,
354 **kwargs
355 )
356
357 if test:
358 # Also run the generator in the target configuration as a test. This
359 # means it gets run with asserts and sanitizers and such when they
360 # are enabled and is counted in coverage.
361 gentbl_test(
362 name = "%s_test" % (gentbl_name,),
363 td_file = td_file,
364 tblgen = tblgen,
365 opts = opts,
366 td_srcs = td_srcs,
367 deps = deps,
368 includes = includes,
369 # Shell files not executable on Windows.
370 # TODO(gcmn): Support windows.
371 tags = ["no_windows"],
372 **kwargs
373 )
374
375 included_srcs = [f for (opts, f) in tbl_outs if not any([skip_opt in opts for skip_opt in skip_opts])]
376 native.filegroup(
377 name = name,
378 srcs = included_srcs,
379 **kwargs
380 )
381
382 def gentbl_cc_library(
383 name,
384 tblgen,
385 td_file,
386 tbl_outs,
387 td_srcs = [],
388 includes = [],
389 deps = [],
390 strip_include_prefix = None,
391 test = False,
392 copts = None,
393 **kwargs):
394 """Create multiple TableGen generated files using the same tool and input.
395
396 All generated outputs are bundled in a cc_library rule.
397
398 Args:
399 name: The name of the generated cc_library rule for use in dependencies.
400 tblgen: The binary used to produce the output.
401 td_file: The primary table definitions file.
402 tbl_outs: Either a dict {out: [opts]} or a list of tuples ([opts], out),
403 where each 'opts' is a list of options passed to tblgen, each option
404 being a string, and 'out' is the corresponding output file produced.
405 td_srcs: See gentbl_rule.td_srcs
406 includes: See gentbl_rule.includes
407 deps: See gentbl_rule.deps
408 strip_include_prefix: attribute to pass through to cc_library.
409 test: whether to create a shell test that invokes the tool too.
410 copts: list of copts to pass to cc_library.
411 **kwargs: Extra keyword arguments to pass to all generated rules.
412 """
413
414 filegroup_name = name + "_filegroup"
415 gentbl_filegroup(
416 name = filegroup_name,
417 tblgen = tblgen,
418 td_file = td_file,
419 tbl_outs = tbl_outs,
420 td_srcs = td_srcs,
421 includes = includes,
422 deps = deps,
423 test = test,
424 skip_opts = ["-gen-op-doc"],
425 **kwargs
426 )
427 native.cc_library(
428 name = name,
429 # strip_include_prefix does not apply to textual_hdrs.
430 # https://github.com/bazelbuild/bazel/issues/12424
431 hdrs = [":" + filegroup_name] if strip_include_prefix else [],
432 strip_include_prefix = strip_include_prefix,
433 textual_hdrs = [":" + filegroup_name],
434 copts = copts,
435 **kwargs
436 )
437
438 def _gentbl_shard_impl(ctx):
439 args = ctx.actions.args()
440 args.add(ctx.file.src_file)
441 args.add("-op-shard-index", ctx.attr.index)
442 args.add("-o", ctx.outputs.out.path)
443 ctx.actions.run(
444 outputs = [ctx.outputs.out],
445 inputs = [ctx.file.src_file],
446 executable = ctx.executable.sharder,
447 arguments = [args],
448 use_default_shell_env = True,
449 mnemonic = "ShardGenerate",
450 )
451
452 gentbl_shard_rule = rule(
453 _gentbl_shard_impl,
454 doc = "",
455 output_to_genfiles = True,
456 attrs = {
457 "index": attr.int(mandatory = True, doc = ""),
458 "sharder": attr.label(
459 doc = "",
460 executable = True,
461 cfg = "exec",
462 ),
463 "src_file": attr.label(
464 doc = "",
465 allow_single_file = True,
466 mandatory = True,
467 ),
468 "out": attr.output(
469 doc = "",
470 mandatory = True,
471 ),
472 },
473 )
474
475 def gentbl_sharded_ops(
476 name,
477 tblgen,
478 sharder,
479 td_file,
480 shard_count,
481 src_file,
482 src_out,
483 hdr_out,
484 test = False,
485 includes = [],
486 strip_include_prefix = None,
487 deps = [],
488 **kwargs):
489 """Generate sharded op declarations and definitions.
490
491 This special build rule shards op definitions in a TableGen file and generates multiple copies
492 of a template source file for including and compiling each shard. The rule defines a filegroup
493 consisting of the source shards, the generated source file, and the generated header file.
494
495 Args:
496 name: The name of the filegroup.
497 tblgen: The binary used to produce the output.
498 sharder: The source file sharder to use.
499 td_file: The primary table definitions file.
500 shard_count: The number of op definition shards to produce.
501 src_file: The source file template.
502 src_out: The generated source file.
503 hdr_out: The generated header file.
504 test: Whether this is a test target.
505 includes: See gentbl_rule.includes
506 deps: See gentbl_rule.deps
507 strip_include_prefix: Attribute to pass through to cc_library.
508 """
509 cc_lib_name = name + "__gentbl_cc_lib"
510 gentbl_cc_library(
511 name = cc_lib_name,
512 strip_include_prefix = strip_include_prefix,
513 includes = includes,
514 tbl_outs = {
515 src_out: [
516 "-gen-op-defs",
517 "-op-shard-count=" + str(shard_count),
518 ],
519 hdr_out: [
520 "-gen-op-decls",
521 "-op-shard-count=" + str(shard_count),
522 ],
523 },
524 tblgen = tblgen,
525 td_file = td_file,
526 test = test,
527 deps = deps,
528 **kwargs
529 )
530 all_files = [hdr_out, src_out]
531 for i in range(0, shard_count):
532 out_file = "shard_copy_" + str(i) + "_" + src_file
533 gentbl_shard_rule(
534 index = i,
535 name = name + "__src_shard" + str(i),
536 testonly = test,
537 out = out_file,
538 sharder = sharder,
539 src_file = src_file,
540 **kwargs
541 )
542 all_files.append(out_file)
543 native.filegroup(
544 name = name,
545 srcs = all_files,
546 **kwargs
547 )
548
549 def gentbl_sharded_op_defs(name, source_file, shard_count):
550 """Generates multiple copies of a source file that includes sharded op definitions.
551
552 Args:
553 name: The name of the rule.
554 source_file: The source to copy.
555 shard_count: The number of shards.
556
557 Returns:
558 A list of the copied filenames to be included in the dialect library.
559 """
560 copies = []
561 for i in range(0, shard_count):
562 out_file = "shard_copy_" + str(i) + "_" + source_file
563 copies.append(out_file)
564 native.genrule(
565 name = name + "_shard_" + str(i),
566 srcs = [source_file],
567 outs = [out_file],
568 cmd = "echo -e \"#define GET_OP_DEFS_" + str(i) + "\n$$(cat $(SRCS))\" > $(OUTS)",
569 )
570 return copies