diff --git a/third_party/mlir/tblgen.bzl b/third_party/mlir/tblgen.bzl index 5585cc89e4c..372e180d974 100644 --- a/third_party/mlir/tblgen.bzl +++ b/third_party/mlir/tblgen.bzl @@ -1,6 +1,6 @@ """BUILD extensions for MLIR table generation.""" -def gentbl(name, tblgen, td_file, tbl_outs, td_srcs = [], td_includes = [], strip_include_prefix = None): +def gentbl(name, tblgen, td_file, tbl_outs, td_srcs = [], td_includes = [], strip_include_prefix = None, test = False): """gentbl() generates tabular code from a table definition file. Args: @@ -13,34 +13,58 @@ def gentbl(name, tblgen, td_file, tbl_outs, td_srcs = [], td_includes = [], stri td_srcs: A list of table definition files included transitively. td_includes: A list of include paths for relative includes. strip_include_prefix: attribute to pass through to cc_library. + test: whether to create a test to invoke the tool too. """ srcs = [] srcs += td_srcs if td_file not in td_srcs: srcs += [td_file] - # Add google_mlir/include directory and the directory with generated td - # files as includes, so derived op td files can import relative to that. - td_includes_str = "-I external/local_config_mlir/include -I external/org_tensorflow -I $(GENDIR) " + td_includes_cmd = ["-I external/local_config_mlir/include -I external/org_tensorflow"] for td_include in td_includes: - td_includes_str += "-I %s " % td_include - td_includes_str += "-I $$(dirname $(location %s)) " % td_file + td_includes_cmd += ["-I%s" % td_include] + local_inc = "-I $$(dirname $(location %s))" % td_file + + if test: + # Rule to generate shell script to invoke tblgen. This generates a very + # bare shell file which the sh_test uses. + native.genrule( + name = "%s_genrule_sh" % name, + srcs = srcs, + outs = ["%s.gen.sh" % name], + cmd = ("echo \"\\$$1\" %s \\$${@:2} -o /dev/null > $@" % local_inc), + executable = 1, + ) + for (opts, out) in tbl_outs: + # All arguments to generate the output except output destination. + base_args = [ + "$(location %s)" % tblgen, + "%s" % opts, + "$(location %s)" % td_file, + "-I$(GENDIR)", + ] + td_includes_cmd rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" ")) + + # Rule to generate code using generated shell script. native.genrule( name = "%s_%s_genrule" % (name, rule_suffix), srcs = srcs, outs = [out], tools = [tblgen], message = "Generating code from table: %s" % td_file, - cmd = (("$(location %s) %s %s $(location %s) -o $@") % ( - tblgen, - td_includes_str, - opts, - td_file, - )), + cmd = (" ".join(base_args) + " %s -o $@" % local_inc), ) + # Optionally generate rule to test tblgen invocation. + if test: + native.sh_test( + name = "%s_%s_genrule_test" % (name, rule_suffix), + srcs = ["%s.gen.sh" % name], + args = base_args, + data = srcs + [tblgen], + ) + # List of opts that do not generate cc files. skip_opts = ["-gen-op-doc"] hdrs = [f for (opts, f) in tbl_outs if opts not in skip_opts] diff --git a/third_party/mlir/test/BUILD b/third_party/mlir/test/BUILD index 90f544e744b..d45c2e3f493 100644 --- a/third_party/mlir/test/BUILD +++ b/third_party/mlir/test/BUILD @@ -41,6 +41,7 @@ gentbl( "@local_config_mlir//:include/mlir/Analysis/CallInterfaces.td", "@local_config_mlir//:include/mlir/Analysis/InferTypeOpInterface.td", ], + test = True, ) cc_library(