Add TensorFlow deps to bazel build, allowing us to pull in RUY and LLVM/MLIR snapshots.

Long term, we'd like to sever these dependencies, but that is going to take a while.

This gets the iree/hal/... package building. Next up is compiler/... which needs the glslang dep added.

PiperOrigin-RevId: 274040448
diff --git a/WORKSPACE b/WORKSPACE
index a01fe23..0403c27 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -2,6 +2,7 @@
 workspace(name = "iree")
 
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+load(":repo_utils.bzl", "maybe")
 
 # Abseil depends on starlark rules that are currently maintained outside
 # of Bazel.
@@ -16,34 +17,87 @@
     ],
 )
 
-local_repository(
-    name = "com_google_absl",
-    path = "third_party/abseil-cpp",
+###############################################################################
+# io_bazel_rules_closure
+# This is copied from https://github.com/tensorflow/tensorflow/blob/v2.0.0-alpha0/WORKSPACE.
+# Dependency of:
+#   TensorFlow (boilerplate for tf_workspace(), apparently)
+http_archive(
+    name = "io_bazel_rules_closure",
+    sha256 = "5b00383d08dd71f28503736db0500b6fb4dda47489ff5fc6bed42557c07c6ba9",
+    strip_prefix = "rules_closure-308b05b2419edb5c8ee0471b67a40403df940149",
+    urls = [
+        "https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz",
+        "https://github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz",  # 2019-06-13
+    ],
+)
+###############################################################################
+
+###############################################################################
+# Skylib
+# Dependency of:
+#   TensorFlow
+http_archive(
+    name = "bazel_skylib",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
+    ],
+    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
+)
+load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
+bazel_skylib_workspace()
+###############################################################################
+
+###############################################################################
+# Bootstrap TensorFlow.
+# Note that we ultimately would like to avoid doing this at the top level like
+# this but need to unbundle some of the deps fromt the tensorflow repo first.
+# In the mean-time: we're sorry.
+# TODO(laurenzo): Come up with a way to make this optional. Also, see if we can
+# get the TensorFlow tf_repositories() rule to use maybe() so we can provide
+# local overrides safely.
+maybe(local_repository,
+    name = "org_tensorflow",
+    path = "third_party/tensorflow",
 )
 
-local_repository(
-    name = "com_google_googletest",
-    path = "third_party/googletest",
+# Import all of the tensorflow dependencies.
+load("@org_tensorflow//tensorflow:workspace.bzl", "tf_repositories")
+
+tf_repositories()
+###############################################################################
+
+maybe(local_repository,
+     name = "com_google_absl",
+     path = "third_party/abseil-cpp",
 )
 
-local_repository(
+maybe(local_repository,
+     name = "com_google_googletest",
+     path = "third_party/googletest",
+)
+
+# Note that TensorFlow provides this as "flatbuffers" which is wrong.
+# It is only used for TFLite and may cause ODR issues if not fixed.
+maybe(local_repository,
     name = "com_github_google_flatbuffers",
     path = "third_party/flatbuffers",
 )
 
-new_local_repository(
+maybe(new_local_repository,
     name = "com_google_tracing_framework_cpp",
     path = "third_party/google_tracing_framework/bindings/cpp",
     build_file = "build_tools/third_party/google_tracing_framework_cpp/BUILD.overlay",
 )
 
-new_local_repository(
+maybe(new_local_repository,
     name = "vulkan_headers",
     path = "third_party/vulkan_headers",
     build_file = "build_tools/third_party/vulkan_headers/BUILD.overlay",
 )
 
-new_local_repository(
+maybe(new_local_repository,
     name = "vulkan_memory_allocator",
     path = "third_party/vulkan_memory_allocator",
     build_file = "build_tools/third_party/vulkan_memory_allocator/BUILD.overlay",
diff --git a/iree/BUILD b/iree/BUILD
index cfa87b6..59eb66a 100644
--- a/iree/BUILD
+++ b/iree/BUILD
@@ -5,7 +5,7 @@
 #   "@local_config_mlir//"
 #   "@llvm//"
 #   "@com_github_google_flatbuffers//:flatbuffers"
-#   "@tensorflow//"
+#   "@org_tensorflow//tensorflow"
 #
 # Various scripts and helpers operate on these prefixes textually, so
 # avoid doing any systematic construction that would break the matching.
diff --git a/iree/build_defs.bzl b/iree/build_defs.bzl
index a40a596..4727d7b 100644
--- a/iree/build_defs.bzl
+++ b/iree/build_defs.bzl
@@ -72,3 +72,6 @@
 
     # TODO(laurenzo): Implement me for OSS builds.
     pass
+
+def iree_glob_lit_tests(**kwargs):
+    print("TODO: glob_lit_tests is presently a no-op")
diff --git a/iree/compiler/IR/Interpreter/test/BUILD b/iree/compiler/IR/Interpreter/test/BUILD
index 4027bc7..084dbc6 100644
--- a/iree/compiler/IR/Interpreter/test/BUILD
+++ b/iree/compiler/IR/Interpreter/test/BUILD
@@ -1,11 +1,11 @@
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/IR/Sequencer/test/BUILD b/iree/compiler/IR/Sequencer/test/BUILD
index 4027bc7..084dbc6 100644
--- a/iree/compiler/IR/Sequencer/test/BUILD
+++ b/iree/compiler/IR/Sequencer/test/BUILD
@@ -1,11 +1,11 @@
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/IR/test/BUILD b/iree/compiler/IR/test/BUILD
index 4027bc7..084dbc6 100644
--- a/iree/compiler/IR/test/BUILD
+++ b/iree/compiler/IR/test/BUILD
@@ -1,11 +1,11 @@
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Transforms/BUILD b/iree/compiler/Transforms/BUILD
index 2b34104..4060276 100644
--- a/iree/compiler/Transforms/BUILD
+++ b/iree/compiler/Transforms/BUILD
@@ -35,7 +35,7 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:TransformUtils",
         "@local_config_mlir//:Transforms",
-        "@tensorflow///compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
     ],
     alwayslink = 1,
 )
diff --git a/iree/compiler/Transforms/Interpreter/BUILD b/iree/compiler/Transforms/Interpreter/BUILD
index 17357a3..e5816fa 100644
--- a/iree/compiler/Transforms/Interpreter/BUILD
+++ b/iree/compiler/Transforms/Interpreter/BUILD
@@ -33,9 +33,9 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:TransformUtils",
         "@local_config_mlir//:Transforms",
-        "@tensorflow///compiler/mlir/xla:hlo",
-        "@tensorflow///compiler/mlir/xla:xla_legalize_to_standard",
-        "@tensorflow///compiler/mlir/xla:xla_lower_general_dot",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_legalize_to_standard",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_lower_general_dot",
     ],
     alwayslink = 1,
 )
diff --git a/iree/compiler/Transforms/Interpreter/test/BUILD b/iree/compiler/Transforms/Interpreter/test/BUILD
index 4bc8ec2..f9289f6 100644
--- a/iree/compiler/Transforms/Interpreter/test/BUILD
+++ b/iree/compiler/Transforms/Interpreter/test/BUILD
@@ -1,13 +1,13 @@
 # Tests for lowering MLIR in various dialects to IREE interpreter bytecode.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Transforms/Interpreter/test/xla/BUILD b/iree/compiler/Transforms/Interpreter/test/xla/BUILD
index b6cbf2e..9f23d5f 100644
--- a/iree/compiler/Transforms/Interpreter/test/xla/BUILD
+++ b/iree/compiler/Transforms/Interpreter/test/xla/BUILD
@@ -1,13 +1,13 @@
 # Tests specific to lowering XLA to IREE.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Transforms/Sequencer/BUILD b/iree/compiler/Transforms/Sequencer/BUILD
index e3299a7..15120bb 100644
--- a/iree/compiler/Transforms/Sequencer/BUILD
+++ b/iree/compiler/Transforms/Sequencer/BUILD
@@ -38,8 +38,8 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:TransformUtils",
         "@local_config_mlir//:Transforms",
-        "@tensorflow///compiler/mlir/xla:hlo",
-        "@tensorflow///compiler/mlir/xla:xla_legalize_to_standard",
-        "@tensorflow///compiler/mlir/xla:xla_lower_general_dot",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_legalize_to_standard",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_lower_general_dot",
     ],
 )
diff --git a/iree/compiler/Transforms/Sequencer/test/BUILD b/iree/compiler/Transforms/Sequencer/test/BUILD
index 9db895b..5c671d5 100644
--- a/iree/compiler/Transforms/Sequencer/test/BUILD
+++ b/iree/compiler/Transforms/Sequencer/test/BUILD
@@ -1,13 +1,13 @@
 # Tests specific to the sequencer.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Transforms/test/BUILD b/iree/compiler/Transforms/test/BUILD
index 252d293..e3f88e8 100644
--- a/iree/compiler/Transforms/test/BUILD
+++ b/iree/compiler/Transforms/test/BUILD
@@ -1,13 +1,13 @@
 # Tests for common transforms.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Translation/Interpreter/BUILD b/iree/compiler/Translation/Interpreter/BUILD
index 98d7475..8dff827 100644
--- a/iree/compiler/Translation/Interpreter/BUILD
+++ b/iree/compiler/Translation/Interpreter/BUILD
@@ -23,9 +23,9 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:Transforms",
         "@local_config_mlir//:Translation",
-        "@tensorflow///compiler/mlir/xla:hlo",
-        "@tensorflow///compiler/mlir/xla:xla_dialect_registration",
-        "@tensorflow///compiler/mlir/xla:xla_legalize_to_standard",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_dialect_registration",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_legalize_to_standard",
     ],
     alwayslink = 1,
 )
diff --git a/iree/compiler/Translation/SPIRV/BUILD b/iree/compiler/Translation/SPIRV/BUILD
index 7912f02..5ccb714 100644
--- a/iree/compiler/Translation/SPIRV/BUILD
+++ b/iree/compiler/Translation/SPIRV/BUILD
@@ -42,7 +42,7 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:Transforms",
         "@local_config_mlir//:Translation",
-        "@tensorflow///compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
     ],
     alwayslink = 1,
 )
diff --git a/iree/compiler/Translation/SPIRV/Kernels/spirv_utils.bzl b/iree/compiler/Translation/SPIRV/Kernels/spirv_utils.bzl
index e177a49..05914f0 100644
--- a/iree/compiler/Translation/SPIRV/Kernels/spirv_utils.bzl
+++ b/iree/compiler/Translation/SPIRV/Kernels/spirv_utils.bzl
@@ -1,7 +1,7 @@
 """Utilities for handling hand-written SPIR-V files."""
 
 load("//third_party/glslang:build_defs.bzl", "glsl_vulkan")
-load("//tools/build_defs/cc:cc_embed_data.bzl", "cc_embed_data")
+load("//iree:build_defs.bzl", "iree_cc_embed_data")
 
 def spirv_kernel_cc_library(name, srcs):
     """Compiles GLSL files into SPIR-V binaries and embeds them in a cc_library.
@@ -22,7 +22,7 @@
         name = name + "_files",
         srcs = spv_files,
     )
-    cc_embed_data(
+    iree_cc_embed_data(
         name = name,
         srcs = spv_files,
         outs = [
diff --git a/iree/compiler/Translation/SPIRV/test/BUILD b/iree/compiler/Translation/SPIRV/test/BUILD
index 252d293..e3f88e8 100644
--- a/iree/compiler/Translation/SPIRV/test/BUILD
+++ b/iree/compiler/Translation/SPIRV/test/BUILD
@@ -1,13 +1,13 @@
 # Tests for common transforms.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/iree/compiler/Translation/Sequencer/BUILD b/iree/compiler/Translation/Sequencer/BUILD
index 8adb084..4245b19 100644
--- a/iree/compiler/Translation/Sequencer/BUILD
+++ b/iree/compiler/Translation/Sequencer/BUILD
@@ -25,9 +25,9 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:Transforms",
         "@local_config_mlir//:Translation",
-        "@tensorflow///compiler/mlir/xla:hlo",
-        "@tensorflow///compiler/mlir/xla:xla_dialect_registration",
-        "@tensorflow///compiler/mlir/xla:xla_legalize_control_flow",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_dialect_registration",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_legalize_control_flow",
     ],
     alwayslink = 1,
 )
diff --git a/iree/compiler/Utils/BUILD b/iree/compiler/Utils/BUILD
index 5d53244..6062540 100644
--- a/iree/compiler/Utils/BUILD
+++ b/iree/compiler/Utils/BUILD
@@ -34,6 +34,6 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:TransformUtils",
         "@local_config_mlir//:Transforms",
-        "@tensorflow///compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
     ],
 )
diff --git a/iree/hal/interpreter/BUILD b/iree/hal/interpreter/BUILD
index 36cfd78..97235f6 100644
--- a/iree/hal/interpreter/BUILD
+++ b/iree/hal/interpreter/BUILD
@@ -89,8 +89,8 @@
         "@com_google_absl//absl/container:inlined_vector",
         "@com_google_absl//absl/memory",
         "@com_google_absl//absl/types:span",
-        "@tensorflow///lite/experimental/ruy",
-        "@tensorflow///lite/experimental/ruy:context",
+        "@org_tensorflow//tensorflow/lite/experimental/ruy",
+        "@org_tensorflow//tensorflow/lite/experimental/ruy:context",
     ],
 )
 
diff --git a/iree/tools/BUILD b/iree/tools/BUILD
index 36f5759..cc2cf65 100644
--- a/iree/tools/BUILD
+++ b/iree/tools/BUILD
@@ -25,8 +25,8 @@
         "@local_config_mlir//:MlirOptLib",
         "@local_config_mlir//:MlirOptMain",
         "@local_config_mlir//:StandardDialectRegistration",
-        "@tensorflow///compiler/mlir/xla:hlo",
-        "@tensorflow///compiler/mlir/xla:xla_dialect_registration",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:hlo",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_dialect_registration",
     ],
 )
 
@@ -78,7 +78,7 @@
         "@local_config_mlir//:Support",
         "@local_config_mlir//:TranslateClParser",
         "@local_config_mlir//:Translation",
-        "@tensorflow///compiler/mlir/xla:xla_dialect_registration",
+        "@org_tensorflow//tensorflow/compiler/mlir/xla:xla_dialect_registration",
     ],
 )
 
diff --git a/repo_utils.bzl b/repo_utils.bzl
new file mode 100644
index 0000000..9f6b997
--- /dev/null
+++ b/repo_utils.bzl
@@ -0,0 +1,15 @@
+# TODO(laurenzo): This is available upstream as of 0.28. Remove when ready.
+# See: https://docs.bazel.build/versions/master/repo/utils.html#maybe
+def maybe(repo_rule, name, **kwargs):
+    """Utility function for only adding a repository if it's not already present.
+    This is to implement safe repositories.bzl macro documented in
+    https://docs.bazel.build/versions/master/skylark/deploying.html#dependencies.
+    Args:
+        repo_rule: repository rule function.
+        name: name of the repository to create.
+        **kwargs: remaining arguments that are passed to the repo_rule function.
+    Returns:
+        Nothing, defines the repository when needed as a side-effect.
+    """
+    if not native.existing_rule(name):
+        repo_rule(name = name, **kwargs)
diff --git a/test/e2e/BUILD b/test/e2e/BUILD
index c655d6b..087bf47 100644
--- a/test/e2e/BUILD
+++ b/test/e2e/BUILD
@@ -1,13 +1,13 @@
 # Tests for end-to-end IREE support.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],
diff --git a/test/e2e/xla/BUILD b/test/e2e/xla/BUILD
index cc629fc..fe72b14 100644
--- a/test/e2e/xla/BUILD
+++ b/test/e2e/xla/BUILD
@@ -1,13 +1,13 @@
 # Tests for end-to-end IREE support starting from the XLA HLO dialect.
 
-load("//third_party/llvm/build_defs:lit.bzl", "glob_lit_tests")
+load("//iree:build_defs.bzl", "iree_glob_lit_tests")
 
 package(
     default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
-glob_lit_tests(
+iree_glob_lit_tests(
     data = [":test_utilities"],
     driver = "//iree/tools:run_lit.sh",
     test_file_exts = ["mlir"],