[llvm] [bazel] Fix testonly declarations in mlir (PR #195159)

Alex Trotta via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 15:59:37 PDT 2026


https://github.com/Ahajha updated https://github.com/llvm/llvm-project/pull/195159

>From 69fcf1b1acd887c88156b13e33c8a9618667e0c5 Mon Sep 17 00:00:00 2001
From: Alex Trotta <ahajha at gmail.com>
Date: Thu, 30 Apr 2026 18:59:01 -0400
Subject: [PATCH] [bazel] Fix testonly declarations in mlir

Some recent Bazel change (unsure what) caused these to start failing because they weren't properly declared. This adds a way to specify `testonly` alongside `test` to a few gentbl rules, since we want the tests but some of these targets are also used in final binaries.
---
 utils/bazel/llvm-project-overlay/mlir/tblgen.bzl    | 13 ++++++++++++-
 .../llvm-project-overlay/mlir/test/BUILD.bazel      |  8 ++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/mlir/tblgen.bzl b/utils/bazel/llvm-project-overlay/mlir/tblgen.bzl
index d28a8854fa896..199cfac2238e9 100644
--- a/utils/bazel/llvm-project-overlay/mlir/tblgen.bzl
+++ b/utils/bazel/llvm-project-overlay/mlir/tblgen.bzl
@@ -306,6 +306,7 @@ def gentbl_filegroup(
         includes = [],
         deps = [],
         test = False,
+        testonly = None,
         skip_opts = [],
         **kwargs):
     """Create multiple TableGen generated files using the same tool and input.
@@ -326,6 +327,7 @@ def gentbl_filegroup(
       includes: See gentbl_rule.includes
       deps: See gentbl_rule.deps
       test: Whether to create a shell test that invokes the tool too.
+      testonly: Forwarded to the underlying rule. If not set uses the same value as `test`.
       skip_opts: Files generated using these opts in tbl_outs will be excluded
         from the generated filegroup.
       **kwargs: Extra keyword arguments to pass to all generated rules.
@@ -334,6 +336,8 @@ def gentbl_filegroup(
     included_srcs = []
     if type(tbl_outs) == type({}):
         tbl_outs = [(v, k) for k, v in tbl_outs.items()]
+    if testonly == None:
+        testonly = test
     for (opts, output_or_outputs) in tbl_outs:
         outs = output_or_outputs if type(output_or_outputs) == type([]) else [output_or_outputs]
         out = outs[0]
@@ -350,6 +354,7 @@ def gentbl_filegroup(
             td_file = td_file,
             tblgen = tblgen,
             opts = opts,
+            testonly = testonly,
             td_srcs = td_srcs,
             deps = deps,
             includes = includes,
@@ -379,6 +384,7 @@ def gentbl_filegroup(
     native.filegroup(
         name = name,
         srcs = included_srcs,
+        testonly = testonly,
         **kwargs
     )
 
@@ -392,6 +398,7 @@ def gentbl_cc_library(
         deps = [],
         strip_include_prefix = None,
         test = False,
+        testonly = None,
         copts = None,
         **kwargs):
     """Create multiple TableGen generated files using the same tool and input.
@@ -410,10 +417,13 @@ def gentbl_cc_library(
       deps: See gentbl_rule.deps
       strip_include_prefix: attribute to pass through to cc_library.
       test: whether to create a shell test that invokes the tool too.
+      testonly: Forwarded to the underlying rule. If not set uses the same value as `test`.
       copts: list of copts to pass to cc_library.
       **kwargs: Extra keyword arguments to pass to all generated rules.
     """
 
+    if testonly == None:
+        testonly = test
     filegroup_name = name + "_filegroup"
     gentbl_filegroup(
         name = filegroup_name,
@@ -423,6 +433,7 @@ def gentbl_cc_library(
         td_srcs = td_srcs,
         includes = includes,
         deps = deps,
+        testonly = testonly,
         test = test,
         skip_opts = ["-gen-op-doc"],
         **kwargs
@@ -434,6 +445,7 @@ def gentbl_cc_library(
         hdrs = [":" + filegroup_name] if strip_include_prefix else [],
         strip_include_prefix = strip_include_prefix,
         textual_hdrs = [":" + filegroup_name],
+        testonly = testonly,
         copts = copts,
         **kwargs
     )
@@ -538,7 +550,6 @@ def gentbl_sharded_ops(
         gentbl_shard_rule(
             index = i,
             name = name + "__src_shard" + str(i),
-            testonly = test,
             out = out_file,
             sharder = sharder,
             src_file = src_file,
diff --git a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
index 87011d5453683..cd79d44d729e2 100644
--- a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
@@ -141,6 +141,7 @@ td_library(
 
 gentbl_cc_library(
     name = "TestOpsSyntaxIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestOpsSyntax.h.inc": ["-gen-op-decls"],
@@ -156,6 +157,7 @@ gentbl_cc_library(
 
 gentbl_cc_library(
     name = "TestOpsIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestOpsDialect.h.inc": [
@@ -178,6 +180,7 @@ gentbl_cc_library(
 
 gentbl_cc_library(
     name = "TestInterfacesIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestAttrInterfaces.h.inc": ["-gen-attr-interface-decls"],
@@ -198,6 +201,7 @@ gentbl_cc_library(
 
 gentbl_cc_library(
     name = "TestAttrDefsIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestAttrDefs.h.inc": [
@@ -219,6 +223,7 @@ gentbl_cc_library(
 
 gentbl_cc_library(
     name = "TestEnumDefsIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestOpEnums.h.inc": ["-gen-enum-decls"],
@@ -234,6 +239,7 @@ gentbl_cc_library(
 
 gentbl_cc_library(
     name = "TestTypeDefsIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Test",
     tbl_outs = {
         "lib/Dialect/Test/TestTypeDefs.h.inc": [
@@ -305,6 +311,7 @@ td_library(
 
 gentbl_cc_library(
     name = "TestTransformDialectExtensionIncGen",
+    testonly = False,
     strip_include_prefix = "lib/Dialect/Transform",
     tbl_outs = {
         "lib/Dialect/Transform/TestTransformDialectExtension.h.inc": ["-gen-op-decls"],
@@ -359,6 +366,7 @@ cc_library(
 
 gentbl_sharded_ops(
     name = "TestDialectOpSrcs",
+    testonly = False,
     hdr_out = "lib/Dialect/Test/TestOps.h.inc",
     shard_count = 20,
     sharder = "//mlir:mlir-src-sharder",



More information about the llvm-commits mailing list