[llvm] [bazel] Implement tblgen as a bazel rule (PR #95324)

Eganyan Alexey via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 15:48:08 PDT 2024


https://github.com/synthMoza created https://github.com/llvm/llvm-project/pull/95324

Currently one might invoke `llvm-tblgen` in Bazel by using `gentbl` macro. But bazel macros do not allow you to use `select` statements with them. For example,  one might use different `tblgen_args` on different OS, or depending on command line options. So, in this PR I implemented tblgen as a rule instead of a macro.

```
tblgen_args = select({
          "@platforms//os:windows": "--long-string-literals=0",
          "//conditions:default": "",
      }),
```

>From 663373b18e741d35576bcb08d5cdc3a355e59a08 Mon Sep 17 00:00:00 2001
From: synthMoza <synthMoza at frkt.ru>
Date: Thu, 13 Jun 2024 01:05:28 +0300
Subject: [PATCH 1/2] tblgen rule

---
 .../llvm-project-overlay/llvm/BUILD.bazel     | 11 ++-
 .../llvm-project-overlay/llvm/tblgen.bzl      | 68 ++++++++++++++-----
 2 files changed, 54 insertions(+), 25 deletions(-)

diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
index cf7ee18f0d068..b70adf41647f0 100644
--- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -2341,13 +2341,10 @@ gentbl(
         strip_include_prefix = "lib/Target/" + target["name"],
         tbl_outs = target["tbl_outs"],
         tblgen = ":llvm-tblgen",
-        # MSVC isn't happy with long string literals, while other compilers
-        # which support them get significant compile time improvements with
-        # them enabled. Ideally this flag would only be enabled on Windows via
-        # a select() on `@platforms//os:windows,`, but that would
-        # require refactoring gentbl from a macro into a rule.
-        # TODO(#92): Refactor gentbl to support this use
-        tblgen_args = "--long-string-literals=0",
+        tblgen_args = select({
+            "@platforms//os:windows": "--long-string-literals=0",
+            "//conditions:default": "",
+        }),
         td_file = "lib/Target/" + target["name"] + "/" + target["short_name"] + ".td",
         td_srcs = [
             ":common_target_td_sources",
diff --git a/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl b/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
index d43390918e390..eebf6ea38e51f 100644
--- a/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
+++ b/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
@@ -41,24 +41,15 @@ def gentbl(
         td_srcs += [td_file]
     for (opts, out) in tbl_outs:
         rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" "))
-        native.genrule(
-            name = "%s_%s_genrule" % (name, rule_suffix),
-            srcs = td_srcs,
-            outs = [out],
-            tools = [tblgen],
-            message = "Generating code from table: %s" % td_file,
-            cmd = (("$(location %s) -I %s/llvm/include " +
-                    "-I %s/clang/include " +
-                    "-I $$(dirname $(location %s)) " +
-                    "%s $(location %s) %s -o $@") % (
-                tblgen,
-                llvm_project_execroot_path,
-                llvm_project_execroot_path,
-                td_file,
-                opts,
-                td_file,
-                tblgen_args,
-            )),
+        _gentbl(
+            name = "%s_%s_rule" % (name, rule_suffix),
+            tblgen = tblgen,
+            td_file = td_file,
+            td_srcs = td_srcs,
+            opts = opts,
+            out = out,
+            tblgen_args = tblgen_args,
+            llvm_project_execroot_path = llvm_project_execroot_path,
         )
 
     # For now, all generated files can be assumed to comprise public interfaces.
@@ -79,3 +70,44 @@ def gentbl(
             features = ["-parse_headers", "-header_modules"],
             **kwargs
         )
+
+def _gentbl_impl(ctx):
+    inputs = depset(ctx.files.td_srcs)
+
+    args = ctx.actions.args()
+    args.add("-I", "%s/llvm/include" % ctx.attr.llvm_project_execroot_path)
+    args.add("-I", "%s/clang/include" % ctx.attr.llvm_project_execroot_path)
+    args.add("-I", ctx.file.td_file.dirname)
+    
+    parsed_opts = ctx.attr.opts.split(' ')
+    for opt in parsed_opts:
+        args.add(opt)
+
+    if ctx.attr.tblgen_args:
+        parsed_args = ctx.attr.tblgen_args.split(' ')
+        for tblgen_arg in parsed_args:
+            args.add(tblgen_arg)
+
+    args.add(ctx.file.td_file)
+    args.add("-o", ctx.outputs.out)
+
+    ctx.actions.run(
+        mnemonic = "tblgen",
+        executable = ctx.executable.tblgen,
+        arguments = [args],
+        inputs = inputs,
+        outputs = [ctx.outputs.out],
+    )
+
+_gentbl = rule(
+    implementation = _gentbl_impl,
+    attrs = {
+        "tblgen": attr.label(executable = True, cfg = "exec", doc = "The binary used to produce the output."),
+        "td_file": attr.label(allow_single_file = True, doc = "The binary used to produce the output"),
+        "td_srcs": attr.label_list(allow_files = True, doc = "A list of table definition files included transitively."),
+        "opts": attr.string(doc = "String of options passed to tblgen."),
+        "out": attr.output(doc = "Corresponding to opts output file."),
+        "llvm_project_execroot_path": attr.string(doc = "Path to llvm-project execroot."),
+        "tblgen_args": attr.string(default = "", doc = "Extra arguments string to pass to the tblgen binary."),
+    }
+)

>From 4413edc6048177fd3362aaadf599147b74c2e746 Mon Sep 17 00:00:00 2001
From: synthMoza <synthMoza at frkt.ru>
Date: Thu, 13 Jun 2024 01:46:00 +0300
Subject: [PATCH 2/2] return comment explaining tblgen flag

---
 utils/bazel/llvm-project-overlay/llvm/BUILD.bazel | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
index b70adf41647f0..4bd9044a15f0a 100644
--- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -2341,6 +2341,9 @@ gentbl(
         strip_include_prefix = "lib/Target/" + target["name"],
         tbl_outs = target["tbl_outs"],
         tblgen = ":llvm-tblgen",
+        # MSVC isn't happy with long string literals, while other compilers
+        # which support them get significant compile time improvements with
+        # them enabled.
         tblgen_args = select({
             "@platforms//os:windows": "--long-string-literals=0",
             "//conditions:default": "",



More information about the llvm-commits mailing list