Change tblgen rule to generate sh_test too

mlir-tblgen invocations for rules generating tests were not considered as test
runs of mlir-tblgen so create extra sh_test to verify their behavior.

PiperOrigin-RevId: 275722547
Change-Id: I30df93f1f9912bd4de36a183383bcabae98375ff
This commit is contained in:
Jacques Pienaar 2019-10-20 08:33:41 -07:00 committed by TensorFlower Gardener
parent 51a379203f
commit 0657d9465b
2 changed files with 37 additions and 12 deletions

View File

@ -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]

View File

@ -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(